Ad Widget

Collapse

Python library for Zabbix API

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xs-
    Senior Member
    Zabbix Certified Specialist
    • Dec 2007
    • 393

    #16
    Originally posted by tekknokrat
    I tried the script with the rpc_test script.
    When connecting I get this message:

    Code:
    Error -32602: Invalid params., Action (checkauth) does not exist
    Zabbix Release 1.8.1
    The checkauth does not exist is a json library thing. The python script either uses json or simplejson python module.

    I am using ubuntu 8.04LTS, on which i can only get simplejson to work. But with simplejson, the checkauth function isn't there. (with the regular json module much more breaks, so i take the lack of checkauth for granted )

    Comment

    • xs-
      Senior Member
      Zabbix Certified Specialist
      • Dec 2007
      • 393

      #17
      Originally posted by wakko
      Sounds like I might just want to update my library to implement the logic to provide those functions.
      First of all, great work on the python lib, kudos for you my friend

      I have a remark tho; our zabbix webinterface is behind a https connection, which the python lib doesnt support as is. May is suggest the following patch?
      It replaces the __init__ arguement for server to url. This will fill 3 variables which are used for connecting: url_method (http/https), url_server (old server var) and url_path (the previous url var).

      By the way, what's the id variable for, nodeID?

      Tom
      Attached Files

      Comment

      • tekknokrat
        Senior Member
        • Sep 2008
        • 140

        #18
        Originally posted by xs-
        The checkauth does not exist is a json library thing. The python script either uses json or simplejson python module.

        I am using ubuntu 8.04LTS, on which i can only get simplejson to work. But with simplejson, the checkauth function isn't there. (with the regular json module much more breaks, so i take the lack of checkauth for granted )
        Hi,

        in the header of zabbix_api.py there is

        Code:
        import httplib
        try:
                # Python 2.5
                import json
        except ImportError:
                # Python 2.4
                import simplejson as json
        I have karmic running so it should use the former one with python2.5. But also when using json only error message comes up again. But at all it seems I get the right behaviour of example script.
        What about using the cjson lib for the api?

        Comment

        • gpmidi
          Member
          • Aug 2009
          • 62

          #19
          Related Version

          I've created a heavily modified version of the API. It includes the entire library, doc strings for almost all parts, and uses proper arguments for each JSON argument. It also fully supports HTTPS and HTTP-based authentication. The change in the handling of arguments makes it non-backwards compatible. A patch has been submitted to Wakko.



          -Paul

          Note: HTTP auth with the library requires a little bit of a hack to get around a problem with 1.8's auth. See the links below for details.

          Last edited by gpmidi; 25-02-2010, 19:29. Reason: Added info about HTTP auth and HTTPS

          Comment

          • gpmidi
            Member
            • Aug 2009
            • 62

            #20
            Patch Accepted

            My changes have been incorporated into http://github.com/wakko666/scripts/b.../zabbix_api.py.

            Comment

            • xs-
              Senior Member
              Zabbix Certified Specialist
              • Dec 2007
              • 393

              #21
              Can you post an example of how the 'new' api should be used? syntax on host.get for example?

              Comment

              • lukus
                Junior Member
                • May 2008
                • 9

                #22
                Note that the latest zabbix_api.py in this thread still uses:
                Code:
                item.add
                host.add
                ...
                Whereas in the latest version of the API, those functions have been changed to create:
                Code:
                item.create
                host.create
                ...
                Anyway, here's an example script for connecting and getting details of a specific item:
                Code:
                import sys
                from getpass import getpass
                import zabbix_api
                
                # The hostname at which the Zabbix web interface is available
                zabbix_server = 'zabbix.example.com'
                
                # Enter administrator credentials for the Zabbix Web Frontend
                username = raw_input('Username: ')
                password = getpass()
                
                zapi = zabbix_api.ZabbixAPI(zabbix_server,'/','http',username,password)
                zapi.login(username, password)
                print "Connected to Zabbix API Version %s" % zapi.api_version()
                
                for i in zapi.item.get(extendoutput=True):
                    if i['itemid'] == '22609':
                        print i
                Here's how to add an item:
                Code:
                zapi.item.create(
                        hostid=template_id,
                        description='Disk Quota Used - %s' % name,
                        key_='diskQuotaUsed[%s]' % name,
                        delay='600',
                        history='30',
                        type='4',
                        snmp_community=snmp_community,
                        snmp_oid='.1.3.6.1.4.1.30540.1.26.1.%s' % id,
                        value_type='3',
                        data_type='0',
                        units='B',
                        multiplier='1',
                        formula='1024',
                        applications=[application_id]
                        )
                Here's how to add a trigger:
                Code:
                zapi.trigger.create(
                        host='10069',
                        description='Soft Quota Exceeded - %s' % name,
                        status=0,
                        type=0,
                        priority=3,
                        expression='{Template_ProjectDirs:diskQuotaUsed[%s].last(0)}>{Template_ProjectDirs:diskSoftQuota[%s].last(0)}' % (name,name)
                        )

                Comment

                • noname
                  Senior Member
                  • Jan 2008
                  • 120

                  #23
                  How can i call method map.addElements? API description:
                  * @param array $elements[0,...]['sysmapid']
                  * @param array $elements[0,...]['elementid']
                  * @param array $elements[0,...]['elementtype']

                  Comment

                  • mike13
                    Member
                    • Apr 2010
                    • 30

                    #24
                    Hi Lukus,

                    I'm using debian linux. When i try to execute your sample script to get details of a secific item, i'm getting some errors:

                    Traceback (most recent call last):
                    File "test.py", line 14, in <module>
                    zapi.login(username, password)
                    File "/home/script/zabbix_api.py", line 176, in login
                    result = self.do_request(obj)
                    File "/home/script/zabbix_api.py", line 222, in do_request
                    jobj = json.loads(response.read())
                    File "/var/lib/python-support/python2.5/simplejson/__init__.py", line 313, in loads
                    return _default_decoder.decode(s)
                    File "/var/lib/python-support/python2.5/simplejson/decoder.py", line 321, in decode
                    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
                    File "/var/lib/python-support/python2.5/simplejson/decoder.py", line 340, in raw_decode
                    raise ValueError("No JSON object could be decoded")
                    ValueError: No JSON object could be decoded



                    Do you have any idea how to solve this issue?

                    Thanks,
                    Mike

                    Comment

                    • grizzlysmit
                      Junior Member
                      • Jul 2010
                      • 3

                      #25
                      what are the aguments expected by map.addElements

                      Originally posted by noname
                      How can i call method map.addElements? API description:
                      * @param array $elements[0,...]['sysmapid']
                      * @param array $elements[0,...]['elementid']
                      * @param array $elements[0,...]['elementtype']
                      yes I need to know this too

                      Comment

                      • zabbix_zen
                        Senior Member
                        • Jul 2009
                        • 426

                        #26
                        Considering the Zabbix API itself is a work in progress,
                        can we expect further developments on this library to catch over Zabbix minor releases ?
                        It would be great if this work would continue to see some action !

                        Comment

                        • Eric
                          Junior Member
                          • Mar 2006
                          • 10

                          #27
                          Problems adding an item using python api

                          Hello,

                          I'm getting the following when calling item.add

                          zabbix_api.ZabbixAPIException: Error -32602: Invalid params., Action (add) does not exist

                          I am using the python zabbix_api and it looks like the item.add is in the library.

                          What am I doing wrong?

                          The calling code is as follows:

                          hostid=options.host
                          description='Used disk space on $1 in %'
                          key='vfs.fs.size['+name+',pused]'


                          zapi.item.add({ 'hostid' : (options.host),
                          'description' : (description),
                          'key_' : (key)
                          })

                          Thanks

                          Comment

                          • gescheit
                            Senior Member
                            • Jul 2007
                            • 156

                            #28
                            Seems api have a little change. I fork wakko666 scripts, and do little change. You can get my fork from http://github.com/gescheit/scripts/tree/master//zabbix/

                            Comment

                            • zabbix_zen
                              Senior Member
                              • Jul 2009
                              • 426

                              #29
                              Gesheit,
                              could you help me with an addRights example to some Hostgroup?
                              Need to add read_write permissions to an HostGroup after creating it,
                              else a new host created over it will fail with
                              CHost::create ] You do not have enough rights for operation
                              Code:
                                  @dojson('usergroup.addRights')
                                  @checkauth
                                  def addRights(self,**opts):
                                      """  * Add rights for UserGroup to HostGroups. Existing rights are updated, new ones added.
                               *
                               * {@source}
                               * @access public
                               * @static
                               * @since 1.8
                               * @version 1
                               *
                               * <code>
                               * $rights = array(
                               *     *string 'groupid' => 'UserGroup ID',
                               *     array 'rights' => array( array('id' => 'HostGroup ID', 'permission' => 'permission'), ..)
                               * )
                               * </code>
                               *
                               * @param array $rights multidimensional array with rights data
                               * @return boolean
                              """

                              Comment

                              • gescheit
                                Senior Member
                                • Jul 2007
                                • 156

                                #30
                                You use deprecated function. Check this documentation - http://www.zabbix.com/documentation/...rgroup/massadd

                                Comment

                                Working...