Ad Widget

Collapse

Python library for Zabbix API

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zabbix_zen
    Senior Member
    • Jul 2009
    • 426

    #31
    Thanks for the timely library update.

    I'm trying this syntax:

    Code:
    >>> zapi.hostgroup.massAdd({ 'usrgrpids' : ['19'], 'rights': [{'permisssion' : '3', 'id' : '69'}] })
    Where 19 is a UserGroup id and 69 is an HostGroup id, however,
    I'm getting a null result:
    []

    Can you point me out what part of my syntax is wrong?

    Comment

    • gescheit
      Senior Member
      • Jul 2007
      • 156

      #32
      zapi.usergroup.massAdd({ 'usrgrpids' : ['19'], 'rights': [{'permisssion' : '3', 'id' : '69'}] })

      Comment

      • zabbix_zen
        Senior Member
        • Jul 2009
        • 426

        #33
        Code:
        zapi.usergroup.massAdd({ 'usrgrpids' : '19', 'rights': [{'permisssion' : '3', 'id' : '69'}] })
        returns,
        zabbix_api.ZabbixAPIException: ('Error -32602: Invalid params., Action (massAdd) does not exist', -32602)

        though
        Code:
        help(zapi.usergroup)
        recognizes it,

        Help on ZabbixAPIUserGroup in module zabbix_api object:
        ...
        massAdd = wrapper(self, opts)

        Comment

        • gescheit
          Senior Member
          • Jul 2007
          • 156

          #34
          Please update your zabbix to 1.8.4.
          Last edited by gescheit; 16-02-2011, 09:07.

          Comment

          • zabbix_zen
            Senior Member
            • Jul 2009
            • 426

            #35
            Hmm... the API calls seem to be working.

            But I'm unable to create an host.

            After logging with a SuperAdmin, API access privileges user,
            Code:
            zapi.login(zbx_user, zbx_passwd)
            I successfully create the HostGroup,
            Code:
            hostgroup_id = zapi.hostgroup.create({"name" : hostgroup})['groupids'][0]
            but it fails creating an host on top of it,
            Code:
            try:
            	hostid = zapi.host.create({ 'groups' : hostgroup_id, 'host' : host_name, 'ip' : host_ip, 'use_ip': '1'})['hostids'][0]
            except ZabbixAPIException, zbx_exc:
            		print 'ERROR creating Host %s. ZabbixAPIException: %s' % (host_name, zbx_exc)
            the result is the same even if I try to add read+write permission on that UserGroup over the HostGroup
            Code:
            # Zabbix internal notation
            usr_grp_perm_dic = {'deny': '0', 'read' : '2', 'read_write' : '3'}
            read_write_perm = usr_grp_perm_dic['read_write']
            
            user_group_id = zapi.usergroup.get({"filter":{"name" : 'API access'}})[0]['usrgrpid']
            	zapi.usergroup.massAdd({ 'usrgrpids' : [user_group_id], 'rights': [{'permisssion' : 'read_write_perm', 'id' : hostgroup_id}] })
            raise ZabbixAPIException(msg,jobj['error']['code'])
            zabbix_api.ZabbixAPIException: ('Error -32500: Application error., [ CHost::create ] No permissions !', -32500)
            What am I missing?

            Comment

            • gescheit
              Senior Member
              • Jul 2007
              • 156

              #36
              What version of zabbix?
              Can you create host thru web interface using login for API?

              Comment

              • zabbix_zen
                Senior Member
                • Jul 2009
                • 426

                #37
                I'm using the latest version of zabbix_api.py from your repo.

                The default behaviour seems to be adding newly created HostGroups to all UserGroups 'Deny' list.

                Weird enough, on 1.8.2 over MySQL I can create the host over any of those HostGroups
                Code:
                hostgroup = 'TEST2'
                host_name = 'test_host'
                
                hostgroup_id = zapi.hostgroup.get({"filter":{"name" : hostgroup}})
                # if a group with the given name was not found
                if hostgroup_id == []:
                                hostgroup_id = zapi.hostgroup.create({"name" : hostgroup})['groupids'][0]
                else:
                                hostgroup_id = hostgroup_id[0]['groupid']
                        hostid = zapi.host.get({"filter" : {"host" : host_name}})
                        
                if hostid == []:
                                try:
                                        hostid = zapi.host.create({ 'groups' : hostgroup_id, 'host' : host_name+ '5', 'ip' : host_ip, 'use_ip': '1'})['hostids'][0]
                                except ZabbixAPIException, zbx_exc:
                I'm scratching my head here,
                in 1.8.4 over Oracle, though I get the same default UserGroups permissions behaviour over newly created HostGroups,
                the host creation was failing,
                Code:
                zapi.host.create({ 'groups' : hostgroup_id, 'host' : host_name + '7', 'ip' : host_ip})
                Traceback (most recent call last):
                ....
                    raise ZabbixAPIException(msg,jobj['error']['code'])
                zabbix_api.ZabbixAPIException: ('Error -32500: Application error., [ CHost::create ] No permissions !', -32500)
                Until I logged in the frontend using the api user,
                successfully created an host over one of the HostGroups (as it should, as I configured the api user as SuperAdmin)
                and reconnected via zabbix_api.ZabbixAPI.login
                and was able to create an host over any of those HostGroups.

                Unfortunately I can't trust having to manually log to the frontend each time I need to call the API to perform mass tasks :/
                What can explain this?

                Comment

                • zabbix_zen
                  Senior Member
                  • Jul 2009
                  • 426

                  #38
                  Gesheit,

                  My goal is to give an UserGroup read permissions over an HostGroup
                  Code:
                  # groupid - UserGroup ID
                  # id - HostGroup ID
                  zapi.usergroup.update({ 'groupid' : (13), 'id' : (89024), 'permission' : (2)})
                  but I get,
                  zabbix_api.ZabbixAPIException: ('Error -32602: Invalid params., [ CUserGroup::create ] Incorrect parameters used for UserGroup while sending {"params": {"permission": 2, "groupid": 13, "id": 89024}, "jsonrpc": "2.0", "method": "usergroup.update", "auth": "8b2e7caaa9fc6567a6b0fec7f1c23485", "id": 3}', -32602)
                  a simple create user group with API access works though,
                  Code:
                  zapi.usergroup.create({ 'name' : (usergroup_name), 'api_access' : (1)})
                  Can you point me the error please?
                  From the Manual these are the UserGroup API options,

                  Usrgrp

                  Parameter Type Description Details

                  usrgrpid integer User group id
                  name string Name
                  gui_access integer GUI access system default(0), internal(1), disabled(2)
                  users_status integer User status enabled(0), disabled(1)
                  api_access integer API access disabled(0), enabled(1)
                  debug_mode integer Debug mode disabled(0), enabled(1)


                  Rights

                  Parameter Type Description Details

                  groupid integer User group ID
                  id integer Host Group ID.
                  permission string Permission. deny(0), read(2), read-write(3)
                  Last edited by zabbix_zen; 22-08-2011, 13:17.

                  Comment

                  • zabbix_zen
                    Senior Member
                    • Jul 2009
                    • 426

                    #39
                    Mea culpa,

                    I should have done,
                    Code:
                    zapi.usergroup.update([{ 'usrgrpid':13 , 'rights':[{'groupid':13, 'id':89024, 'permission':2}] }])
                    Which returns the success message:
                    Code:
                    {'usrgrpids': [13]}
                    and added the intended read privileges

                    Comment

                    • Turbid
                      Junior Member
                      • Nov 2011
                      • 9

                      #40
                      when I try to call
                      Code:
                      zapi.host.exist({"name": hostname})
                      I get

                      Code:
                      Traceback (most recent call last):
                        File "./test.py", line 30, in <module>
                          zapi.host.exist({"name": hostname})
                      AttributeError: 'ZabbixAPIHost' object has no attribute 'exist'
                      http://www.zabbix.com/documentation/1.8/api/host/exists

                      zabbix: 1.8.6, zabbix_api from https://github.com/gescheit/scripts/.../zabbix_api.py

                      Comment

                      • Yello
                        Senior Member
                        • Apr 2011
                        • 309

                        #41
                        Hi,
                        I'm beginning to look at using this library. I'm also new to python so please bear with me.

                        I see that you use the json library for this api. I am using python 2.4 on rhel5. From what I've read the json library doesn't work with python 2.4 while simplejson does. Is this true?

                        In short, will python 2.4 work with your api module? I have looked at upgrading to python 2.6 but that broke yum so I regressed and am looking to move ahead against 2.4.

                        BTW, you say that some of the module is currently broken. Have you listed anywhere those elements which don't yet work properly?


                        Regards,
                        David

                        Comment

                        • Yello
                          Senior Member
                          • Apr 2011
                          • 309

                          #42
                          Hi,
                          Just noticed in the source that there's an accomodation for python 2.4. I'll continue and see how I get on.


                          Regards,
                          David

                          Comment

                          • Yello
                            Senior Member
                            • Apr 2011
                            • 309

                            #43
                            Hi,
                            I'm seeing problems instantiating the api. I suspect it's because of the python version I'm using (again):

                            zapi = ZabbixAPI(server=zabbix_server,path="",log_level=6 )
                            File "/home/davidr/zabbix_api/python_api/zabbix_api.py", line 175, in __init__
                            self.r_query = deque([], maxlen=r_query_len)
                            TypeError: deque() does not take keyword arguments

                            If you can suggest a means by which I can resolve this I'll appreciate it greatly.


                            Regards,
                            David

                            Comment

                            • lukus
                              Junior Member
                              • May 2008
                              • 9

                              #44
                              PyZabbix Released

                              Hi Everyone,

                              You might be interested in the PyZabbix project on github, which is a fork of zabbix_api.py with the priority of creating a stable, well-documented module. It is packaged and available on PyPi, so it is easy to install and update.

                              For more information, see:


                              If you discover issues, please open a ticket in the github project.

                              I'd love to hear your feedback, ideas for improvement, and of course I'd especially like contributions of code, documentation, or tests.

                              Thanks!

                              Comment

                              • gescheit
                                Senior Member
                                • Jul 2007
                                • 156

                                #45
                                Why need yet another python module?
                                Stable? zabbix_api very stable. All problems that i catched was linked with zabbix side.
                                Documentation? Main documentations - zabbix wiki. I think don't need copy and paste it to module.
                                Don't do meaningless forks. Would be better if you help with me "well-documented" and examples.

                                Comment

                                Working...