Wednesday, August 24, 2011

Nagios Alert using Gammu


Nagios Alert Notification Using Gammu

Install Epel repo

  • Download the latest EPEL repository
#wget http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-3.noarch.rpm
  • Run the rpm install command
#rpm -Uvh epel-release-5*.rpm

Install Gammu

There are 3 package need to be install
  1. gammu
  2. gammu-devel
  3. gammu-libs
After install, Open gammu config file and edit it
#vi /etc/gammurc
[gammu]
port = /dev/ttyS0
connection = at19200

for the connection type, please refer to the mobile model at http://wammu.eu/phones/
plug the mobile to the server and test if it can detect the mobile by using this command
#gammu –identify
#gammu –monitor 1

# ls -l /dev/ttyS0 crw-rw—- 1 root dialout 4, 64 jan 5 16:14 /dev/ttyS0
Nagios is usually running as user “nagios”, so any notification command would be executed as “nagios”. We can see user nagios wouldn’t have permissions to access devices connected to /dev/ttyS0.
We’ll apply the SUID bit (set-UID) on the command in charge of sending SMS notifications, this will execute gammu on behalf of user root.
# chmod 4755 /usr/bin/gammu
Now we test to send out a sms testing by
# echo “test” | gammu –sendsms TEXT 0128157399
If you want break, press Ctrl+C…
Sending SMS 1/1….waiting for network answer..OK, message reference=181

This proof it is success

Edit Nagios

We will start to edit nagios to allow send out command to trigger sms notification.
#vi /etc/nagios/objects/commands.cfg
then add this part into it

this will ask nagios to execute a script and pass the parameter to script

define command{
      command_name    host-notify-by-sms
      command_line    /bin/bash /etc/nagios/nagios-test "$HOSTNAME$" "$HOSTSTATES$" "$HOSTOUTPUT$" "$SHORTDATETIME$"
      }
define command{
      command_name    notify-by-sms
      command_line    /bin/bash /etc/nagios/nagios-test "$HOSTALIAS$" "$SERVICESTATE$" "$SERVICEOUTPUT$" "$SHORTDATETIME$"
      }
then we edit the contact part
#vi /etc/nagios/objects/contacts.cfg
then add this part into it
define contact{
      contact_name                    sms
      alias                           sms
      service_notification_period     24x7
      host_notification_period        24x7
      service_notification_options    c,r
      host_notification_options       d,r
      service_notification_commands   notify-by-sms
      host_notification_commands      host-notify-by-sms
      }
define contactgroup{
      contactgroup_name       admins
      alias                   Nagios Administrators
      members                 nagiosadmin, sms
      }

Script

hostname=$1
 hoststates=$2
 hostoutput=$3
 shortdatetime=$4
 
 echo "Host: $hostname / state: $hoststates / Info: $hostoutput / Date: $shortdatetime" | ssh root@172.30.10.98 'gammu --sendsms TEXT 012345678'
 echo "Host: $hostname / state: $hoststates / Info: $hostoutput / Date: $shortdatetime" | ssh root@172.30.10.98 'gammu --sendsms TEXT 0123338888'

The script will accept the parameter and form a message using echo and pass it to another server which was install with Gammu to send out the sms
So in order to allow Nagios to use ssh, below section need to be perform


Nagios SSH

We will using Nagios to setup ssh keygen so that it can no need input password when ssh to gammu server

  • login to Nagios server as root
  • change to nagios user using
#su nagios
  • then we create the keygen for nagios
#ssh-keygen
  • then scp the id_rsa.pub to the gammu server and add into authorized keys
#cat id_rsa.pub » /root/.ssh/authorized_keys
Done, now nagios allow to use SSH and able to enter the gammu server

Nagios timeout

Nagios default timeout for notification was 30 second and was not enough if we need to send out sms alert more than 3 number.
Therefor we will need to increase the timeout #vi /etc/nagios/nagios.cfg
Original was
notification_timeout=30
then edit
notification_timeout=60

1 comment: