Zabbix API Statistics Zabbix the number of hosts and host names monitored

Source: Internet
Author: User

Python Gets the host name and number of hosts monitored through the Zabbix API

#!/usr/bin/env python#-*-Encoding:utf8-*-#Import module, URLLIB2 is a module that simulates the browser HTTP methodImportJSONImportUrllib2ImportSYSImportSmtplib fromUrllib2ImportRequest,urlopen,urlerror,httperror#URL and URL header#Zabbix's API address, user name, password, change to the actual parameters hereZabbix_url="http://ip/api_jsonrpc.php"Zabbix_header= {"Content-type":"Application/json"}zabbix_user="Zabbix"Zabbix_pass=the admin"Auth_code=""#auth user and password#part of the user authentication information, the ultimate goal is to get a SessionID#here is the data generated in JSON format: User name and passworddefAuto_login (): Auth_data=Json.dumps ({"Jsonrpc":"2.0",            "Method":"User.login",            "params" :                      {                      "User": Zabbix_user,"Password": Zabbix_pass},"ID": 0})#Create Request ObjectRequest =Urllib2. Request (Zabbix_url,auth_data) forKeyinchZabbix_header:request.add_header (Key,zabbix_header[key])#authentication and get session ID    Try: Result=Urllib2.urlopen (Request)#for processing of authentication errors    excepthttperror,e:Print 'the server couldn\ ' t fulfill the request, Error code:', E.codeexcepturlerror,e:Print 'We failed to reach a server. Reason:', E.reasonElse: Response=json.loads (Result.read ())returnResponseres=Auto_login ()#determine if the SessionID is in the returned datadefCheck_sessionid ():if 'result' inchRes:#if ' result ' in Auto_info:Auth_code = res['result']   Else:       Printres['Error']['Data'] Json_data={       "Method":"Host.get",       "params":{                 "Output":"Extend",       }   } Json_base={        "Jsonrpc":"2.0",        "Auth": Auth_code,"ID": 1} json_data.update (json_base)#use the obtained SessionID to authenticate, get the host information (using the Http.get method)   ifLen (auth_code) = =0:sys.exit (1) Host=[]   ifLen (auth_code)! =0:get_host_data=json.dumps (json_data)#Create Request ObjectRequest =Urllib2. Request (Zabbix_url,get_host_data) forKeyinchZabbix_header:request.add_header (Key,zabbix_header[key])#Get host list       Try: Result=Urllib2.urlopen (Request)exceptUrlerror as E:ifHasattr (E,'reason'):                       Print 'We failed to reach a server.'                       Print 'Reason:', E.reasonelifHasattr (E,'Code'):                       Print 'The server could not fulfill the request.'                       Print 'Error Code:', E.codeElse: Response=json.loads (Result.read ()) Result.close ()#Display all the host information              #Print Response    forIinchresponse['result']:           #Print I            ifUnicode (str (i['Host'][-1]). Isdecimal () = =true:h= i['Host'] Host.append (H)returnHostif __name__=="__main__": Return_host=Check_sessionid ()PrintReturn_hostPrintLen (return_host)

Zabbix API Statistics Zabbix the number of hosts and host names monitored

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.