Skip to content

Instantly share code, notes, and snippets.

@aikar
Last active April 5, 2018 08:24
Show Gist options
  • Save aikar/e2b58ca22d1c84537111 to your computer and use it in GitHub Desktop.
Save aikar/e2b58ca22d1c84537111 to your computer and use it in GitHub Desktop.
Zabbix -> ZAX PubNub Push Notifications with PHP - No Perl needed
#!/usr/bin/env php
<?php
// SET KEYS HERE
$pubkey = "pub-c-PUB_KEY";
$subkey = "sub-c-SUB_KEY";
// OPTIONAL USER AGENT - It's good practice to identify to an API who you are.
define("USER_AGENT", "Zabbix Alert");
$url = "http://pubsub.pubnub.com/publish/$pubkey/$subkey/0/zabbixmobile/0/";
$argv = $_SERVER['argv'];
$subj = $argv[2];
$body = array_splice($argv, 3);
$body = implode("\n", $body);
$status = "PROBLEM";
if (preg_match("/status: (PROBLEM|OK)/", $body, $m) && $m[1]) {
$status = $m[1];
}
$payload = [
'triggerid' => $argv[1],
'status' => $status,
'message' => $body
];
print_r(http_post($url, json_encode($payload)));
function http_post ($url, $data) {
$data_len = strlen($data);
return array ('content'=>file_get_contents ($url, false, stream_context_create (array ('http'=>array (
'method'=>'POST'
, 'header'=>
"User-Agent: " . USER_AGENT . "\r\n" .
"Content-Type: application/json\r\n" .
"Connection: close\r\n" .
"Content-Length: $data_len\r\n"
, 'content'=>$data
))))
, 'headers'=>$http_response_header
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment