Need Assistance with sending SMS alerts

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Berto
Posts: 162
Joined: Tue Jul 01, 2014 6:12 pm

Re: Need Assistance with sending SMS alerts

Post 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
User avatar
eloyd
Cool Title Here
Posts: 2190
Joined: Thu Sep 27, 2012 9:14 am
Location: Rochester, NY
Contact:

Re: Need Assistance with sending SMS alerts

Post 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.
Image
Eric Loyd • http://everwatch.global • 844.240.EVER • @EricLoyd
I'm a Nagios Fanatic! • Join our public Nagios Discord Server!
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: Need Assistance with sending SMS alerts

Post 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$
Former Nagios employee
"It is turtles. All. The. Way. Down. . . .and maybe an elephant or two."
VI VI VI - The editor of the Beast!
Come to the Dark Side.
User avatar
rhassing
Posts: 416
Joined: Sat Oct 05, 2013 10:29 pm
Location: Netherlands

Re: Need Assistance with sending SMS alerts

Post 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.
Rob Hassing
Image
User avatar
eloyd
Cool Title Here
Posts: 2190
Joined: Thu Sep 27, 2012 9:14 am
Location: Rochester, NY
Contact:

Re: Need Assistance with sending SMS alerts

Post 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." :-)
Image
Eric Loyd • http://everwatch.global • 844.240.EVER • @EricLoyd
I'm a Nagios Fanatic! • Join our public Nagios Discord Server!
User avatar
rhassing
Posts: 416
Joined: Sat Oct 05, 2013 10:29 pm
Location: Netherlands

Re: Need Assistance with sending SMS alerts

Post by rhassing »

Just providing a nice solution :-)
Rob Hassing
Image
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: Need Assistance with sending SMS alerts

Post by abrist »

Quit being so helpful!!!!!! (just kidding, please continue)
Former Nagios employee
"It is turtles. All. The. Way. Down. . . .and maybe an elephant or two."
VI VI VI - The editor of the Beast!
Come to the Dark Side.
Berto
Posts: 162
Joined: Tue Jul 01, 2014 6:12 pm

Re: Need Assistance with sending SMS alerts

Post 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
User avatar
eloyd
Cool Title Here
Posts: 2190
Joined: Thu Sep 27, 2012 9:14 am
Location: Rochester, NY
Contact:

Re: Need Assistance with sending SMS alerts

Post 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
Image
Eric Loyd • http://everwatch.global • 844.240.EVER • @EricLoyd
I'm a Nagios Fanatic! • Join our public Nagios Discord Server!
tmcdonald
Posts: 9117
Joined: Mon Sep 23, 2013 8:40 am

Re: Need Assistance with sending SMS alerts

Post by tmcdonald »

Thank you eloyd!

Berto, let us know how that works out for you.
Former Nagios employee
Locked