Merge pull request #1190 from neokjames/master

Improved Pushover transport code
This commit is contained in:
Neil Lathwood
2015-06-03 16:32:22 +01:00
2 changed files with 52 additions and 25 deletions

View File

@@ -33,3 +33,4 @@ Contributors to LibreNMS:
- James Campbell <neokjames@gmail.com> (neokjames)
[1]: http://observium.org/ "Observium web site"

View File

@@ -36,31 +36,57 @@
*/
foreach( $opts as $api ) {
$data = array();
$data['token'] = $api['appkey'];
$data['user'] = $api['userkey'];
if( $obj['severity'] == "critical" ) {
if( !empty($api['sound_critical']) ) {
$data['sound'] = $api['sound_critical'];
}
$severity = "Critical";
$data['priority'] = 1;
}
elseif( $obj['severity'] == "warning" ) {
$severity = "Warning";
$data['priority'] = 0;
$data = array();
$data['token'] = $api['appkey'];
$data['user'] = $api['userkey'];
switch( $obj['severity'] ) {
case "critical":
$severity = "Critical";
$data['priority'] = 1;
if( !empty( $api['sound_critical'] ) ) {
$data['sound'] = $api['sound_critical'];
}
break;
case "warning":
$severity = "Warning";
$data['priority'] = 0;
if( !empty( $api['sound_warning'] ) ) {
$data['sound'] = $api['sound_warning'];
}
break;
}
switch( $obj['state'] ) {
case 0:
$title_text = "OK";
if( !empty( $api['sound_ok'] ) ) {
$data['sound'] = $api['sound_ok'];
}
break;
case 1:
$title_text = $severity;
break;
case 2:
$title_text = "Acknowledged";
break;
}
$data['title'] = $title_text." - ".$obj['hostname']." - ".$obj['name'];
$message_text = "Timestamp: ".$obj['timestamp'];
if( !empty( $obj['faults'] ) ) {
$message_text .= "\n\nFaults:\n";
foreach($obj['faults'] as $k => $faults) {
$message_text .= "#".$k." ".$faults['string']."\n";
}
}
$data['message'] = $message_text;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://api.pushover.net/1/messages.json');
curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$ret = curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if( $code != 200 ) {
var_dump("Pushover returned error"); //FIXME: proper debugging
return false;
}
$curl = curl_init();
$data['title'] = $severity." - ".$obj['hostname'];
$data['message'] = $obj['name'];
curl_setopt($curl, CURLOPT_URL, 'https://api.pushover.net/1/messages.json');
curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$ret = curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if( $code != 200 ) {
var_dump("Pushover returned error"); //FIXME: proper debugging
return false;
}
}
return true;