Back to Home

Nagios + SMS using a mobile phone

nagios · linux · sms · python · gammu · scripting

Nagios + SMS using a mobile phone

    A few posts previously had topics about using SMS notifications in Nagios. Today I’ll talk about another notification method. The method described below is somewhat more reliable than previously described, but also requires some cash investment. It is useful when some of the notifications are critical (such as a failure of the air conditioner or an increase in humidity).

    The way is to use a mobile phone with a corporate tariff (so that the money on the phone does not end 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 unavailable, then nagios sends an email.

    Both scripts are written in python and use the gammu library to connect to the phone.


    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)
    


    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
    


    Some comments


    • The script sendsms.py as the first parameter takes phone numbers in the form + 7xxxxxxxxxx, separated by commas, and as the second message text. In addition, the SMS message is truncated to 160 characters, so as not to complicate the system using Multi Part SMS.

    • Sample nagios sms notification commands
      # 'notify-service-by-sms' command definition
      define command {
              command_name notify-service-by-sms
              command_line /etc/_orga/nagios/nagios-sms/sendsms.py $ CONTACTPAGER $ "Nagios - $ NOTIFICATIONTYPE $: $ HOSTALIAS $ / $ SER
      VICEDESC $ is $ SERVICESTATE $ ($ SERVICEOUTPUT $) "
              }
      # 'notify-host-by-sms' command definition
      define command {
              command_name notify-host-by-sms
              command_line /etc/_orga/nagios/nagios-sms/sendsms.py $ CONTACTPAGER $ "Nagios - $ NOTIFICATIONTYPE $: Host $ HOSTALIAS $
       is $ HOSTSTATE $ ($ HOSTOUTPUT $) "
              }

    • Network Status Verification Command Example
      # 'check_sendsms' command definition
      define command {
              command_name check_sendsms
              command_line $ USER1 $ / check_sendsms.py
      }

    • / etc / gammurc for bluetooth will look like this
      [gammu]
      port = / dev / rfcomm0
      connection = at19200

    Read Next