Announcement

Collapse
No announcement yet.

Python library for Zabbix API

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    #61
    hi there

    very interesting thank

    Comment


      #62
      Originally posted by mcmyst View Post
      Hi,

      Thank you for your job on this library !

      I am discovering the functionnality of the zabbix API, I would like to be able to see all the details about an item, but I can't see how ?

      I know how to list all items id about a particular host:
      Code:
      list_item = zapi.item.get({"filter":{"hostids":hostid}})
      for item in list_item:
      	if item['itemid'] == '26062':
      		print item
      Now that I have the item id, what is the way to describe it ?

      Thank you.
      I have reached to do it with:
      Code:
      list_item = zapi.item.get({"output": "extend","filter":{"hostids":hostid}})

      Comment


        #63
        Hi,
        When do you plan to add 'item.exists' to the api library?


        Regards,
        David

        Comment


          #64
          Module (https://github.com/gescheit/scripts/tree/master/zabbix) translate all methods to zabbix server. So dont need any action in this module, then zabbix add new methods in API.

          Comment


            #65
            Hi my friend, I am working on the python library of zabbix as well. My library more focus on Class and abstract and relation between Class because of I need to communicate with our CMDB. Your core thought of decorator is merge into my library. A lot thanks to you! My library:http://baniu.me/2012/04/zabbix-api-v0-1/

            Comment


              #66
              Hello,

              I am working on a program to synchronise our equipment/connection database with zabbix. I am using your library to do this in python.
              I am facing a problem about encoding:

              Code:
              zabbix_api.ZabbixAPIException:(u'Error -32602:Invalid params.,
              [ CHost::create ] Incorrect characters used for Hostname 
              [ SWT_Ranc\xc3\x83\xc2\xa9_01 ] while sending {"params": 
              [{"ip": IP_ADDR, "host": "SWT_Ranc\\u00c3\\u0083\\u00c2\\u00a9_01", 
              "useip": 1, "port": 10050, "groups": [{"groupid": "130"}]}], "jsonrpc": "2.0", 
              "method": "host.create", "auth": "52fadd077508d4170738ea71f9dd046e", "id": 2}', 
              -32602)
              Here is my code

              Code:
              host_name = host_tab_infos[1].decode('latin1')
              host_name = host_name.replace(":","").replace(";","").replace(",","")
              host_name = host_name.encode('utf-8')
              zapi.host.create({"host": host_name,"ip": host_ip,"port": 10050,"useip": 1,"groups": [{"groupid": 6}]})
              My database is encoded in latin1 and I am trying to encode it in utf-8 (the name should be SWT_Rancé_01).

              Do you have an idea ?

              Thank you
              Last edited by mcmyst; 19-04-2012, 07:15.

              Comment


                #67
                a little updated

                create object(host, item and etc) dynamically:

                def __getattr__(self, name):
                if name not in self._zabbix_api_object_list:
                raise ZabbixAPIException('No such API object: %s' % name)
                if not self.__dict__.has_key(name):
                self.__dict__[name] = ZabbixAPIObjectFactory(self, name)
                return self.__dict__[name]

                Comment


                  #68
                  Originally posted by mcmyst View Post
                  Hello,

                  Here is my code

                  Code:
                  host_name = host_tab_infos[1].decode('latin1')
                  host_name = host_name.replace(":","").replace(";","").replace(",","")
                  host_name = host_name.encode('utf-8')
                  zapi.host.create({"host": host_name,"ip": host_ip,"port": 10050,"useip": 1,"groups": [{"groupid": 6}]})
                  Have you tried something like this:

                  host_name = unicode(host_tab_infos[1],"utf-8")

                  Regards,
                  David

                  Comment


                    #69
                    Originally posted by Yello View Post
                    Have you tried something like this:

                    host_name = unicode(host_tab_infos[1],"utf-8")

                    Regards,
                    David
                    Yes I have already tried it.
                    In fact I think zabbix refuse a hostname with accent, it is not a problem of encoding...


                    Thank you

                    Comment


                      #70
                      For people willing to manage IT service with this library, you need to modify the zabbix_api.py file as follow:
                      Code:
                      diff zabbix_api_final.py zabbix_api_original.py
                      128,129d127
                      <     service = None
                      <
                      157d154
                      <         self.service = ZabbixAPISubClass(self, dict({"prefix": "service"}, **kwargs))
                      And then, this should work:
                      Code:
                      print zapi.service.get({"output": "extend","serviceids":"1"})
                      [{u'status': u'0', u'showsla': u'1', u'name': u'SLA XXXXX', u'algorithm': u'1', u'triggerid': u'0', u'serviceid': u'1', u'sortorder': u'0', u'goodsla': u'99.9000'}]

                      Comment


                        #71
                        Problem creating new item

                        I have a script which used to work with Zabbix 1.8, but sometime since i upgraded to 2.0, one part of it isn't.

                        The scrpt works when updating items, but not when creating a new one.

                        Here is the code in question:
                        newitem = {
                        'hostid' : str(hostid),
                        'description' : (s[1]),
                        'key_' : key,
                        'applications': (allapps)
                        }
                        zapi.item.create(newitem)

                        and here is the error:


                        Traceback (most recent call last):
                        File "./zabbix_updatefs.py", line 171, in <module>
                        zapi.item.create(newitem)
                        File "/etc/zabbix/python/zabbix_api.py", line 311, in wrapper
                        return self.do_request(self.json_obj(name,opts))['result']
                        File "/etc/zabbix/python/zabbix_api.py", line 293, in do_request
                        return self.parent.do_request(req)
                        File "/etc/zabbix/python/zabbix_api.py", line 258, in do_request
                        raise ZabbixAPIException(msg,jobj['error']['code'])
                        zabbix_api.ZabbixAPIException: (u'Error -32602: Invalid params., Incorrect arguments passed to function. while sending {"params": {"applications": ["1963", "1965"], "hostid": "10262", "key_": "vfs.fs.inode[/boot,free]", "description": "Free number of inodes on $1"}, "jsonrpc": "2.0", "method": "item.create", "auth": "1fcc445765110c17eb35918153cbe18a", "id": 5}', -32602)


                        Any ideas?

                        Thanks in advance

                        JBB

                        Comment


                          #72
                          Hi,
                          The api has changed in version 2.0 and isn't compatible with api 1.8. I suspect that's what's caused the failure.


                          Regards,
                          David

                          Comment


                            #73
                            and that's exactly what it was.

                            I've updated my script and it's now working.

                            Thanks


                            JBB

                            Comment


                              #74
                              Originally posted by linuxgurugamer View Post
                              and that's exactly what it was.

                              I've updated my script and it's now working.

                              Thanks


                              JBB
                              hello
                              can someone share the python code which works with Zabbix 2.0 API ?
                              I need to write the script which does batch add of hosts

                              Comment


                                #75
                                script for zabbix python api import from nocproject

                                I wrote little script for importing hosts from
                                nocproject to zabbix
                                - create group for imported hosts in zabbix - say TestGroup
                                - create template for hosts - say TestTemplate
                                - select desired hosts in nocproject, export them to CSV
                                - run this script, modified to your custom needs

                                Code:
                                #!/usr/bin/python
                                # import hosts from nocproject csv-exported file to zabbix via API
                                # and assign template 'TestTemplate' and group 'TestGroup' to them
                                import csv
                                from zabbix_api import ZabbixAPI
                                
                                server="http://127.0.0.1/zabbix"
                                username="Admin"
                                password="XXXXX"
                                
                                zapi = ZabbixAPI(server=server, path="", log_level=6)
                                zapi.login(username, password)
                                
                                # Get hosts in the hostgroup
                                group_id = zapi.hostgroup.get({"filter" : {"name" : 'TestGroup'}})[0]['groupid']
                                
                                template_id = zapi.template.get({"filter" : {"name" : 'TestTemplate'}})[0]['templateid']
                                
                                file = open("mo-list.txt",'rb')
                                reader = csv.DictReader( file )
                                
                                for line in reader:
                                    print line['name'],line['address']
                                    t = zapi.host.create (
                                    {
                                        "host": line['name'],
                                        "interfaces":[{
                                            "type":1,
                                                "dns":"",
                                                "main":1,
                                                "ip": line['address'],
                                                "port": 10050,
                                                "useip": 1,
                                        }],
                                        "groups": [{ "groupid": group_id }],
                                        "templates": [{ "templateid": template_id }],
                                    })
                                
                                file.close()

                                Comment

                                Working...
                                X