Ad Widget

Collapse

JSON parsing by using Zabbix JSON functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jan.garaj
    Senior Member
    Zabbix Certified Specialist
    • Jan 2010
    • 506

    #1

    JSON parsing by using Zabbix JSON functions

    Zabbix provides some json parsing functions (see zbxjson.h):
    Code:
    int		zbx_json_open(const char *buffer, struct zbx_json_parse *jp);
    const char	*zbx_json_next(const struct zbx_json_parse *jp, const char *p);
    const char	*zbx_json_next_value(const struct zbx_json_parse *jp, const char *p, char *string, size_t len, int *is_null);
    const char	*zbx_json_next_value_dyn(const struct zbx_json_parse *jp, const char *p, char **string,
    		size_t *string_alloc, int *is_null);
    const char	*zbx_json_pair_next(const struct zbx_json_parse *jp, const char *p, char *name, size_t len);
    const char	*zbx_json_pair_by_name(const struct zbx_json_parse *jp, const char *name);
    int		zbx_json_value_by_name(const struct zbx_json_parse *jp, const char *name, char *string, size_t len);
    int		zbx_json_value_by_name_dyn(const struct zbx_json_parse *jp, const char *name, char **string, size_t *string_alloc);
    int		zbx_json_brackets_open(const char *p, struct zbx_json_parse *out);
    int		zbx_json_brackets_by_name(const struct zbx_json_parse *jp, const char *name, struct zbx_json_parse *out);
    int		zbx_json_object_is_empty(const struct zbx_json_parse *jp);
    int		zbx_json_count(const struct zbx_json_parse *jp);
    How can I use these functions for parsing this JSON (Docker JSON API output):
    Code:
    [{"Command":"/bin/docker-zabbix run","Created":1425672492,"Id":"ba52fb59230e2767ef04ff9209b8767801b80cd182447f68186af37c9998ec93","Image":"berngp/docker-zabbix:latest","Names":["/zabbix4"],"Ports":[{"IP":"0.0.0.0","PrivatePort":80,"PublicPort":49167,"Type":"tcp"},{"IP":"0.0.0.0","PrivatePort":10051,"PublicPort":49168,"Type":"tcp"},{"IP":"0.0.0.0","PrivatePort":10052,"PublicPort":49165,"Type":"tcp"},{"IP":"0.0.0.0","PrivatePort":2812,"PublicPort":49166,"Type":"tcp"}],"Status":"Up 14 hours"}
    ,{"Command":"/bin/docker-zabbix run","Created":1425672488,"Id":"7f175c4c9e0efd03b4a5823d922f13e57796fc592441f484196bed5297036cc2","Image":"berngp/docker-zabbix:latest","Names":["/zabbix3"],"Ports":[{"IP":"0.0.0.0","PrivatePort":10051,"PublicPort":49161,"Type":"tcp"},{"IP":"0.0.0.0","PrivatePort":10052,"PublicPort":49162,"Type":"tcp"},{"IP":"0.0.0.0","PrivatePort":2812,"PublicPort":49163,"Type":"tcp"},{"IP":"0.0.0.0","PrivatePort":80,"PublicPort":49164,"Type":"tcp"}],"Status":"Up 14 hours"}
    ,{"Command":"/bin/docker-zabbix run","Created":1425604740,"Id":"d5bf68ec1fb570d8ac3047226397edd8618eed14278ce035c98fbceef02d7730","Image":"berngp/docker-zabbix:latest","Names":["/zabbix2"],"Ports":[{"IP":"0.0.0.0","PrivatePort":80,"PublicPort":49157,"Type":"tcp"},{"IP":"0.0.0.0","PrivatePort":10051,"PublicPort":49158,"Type":"tcp"},{"IP":"0.0.0.0","PrivatePort":10052,"PublicPort":49159,"Type":"tcp"},{"IP":"0.0.0.0","PrivatePort":2812,"PublicPort":49160,"Type":"tcp"}],"Status":"Up 33 hours"}
    ]
    A few tests:
    zbx_json_object_is_empty: -1 -> OK, json is not empty
    zbx_json_count: 3 -> OK, we have 3 json objects there, I want to iterate through these objects
    zbx_json_open: cannot parse as a valid JSON object: invalid object format, expected opening character '{' at: '[{"Command".....

    I've checked zabbix source codes, but no success.
    Devops Monitoring Expert advice: Dockerize/automate/monitor all the things.
    My DevOps stack: Docker / Kubernetes / Mesos / ECS / Terraform / Elasticsearch / Zabbix / Grafana / Puppet / Ansible / Vagrant
  • jan.garaj
    Senior Member
    Zabbix Certified Specialist
    • Jan 2010
    • 506

    #2
    For reference:

    Resolved. Full code available, see function zbx_module_docker_discovery_extended():


    Thx @asaveljevs
    Devops Monitoring Expert advice: Dockerize/automate/monitor all the things.
    My DevOps stack: Docker / Kubernetes / Mesos / ECS / Terraform / Elasticsearch / Zabbix / Grafana / Puppet / Ansible / Vagrant

    Comment

    • timbo
      Member
      Zabbix Certified SpecialistZabbix Certified Professional
      • Sep 2013
      • 50

      #3
      Hi there,

      Looks like a cool project. I've been keeping my eye out for Zabbix/Docker monitoring solutions as I expect to see Docker a lot more in my future, so this will no doubt be a useful resource.

      Something else you may want to consider looking into would be this:


      I'm really loving Sysdig at the moment.

      -Timbo

      Comment

      • jan.garaj
        Senior Member
        Zabbix Certified Specialist
        • Jan 2010
        • 506

        #4
        Cool. Nice reference code. Thx
        Devops Monitoring Expert advice: Dockerize/automate/monitor all the things.
        My DevOps stack: Docker / Kubernetes / Mesos / ECS / Terraform / Elasticsearch / Zabbix / Grafana / Puppet / Ansible / Vagrant

        Comment

        Working...