Opsview and Nagios notifiy via Asterisk script

This script is to help you enable Opsview or Nagios to call via an Asterisk server when a notification is rised.

DEPENDENCIES

Perl Modules

  • Asterisk::Manager
  • File::NFSLock

INSTALLATION

Opsview 2.10.x
Warning: This will break the RSS Feeds functionality. If you want to conserve it, be consequent, and write a wrapper script that will execute the asterisk script and the atom-generator script.
Install the script on the master Opsview server. Chown it to the nagios user and give it execution for the nagios user.

mv /usr/local/nagios/bin/atom-generator /usr/local/nagios/bin/atom-generator.original
ln -s /PATH/TO/asterisk_notify /usr/local/nagios/bin/
If you are using a distributed Opsview setup, you have to apply a patch to the nagconfgen script.

cd /usr/local/nagios/bin
patch < /PATH/TO/patch_contactinfo_for_distprofile.patch

Opsview 2.12
Configuring the script for Opsview 2.12 is as easy as putting the script in /usr/local/nagios/libexec/notifications and running /usr/local/nagios/bin/send2slaves -p as the nagios user (if you have slaves). Then go to the Opsview administration interface, and under Advanced -> SMS Notifications -> Create new SMS Notification Methods:

  • Name: Asterisk
  • Run On:    
    • Monitoring Server: The command will be run on the Master monitoring server. This is for situations where the asterisk box can only be seen by the master monitoring server, or if you have only one box.
    • Slave: This means the command will be run on the slave that has detected the alert. If your slaves have visibility of your asterisk box, you can activate this option. Note that locking is done per machine, so you could get two simultaneous calls.
  • Command: asterisk_notify

Go to: Advanced -> System Preferences, choose "Asterisk", and submit the changes

For every contact with an "SMS number" filled in, the asterisk_notify script will be run.

Nagios
Opsview is based on Nagios, so the script will work with Nagios, but Nagios is very configurable and flexible, so I won't bother. If you send me an example of how you have configured Nagios to use this script, I will be happy to paste the instructions here.

CONFIGURATION

At the start of the script there are a couple of variables that you have to touch. I know this is not elegant (look at the TODO list), but it works for us and lets the script be VERY flexible.

my $TELEPHONE = $ENV{'NAGIOS_CONTACTPAGER'};
$TELEPHONE =~ s/^\+[0-9]{2}// if (defined $TELEPHONE);
Where to get the telephone to call from. We ajust the SMS number in Opsview to the on-guard persons telephone number. Opsview 2.10 has an format restriction on the SMS number, so we added the regex because our asterisk wants the phone without the +34. This restriction will not be in Opsview 2.11.
my $ASTMAN_HOST = 'asterisk.example.com';
my $ASTMAN_USERNAME = 'admin';
my $ASTMAN_SECRET   = 'changeme';
The host and credentials for the asterisk manager protocol.
my $LOCK_DIR = '/tmp/';
Directory for the lock files to live in.
my $LOCK_PREFIX = 'nagios-asterisk-';
A prefix for the lock files, so they can be identified, or don't collide with other lock files.
my $CALLER_KEY = 'nagios-phone';
asterisk_notify will not call a CALLER_KEY if it has already called CALLER_KEY less than DONTCALL_BEFORE seconds. You can play with this configuration depending on your needs. Examples are: $CALLER_KEY = "$ENV{NAGIOS_CONTACTPAGER}" wont call the same phone number two times in a row. $CALLER_KEY = "$ENV{NAGIOS_HOSTNAME}-$TELEPHONE" will call the same number if the notification is for different hosts.
my $DONTCALL_BEFORE = 60*15 #15 minutes
Don't call a CALLER_KEY if less than this number of seconds have passed since the last call to the CALLER_KEY value.
my %ASTERISK_COMMAND = (
    Action => 'Originate',
    Channel => 'Local/s@custom-nagiosnotify',
    Timeout => 3600000,
    Exten => 's',
    Priority => 1,
    Context => 'custom-nagiosnotify',
    Callerid => 'NAGIOS',
    Variable => "NUM=0$TELEPHONE#"
);

The Asterisk Manager command to send to asterisk. See this documentation for details. Here it is calling a custom extension called "nagios-notify", which expects a variable NUM with the telephone number to call.

my $LOG_FILE = '/var/log/asterisk_notify.log';

The log file has to go somewhere...

ASTERISK CONFIGURATION

You can be as creative as you want in asterisk. The notification script lets you specify anything you want: from a simple phone call to an operator huntdown.

You have to give the opsview master permission to connect and use the asterisk manager protocol in asterisks manager.conf. Add a new user with call permissions, and permit connection with that user from the opsview master.

TODO

  • Configuration File: I want to keep it as flexible as possible. I am open to contributions and suggestions.
  • Contribute a patch to Asterisk::Manager so this line doesn't appear in logs:
    Use of uninitialized value in string eq at
    /usr/share/perl5/Asterisk/Manager.pm line 372
    

ALTERNATIVES

  • Nagios2Asterisk: Wasn't flexible enough for us... maybe it is for you. Just creates a call file on the asterisk server. Doesn't limit the number of parallel calls it will make.
  • Do you know of anything else?

CONTACT

You can contact me at join('jl','martinez','@','capside.com')

LICENSE

This software is distributed "as-is" and with no guarantee. Use with care and at your own risk.

MORE INFO

DOWNLOAD

Version 1.00