Last active
September 3, 2018 11:02
-
-
Save sebastiansommer/6ed5e9656df100ab731caa3f2dcb8268 to your computer and use it in GitHub Desktop.
Simple nagios script to integrate flock.
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 | |
/** | |
* Example call in nagios xi: | |
* /usr/local/bin/flock_nagios.php hostname="$HOSTNAME$" state="$HOSTSTATE$" message="$HOSTOUTPUT$" notificationType="$NOTIFICATIONTYPE$" | |
*/ | |
$flockUrl = 'YOUR_FLOCK_URL'; | |
$parameters = []; | |
$arguments = $argv; | |
unset($arguments[0]); | |
foreach ($arguments as $key => $argument) { | |
$explodedArgument = explode('=', $argument); | |
$parameters[$explodedArgument[0]] = $explodedArgument[1]; | |
} | |
$message = strtoupper($parameters['notificationType']) . ' - ' . $parameters['hostname'] . ' - ' . $parameters['message'] . ' (' . $parameters['state'] . ')'; | |
$jsonData = [ | |
'text' => $message | |
]; | |
$jsonDataString = json_encode($jsonData); | |
$ch = curl_init($flockUrl); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataString); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, [ | |
'Content-Type: application/json', | |
'Content-Length: ' . strlen($jsonDataString) | |
]); | |
$result = curl_exec($ch); | |
curl_close($ch); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
First you need to create a new WebHook in Flock. After this you just upload that file to your Nagios server and make sure its executable. You can simply test that by calling the script like this:
php flock_nagios.php hostname="TEST" state="TEST" message="TEST" notificationType="TEST"
Then you just need to add 2 commands in Nagios XI: Configure -> Core Config Manager -> Commands:
flock_host_notification_handler
/usr/local/bin/flock_nagios.php hostname="$HOSTNAME$" state="$HOSTSTATE$" message="$HOSTOUTPUT$" notificationType="$NOTIFICATIONTYPE$
flock_service_notification_handler
/usr/local/bin/flock_nagios.php hostname="$HOSTNAME$" state="$SERVICESTATE$" message="$SERVICEOUTPUT$" notificationType="$NOTIFICATIONTYPE$"