2
3

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 serverにAPIを使ってhost登録したり、agentを一瞬で構築したり

Posted at

こんにちは。
株式会社クラスアクト インフラストラクチャ事業部の大塚です。

前回、前々回とZabbix server/agentを導入し、監視環境を作ってみました。
1台2台程度であれば、コマンドをポチポチ入力したり、WebUIで設定するのも良いかもしれませんが台数が増えるとだるさが出てきます。
ということで、今回はタイトルの通りAPIを使ってCUIベースでZabbix serverに監視対象のホストを登録したり、agentをコマンドを流し込むだけで作れるようにしたいと思います。

構築

agentの環境を簡単に作れるコマンドセット

以下になります。sedコマンドの127.0.0.1ではない方のIPアドレスはZabbix serverのIPアドレスを設定してください。これをTeratermで接続したサーバ上で右クリックでペーストし、実行することで簡単に作れます。

wget https://repo.zabbix.com/zabbix/6.4/ubuntu/pool/main/z/zabbix-release/zabbix-release_6.4-1+ubuntu22.04_all.deb
dpkg -i zabbix-release_6.4-1+ubuntu22.04_all.deb
apt update
apt install zabbix-agent2 zabbix-agent2-plugin-* -y 
cp -p /etc/zabbix/zabbix_agent2.conf /etc/zabbix/zabbix_agent2.conf.org 
sed 's/Server=127.0.0.1/Server=192.168.2.222/' -i /etc/zabbix/zabbix_agent2.conf
sed 's/ServerActive=127.0.0.1/ServerActive=192.168.2.222/' -i /etc/zabbix/zabbix_agent2.conf
systemctl restart zabbix-agent2systemctl restart zabbix-agent2
systemctl enable zabbix-agent2

serverに監視対象のホストをAPIを使って登録する

これを実行する為にはまずZabbixのAPI tokenを作成する必要があります。
ZabbixにWebでログインし、UsersのAPI tokensを押下します。
image (70).png
右上にあるCreate API tokenを押下します。
image (71).png
以下の様に設定を入れ込み、API tokenを作成。クリップボードにコピーしておきましょう。
image (72).png
image (73).png
APIでホストを登録する際、GroupIDとTemplateIDを確認する必要があります。
それぞれ以下の様にコマンドで確認することが出来ます。
authの部分は先ほどコピーしたものに置き換えてください。またnameやhostの後の[]に囲われているものはそれぞれ確認したいGroupIDとTemplateIDをもつGroupの名前とTemplateの名前に置き替えて入力ください。

root@zabbix:~# curl -X POST -H "Content-Type: application/json" -d '{
    "jsonrpc": "2.0",
    "method": "hostgroup.get",
    "params": {
        "output": "extend",
        "filter": {
            "name": [
                "Linux servers"
            ]
        }
    },
    "auth": "024a78e1a2dca90dfd2e274d012ef13d8ce3429fae1408a4fd77d1842da6afcd",
    "id": 1
}' http://localhost/zabbix/api_jsonrpc.php

★出力結果
{"jsonrpc":"2.0","result":[{"groupid":"2","name":"Linux servers","flags":"0","uuid":"dc579cd7a1a34222933f24f52a68bcd8"}],"id":1}
root@zabbix:~# curl -X POST -H "Content-Type: application/json" -d '{
    "jsonrpc": "2.0",
    "method": "template.get",
    "params": {
        "output": "extend",
        "filter": {
            "host": [
                "Linux by Zabbix agent"
            ]
        }
    },
    "auth": "024a78e1a2dca90dfd2e274d012ef13d8ce3429fae1408a4fd77d1842da6afcd",
    "id": 1
}' http://localhost/zabbix/api_jsonrpc.php

★出力結果
{"jsonrpc":"2.0","result":[{"proxy_hostid":"0","host":"Linux by Zabbix agent","status":"3","ipmi_authtype":"-1","ipmi_privilege":"2","ipmi_username":"","ipmi_password":"","maintenanceid":"0","maintenance_status":"0","maintenance_type":"0","maintenance_from":"0","name":"Linux by Zabbix agent","flags":"0","templateid":"10001","description":"Official Linux template. Requires agent of Zabbix 6.4 or newer.\r\n      \r\nYou can discuss this template or leave feedback on our forum https://www.zabbix.com/forum/zabbix-suggestions-and-feedback/387225-discussion-thread-for-official-zabbix-template-for-linux\r\n\r\nGenerated by official Zabbix template tool \"Templator\" 2.0.0","tls_connect":"1","tls_accept":"1","tls_issuer":"","tls_subject":"","tls_psk_identity":"","tls_psk":"","proxy_address":"","auto_compress":"1","custom_interfaces":"0","uuid":"f8f7908280354f2abeed07dc788c3747","vendor_name":"Zabbix","vendor_version":"6.4-0"}],"id":1}

これらで必要なデータは揃いました。
以下のコマンドをserver上で実施することでAPIを使って監視対象のホストを登録することが出来ます。GroupやTemplateが同じであればhostとipを変更するだけで良いので、使いまわしも簡単です。

root@zabbix:~# curl -X POST -H "Content-Type: application/json" -d '{
    "jsonrpc": "2.0",
    "method": "host.create",
    "params": {
        "host": "k8s-worker01",
        "interfaces": [
            {
                "type": 1,
                "main": 1,
                "useip": 1,
                "ip": "192.168.2.31",
                "dns": "",
                "port": "10050"
            }
        ],
        "groups": [
            {
                "groupid": "2"
            }
        ],
        "templates": [
            {
                "templateid": "10001"
            }
        ]
    },
    "auth": "024a78e1a2dca90dfd2e274d012ef13d8ce3429fae1408a4fd77d1842da6afcd",
    "id": 1
}' http://localhost/zabbix/api_jsonrpc.php

★出力結果 
{"jsonrpc":"2.0","result":{"hostids":["10603"]},"id":1}

そんなこんなで3台追加で監視対象としました。
一回手順が分かってしまえば、こちらの方が圧倒的に楽だなと思いました。
image (74).png

2
3
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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?