The method of Python parsing XML monitoring nowsms script

Source: Internet
Author: User

Nowsms SMS Multimedia frame of the admin page has provided an XML page, this page details the various states of the modem. Then combined with Nagios, can be very good monitoring of modem state. The XML address is similar to the following: Http://192.168.0.115:8800/admin/xmlstatus?username=sijitao&password=sijitao.

If it's not clear how Python parses XML, refer to this URL: http://www.runoob.com/python/python-xml.html.

Script Content

In the script only need to get the status in Smscstatus, the display OK indicates modem receive send normal, the script resembles the following.

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

Import Urllib2
From Xml.dom.minidom Import Parse
Import Sys
Import Argparse

Parser=argparse. Argumentparser (description= "Check nowsms status script.")
Parser.add_argument ("-H", dest= "host", help= "host Name argument for servers using host Headers")
Parser.add_argument ("-P", dest= "Port", default=8800,help= "nowsms http port, default 8800.")
Parser.add_argument ("-U", dest= "username", help= "nowsms admin username")
Parser.add_argument ("P", dest= "password", help= "nowsms Admin user ' s password")

Args=parser.parse_args ()

If not args.host or not args.port or not args.username or not args.password:
Print "Critical-invalid command,please check."
Sys.exit (2)

Url= ' http://%s:%s/admin/xmlstatus?username=%s&password=%s '% (Args.host,args.port,args.username, Args.password)
Req=urllib2. Request (URL)
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 ")

Try
Response=urllib2.urlopen (req,timeout=5)
Domtree = Parse (response)
Except
Print "Critical-nowsms admin status get error."
Sys.exit (2)

Nowsms = Domtree.documentelement

SMSC = Nowsms.getelementsbytagname ("Smscstatus")

For S in SMSC:
#print s.getelementsbytagname (' Name ') [0].childnodes[0].data
Status = S.getelementsbytagname (' status ') [0].childnodes[0].data
If status!= ' OK ':
Detail=s.getelementsbytagname (' Statusdetail ') [0].childnodes[0].data
Print '%s-%s '% (status,detail)
Sys.exit (2)
Else
print '%s-nowsms is ok. '% status
Sys.exit (0)
Script deployment

1, copy the contents of the script above, save to the Nagios server, generally saved in the/usr/local/nagios/libexec directory.

2, modify commands.cfg, add Command command. Port, user name password can also use parameters, because I have a few on this side of the same so it is written in the command.

3, add service, after the definition is complete.

Related Article

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.