So, In this article we are going to add SMS notification in Nagios.
Nagios is the most used open source Network and Server Monitoring tool. So, we use Nagios to monitor different type of SNMP enabled hosts like Switch, Routers, Servers for critical services. Moreover, Nagios can also fetch server performance data using NRPE agent. Furthermore visual web portal and sound alarm, we can send email notification using Nagios. So, now we will send SMS notification from Nagios using Kannel SMS Gateway.
We already have a Nagios server installed and running. So, to install and configure Nagios Monitoring server please check our other topics on Nagios Installation and configuration.
So, this will be our Lab Environment:
Nagios and Kannel Server IP: 192.168.10.38
Kannel Send SMS User: kannel
Kannel Send SMS User Password: kannelpasswd
Configuring Nagios:
So, in this Lab Environment we have installed Nagios from source tarball in default directory /usr/local/nagios/
First we will need to define commands in /usr/local/nagios/etc/objects/commands.cfg object configuration file. So, to define commands add below lines in commands.cfg file
# 'notify-host-by-sms' command definition
define command{
command_name notify-host-by-sms
command_line /usr/bin/printf "%b" "Nagios: *$NOTIFICATIONTYPE$* \n$HOSTALIAS$ is $HOSTSTATE$ Info is: $HOSTOUTPUT$ Time: $SHORTDATETIME$" | /usr/bin/curl "http://192.168.10.38:13013/cgi-bin/sendsms?username=kannel&password=kannelpasswd&to=$CONTACTPAGER$" -G --data-urlencode text@-
}
# 'notify-service-by-sms' command definition
define command{
command_name notify-service-by-sms
command_line /usr/bin/printf "%b" "Nagios: *$NOTIFICATIONTYPE$*\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nStat: $SERVICESTATE$\nInfo: $SERVICEOUTPUT$\nTime: $SHORTDATETIME$" | /usr/bin/curl "http://192.168.10.38:13013/cgi-bin/sendsms?username=kannel&password=kannelpasswd&to=$CONTACTPAGER$" -G --data-urlencode text@-
}
Then, now we will add notification target user’s Mobile Phone number in Nagios contact object configuration file: /usr/local/nagios/etc/objects/contacts.cfg
Sample contact definition:
define contact{
contact_name omar
use generic-contact
alias Omar Ali
service_notification_commands notify-service-by-sms
host_notification_commands notify-host-by-sms
email [email protected]
pager +88017XXXXXXXX
}
Additionally, we can send SMS notification as well as email to the same contact by using below definition
define contact{
contact_name shamim
use generic-contact
alias Shamim Ahmed
service_notification_commands notify-service-by-email,notify-service-by-sms
host_notification_commands notify-host-by-email,notify-host-by-sms
email [email protected]
pager +88017YYYYYYYY
}
After that, create a contactgroup for those who will get notification for a service. So, let’s define a contactgroup under contract definition for Network Admins who will get notified if any network service fails
define contactgroup {
contactgroup_name netadmins
alias Network Administrators
members omar,shamim
}
So, we have a MikroTik router having 3 upstream ports: sfp1, sfp2 and sfp3, which we are going to add in monitoring system.
Then, create an object configuration file /usr/local/nagios/etc/objects/nethosts.cfg for monitoring Network Devices with below contents
#### Host Defination ####
define host {
use generic-switch
host_name router-1
alias Router-1
address 192.168.10.1
hostgroups net-hosts
notifications_enabled 1
contact_groups netadmins
}
#### Host Group Defination ####
define hostgroup {
hostgroup_name net-hosts
alias Network Hosts
}
#### Service Defination for Network hosts ####
define service {
use generic-service
host_name router-1
service_description SFP-1
check_command check_snmp!-C snmpCommunity -o ifOperStatus.1 -r 1 -m RFC1213-MIB
check_interval 3
retry_interval 1
notifications_enabled 1
contact_groups netadmins
}
define service {
use generic-service
host_name router-1
service_description SFP-2
check_command check_snmp!-C snmpCommunity -o ifOperStatus.2 -r 1 -m RFC1213-MIB
check_interval 3
retry_interval 1
notifications_enabled 1
contact_groups netadmins
}
define service {
use generic-service
host_name router-1
service_description SFP-3
check_command check_snmp!-C snmpCommunity -o ifOperStatus.3 -r 1 -m RFC1213-MIB
check_interval 3
retry_interval 1
notifications_enabled 1
contact_groups netadmins
}
Above check_snmp commands needs MIB: RFC1213-MIB, that should be preinstalled in your system. So, if it is not present in your system, you may download and install it by using below packages
For Debian/Ubuntu: Install snmp-mibs-downloader from non-free repo, then run download-mibs command from shell
# apt-get install -y snmp-mibs-downloader
# download-mibs
So, if you use CentOS 7/8
# yum install net-snmp-libs
Also, include the object configuration file nethosts.cfg in /usr/local/nagios/etc/nagios.cfg
So, run /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg to verify your configurations
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
So, if there is no error, restart Nagios Service
# systemctl restart nagios.service
Nagios Notifications:
Then, check the Nagios portal for Router-1 port Status
So, We found all 3 SFP ports are in up/running state. Now we are going to manually disconnect SFP-1 port which should trigger a down notification and send SMS to Network Admin Group
Nagios Notification Log:
Kannel Log:
Reconnecting the cable:
Also, check the notification Log:
Kannel Log:
Finally, the tecipient Mobile Phone: