📜 ⬆️ ⬇️

Nagios + SMS using a mobile phone

A few posts before were already topics on the use of SMS notifications in Nagios. Today I will talk about another method of notification. The method described below is somewhat more reliable than those described earlier, but it also requires some monetary investment. It is useful in the case when some of the notifications are critical (such as the failure of the air conditioner or an increase in humidity).

The way is to use a mobile phone with a corporate rate (so that the money on the phone does not run out unexpectedly).

Physically connects to the server via bluetooth, com or usb. At the software level, we will use two scripts: one of them can send sms, the second checks the status of the mobile network. If the mobile network is not available, then nagios sends a message to the email.
')
Both scripts are written in python and use the gammu library to connect to the phone.


The first script: check_sendsms.py - check network status



#!/usr/bin/env python import gammu import sys # Create state machine object sm = gammu.StateMachine() # Read /etc/gammurc sm.ReadConfig() # Connect to phone sm.Init() # Reads network information from phone netinfo = sm.GetNetworkInfo() # Print information print 'State: %s' % netinfo['State'] if netinfo['State'] != "HomeNetwork": sys.exit(2) 


The second script: sendsms.py - actually sending sms



 #!/usr/bin/env python import gammu import sys if len(sys.argv) != 3: print 'Usage: sendsms.py number1[,number2][...] "message"' sys.exit(1) # we are going to send first 160 characters only text_message=sys.argv[2][:160] # connect to phone sm = gammu.StateMachine() # Read /etc/gammurc sm.ReadConfig() sm.Init() # send messages for phone_number in sys.argv[1].split(','): sms_message = {'Text': text_message, 'SMSC': {'Location': 1}, 'Number': phone_number} try: sm.SendSMS(sms_message) except: print "Sorry, I can't send message to %s" % phone_number 


Few comments


Source: https://habr.com/ru/post/96610/


All Articles