OP5 Monitor notifications by RSS

If you are using OP5 Monitor free version for personal/non-production/lab use you will find that notifications by e-mail or SMS is a bit overkill and annoying.
So I created this simple RSS function that consists of a notification script that is called from a check commmand that writes the notification to a text file on the op5-monitor server, and a custom PHP-script that reads the same text file and presents it in RSS format.
This way I can add https://op5monitor.mydomain.com/rss.php?channel=contactname in a RSS browser and receive alerts that way instead.

/opt/plugins/custom/notify
NOTE: In OP5 Monitor 7+ it looks like you should use: /opt/monitor/op5/notify/custom instead of /opt/plugins/custom , I had trouble with this after upgrading.

#!/bin/sh
LASTSTATECHANGE=$1
CONTACTNAME=$2
HOSTNAME=$3
SERVICEDESC=$4
# Comma is csv seperator so replace it with dot if found in output
OUTPUT=$(echo $5 | sed 's/,/./g')
STATE=$6
 
UUID=$(cat /proc/sys/kernel/random/uuid)
 
notificationfile=/opt/monitor/var/notifications-$CONTACTNAME.csv
echo "$UUID,$LASTSTATECHANGE,$HOSTNAME.$SERVICEDESC is $STATE,$OUTPUT" >>$notificationfile

Change the check command host-notify to the below (or create a new one)

$USER3$/notify/notify "$LASTHOSTSTATECHANGE$" "$CONTACTNAME$" "$HOSTNAME$" "Connectivity" "$HOSTOUTPUT$" "$HOSTSTATE$"

Change the check command service-notify to the below (or create a new one)

$USER3$/notify/notify "$LASTSERVICESTATECHANGE$" "$CONTACTNAME$" "$HOSTNAME$" "$SERVICEDESC$" "$SERVICEOUTPUT$" "$SERVICESTATE$"

Create the file /var/www/html/rss.php with the below content (note that “channel” is the same as contact)
Modify text, domain, etc as needed. Add your contacts/channels at the end.

<?php
date_default_timezone_set('UTC');
 
$channel = $_GET['channel'];
if ($channel != "") {
 
          $monitoringfile = "/opt/monitor/var/notifications-$channel.csv";
          header("Content-Type: application/xml; charset=ISO-8859-1");
          $item = $_GET['item'];
          echo "
<rss version=\"2.0\">
  <channel>
    <title>OP5 Monitor - $channel alerts</title>
    <link>http://op5monitor.mydomain.com/rss.php?channel=monitor</link>
    <description>A list of monitoring alerts</description>
";
         $file = file($monitoringfile);
         $file = array_reverse($file);
         $counter=0;
         foreach($file as $line){
            $displayItem = false;
            if (strlen($line) > 5) {
              if (strlen($item) > 0) {
                if(strpos($line, $item) !== false) {
                  $displayItem = true;
                }
              } else {
                $displayItem = true;
              }
              if ($displayItem) {
                $columns=str_getcsv($line);
                $datetime=date('D, d M Y H:i:s +0000', $columns[1]);
                if (strpos($columns[3], "|") > 0) {
                  $desc=split("\|", $columns[3]);
                  $description=$desc[0];
                } else {
                  $description=$columns[3];
                }
                echo "
    <item>
      <guid isPermaLink=\"false\">$columns[0]</guid>
      <pubDate>$datetime</pubDate>
      <title>$columns[2]</title>
      <description>$description</description>
      <link>
      http://op5monitor.mydomain.com/rss.php?channel=$channel&amp;item=$columns[0]
      </link>
      <author>OP5 Monitor</author>
    </item>
";
              }
            }
           $counter++;
           if ($counter > 100) break;
         }
         fclose($file);
          echo "
  </channel>
</rss>
";
} else {
       echo "
<HTML>
<HEAD>
<TITLE>op5monitor.mydomain.com RSS Alerts</TITLE>
</HEAD>
<BODY>
<h1>Choose channel</h1>
<ul>
  <li><a href=\"rss.php?channel=monitor\">All alerts</a></li>
  <li><a href=\"rss.php?channel=lab\">Lab alerts</a></li>
</ul>
</BODY>
</HTML>
";
}
?>

And add this if you feel its needed.
/etc/cron.daily/clear-notifications-rss.sh

#!/bin/sh
 
echo >/opt/monitor/var/notifications-lab.csv
echo >/opt/monitor/var/notifications-monitor.csv

Leave a Reply

Your email address will not be published. Required fields are marked *