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