SMS notifications from nagios using clickatell.com and a bash site monitor

Good afternoon comrades
Like everyone who deals with remote systems, I needed to monitor a lot of machines and services on them. Scrolling through the descriptions and manuals of several programs, he stopped at Nagios. Many articles and examples on it and a very rich setup turned out to be what you need. And so I decided to share a few points in the implementation and configuration of a self-written plugin written in Bash and an SMS notification system using clickatell.com.
Why clickatell.com? Yes, it’s just that it’s already used in the organization, but I decided to use it also for this action.
How to configure nagios there are a lot of articles, and I will not focus on this, I will go straight to the description of what I learned and how it works.
Message sending service
By looking at the API of clickatell.com itself, it became much clear and understood how everything was implemented there, and since I know only bash decided to write on it.
text1 = $ 1
nombertel = $ 2
wget --post-data "api_id = 111111 & user = User1 & password = pass1" api.clickatell.com/http/auth --quiet -O -> / tmp / sms_tmp_id
session_id = $ (cut -f2 -d "" / tmp / sms_tmp_id)
wget --post-data "session_id = $ session_id & to = $ nombertel & text = $ text1" api.clickatell.com/http/sendmsg --quiet -O - rm / tmp / sms_tmp_id
exit 0
The first wget --post-data is sending data to generate the session id, it consists of api_id, this is the service id used when sending SMS, user is the login user name clickatell.com and password, respectively, is the password. Clickatell.com has an APi check for session_id, but I went in a simple way, by simply generating a new one. Alas, the clickatell.com API can only be viewed after registering on their site.
The second wget --post-data we already send some text to the specified number. session_id is the session identifier, to the phone number to which the message is sent and the text text of the message. A check showed that everything works, but of course there is, but this is a maximum message length of 70 characters. Therefore, we transmit only the most important, unlike the mailing list service.
Here is the service itself responsible for sending messages from from nagios (one is responsible for the hosts of the other for services)
command_name sms-host
command_line /usr/bin/sms_get_number.sh "** $ NOTIFICATIONTYPE $ Host Alert: $ HOSTNAME $ is $ HOSTSTATE $ **" $ CONTACTADDRESS1 $
}
define command {
command_name sms-services
command_line / usr / bin / sms_get_number.sh "$ SERVICEDESC $ Host: $ HOSTALIAS $ $ SERVICEOUTPUT $" $ CONTACTADDRESS1 $
}
And this is the user config to whom to receive notifications
contact_name mobile
alias mobile
service_notification_period noworktime
host_notification_period noworktime
service_notification_options w, u, c
host_notification_options d
service_notification_commands sms-services # Names of the service described above
host_notification_commands sms-host # Names of the service described above
address1
} 791111111
With this setting, we will receive notifications about the fall of services and hosts in the time period described as noworktime.
Yes, if anyone is interested in the noworktime config, it’s a non-working time and days off, so that the phone doesn’t block messages again, because on a working day everything will go to the mail.
timeperiod_ noworktime
alias no work region SPB
sunday 00: 00-24: 00
monday 00: 00-05: 30,14: 30-24: 00
tuesday 00: 00-05: 30,14: 30-24: 00
wednesday 00: 00-05: 30,14: 30-24: 00
thursday 00: 00-05: 30,14: 30-24: 00
friday 00: 00-05: 30,14: 30-24: 00
saturday 00 : 00-24: 00
}
# The time on the server is offset by a couple of hours from this, and such numbers.
And yes, I almost forgot. I also wanted to throw the script on public display, which sends an error from a specific resource.
Web site error monitoring service
Such a service was needed to quickly respond to the situation with the fall of the site. There is a web server diagnostic service in nagios, but it doesn’t suit me because it diagnoses the whole server and does not return error codes. And on one server there can be many resources, so write your own.
In general, here is the script for diagnosing the web resource /usr/bin/check_http_error.sh
Config for a specific site (you can just have an IP address)
#! / bin / bash
site1 = $ 1
exitservice = 3
WGET = `wget $ site1 -O / tmp / check_site_error 2> / tmp / error_service.tmp`
if [$? -ne 0]
then
outmes = `grep -o“ ERROR. * ”/ tmp / error_service.tmp`
echo" $ outmes $ site1 "
exitservice = 2
else
outmes =" OK $ site1 "
echo $ outmes
exitservice = 0
fi
rm / tmp / error_service.tmp
rm / tmp / check_site_error
exit $ exitservice
And this is the nagios config using the script /usr/bin/check_http_error.sh
command_name check_site
command_line /usr/bin/check_http_error.sh $ ARG1 $
}
Yes, here is a description of the service itself.
Alas, each service has to be described separately because of its specificity and I enter it in localhost.
name check_site
use base-service
service_description check_site
check_command check_site! http: //mai.ru
host_name localhost
}
Well, in general, that's all, if there is any problem with the site, you will receive an error code and on which resource. It helps me in a timely manner to determine DDoS attacks and intrusions.
And yes, if it interests me, I can still publish an article about the service for nagios that determines the free space by snmp in percentage terms.
I have everything, thank you for your attention.