Ad Widget

Collapse

Zabbix api: create template with web scenario

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Andrius Jurkus
    Junior Member
    • Jul 2019
    • 1

    #1

    Zabbix api: create template with web scenario

    Hello,

    I m trying to create template with web scenarios through zabbix api (through salt originally https://docs.saltstack.com/en/latest..._template.html ).

    Problem is zabbix api doenst return errors, I assume there is some mistake in my json, but zabbix api returns

    Code:
    {'jsonrpc': '2.0', 'result': {'templateids': ['10282']}, 'id': '1'}
    At first I thought its salt state which doesnt return anything so I tried using something different. "pyzabbix" with simple json example, It does create template, but no webscenario and no errors. How can I get proper debug output from zabbix? or can someone point out where mistake is in my httptest json example?

    PS I tried many variations this is very simplified down json. But trying to guess in the dark is hard.

    PPS I can create web scenario directly to host, but I want to make a template.

    Code:
    from pyzabbix.api import ZabbixAPI
    import pdb
    
    # Create ZabbixAPI class instance
    zapi = ZabbixAPI(url='http://10.10.10.102/zabbix/', user='Admin', password='zabbix')
    
    # Get all monitored hosts
    result1 = zapi.host.get(monitored_hosts=1, output='extend')
    
    # Get all disabled hosts
    result2 = zapi.do_request('template.create',
                              {
    "host": "Linux templateaa",
    "groups": {
      "groupid": 1
    },
    "applications": {
      "name": "web checks"
    },
    "httptests": {
      "name": "google.lt",
      "steps": {
        "name": "google.lt",
        "url": "https://google.lt",
        "status_codes": 200,
        "no": 1
      }
    
    
    }
                              })
    print ("\nprinting result below")
    print ( result2)
    #pdb.set_trace()
    # Logout from Zabbix
    zapi.user.logout()
  • mcxian
    Junior Member
    • Mar 2013
    • 10

    #2
    I found myself trying to do exactly the same thing and found your post. Turns out, creating a web scenario is not part of the template API, but instead has its own (this probably is a legacy holdover from before web scenarios were part of templates)

    Here's an example of creating a web scenario through the API. The hostid I used was that of an existing template, which resulted in this scenario being created on the template as desired.
    Code:
    from pyzabbix.api import ZabbixAPI
    zapi = ZabbixAPI(url='http://zab-server/zabbix/', user='Admin', password='zabbix')
    web_results = zapi.do_request('httptest.create',
    {
    "name": "myapp test api2",
    "hostid": "10327",
    "steps": [
        {
          "name": "load page",
          "url": "http://salt-client1/",
          "status_codes": "200",
          "required": "name:",
          "no": 1
        }
      ]
    })
    print (web_results)
    # Logout from Zabbix
    zapi.user.logout()
    Since this uses the httptest.create function rather than template.create, Saltstack does not yet support it. I'll see if I can submit a PR to incorporate this functionality.

    Comment

    Working...