Created
February 19, 2021 11:34
-
-
Save krymtkts/2666307b69fd38e8f527000e3506af82 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ZabbixApi { | |
[string] $Ip | |
[string] $Port | |
[string] $Auth | |
static [PSCustomObject] Invoke($Body, $Ip, $Port) { | |
return Invoke-RestMethod -Method POST -Uri "http://$Ip`:$port/api_jsonrpc.php" -Headers @{'Content-Type' = 'application/json-rpc' } -Body $Body | |
} | |
static [string] GenerateBody($Method, $Params, $Auth) { | |
if ($Auth) { | |
$authvalue = "`"$Auth`"" | |
} | |
else { | |
$authvalue = 'null' | |
} | |
return @" | |
{ | |
"jsonrpc": "2.0", | |
"method": "$Method", | |
"params": $($Params | ConvertTo-Json), | |
"id": 1, | |
"auth": $authvalue | |
} | |
"@ | |
} | |
static [ZabbixApi] LogIn($Ip, $Port, $User, $Password) { | |
$me = New-Object -TypeName ZabbixApi | |
$body = [ZabbixApi]::GenerateBody("user.login", @{ | |
"user" = "$User"; | |
"password" = "$Password"; | |
}, | |
$null | |
) | |
write-host $body | |
$res = [ZabbixApi]::Invoke($body, $Ip, $Port) | |
Write-Host $res.result | |
$me.Ip = $Ip | |
$me.Port = $Port | |
$me.Auth = $res.result | |
return $me | |
} | |
static [ZabbixApi] LogIn() { | |
return [ZabbixApi]::LogIn('127.0.0.1', 10080, 'Admin', 'zabbix') | |
} | |
[PSCustomObject] InvokeAuthenticatedMethod($Body) { | |
return [ZabbixApi]::Invoke($body, $this.Ip, $this.Port) | |
} | |
[PSCustomObject] CreateHostGroupIfNotExists($HostGroupName) { | |
$body = [ZabbixApi]::GenerateBody("hostgroup.get", @{ | |
"output" = "extend"; | |
"filter" = @{"name" = @($HostGroupName) } | |
}, $this.Auth) | |
$res = [ZabbixApi]::Invoke($body, $this.Ip, $this.Port) | |
if ($res.result.count -eq 0) { | |
$body = [ZabbixApi]::GenerateBody("hostgroup.create", @{"name" = $HostGroupName }, $this.Auth) | |
$res = [ZabbixApi]::Invoke($body, $this.Ip, $this.Port) | |
return $res.result.groupids[0] | |
} | |
return $res.result[0].groupid | |
} | |
[PSCustomObject] CreateTemplateIfNotExists($TemplateName, $HostGroupId) { | |
$body = [ZabbixApi]::GenerateBody("template.get", @{ | |
"output" = "extend"; | |
"filter" = @{"host" = @($TemplateName) } | |
}, $this.Auth) | |
$res = [ZabbixApi]::Invoke($body, $this.Ip, $this.Port) | |
if ($res.result.count -eq 0) { | |
$body = [ZabbixApi]::GenerateBody("template.create", @{ | |
"host" = $TemplateName; | |
"groups" = @(@{ | |
"groupid" = $HostGroupId; | |
}); | |
}, $this.Auth) | |
$res = [ZabbixApi]::Invoke($body, $this.Ip, $this.Port) | |
return $res.result.templateids[0] | |
} | |
return $res.result[0].templateid | |
} | |
[PSCustomObject] CreateHostIfNotExists($HostName, $HostGroupId, $TemplateIds) { | |
$body = [ZabbixApi]::GenerateBody("host.get", @{ | |
"output" = "extend"; | |
"filter" = @{ | |
"host" = @($HostName); | |
} | |
}, $this.Auth) | |
$res = [ZabbixApi]::Invoke($body, $this.Ip, $this.Port) | |
if ($res.result.count -eq 0) { | |
$templates = $TemplateIds | ForEach-Object { @{"templateid" = $_; } } | |
$body = [ZabbixApi]::GenerateBody("host.create", @{ | |
"host" = $HostName; | |
"interfaces" = @( | |
@{ | |
"type" = 1; | |
"main" = 1; | |
"useip" = 1; | |
"ip" = "127.0.0.1"; | |
"dns" = ""; | |
"port" = "10050"; | |
} | |
); | |
"groups" = @(@{ | |
"groupid" = $HostGroupId | |
}); | |
"templates" = $templates; | |
}, $this.Auth) | |
$res = [ZabbixApi]::Invoke($body, $this.Ip, $this.Port) | |
return $res.result.hostids[0] | |
} | |
return $res.result[0].hostid | |
} | |
} | |
$Zapi = [ZabbixApi]::LogIn() | |
$hgid = $Zapi.CreateHostGroupIfNotExists('Host Group') | |
$tpids = @() | |
$tpids += $Zapi.CreateTemplateIfNotExists('Template', $hgid) | |
$Zapi.CreateHostIfNotExists('Host', $hgid, $tpids) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment