You are here

Blog

Light Up Your Monitoring With Opsview and a Raspberry Pi

Dashboards and reports are brilliant ways to keep an eye on your systems, and an e-mail or ticket straight into your ticketing system lets you know about critical issues. However, nothing can beat a massive flashing light! Make sure none of your alerts are ever missed again by anyone in your office (or a ten block radius for that matter!).

What you will need

Opsview – An install of Opsview, which is set up to monitor whatever it is you really need to know about.

Flashing light – These can easily be found online, ours actually came from an online disco equipment store. Who wouldn’t be keen to getting some disco equipment in the office!

light.jpg

 

Raspberry Pi – Easy to set up and configure, Raspberry Pi is an affordable credit card–sized single-board computer used to drive your lights.

Remote control sockets – Plugs straight into the Raspberry Pi and can turn on and off your light. Find it here.

Raspberry Pi setup and configuration

First, just plug the plug socket controller into the Raspberry Pi, it fits straight onto the io header.

image 2.png

 

Plug the light into the remotely controlled socket.

image 3.png

Install an operating system onto the Pi, we used Raspbian. Then, configure networking on the Pi so you can reach it from your Opsview system. You will need access to the internet to download some packages and scripts.

Firstly, on the Pi we need to wirelessly connect the controller board to the remotely controlled socket. Instructions can be found below.

https://energenie4u.co.uk/res/pdfs/ENER314%20UM.pdf

You will need a script on the Pi that can be called by Opsview to turn the light on and off, this is a Perl script that can be triggered by making web calls to the Pi.

To configure the Pi with this script:

  1. Install Apache on the Pi:

# sudo apt-get update

# sudo apt-get install apache

  1. Download the scripts, you can find them here
  2. Install the script on the Pi:

# tar –cvf OpsviewLights.tar –C /usr/lib/cgi-bin/

  1. Configure the file permissions

# chown www-data: www-data /usr/lib/cgi-bin/*

# chmod u+x /usr/lib/cgi-bin/*

  1. To control the Raspberry Pi’s io, you must have elevated access rights so we run the scripts as root, to do this you must add www-data to the sudoers file:

# sudo visudo

And add the entry:

www-data ALL=(ALL) NOPASSWD: /usr/bin/python

You can test the scripts by running them directly on the Pi:

# sudo /usr/bin/python /usr/lib/cgi-bin/LightsOn.py

This should turn the lights on.

# sudo /usr/bin/python /usr/lib/cgi-bin/LightsOff.py

This should turn the lights off.

To call the CGI script from your web browser:

http://<RASPBERRY-PI-IP>/cgi-bin/lights.pl?Alert=1

This should turn the lights on.

http:// <RASPBERRY-PI-IP>/cgi-bin/lights.pl?Alert=0

This should turn the lights off.

Opsview configuration

First, you will need to add the notification script to your Opsview system.

You can find a copy of the script below.  

  -h - this help text

  -H - The host URL to send to

};

#ENV VAR for input parameter

if ( $ENV{NAGIOS__CONTACTRASPBERRY_URL} ) {

                $URL = $ENV{NAGIOS__CONTACTRASPBERRY_URL};

        }

# Get the options

GetOptions(

    'h'     => \$help_mode,

    'H'     => \$URL,

);

# Run the subroutines for the options.

if ($help_mode) {

     Help

    usage();

    exit(0);

}

if ($URL) {

    send_rpi_message();

}

else {

                usage();

                exit(0);

}

sub send_rpi_message {

    require LWP::UserAgent;

    my $req;

    my $ua = LWP::UserAgent->new;

                if ( $ENV{OPSVIEW_SERVICESTATE} ) {

                                $state = $ENV{NAGIOS_SERVICESTATE};

                }

                elsif ( $ENV{NAGIOS_SERVICESTATE} ) {

                $state = $ENV{NAGIOS_SERVICESTATE};

                }

                if ($state eq 'OK') {

                                 $req = HTTP::Request->new(GET => "$URL" . "?Alert=0");

                }

                elsif ($state eq 'WARNING') {

                                 $req = HTTP::Request->new(GET => "$URL" . "?Alert=1");

                }

                elsif ($state eq 'CRITICAL') {

                                 $req = HTTP::Request->new(GET => "$URL" . "?Alert=1");

                }

                elsif ($state eq 'UNKNOWN') {

                                 $req = HTTP::Request->new(GET => "$URL" . "?Alert=1");

                }

                else {

                                print "Error, unknown state";

                }

    $ua->timeout(5);

    my $res = $ua->request($req);

    my $err_line = $res->is_success ? '' : $res->status_line;

    my $err_message = '';

    if ($err_line) {

        $err_message = $res->content;

        print "$err_message\n$URL";

    }

}

sub usage {

    if ( $_ = shift ) { print "Error: $_\n" }

    print $usage;

    exit 1;

}

The script needs to be placed in the directory:

 /usr/local/nagios/libexec/notifications/

The script needs to be owned by the Nagios user:

# chown nagios:nagios /usr/local/nagios/libexec/notifications/notify_by_raspberrypi.pl

And have read and execute permissions set:

# chmod u+rx /usr/local/nagios/libexec/notifications/notify_by_raspberrypi.pl

Once the notification script has been created, you can configure the Notification Methods.

2015-09-02_0945.png

To use the new script, add a new notification method by clicking the green cross in the top left hand corner of the notification methods page. Example settings for the new method are shown below:

2015-09-02_0957.png

You will notice that we are using a “Contact Variables” named “RASPBERRY_URL”, this will be the url that we will pass to the notification script so it is able to turn the light on and off. We will configure this variable later in the contact settings.

Next, we configure the Shared Notification Profile by navigating to the Shared Notification Profiles page.

2015-09-02_1024.png

 

Create a new Shared Notification Profile using the green plus in the top left corner. Give the profile a name, select the role you would like to make the profile available to, and select the notification method in the “Notify by” section. I have named it raspberrypi in this example.

2015-09-02_1034.png

You will then need to select a system to notify against. In this example, I am using a keyword named ServiceCloud.

2015-09-02_1037.png

Here at Opsview, we are using the light to alert us to new tickets coming into our customer success team so we know about them right away. It could be used for any alerts on your systems.

For a complete guide to configuring alerts, read our “Opsview Notifications & Setting Up Alerts” guide.

Lastly, we will apply the notification to one of our Opsview users by navigating to the Contact settings page.

2015-09-02_1041.png

Under the Notifications tab in the contact menu, you can configure the Shared Notification Profiles applied to the user and set the notification variables. Make sure you select the shared notification profile you just set up and then set the “raspberry url” variable. This comes from the Contact Variable we set earlier and should be in the following format:

http://<RASPBERRY-PI-IP>/cgi-bin/lights.pl

 

2015-09-02_1042.png

Run an Opsview reload and your light will be ready to go! If you want to test it, you can “Submit check result” to simulate a system failure.

2015-09-02_1044.png

Conclusion

Now that you have your alerts configured to notify by your Raspberry Pi, you can have your flashing light go off for all kinds of alerts out of your monitoring system. Using the remotely controlled socket, you can even control loads of other things besides lights, you could set up a siren or anything else that will grab your attention. Happy monitoring and enjoy the lights!

 

 

Get unified insight into your IT operations with Opsview Monitor

webteam's picture
by Opsview Team,
Administrator
Opsview is passionately focused on monitoring that enables DevOps teams to deliver smarter business services, faster.

More like this

Nov 21, 2011
Blog
By Opsview Team, Administrator

 Opsview gives you lots of standard options for receiving status notifications, but with a bit of custom scripting you can also get status...

Jun 19, 2012
Blog
By Opsview Team, Administrator

Sometimes it may be necessary to create and run custom notification methods depending on your environment and requirements in accordance with...

Feb 05, 2016
Whitepapers
By Opsview Team, Administrator

Monitoring software does not serve the purpose of replacing technical talent. Instead, it is there to make the lives of technical resources easier...