Ad Widget

Collapse

Use of Zabbix API and auto-creation of Maps

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rockandstone
    Junior Member
    • Mar 2021
    • 24

    #1

    Use of Zabbix API and auto-creation of Maps

    Hello!
    I am trying to understand how to auto-create maps in Zabbix and use Zabbix API.
    I read documentation but I dont' know where to start
    If someone will be so kind to route me to the right way... I think Zabbix has a great potential if used in the right way with all of its features.

    I would like to use Zabbix API in order to create maps automatically and "devices" schema (using maps).

    Thanks for taking the time

    Rockandstone
  • PeterMcD
    Junior Member
    • May 2021
    • 6

    #2
    What have you looked at so far?
    What language are you looking to use?
    Have you worked with the API already?

    the two API endpoints you need are map.create and map.update. Prior to using these the devices would need to be in Zavvix already.

    If you have not worked with the API as of yet I would suggest doing so in something like Postman. This will remove any issues with the language of choice.

    Comment

    • PeterMcD
      Junior Member
      • May 2021
      • 6

      #3
      I was just playing about with the API using Postman myself and thought I would post an example of creating a device:

      URL: http://ZABBIX_URL/api_jsonrpc.php

      Firstly login (replace username and password with appropriate values).

      Code:
      [COLOR=#000000]{[/COLOR]
      [COLOR=#a31515]  "jsonrpc"[/COLOR][COLOR=#000000]: [/COLOR][COLOR=#0451a5]"2.0"[/COLOR][COLOR=#000000],[/COLOR]
      [COLOR=#a31515]  "method"[/COLOR][COLOR=#000000]: [/COLOR][COLOR=#0451a5]"user.login"[/COLOR][COLOR=#000000],[/COLOR]
      [COLOR=#a31515]  "params"[/COLOR][COLOR=#000000]: {[/COLOR]
      [COLOR=#a31515]      "user"[/COLOR][COLOR=#000000]: [/COLOR][COLOR=#0451a5]"USERNAME"[/COLOR][COLOR=#000000],[/COLOR]
      [COLOR=#a31515]      "password"[/COLOR][COLOR=#000000]: [/COLOR][COLOR=#0451a5]"PASSWORD"[/COLOR]
      [COLOR=#000000]  },[/COLOR]
      [COLOR=#a31515]  "id"[/COLOR][COLOR=#000000]: [/COLOR][COLOR=#098658]1[/COLOR][COLOR=#000000],[/COLOR]
      [COLOR=#a31515]  "auth"[/COLOR][COLOR=#000000]: [/COLOR][COLOR=#0451a5][B]null[/B][/COLOR]
      [COLOR=#000000]}[/COLOR]
      This should result in a response such as:

      Code:
      [COLOR=#000000]{[/COLOR]
      [COLOR=#a31515]  "jsonrpc"[/COLOR][COLOR=#000000]: [/COLOR][COLOR=#0451a5]"2.0"[/COLOR][COLOR=#000000],[/COLOR]
      [COLOR=#a31515]  "result"[/COLOR][COLOR=#000000]: [/COLOR][COLOR=#0451a5]"AUTH_TOKEN"[/COLOR][COLOR=#000000],[/COLOR]
      [COLOR=#a31515]  "id"[/COLOR][COLOR=#000000]: [/COLOR][COLOR=#098658]1[/COLOR]
      [COLOR=#000000]}[/COLOR]
      Make sure you did not receive an error, if not take a note of the value associated with "result", you need this for future requests.

      Now that we have a token we can make an API request. The following creates a device with an SNMP interface. Most values are dummyvalues but it will work. Prior to sending such a payload you need to update the AUTH_TOKEN received from the authentication step above. Please note the group id and template id may not work in your environment, if I remember correctly I created these 2 myself.

      Code:
      [COLOR=#000000]{[/COLOR]
      [COLOR=#a31515]   "jsonrpc"[/COLOR][COLOR=#000000]: [/COLOR][COLOR=#0451a5]"2.0"[/COLOR][COLOR=#000000],[/COLOR]
      [COLOR=#a31515]   "method"[/COLOR][COLOR=#000000]: [/COLOR][COLOR=#0451a5]"host.create"[/COLOR][COLOR=#000000],[/COLOR]
      [COLOR=#a31515]   "params"[/COLOR][COLOR=#000000]:[/COLOR][COLOR=#000000] {[/COLOR]
      [COLOR=#a31515]       "host"[/COLOR][COLOR=#000000]: [/COLOR][COLOR=#0451a5]"Test-Device"[/COLOR][COLOR=#000000],[/COLOR]
      [COLOR=#a31515]       "name"[/COLOR][COLOR=#000000]: [/COLOR][COLOR=#0451a5]"test device"[/COLOR][COLOR=#000000],[/COLOR]
      [COLOR=#a31515]       "inventory_mode"[/COLOR][COLOR=#000000]: [/COLOR][COLOR=#098658]1[/COLOR][COLOR=#000000],[/COLOR]
      [COLOR=#a31515]       "templates"[/COLOR][COLOR=#000000]: [[/COLOR]
      [COLOR=#000000]           {[/COLOR][COLOR=#a31515]"templateid"[/COLOR][COLOR=#000000]: [/COLOR][COLOR=#098658]10393[/COLOR][COLOR=#000000]}[/COLOR]
      [COLOR=#000000]       ],[/COLOR]
      [COLOR=#a31515]       "groups"[/COLOR][COLOR=#000000]: [[/COLOR]
      [COLOR=#000000]           {[/COLOR][COLOR=#a31515]"groupid"[/COLOR][COLOR=#000000]: [/COLOR][COLOR=#098658]20[/COLOR][COLOR=#000000]}[/COLOR]
      [COLOR=#000000]       ],[/COLOR]
      [COLOR=#a31515]       "interfaces"[/COLOR][COLOR=#000000] : [[/COLOR][COLOR=#000000]{[/COLOR]
      [COLOR=#a31515]           "type"[/COLOR][COLOR=#000000]: [/COLOR][COLOR=#098658]2[/COLOR][COLOR=#000000],[/COLOR]
                  [COLOR=#a31515]"main"[/COLOR][COLOR=#000000]: [/COLOR][COLOR=#098658]1[/COLOR][COLOR=#000000],[/COLOR]
      [COLOR=#a31515]           "useip"[/COLOR][COLOR=#000000]: [/COLOR][COLOR=#098658]1[/COLOR][COLOR=#000000],[/COLOR]
      [COLOR=#a31515]           "ip"[/COLOR][COLOR=#000000]: [/COLOR][COLOR=#0451a5]"1.2.3.4"[/COLOR][COLOR=#000000],[/COLOR]
      [COLOR=#a31515]           "dns"[/COLOR][COLOR=#000000]: [/COLOR][COLOR=#0451a5]""[/COLOR][COLOR=#000000],[/COLOR]
      [COLOR=#a31515]           "port"[/COLOR][COLOR=#000000]: [/COLOR][COLOR=#0451a5]"161"[/COLOR][COLOR=#000000],[/COLOR]
      [COLOR=#a31515]           "details"[/COLOR][COLOR=#000000]: {[/COLOR]
      [COLOR=#a31515]               "version"[/COLOR][COLOR=#000000]: [/COLOR][COLOR=#098658]3[/COLOR][COLOR=#000000],[/COLOR]
      [COLOR=#a31515]               "bulk"[/COLOR][COLOR=#000000]: [/COLOR][COLOR=#098658]1[/COLOR][COLOR=#000000],[/COLOR]
      [COLOR=#a31515]               "securityname"[/COLOR][COLOR=#000000]: [/COLOR][COLOR=#0451a5]"snmp_username"[/COLOR][COLOR=#000000],[/COLOR]
      [COLOR=#a31515]               "securitylevel"[/COLOR][COLOR=#000000]: [/COLOR][COLOR=#098658]1[/COLOR][COLOR=#000000],[/COLOR]
      [COLOR=#a31515]               "authpassphrase"[/COLOR][COLOR=#000000]: [/COLOR][COLOR=#0451a5]"snmp_password"[/COLOR][COLOR=#000000],[/COLOR]
      [COLOR=#a31515]               "contextname"[/COLOR][COLOR=#000000]: [/COLOR][COLOR=#0451a5]""[/COLOR]
      [COLOR=#000000]           }[/COLOR]
      [COLOR=#000000]       }[/COLOR][COLOR=#000000]][/COLOR]
      [COLOR=#000000]   },[/COLOR]
      [COLOR=#a31515]   "id"[/COLOR][COLOR=#000000]: [/COLOR][COLOR=#098658]2[/COLOR][COLOR=#000000],[/COLOR]
      [COLOR=#a31515]   "auth"[/COLOR][COLOR=#000000]: [/COLOR][COLOR=#0451a5]"AUTH_TOKEN"[/COLOR]
      [COLOR=#000000]}[/COLOR]
      This will result in a response such as, in this instance we receive the ID of the new device.:

      Code:
      [COLOR=#000000]{[/COLOR]
      [COLOR=#a31515]   "jsonrpc"[/COLOR][COLOR=#000000]: [/COLOR][COLOR=#0451a5]"2.0"[/COLOR][COLOR=#000000],[/COLOR]
      [COLOR=#a31515]   "result"[/COLOR][COLOR=#000000]: {[/COLOR]
      [COLOR=#a31515]       "hostids"[/COLOR][COLOR=#000000]: [[/COLOR]
      [COLOR=#0451a5]           "10413"[/COLOR]
      [COLOR=#000000]       ][/COLOR]
      [COLOR=#000000]   },[/COLOR]
      [COLOR=#a31515]   "id"[/COLOR][COLOR=#000000]: [/COLOR][COLOR=#098658]2[/COLOR]
      [COLOR=#000000]}[/COLOR]
      There are a couple of things I have not explained, so shall now.

      You will notice I have only mentioned 1 URL, all API requests are sent to this URL, the action that you want to acheive is specified in the method attribute of the payload user.login for authentication, host.create for creating a device. The documentation lists the available parameters for the specific type of request, you can include any number of them as longs as you include ALL of the required fields. Each of those parameters will be assigned to the params attribute of the payload.

      If you are planning on using Python for the requests there is a Pypi package called pyZabbix that can help making API calls easier. For example the following would create the above device:

      Code:
      from pyzabbix import ZabbixAPI
      
      zabbix = ZabbixAPI("http://ZABBIX_URL")
      zabbix.session.verify = False
      zabbix.login("Peter", "c0erVLa3RXmtHlTcqTrV")
      zabbix.host.create(
          host = "Test-Device",
          name = "test device",
          inventory_mode = 1,
          templates = [{"templateid": 10393}],
          groups = [{"groupid": 20}],
          interfaces = [
          {
            "type": 2,
            "main": 1,
            "useip": 1,
            "ip": "1.2.3.4",
            "dns": "",
            "port": "161",
            "details": {
               "version": 3,
               "bulk": 1,
               "securityname": "snmp_username",
               "securitylevel": 1,
               "authpassphrase": "snmp_password",
               "contextname": ""
            }
         }]
      )
      Last edited by PeterMcD; 13-05-2021, 21:24.

      Comment


      • rockandstone
        rockandstone commented
        Editing a comment
        Hello Peter, your explanation is a great help and now I have got it.
        I have never used Python professionally, so It would be awesome to use Postman. I am programming with Powershell for Zabbix.
        I will definitely check it out, but I think it is a big world that needs a lot of time to master. Thanks for your time
        Last edited by rockandstone; 14-05-2021, 09:01.
    • rockandstone
      Junior Member
      • Mar 2021
      • 24

      #4
      Hello PeterMcD!

      I am new with the API and I have looked documentation and some example online.
      First i thought there was a problem with firewall which was blocking something, so I tried to do a bash script on the server. And it barely work (maybe I used an user who doesn't own privileges to use Zabbix API). If I try to reach URL through a Browser I get a HTTP ERROR 412.
      Anyway I think it.is impossible what I would like to reach: discover a lot of network devices, get some data using LLDP and SNMP and auto-create maps. I should read history table and then create Python code and send data to zabbix server and this could overload it?
      Last edited by rockandstone; 14-05-2021, 09:02.

      Comment

      • PeterMcD
        Junior Member
        • May 2021
        • 6

        #5
        It could be an issue with permissions, but would need to know a little more.

        Try the commands alone and see if they produce errors, if a firewall is blocking it I would expect a http timeout error, if its a permissions error it will be quite apparent from the error message (something along the lines of "item does not exist or you do not have permissions to view this item" not exact wording but along those lines.

        Re overloading, I guess that really depends on the hardware that you are using for Zabbix and the size of the history logs you are using (and how many API calls this generates. You can mitigate this of course by keeping track of state you are sending to zabbix within your script and comparin g proposed changes to what you already have. For example no point adding deviceb if you already added it, this will of course sacrifice ram foir a reduced number of API calls. To start with I would build your basic toolkit first, create a proof of concept. Tasks such as reading the history and making the API calls will not change regardless of the approach, once you know the individual components work you can then but then glue them together, it would be this bit that has the logic.
        Last edited by PeterMcD; 14-05-2021, 12:29.

        Comment

        • rockandstone
          Junior Member
          • Mar 2021
          • 24

          #6
          Ok thank you, I will try asap, I am busy with another work now (switches monitoring)... Would love to try it now

          You gave me plenty of great informations. I am going to write here as I get news

          Comment

          • rockandstone
            Junior Member
            • Mar 2021
            • 24

            #7
            PeterMcD I started to study Pyhton and Zabbix API... And I started to understand how to create things in a map, thank you!
            Last edited by rockandstone; 20-08-2021, 16:20.

            Comment

            • PeterMcD
              Junior Member
              • May 2021
              • 6

              #8
              Great to hear

              Comment

              Working...