(iii) Python calls the Zabbix API from the beginning to the abort--Starting from this section

Source: Internet
Author: User
Tags get ip

Because I'm busy writing the project. So no time to go back and forth Daoteng! So began to hate to write some technical blog, so from this section started to give up, just started to give up! Maybe I am a person who is not good at sharing, or maybe I am a selfish person. Have to admit that Aries people do things really are three minutes of heat. Yesterday also wanted to write a good series of articles, in addition to doing some of their own summary, but also to help the needy friends. However, it is really three minutes of heat, today is pulled cool pull cold.

OK, here is the Python code to get ip,hosts,hostid,groupid,itemid, casually affixed to the point, I hope to play the effect. Of course, it is not complete.

# -*- coding:utf-8 -*-class token:     "" "Request zabbix  Server token "" "    __REQ = {          "JSONRPC":  "2.0",         "method":  "User.login",          "params": {              "User": zbxuser,              "Password": zbxpass        },          "id": 0    }     @classmethod      def request (CLS):        try:             try:               &nbSp; return zbxhttp.request (token.__req) [' Result ']             except Exception:                 raise monalarmexception (' Invalid username or password,  unable to request zabbix token ')          except monalarmexception as e:             e.error ()             return  Response.format (Errordesc=e, errorcode=monalarmerrcode.unable_req_token) class BasicData:     __REQ = {         "Jsonrpc":  "2.0",          "Method":  "Host.get",          "params": {             "Output":  [' host '],              ' selectinterfaces ':  [' IP '],              ' selectgroups ':  '          },         "auth":  token.request (),          "id": 1    }     @classmethod      def __request (CLS):        result =  Zbxhttp.request (basicdata.__req)         err = result.get (' Error ',  none)         if err:             try:                &nBsp;raise monalarmexception (' unable to request basic data, session  Terminated, re-login, please. ')             except monalarmexception as  e:                 E.error ()                  Return response.format (Errordesc=e, errorcode=monalarmerrcode.unable_req_basicdata)          else:             return result     @classmethod     def get_summary (CLS):         ip = []         host = []        hostid = []  &nbsP;     groupid = []        result  = cls.__request () [' Result ']        for item in  result:            ip.append (item[' Interfaces '][0][' IP '])             host.append (item [' Host '])             hostid.append (item[' HostID '))              groupid.append (item[' groups '][0][' GroupID ')         return [ip, host, hostid, groupid]class  ItemsID:     @classmethod     def request (cls,host_id,  Item_name):        __req = {              "Jsonrpc":  "2.0",              "Method":  "Item.get",             " Params ": {                  "Output":  "Itemid",                  "Hostids": host_id,                  "Search":  {"Key_": item_name}             },             "Auth":  token.request (),             "id":  2        }        try:       &nbSp;     return zbxhttp.request (__req) [' Result '][0][' itemid ']         except Exception:             return 0
# -*- coding:utf-8 -*-class action:     @classmethod      def get_cpu_data (CLS):        summary =  Basicdata.get_summary ()         ip = summary[0]         host = summary[1]         hostid = summary[2]        groupid = summary [3]        count = len (summary[0])          for index in range (count):             itemid = itemsid.request (hostid[index], cpuitem.item_cpu_used)             print ({                  ' IP ': ip[index],                  ' Host ': host[index],                  ' HostID ': hostid[index],                  ' GroupID ': groupid[index],                  ' ItemName ':  cpuitem.item_cpu_load,                  ' itemid ':  itemid            }) if  __name__ ==  ' __main__ ':     action.get_cpu_data ()


(iii) Python calls the Zabbix API from the beginning to the abort--Starting from this section

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.