Ad Widget

Collapse

Zabbix API - No items returned

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • slpefanis
    Member
    • Jan 2020
    • 52

    #1

    Zabbix API - No items returned

    Hi All. I'm trying to write some powershell to get the list of items from Zabbix to use the zabbix_sender. For some reason when I run the code below againse a host I get zero items returnd, but run it against another host, I am getting data.

    I'm running this against a "dummy" host that I will be recording O365 service health against. When I run this again's a normal Host I get items returned.

    The Dummy host has 24 items and the normal host has 64 items.

    The main difference is one is monitoried by the Zabbix Proxy via Agent (windows server), the dummy host is monitored by the zabbix server

    EDIT: noticed it only returns items for things that are monitored by an "Agent", doesn't return items for things monitored by SNMP?


    Code:
    $result = @();
    
    $host_name = "O365-Service-Status"; #$args[0];;
    
    $progressPreference = "silentlyContinue";
    
    $zabbix_url = "http://zabbix.domain";
    
    $working_dir = "/usr/lib/zabbix/externalscripts";
    
    write-host $("Working dir: " + $working_dir);
    
    
    
    
    
    function Login-Zabbix
    
    {
    
        param (
    
            [Parameter(Mandatory = $true)][PSCredential]$creds,
    
            [Parameter(Mandatory = $true)][string]$url
    
        );
    
    
    
        Write-Host "Logging to Zabbix...";
    
    
    
        $params = @{
    
            body =  @{
    
                "jsonrpc" = "2.0";
    
                "method" = "user.login";
    
                "params" = @{
    
                    "user" = "apiaccess"; #$creds.UserName;
    
                    "password" = "PASSWORD"; #$creds.GetNetworkCredential().Password;
    
                };
    
                "id" = 1;
    
                "auth" = $null;
    
            } | ConvertTo-Json;
    
            uri = "$url/api_jsonrpc.php";
    
            headers = @{"Content-Type" = "application/json"};
    
            method = "Post";
    
        };
    
    
    
        $result_json = Invoke-WebRequest @params -UseBasicParsing #
    
        #-SkipCertificateCheck;
    
        $result_object = $result_json | ConvertFrom-Json;
    
        if ($result_object.error -ne $null)
    
        {
    
            Write-Host $("  Error: " + $result_object.error.message + " " + $result_object.error.data);
    
            return $null;
    
        }
    
        else
    
        {
    
            Write-Host "Successfully logged to Zabbix.";
    
            return $result_object.result;
    
        }
    
    }
    
    
    
    function Get-ZabbixHostItems
    
    {
    
        param (
    
            [Parameter(Mandatory = $true)][string]$auth,
    
            [Parameter(Mandatory = $true)][string]$url,
    
            [Parameter(Mandatory = $true)][string]$zabbix_host
    
        );
    
    
    
        Write-Host "Getting items from Zabbix host...";
    
    
        $params = @{
    
            body =  @{
    
                "jsonrpc" = "2.0";
    
                "method" = "item.get";
    
                "params" = @{
    
                    "host" = $zabbix_host
    
                };
    
                "id" = 1;
    
                "auth" = $auth;
    
            } | ConvertTo-Json;
    
            uri = "$url/api_jsonrpc.php";
    
            headers = @{"Content-Type" = "application/json"};
    
            method = "Post";
    
        };
    
    
        $result_json = Invoke-WebRequest @params -UseBasicParsing
    
        # -SkipCertificateCheck;
    
        $result_object = $result_json | ConvertFrom-Json;
    
        if ($result_object.error -ne $null)
    
        {
    
            Write-Host $("  Error: " + $result_object.error.message + " " + $result_object.error.data);
    
            return $null;
    
        }
    
        else
    
        {
    
            Write-Host "Successfully fetched items objects from Zabbix.";
    
            $result_object = $result_object.result
    
            #| ?{$_.key_ -notmatch "azure\.monitor\.metrics\.sh"};
    
            return $result_object;
    
        }
    
    }
    
    
    
    
    
    
    
    
    #Logging to Zabbix
    
    $zabbix_creds = New-Object System.Management.Automation.PSCredential ("username", $(ConvertTo-SecureString "password" -AsPlainText -Force));
    
    $zabbix_auth = Login-Zabbix -url $zabbix_url -creds $zabbix_creds;
    
    if ($zabbix_auth -eq $null)
    
    {
    
        Write-Host "Failed Logging to Zabbix. Exiting.";
    
        Write-Host 1;
    
        exit;
    
    }
    
    #/Logging to Zabbix
    
    
    #Getting metrics from Zabbix host
    
    $host_items = Get-ZabbixHostItems -url $zabbix_url -auth $zabbix_auth -zabbix_host $host_name
    write-host Found Items: $host_items.count;
    Last edited by slpefanis; 24-04-2020, 00:04.
  • dimir
    Zabbix developer
    • Apr 2011
    • 1080

    #2
    The item.get request looks correct. I'd check the rest of your code. And make sure the host name is exactly as configured, case-sensitive.

    Comment

    • slpefanis
      Member
      • Jan 2020
      • 52

      #3
      Thanks. Interesting when I run a host.get without anything my host isn't even showing up, but it lists all my hosts that are monitored by agent. Doesn't list anything monitored by SNMP (e.g. switches)

      Is there something within the Params that's used for filtering?

      The rest of the code works from what I can see as using another host that is monitored by agent seems to work fine.

      I noticed the 'id = 1' but can't seem to find anything that indicated what that means?

      Comment

      • dimir
        Zabbix developer
        • Apr 2011
        • 1080

        #4
        The 'id' means nothing. The idea is to e. g. identify requests in one login session.

        You should get all host items by default (I don't see no "filter" with "type" in your request).

        But that's interesting. Another way of getting host items would be using host.get with
        Code:
        "params" => {
           "selectItems": "extend",
           "filter": {"host" => $zabbix_host}
        }
        I wonder if that works.

        Comment

        • slpefanis
          Member
          • Jan 2020
          • 52

          #5
          Thanks for the response. I've tried what you suggested. Unfortunately, it still doesn't return anything, Code seems right as when I run it agains an PC, it returns the item?
          So far I can't seemt to return anything with SNMP, My zabbix Proxy's (Even though they are monitored) or my "Dummy" host for this. I've tried changing the Name and alsy tried by ID, but no luck either way.

          EDIT: I suddently had a thought, I am using a dedicated "apiuser, tested this with my user, it worked. So it's a permission issue with the user. DUH!. thanks for the help.
          Last edited by slpefanis; 24-04-2020, 00:04.

          Comment

          • dimir
            Zabbix developer
            • Apr 2011
            • 1080

            #6
            Ooooh. Great catch. Forgot about the permission thingie. Glad you've solved it!

            Comment

            • votantonio
              Junior Member
              • Jan 2021
              • 1

              #7
              Hi to all,
              Using this amazing repo https://github.com/vicioussn/zabbix-azure to get Azure metrics on Zabbix.
              Unfortunaly in this repo there are something that don't work for me on getting Zabbix Macro and Items.
              I have the proper value in the variable $result_json, but after when convert with: $result_object = $result_json | ConvertFrom-Json; the result_object is empty.
              Follow the scripts and also the result:

              Code:
              Write-Host "Getting macros for Zabbix host ID '$hostId'.";
              
              $params = @{
              body = @{
              "jsonrpc" = "2.0";
              "method" = "usermacro.get";
              "params" = @{
              "hostids" = $hostId;
              };
              "id" = 1;
              "auth" = $auth;
              } | ConvertTo-Json;
              uri = "$url/api_jsonrpc.php";
              headers = @{"Content-Type" = "application/json"};
              method = "Post";
              };
              
              $result_json = Invoke-WebRequest @params -UseBasicParsing -Verbose:$false -ErrorAction Stop;
              
              Write-Host "Result json: $result_json";
              
              $result_object = $result_json | ConvertFrom-Json -ErrorAction Stop ;
              
              Write-Host "Result object: $result_object";
              and the result:

              Code:
              Result json: {"jsonrpc":"2.0","result":[{"hostmacroid":"1330","hostid":"10526","macro":"{$ RESOURCEGROUP}","value":"resources","description":"", "type":"0"},{"hostmacroid":"1331","hostid":"10526" ,"macro":"{$SUBSCRIPTIONID}","value":"XXXXXXXXXXXXXXXXX","description":"","type":"0"}],"id":1} 
              
              Result object: @{jsonrpc=2.0; result=System.Object[]; id=1}
              So I don't think there are some problem with zabbix auth, but I can't understand what is wrong.

              I have Zabbix server on docker with alpine with this image: zabbix/zabbix-server-pgsql:alpine-5.0-latest and I run the script using powershell on alpine.

              Thanks for you time

              Comment

              Working...