Created
January 4, 2019 00:46
-
-
Save sammy8806/227cc4a22ede384ca6417c63e22ef882 to your computer and use it in GitHub Desktop.
Icinga2 Notification via Prosody XMPP Push
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
/* | |
* XMPP Notification Script for Icinga2 | |
* | |
* This script uses prosody webpush as target to contact the user | |
*/ | |
$xmppUser = '<user>'; | |
$xmppPass = '<pass>'; | |
$args = []; | |
for($i = 1; $i<=$argc; $i = $i + 2) { | |
$args[$argv[$i]] = $argv[$i + 1]; | |
} | |
$address = $args['-a']; | |
$serviceName = $args['-e']; | |
$serviceDisplayName = $args['-f']; | |
$hostName = $args['-l']; | |
$output = $args['-o']; | |
$serviceState = $args['-s']; | |
$notificationType = $args['-t']; | |
$notificationComment = $args['-c']; | |
$notificationAuthor = $args['-b']; | |
$xmppTarget = '<default contact>'; | |
$xmppTargetArg = $args['-u']; | |
if(strlen($xmppTargetArg) > 0) { | |
$xmppTarget = $xmppTargetArg; | |
} | |
$textMsg = $hostName . ':' . $notificationType . ' (' . $serviceName . ') => ' . $serviceState . "\n\n" . $output . "\n\n"; | |
$htmlMsg = "<b>$hostName : $notificationType</b> (<i>$serviceName</i>)<br />=> $serviceState<br />$output"; | |
if (strlen($notificationComment) > 0) { | |
$textsg .= "Comment by $notificationAuthor: \"$notificationComment\""; | |
$htmlMsg .= "<br />Comment: \"$notificationComment\" <i><small>by $notificationAuthor</small></i>"; | |
} | |
$postMsg = 'to=' . urlencode($xmppTarget) | |
. '&type=chat' | |
. '&body=' . urlencode($textMsg) | |
. '&html=' . urlencode( | |
'<body xmlns="http://www.w3.org/1999/xhtml">' | |
. $htmlMsg | |
. '</body>' | |
); | |
$ctx = stream_context_create( | |
array( | |
'http' => array( | |
'method' => 'POST', | |
'header' => array( | |
'Content-type: application/x-www-form-urlencoded', | |
'Authorization: Basic ' . base64_encode($xmppUser . ':' . $xmppPass), | |
), | |
'content' => $postMsg | |
) | |
) | |
); | |
file_get_contents('http://<prosody host>:5280/msg/', false, $ctx); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment