3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Zabbix API を操作するためのメモ・リンク集

Last updated at Posted at 2022-08-21

Zabbix API をコマンドライン・スクリプトから使う

Zabbix API (REST) を使って、Zabbix サーバの設定を取得する・設定を一括変更するために調べたリンク, snipet をメモしておく。というわけで、随時、変更・修正する私的・動的メモ。

本家ドキュメント

スプリクト言語から叩く

bash/curl, powershell, python, ruby から、API を叩く例を

bash / curl

powershell / Invoke-WebRequest

スクラッチからだと、こんな感じ(机上検討のみ、未検証)
# Zabbix 認証情報を取得
if(!$credential){
    $credential = Get-Credential
}

# 認証トークン(sessionid)リクエスト(user.login)用パラメータ 
$baseurl = 'hoge.com/zabbix'
$params = @{
    body =  @{
        "jsonrpc" = "2.0"
        "method" = "user.login"
        "params" = @{
            "user" = $credential.UserName
            "password" = $credential.GetNetworkCredential().Password
        }
        "id" = 1
        "auth" = $null
    } | ConvertTo-Json
    uri = "$baseurl/api_jsonrpc.php"
    headers = @{"Content-Type" = "application/json"}
    method = "Post"
}

# user.login を実行
$result = Invoke-WebRequest @params
$auth_token = ($result.Content | ConvertFrom-Json).result

# 情報取得事例: ホストグループ取得(hostgroup.get)用パラメータ 
$params.body = @{
    "jsonrpc" = "2.0"
    "method" = "hostgroup.get"
    "params" = @{
        output = "extend"
        "filter" = @{ "name" = "Zabbix servers"}
    }
    auth = $auth_token
    id = 2
} | ConvertTo-Json

# ホストグループ取得
$result = Invoke-WebRequest @params
$result = $result.Content 

# 取得情報を表示
$result

Python

Ruby

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?