Modification for zabbix API

126 views
Skip to first unread message

Alan Robson

unread,
Aug 11, 2011, 2:06:09 PM8/11/11
to jsonr...@googlegroups.com
The zabbix API expects there to be some more key:value pairs at the top level of the json dictionary. I was not able to see how to add them so I modified the code slightly to permit them.
 
For example, when you log into zabbix it sends back an authorization id that must be included in future requests...
 
{
   "jsonrpc":"2.0",
   "method":"hostgroup.get",
   "params":{
      "output":"extend",
      "sortfield":"name"
   },
   "id":1,
   "auth":"7cd4e1f5ebb27236e820db4faebc1769"
}
 
I could see no way to add this using jsonrpclib, so I added an "extras" parameter to config.py...
 
    <snip>user_agent = 'jsonrpclib/0.1 (Python %s)' % \
        '.'.join([str(ver) for ver in sys.version_info[0:3]])
    # User agent to use for calls.
    _instance = None
 
    # Added by Alan
    extras = {}
 
    @classmethod<snip>

 

And modified the code slightly in jsonrpc.py...
 
class Payload(dict):
    def __init__(self, rpcid=None, version=None):
        if not version:
            version = config.version
        self.id = rpcid
        self.version = float(version)

        # Added by Alan
        self.extras = config.extras
 
    def request(self, method, params=[]):
        if type(method) not in types.StringTypes:
            raise ValueError('Method name must be a string.')
        if not self.id:
            self.id = random_id()
        request = { 'id':self.id, 'method':method }

        # Added by Alan
        request.update(self.extras)

        if params:
 
This allowed me to add extra top-level items into the request...
 
Python 2.7 (r27:82500, Aug 18 2010, 23:22:02)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import jsonrpclib
>>> jsonrpclib.config.extras={"alan":"hello"}
>>> server = jsonrpclib.Server('http://XXX.XXX.net/zabbix/api_jsonrpc.php')
>>> server.user.authenticate(user="noone", password = "******")
<snip>...
 
>>> print jsonrpclib.history.request
{"jsonrpc": "2.0", "params": {"password": "******", "user": "noone"}, "id": "gvdpmgo9", "alan": "hello", "method": "user.authenticate"}
>>>

I have no idea whether this is a good thing to do or not, but it got me what I wanted, please let me know if there was a way to do this without moding the code.
 
Thanks for the library
 
Alan
 
Reply all
Reply to author
Forward
0 new messages