SMS in Nagios Core

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.
Locked
mohamadsh
Posts: 2
Joined: Thu Jun 22, 2017 6:05 am

SMS in Nagios Core

Post by mohamadsh »

Dear All ,

I want to add a SMS (PHP API) when Nagios send alert Email , i just need the script code when Nagios send alert emails .

Best Regards
Mohammad Alshoaibi
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

Re: SMS in Nagios Core

Post by mcapra »

You might want to review this documentation:
https://assets.nagios.com/downloads/nag ... dlers.html

If you're using the stock Nagios Core configurations, you should have the following command definitions:

Code: Select all

# 'notify-host-by-email' command definition
define command{
	command_name	notify-host-by-email
	command_line	/usr/bin/printf "%b" "***** Nagios *****\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$
	}

# 'notify-service-by-email' command definition
define command{
	command_name	notify-service-by-email
	command_line	/usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
	}
And the way those command definitions get referenced is in terms of contacts:

Code: Select all

define contact{
        name                            generic-contact    	; The name of this contact template
        service_notification_period     24x7			; service notifications can be sent anytime
        host_notification_period        24x7			; host notifications can be sent anytime
        service_notification_options    w,u,c,r,f,s		; send notifications for all service states, flapping events, and scheduled downtime events
        host_notification_options       d,u,r,f,s		; send notifications for all host states, flapping events, and scheduled downtime events
        service_notification_commands   notify-service-by-email	; send service notifications via email
        host_notification_commands      notify-host-by-email	; send host notifications via email
        register                        0       		; DONT REGISTER THIS DEFINITION - ITS NOT A REAL CONTACT, JUST A TEMPLATE!
        }
Notice the service_notification_commands and host_notification_commands variables in the above contact definition. Those commands are what gets fired when Nagios receives information that a notification should be sent to a specific contact. The commands can be anything; an echo to stdout, a blaring alarm located in some NOC, a rube goldberg maching that dumps water on your sysadmin, whatever.

So, if you wanted a custom PHP script that is responsible for sending SMS messages to be called when notifications occur:
  • Write the PHP script
  • Define it as a Nagios command
  • Assign that Nagios command to a Nagios contact
  • Assign that Nagios contact to whatever Nagios hosts/services
Last edited by mcapra on Thu Jun 22, 2017 11:56 am, edited 1 time in total.
Former Nagios employee
https://www.mcapra.com/
dwhitfield
Former Nagios Staff
Posts: 4583
Joined: Wed Sep 21, 2016 10:29 am
Location: NoLo, Minneapolis, MN
Contact:

Re: SMS in Nagios Core

Post by dwhitfield »

Additionally, if you want to dig into how it all gets handled on the back end, the code is available at https://github.com/NagiosEnterprises/nagioscore/
mohamadsh
Posts: 2
Joined: Thu Jun 22, 2017 6:05 am

Re: SMS in Nagios Core

Post by mohamadsh »

Write the PHP script
this is PHP script for example :

Code: Select all

$url_API = "http://sms.provider.co/http/send_sms_http.php?login_name='.$user_name.'&login_password='.$pass.'&msg='.$body.'&mobile_number='.$to.'&from='.$from.'&charset='.$charset.'&dlr=1 ";
$result = file_get_contents($url_API);
Q1 : where i should add this code .. in any file ?!
Q2 : how i can get the value of " SMS body " variable .. the body of notification that Nagios Sent by Email. ?!
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

Re: SMS in Nagios Core

Post by mcapra »

mohamadsh wrote: Q1 : where i should add this code .. in any file ?!
mcapra wrote: Notice the service_notification_commands and host_notification_commands variables in the above contact definition. Those commands are what gets fired when Nagios receives information that a notification should be sent to a specific contact.
You wouldn't add the code anywhere. You would create it as a script that accepts arguments, then use that script in a Nagios command definition, then assign that command to the Nagios contact's service_notification_commands and host_notification_commands variables.
mohamadsh wrote: Q2 : how i can get the value of " SMS body " variable .. the body of notification that Nagios Sent by Email. ?!
In your Nagios command definition, you have macros available to you. You can see several of them on the stock notify-service-by-email and notify-host-by-email command definitions. Stuff like $HOSTOUTPUT$, $HOSTNAME$, $HOSTSTATE$, etc. If you wanted the SMS body to be the output of the check that was run, you might use $SERVICEOUTPUT$ or $LONGSERVICEOUTPUT$.

Here's all the available Nagios macros and their respective contexts. Your context would be "Service Notifications" and "Host Notifications", so anything with a green "Yes" is something you can use in the command definition.
https://assets.nagios.com/downloads/nag ... olist.html
Former Nagios employee
https://www.mcapra.com/
dwhitfield
Former Nagios Staff
Posts: 4583
Joined: Wed Sep 21, 2016 10:29 am
Location: NoLo, Minneapolis, MN
Contact:

Re: SMS in Nagios Core

Post by dwhitfield »

Thanks @mcapra!
DiegoAnjos
Posts: 49
Joined: Thu Feb 14, 2013 9:32 am

Re: SMS in Nagios Core

Post by DiegoAnjos »

I don't know about SMS, but I am receiving alerts by Telegram

I followed the steps as on https://pommi.nethuis.nl/nagios-notific ... a-telegram
But be aware that there is missing information on the step-by-step. You must install Unofficial Telegram Bot API Client (https://github.com/datamachine/twx.botapi) to get it working.

Hope it helps you.
dwhitfield
Former Nagios Staff
Posts: 4583
Joined: Wed Sep 21, 2016 10:29 am
Location: NoLo, Minneapolis, MN
Contact:

Re: SMS in Nagios Core

Post by dwhitfield »

@mohamadsh, were you able to resolve your issue? Are we ready to lock this up?

@DiegoAnjos, thanks so much for the helpful tip!
Locked