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?
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;
Comment