Ad Widget

Collapse

Getting started with Zabbix API

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Franca
    Junior Member
    • Aug 2018
    • 9

    #1

    Getting started with Zabbix API

    Hi All,

    I am trying to learning about Zabbix API and I need some help.
    I am reading the documentation https://www.zabbix.com/documentation/4.0/manual/api and I am literally stuck at the very start.
    I am not a json expert.

    For instance, this block of code

    POST http://127.0.0.1/zabbix/api_jsonrpc.php HTTP/1.1
    Content-Type: application/json-rpc
    {"jsonrpc":"2.0","method":"apiinfo.version","id ":1 ,"auth":null,"params":{}}

    Do I need to create a file and past the content of it and give execute permission to it?
    I am trying to copy and past the first line into my bash and as soon as I paste this POST http://127.0.0.1/zabbix/api_jsonrpc.php HTTP/1.1 and hit enter I get stuck, no matter what I try to paste after nothing happens so I need to kill the input by control+c.


    How is the best way to learn Zabbix API? Any book that might help me? Can anyone help me with the first steps?


    Cheers, Peter Franca
    Last edited by Peter Franca; 24-01-2019, 17:10.
  • LenR
    Senior Member
    • Sep 2009
    • 1005

    #2
    IMHO, I think it's easier to use a higher level language and API add-on's. Coming from other platforms, I didn't really know any of the preferred Linux scripting languages. I had had to maintain some Perl code, so I looked for something better and chose python, then this python library https://github.com/lukecyca/pyzabbix

    Below is the last thing I did, not that it's great, but I needed it. Pass a host group and it gets all items of type 8. I use environment vars for the Zabbix server, userid and password. Uncommenting the 2 lines at "import logging" will log the json traffic between the script and Zabbix which is a good learning and debugging tool. Python handles the JSON, usually returning lists of dicts. So the hostgroup.get returns a list of groups and their hosts, but I'm only gong to expect and process the first group matched.

    It looks like it lost the indenting...

    #!/usr/bin/python3

    from pyzabbix import ZabbixAPI

    import os
    import sys
    import pprint

    # activate these lines for tracing
    #import logging
    #logging.basicConfig(filename='pyzabbix_debug.log' ,level=logging.DEBUG)

    # The hostname at which the Zabbix web interface is available
    ZABBIX_SERVER = 'https://'+os.environ['Zhost']+'/zabbix'

    zapi = ZabbixAPI(ZABBIX_SERVER)

    # Login to the Zabbix API
    zapi.login(os.environ['Zuser'], os.environ['Zpass'])

    # Command line arg is the host_group to process
    t_group = str(sys.argv[1])

    host_group = zapi.hostgroup.get(output='extend',filter={'name': t_group},selectHosts=["name", "hostid"])

    for host in host_group[0]["hosts"]:
    host_info = zapi.host.get(hostids=host["hostid"],selectItems=["name", "description", "key_", "type"])
    for item in host_info[0]["items"]:
    if item["type"] == "8":
    print(host_info[0]["name"], item["name"], item["description"], item["key_"])
    Last edited by LenR; 24-01-2019, 17:57.

    Comment

    • Peter Franca
      Junior Member
      • Aug 2018
      • 9

      #3
      Hey, LenR

      Thanks for your opinion. I am going to look this pyzabbix library up.
      I briefly saw that yesterday as I was studying but couldn't grasp the ideia of it. It's making more sense now.
      Any suggest for study material to read? besides of Zabbix documentation? Youtube video perhaps?

      This whole thing (API) is new to me but I am keen to learn it, this will help me a lot.

      Cheers,
      Peter Franca

      Comment

      • LenR
        Senior Member
        • Sep 2009
        • 1005

        #4
        Start simple, that's about all I could do, I was learning python and the api at the same time. If you already know python, you're ahead. Other than that, start with reports or anything read-only to the zabbix configuration. You can cause lots of change with the update parts of the api, for better or not :-)

        Comment

        • Peter Franca
          Junior Member
          • Aug 2018
          • 9

          #5
          Thanks LenR for the sounds advises.

          Comment

          • kloczek
            Senior Member
            • Jun 2006
            • 1771

            #6
            zapish - Zabbix API SHell binding. Contribute to kloczek/zapish development by creating an account on GitHub.
            http://uk.linkedin.com/pub/tomasz-k%...zko/6/940/430/
            https://kloczek.wordpress.com/
            zapish - Zabbix API SHell binding https://github.com/kloczek/zapish
            My zabbix templates https://github.com/kloczek/zabbix-templates

            Comment

            Working...