Skip to content

Instantly share code, notes, and snippets.

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 sammy8806/227cc4a22ede384ca6417c63e22ef882 to your computer and use it in GitHub Desktop.
Save sammy8806/227cc4a22ede384ca6417c63e22ef882 to your computer and use it in GitHub Desktop.
Icinga2 Notification via Prosody XMPP Push
#!/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