Nagios SMS message warning with SMS platform interface

Source: Internet
Author: User
Tags error code split uuid

directly on the code.

#!/usr/bin/env python
#coding: Utf-8

Import Urllib2
Import Urllib
Import JSON
Import UUID
Import Argparse


def url_request (url,values={},method= ' get '):
If method = = ' Get ':
If Len (values)!= 0:
Url_values=urllib.urlencode (values)
furl=url+ '? ' +url_values
Else
Furl=url
Req=urllib2. Request (Furl)
Elif method = = ' POST ':
Data=json.dumps (Values,ensure_ascii=false)
Req=urllib2. Request (Url,data)
Req.get_method=lambda: ' POST '
Else
Pass

Try
Req.add_header (' user-agent ', ' mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/31.0.1650.63 safari/537.36 ')
Response = Urllib2.urlopen (req)
Result=json.loads (Response.read ())
Except Urllib2. Urlerror as E:
If Hasattr (E, ' Code '):
print ' Error code: ', E.code
Elif hasattr (E, ' reason '):
print ' Reason: ', E.reason

result={}
Except
result={}

return result

def send_text_message (content):
Content_data=content.split ('-@@-')
NOTIFY_TYPE=CONTENT_DATA[0]
if Notify_type = = ' Host ':
TYPE1=CONTENT_DATA[1]
HOST_NAME=CONTENT_DATA[2]
HOST_STATE=CONTENT_DATA[3]
HOST_ADDRESS=CONTENT_DATA[4]
HOST_INFO=CONTENT_DATA[5]
NOTIFY_CONTACT=CONTENT_DATA[6]
Notify_content= ' * * Nagios **\n\nnotification Type: ' + type1 + \
' \nhost: ' + host_name +
' \nstate: ' + host_state +
' \naddress: ' + host_address +
' \ninfo: ' + host_info + ' \ n '
elif Notify_type = = ' Service ':
TYPE1=CONTENT_DATA[1]
SERVICE_DESC=CONTENT_DATA[2]
HOST_NAME=CONTENT_DATA[3]
HOST_ADDRESS=CONTENT_DATA[4]
SERVICE_STATE=CONTENT_DATA[5]
SERVICE_INFO=CONTENT_DATA[6]
NOTIFY_CONTACT=CONTENT_DATA[7]
Notify_content= ' * * Nagios **\n\nnotification Type: ' + type1 + \
' \nservice: ' + Service_desc +
' \nhost: ' + host_name +
' \naddress: ' + host_address +
' \nstate: ' + service_state +
' \ninfo: ' + service_info + ' \ n '
Else
Notify_content= ' Get Nagios message notify Info error.\n\ncontent:%s '% content
notify_contact= ' 13800000000 '

Url= "Http://192.168.1.250/sms.do"
values={
' LoginName ': ' Sijitao ',
' Password ': ' Www.sijitao.net ',
' Content ': notify_content,
' Mobileid ': notify_contact,
' Flowid ': uuid.uuid1 (),
}

Return url_request (URLs, values, method= ' get ')

def main ():
Parser=argparse. Argumentparser (description= "Nagios notify by SMS")
Parser.add_argument ("Content", default=none,help= "notify Content,split with-@@-")
args = Parser.parse_args ()

Content=args.content
Send_text_message (content)

if __name__ = = "__main__":
Main ()
This code is simpler than the send_text_message alarm, calling the request method in the function and initiating a GET request to the SMS platform interface. When a GET request completes, the message is sent out.

After the script is finished, it's still the same, put it in/usr/local/nagios/python, and add the Send SMS command in Nagios's profile commands.cfg. For example:

Define Command{
Command_name notify-host-by-sms
command_line/usr/local/nagios/python/notifybysms.py "host-@@-$NOTIFICATIONTYPE $-@@-$HOSTNAME $-@@-$HOSTSTATE $-@@ -$HOSTADDRESS $-@@-$HOSTOUTPUT $-@@-$CONTACTALIAS $ "
}
Define Command{
Command_name notify-service-by-sms
command_line/usr/local/nagios/python/notifybysms.py "service-@@-$NOTIFICATIONTYPE $-@@-$SERVICEDESC $-@@-$ hostalias$-@@-$HOSTADDRESS $-@@-$SERVICESTATE $-@@-$SERVICEOUTPUT $-@@-$CONTACTALIAS $ "
}
To add a contact template to the templates.cfg template file:

Define Contact{
Name Sms-contact
Service_notification_period 24x7
Host_notification_period 24x7
Service_notification_options w,u,c,r,f,s
Host_notification_options d,u,r,f,s
Service_notification_commands notify-service-by-sms
Host_notification_commands notify-host-by-sms
Register 0
}
Add a contacts.cfg contact to the contact person, for example:

Define contact{
Contact_Name zhangnq-sms
Use sms-contact
alias 13800000000
Email admin@sijitao.net
}
Finally, when the service is configured to add zhangnq-sms This contact, you can send an alert message via the SMS platform

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.