Skip to content

Instantly share code, notes, and snippets.

@dlundgren
Last active March 21, 2019 23:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dlundgren/5c44cf70c791288baddb0a689bf54bcc to your computer and use it in GitHub Desktop.
Save dlundgren/5c44cf70c791288baddb0a689bf54bcc to your computer and use it in GitHub Desktop.
Slackify the Zabbix alerts
#!/usr/bin/env php
<?php
/**
* Slackify Zabbix Alerts
*
* @copyright 2019, David Lundgren <dlundgren@syberisle.net>
* @license MIT
*/
function get_key($argv, $pos, $shopts, $lopts)
{
$key = $check = $value = null;
if (array_key_exists($pos, $argv) && $argv[$pos]{0} === '-') {
$key = ltrim($argv[$pos], '-');
if (strpos($key, '=') !== false) {
list($key, $value) = explode('=', $key, 2);
$value = trim($value, '"\'');
}
if (array_key_exists($key, $lopts)) {
$check = $lopts[$key];
}
elseif (array_key_exists($key, $shopts)) {
$check = $shopts[$key];
}
elseif (array_key_exists($key{0}, $shopts)) {
$check = $shopts[$key{0}];
$value = substr($key, 1);
$key = $key{0};
}
else {
$key = null;
}
}
return array($key, $check, $value);
}
function parse_args($shopt = '', $lopt = [], $argv = null)
{
if ($argv === null) {
$argv = $GLOBALS['argv'];
}
$lopts = array();
foreach ($lopt as $v) {
@list($lo, $par) = explode(':', $v, 2);
$lopts[$lo] = $par;
}
foreach (preg_split('/([a-zA-Z0-9]:?:?)/i', $shopt, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $v) {
@list($chr, $par) = explode(':', $v, 2);
$shopts[$chr] = $par;
}
$options = array();
$argn = count($argv);
for ($pos = 0; $pos < $argn; $pos++) {
list($key, $check, $value) = get_key($argv, $pos, $shopts, $lopts);
if ($key) {
$next = false;
if ($value === null) {
list($next) = get_key($argv, $pos + 1, $shopts, $lopts);
if ($next === null) {
$value = $argv[$pos + 1];
}
}
switch ($check) {
case '':
if ($next && $value === null) {
die("Missing value for `{$key}`");
}
if ($next !== false) {
$pos++;
}
break;
case ':':
if ($next === false) {
$pos++;
break;
}
case null:
$value = array_key_exists($key, $options)
? $options[$key] + 1
: 1;
break;
}
}
if ($value) {
$options[$key] = $value;
}
}
return $options;
}
// }}}
// {{{ Script Configuration
// set the options that will be used
$opts = array(
'u:' => 'url:',
'c:' => 'channel:',
'f:' => 'from:',
's:' => 'subject:',
'm:' => 'message:',
);
$colors = array(
'Not Classified' => '#DBDBDB',
'Information' => '#33CCFF',
'Warning' => 'warning',
'Average' => 'warning',
'High' => 'warning',
'PROBLEM:' => 'danger',
'RESOLVED:' => 'good',
'UPDATED:' => 'warning',
'PROBLEM>:' => 'danger',
'RESOLVED>:' => 'good',
'UPDATED>:' => 'warning',
'Disaster' => 'danger',
'OK' => 'good',
);
// Default Configuration
$config = array(
'url' => 'https://www.example.com',
'channel' => '#alerts',
'from' => 'Zabbix',
'subject' => 'Not Classified',
'message' => 'Message'
);
// }}}
// {{{ Execution
// get the console arguments
$options = parse_args(join('', array_keys($opts)), array_values($opts));
foreach ($opts as $short => $long) {
$short = rtrim($short, ':');
$long = rtrim($long, ':');
if (isset($options[$short])) {
$config[$long] = $options[$short];
}
if (isset($options[$long])) {
$config[$long] = $options[$long];
}
}
$color = $colors['Not Classified'];
foreach ($colors as $status => $hex) {
if (strpos($config['subject'], $status) !== false) {
$color = $hex;
break;
}
}
// parse the message for any buttons
$buttons = [];
if (preg_match_all('/{{(.*?)}}\n?/', $config['message'], $m)) {
foreach ($m[1] as $link) {
$b = explode("|", $link);
$button = [
'type' => 'button',
'text' => $b[1],
'url' => $b[0]
];
if (isset($b[2])) {
$button['style'] = $b[2];
}
$buttons[] = $button;
}
foreach ($m[0] as $link) {
$config['message'] = str_replace($link, '', $config['message']);
}
}
$payload = array(
'channel' => $config['channel'],
'username' => $config['from'],
'attachments' => array(
array(
"fallback" => $config['subject'],
"title" => $config['subject'],
"text" => trim($config['message']),
"color" => $color,
)
)
);
if (!empty($buttons)) {
$payload['attachments'][0]['actions'] = $buttons;
}
$c = curl_init($config['url']);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, array('payload' => json_encode($payload)));
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($c);
curl_close($c);
@dlundgren
Copy link
Author

dlundgren commented Jan 29, 2019

zabbix-slack.php --url="https://www.slack.com/some/hook/path" --from="Your Zabbix" --message="{ALERT.MESSAGE}" --subject="{ALERT.SUBJECT}"

@dlundgren
Copy link
Author

Your message can now contain buttons

message {{https://zabbix.example.com/zabbix.php/tr_events.php?triggerid={TRIGGER.ID}&eventid={EVENT.ID}|View Incident|primary}}
{{https://www.example.com|Visit Site}}

Those will display 2 buttons.

@dlundgren
Copy link
Author

screen shot 2019-03-06 at 12 55 52 pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment