Page 2 of 3
Re: Need Assistance with sending SMS alerts
Posted: Wed Jan 14, 2015 5:19 pm
by Berto
This is what my contact.cfg looks like....I added the "address1" line.
define contact{
contact_name nagiosadmin ; Short name of user
use generic-contact ; Inherit default values from generic-contact template (defined above)
alias Nagios Admin ; Full name of user
email
[email protected] ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
address1
[email protected] ; Cell Carrier
}
Yes my cell carrier supports it as I tested sending myself a text via email (Outlook).
--Berto
Re: Need Assistance with sending SMS alerts
Posted: Wed Jan 14, 2015 5:26 pm
by eloyd
"addressX" is an additional set of addresses that you can define for a contact, but unless you also change how the contacts are notified, they will not be used by default.
Just change the email address to be your SMS address and you should be able to get the messages without issue. If you want both email and SMS, then you can create two contacts, one with the email set to your email and one with the email set to your SMS. Then change the notification group to include both contacts and you will get both.
Re: Need Assistance with sending SMS alerts
Posted: Wed Jan 14, 2015 5:55 pm
by abrist
To add to this, you could have the notification command send to both, but you will have to alter the command to take two actions, one on the standard $CONTACTEMAIL$ and the other on $CONTACTADDRESSn$. Like:
Code: Select all
/usr/bin/printf "%b" "***** Nagios Monitor XI Alert *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$; /usr/bin/printf "%b" "***** Nagios Monitor XI Alert *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTADDRESS1$
Re: Need Assistance with sending SMS alerts
Posted: Wed Jan 14, 2015 6:32 pm
by rhassing
What if the internet connection fails? You will not get notified if the system is unable to send mail?
That's why we are using a device which is capable of sending text messages and are we sending mails as a backup mechanism.
The contact looks like this:
Code: Select all
define contact {
contact_name Standby_pool1
alias Standby_pool1
contactgroups deltics
host_notifications_enabled 1
service_notifications_enabled 1
host_notification_period 24x7 except maintenance
service_notification_period 24x7 except maintenance
host_notification_options d,u,r
service_notification_options w,u,c,r
host_notification_commands host-notify-by-email,host-notify-by-sms
service_notification_commands notify-by-email,notify-by-sms
retain_status_information 1
email [email protected]
pager 31653501111
}
Notify by mail is not really special:
Code: Select all
define command {
command_name host-notify-by-email
command_line /usr/bin/printf "%b" "***** Nagios Server @ AZP *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInf$
}
To send text messages we do the following:
Code: Select all
define command {
command_name host-notify-by-sms
command_line /usr/local/bin/sms_nagios.pl $CONTACTPAGER$ ' Host $HOSTNAME$ ($HOSTALIAS$) is $HOSTSTATE$ @$SHORTDATETIME$ Info: $HOSTOUTPUT$ $NOTIFICATIONTYPE$'
}
The script for sending the mail and putting it into MySQL:
Code: Select all
#!/usr/bin/perl -w
# flooding timer, how many seconds between messages
#my $numsec='300'; # 5 minuts
my $numsec='0'; # 0 minuts
#Declaration of Perl Modules to use.
use strict;
use DBI;
use DBD::mysql;
use Net::SMTP;
#Declaration of used Constants/Variables
my ($msg_body, $msg_sender , $msg_rcpt, $sthm, $sql, @row, $sthm2, @lockedrow, $sql_todo, $smtp);
#check ob wir richtig aufgerufen werden:
die unless ( scalar(@ARGV)==2 ) ;
#Database Setup
#mysql server
my $DBuser="sms";
my $DBpass="smstool";
my $dbhm=DBI->connect("dbi:mysql:sms:localhost","$DBuser","$DBpass",
{
PrintError => 1,
}
);
unless ( $dbhm ){
die("connection does not work properly!");
}
my $dbhm2=DBI->connect("dbi:mysql:sms:localhost","$DBuser","$DBpass",
{
PrintError => 1,
}
);
## end database
#main - read incoming sms message and queue it to smsd
$msg_sender="nagios";
$msg_rcpt=shift();
$msg_body=shift();
my $msg_body_160 = substr($msg_body,0,160);
## delete old flood lock
$sql = "DELETE FROM floodprotection WHERE lastsend < DATE_SUB(now(),INTERVAL $numsec SECOND)";
$sthm=$dbhm->prepare($sql);
$sthm->execute();
$sql_todo="SELECT count(*) as counter FROM floodprotection WHERE phonenumber = $msg_rcpt ";
$sthm2=$dbhm2->prepare($sql_todo);
$sthm2->execute();
@lockedrow=$sthm2->fetchrow_array();
unless ($msg_body =~ m/SMS Ack/g) { # Wichtig damit SMS Ack's zurümmen
exit if ($lockedrow[0] == 1) ;
}
# now send the sms's
system("/usr/local/bin/sendsms $msg_rcpt '$msg_body_160'");
# sms sent set up the flood lock
$sql_todo ="INSERT INTO floodprotection values ( '' , $msg_rcpt , now() ) ";
$sthm2=$dbhm2->prepare($sql_todo);
$sthm2->execute();
The sendsms script:
Code: Select all
#!/bin/sh
# This script send a text sms at the command line by creating
# a sms file in the outgoing queue.
# $1 is the destination phone number.
# $2 is the message text.
# If you leave $2 or both empty, the script will ask you.
# If you give more than 2 arguments, last is taken as a text and
# all other are taken as destination numbers.
# If a destination is asked, you can type multiple numbers
# delimited with spaces.
smsd_user="smsd"
# Will need echo which accepts -n argument:
ECHO=echo
case `uname` in
SunOS)
ECHO=/usr/ucb/echo
;;
esac
DEST=$1
TEXT=$2
if [ -z "$DEST" ]; then
printf "Destination(s): "
read DEST
if [ -z "$DEST" ]; then
echo "No destination, stopping."
exit 1
fi
fi
if [ -z "$TEXT" ]; then
printf "Text: "
read TEXT
if [ -z "$TEXT" ]; then
echo "No text, stopping."
exit 1
fi
fi
if [ $# -gt 2 ]; then
n=$#
while [ $n -gt 1 ]; do
destinations="$destinations $1"
shift
n=`expr $n - 1`
done
TEXT=$1
else
destinations=$DEST
fi
echo "Text: $TEXT"
for destination in $destinations
do
echo "To: $destination"
owner=""
if [ -f /etc/passwd ]; then
if grep $smsd_user: /etc/passwd >/dev/null; then
owner=$smsd_user
fi
fi
TMPFILE=`mktemp /tmp/smsd_XXXXXX`
$ECHO "To: $destination" >> $TMPFILE
$ECHO "" >> $TMPFILE
$ECHO -n "$TEXT" >> $TMPFILE
if [ "x$owner" != x ]; then
chown $owner $TMPFILE
fi
FILE=`mktemp /var/spool/sms/outgoing/send_XXXXXX`
mv $TMPFILE $FILE
done
But this is part of the smstools3 files.
Re: Need Assistance with sending SMS alerts
Posted: Wed Jan 14, 2015 6:44 pm
by eloyd
I think we're getting way off the original poster's question: He just wants to know how to send SMS messages. Best practices, fault tolerance, and other issues are probably "wait until tomorrow."

Re: Need Assistance with sending SMS alerts
Posted: Wed Jan 14, 2015 6:49 pm
by rhassing
Just providing a nice solution

Re: Need Assistance with sending SMS alerts
Posted: Thu Jan 15, 2015 10:27 am
by abrist
Quit being so helpful!!!!!! (just kidding, please continue)
Re: Need Assistance with sending SMS alerts
Posted: Fri Jan 16, 2015 9:10 am
by Berto
Thank you for all the replies that I have gotten. I have a small issue that maybe I'm just overlooking since I'm new to Nagios. When I add just the cell phone number to the email address line, I do receive the alert in a text which is great. So I added another contact in contacts.cfg and added my work email address so I can receive both. I get the email but not the text anymore, so not sure if I need to do something else other then adding another contact. Here is what my contact.cfg looks like;
define contact{
contact_name nagiosadmin
use generic-contact
alias Nagios Admin
email
[email protected]
}
define contact{
contact_name nagiosoncall
use generic-contact
alias Nagios Oncall
email
[email protected]
}
define contactgroup{
contactgroup_name admins
alias Nagios Administrators
members nagiosadmin
}
define contactgroup{
contactgroup_name oncall
alias UNIX Open Oncall
members nagiosoncall
}
To reply back to rhassing;
Our Network/Internet for my company is handled at our Corp level and our division is then given subnets for our data centers to use, so if our network/internet fails then our corp IT would be the dealing with those issues and not my unix group as we have not control over firewalls, routers, switches, etc.
Thanks again for all the assistance.
--Berto
Re: Need Assistance with sending SMS alerts
Posted: Fri Jan 16, 2015 9:16 am
by eloyd
A nagios contactgroup contains one or more contacts. The group name can then be used to send notifications to everyone in the group at once. So try:
Code: Select all
define contactgroup{
contactgroup_name admins
alias Nagios Administrators
members nagiosadmin,nagiosoncall
}
Then make sure that the services and hosts that you are notifying use the nagiosadmin contactgroup. This will send to both nagiosadmin and nagiosoncall at the same time.
This, and more, documentation is avaiable at
http://nagios.sourceforge.net/docs/nagi ... ml#contact
Re: Need Assistance with sending SMS alerts
Posted: Fri Jan 16, 2015 2:56 pm
by tmcdonald
Thank you eloyd!
Berto, let us know how that works out for you.