1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Innmind\ProvisionerBundle\Alert; |
4
|
|
|
|
5
|
|
|
use Frlnc\Slack\Core\Commander; |
6
|
|
|
use Psr\Log\LoggerInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Sends a channel notification when an alert is raised |
10
|
|
|
*/ |
11
|
|
|
class SlackAlerter implements AlerterInterface |
12
|
|
|
{ |
13
|
|
|
protected $commander; |
14
|
|
|
protected $channel; |
15
|
|
|
protected $logger; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Set the slack command runner |
19
|
|
|
* |
20
|
|
|
* @param Commander $commander |
21
|
|
|
*/ |
22
|
6 |
|
public function setCommander(Commander $commander) |
23
|
|
|
{ |
24
|
6 |
|
$this->commander = $commander; |
25
|
6 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Set the channel where to send notifications to |
29
|
|
|
* |
30
|
|
|
* @param string $channel |
31
|
|
|
*/ |
32
|
6 |
|
public function setChannel($channel) |
33
|
|
|
{ |
34
|
6 |
|
$this->channel = (string) $channel; |
35
|
6 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Set the logger |
39
|
|
|
* |
40
|
|
|
* @param LoggerInterface $logger |
41
|
|
|
*/ |
42
|
|
|
public function setLogger(LoggerInterface $logger) |
43
|
|
|
{ |
44
|
|
|
$this->logger = $logger; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
6 |
|
public function alert(Alert $alert) |
51
|
|
|
{ |
52
|
6 |
|
if ($alert->isOverUsed()) { |
53
|
3 |
|
$text = sprintf( |
54
|
3 |
|
'Server at full capacity! Command: %s | CPU: %s | Load: %s | Required: %s | Running: %s', |
55
|
3 |
|
(string) $alert->getCommandInput(), |
56
|
3 |
|
$alert->getCpuUsage(), |
57
|
3 |
|
$alert->getLoadAverage(), |
58
|
3 |
|
$alert->getLeftOver(), |
59
|
3 |
|
$alert->getRunningProcesses() |
60
|
3 |
|
); |
61
|
3 |
|
} else { |
62
|
3 |
|
$text = sprintf( |
63
|
3 |
|
'Server under used. You may take it down! Command: %s | CPU: %s | Load: %s', |
64
|
3 |
|
(string) $alert->getCommandInput(), |
65
|
3 |
|
$alert->getCpuUsage(), |
66
|
3 |
|
$alert->getLoadAverage() |
67
|
3 |
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
6 |
|
$response = $this->commander->execute('chat.postMessage', [ |
71
|
6 |
|
'channel' => $this->channel, |
72
|
6 |
|
'text' => $text, |
73
|
6 |
|
]); |
74
|
|
|
|
75
|
6 |
|
if ($this->logger && !$response['ok']) { |
76
|
|
|
$this->logger->error('Slack notification didn\'t worked', [ |
77
|
|
|
'channel' => $this->channel, |
78
|
|
|
'response' => $response, |
79
|
|
|
]); |
80
|
|
|
} |
81
|
6 |
|
} |
82
|
|
|
} |
83
|
|
|
|