LoginSignup
3
0

More than 1 year has passed since last update.

はじめに

今回は流行ってから久しいRestAPIをテーマに投稿したいと思います。
いざAPIを実行しようとしてもメーカー毎に仕様が異なるので、公式ページ見て調べるかと思います。
ところが最初のとっかかりが分かりづらく時間がかかったりするので纏めておきたいと思ったことがきっかけです。

対象のAPI

・Zabbix
(そのうちvCenterやAWXなども投稿できれば。)

実行環境

RestAPIをテスト的に実行するツールにはPostmanやVScodeのThunder Clientなどがありますが、今回は基本中の基本LinuxでCURLを実行したいと思います。

※このページはAPI実行対象のアプリケーションを理解しているということを前提としています。
※また、CURLコマンドについては先人の方々の記事がいっぱいありますのでわからない方は検索お願いします。

qiita用.drawio.png

APIをたたくLinux

[root@hon-cent8-01 ~]# cat /etc/redhat-release
CentOS Stream release 8
[root@hon-cent8-01 ~]# uname -a
Linux hon-cent8-01 4.18.0-383.el8.x86_64 #1 SMP Wed Apr 20 15:38:08 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
[root@hon-cent8-01 ~]#

※Zabbixに対してHTTP通信できるように設定してあります。

APIをたたかれるZabbixのVersion

5.0

ZabbixのAPIを実行する

公式マニュアル
https://www.zabbix.com/documentation/5.0/en/manual/api

正確な情報を知りたい方は上記から確認お願いします。

APIを実行するにはざっくり以下の手順になります。
1. 認証して認証トークンを取得する
2. 1.で取得したトークンを使って様々なメソッドを実行する

1.認証して認証トークンを取得する

先ほどのURLに具体的な例がマニュアルに記載されています。

POST http://company.com/zabbix/api_jsonrpc.php HTTP/1.1
       Content-Type: application/json-rpc

       {"jsonrpc":"2.0","method":"apiinfo.version","id":1,"auth":null,"params":{}}

ここから理解できるのは以下の3つ
1.HTTP メソッドはPOSTを使う
2.URLはhttp://<サーバのIP or FQDN>/zabbix/api_jsonrpc.php
3.HTTP RequestのHeaderにはContent-Type: application/json-rpcを含める

それでは実際に実行してみましょう。
事前準備としてまずはRequest Bodyのファイルを作成しておきます。

zabbix_01_auth.json
[root@hon-cent8-01 ~]# cat zabbix_01_auth.json
{
  "jsonrpc": "2.0",
  "method": "user.login",
  "params":{
    "user": "Admin",
    "password": "zabbix"
  },
  "id": 1,
  "auth": null
}

ユーザ、パスワードに関してはデフォルトのユーザを使っています。
それではCURLを実行してみます。
※jqコマンドは事前にインストールしています。

jqとは??
https://stedolan.github.io/jq/
jq is like sed for JSON data - you can use it to slice and filter and map and transform structured data with the same ease that sed, awk, grep and friends let you play with text.

[root@hon-cent8-01 ~]# curl -X POST -H "Content-Type: application/json-rpc" -d '@zabbix_01_auth.json' http://192.168.0.220/zabbix/api_jsonrpc.ph
p |jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   198    0    68  100   130     14     27  0:00:04  0:00:04 --:--:--    41
{
  "jsonrpc": "2.0",
  "result": "3f1f54fe798da1311e857ade0b602c3a",
  "id": 1
}

Responseの中のresultにトークンが含まれています。

2. 1.で取得したトークンを使って様々なメソッドを実行する

今回は監視対象となっているホスト情報を取得してみます。
https://www.zabbix.com/documentation/5.0/en/manual/api/reference/host
公式にはhostに対するメソッドは以下の7個があります。

host.create - creating new hosts
host.delete - deleting hosts
host.get - retrieving hosts
host.massadd - adding related objects to hosts
host.massremove - removing related objects from hosts
host.massupdate - replacing or removing related objects from hosts
host.update - updating hosts

まずは監視対象として登録されているホストの情報取得したいと思います。
情報取得のメソッドはhost.getっぽいので詳細を確認しに行きます。
https://www.zabbix.com/documentation/5.0/en/manual/api/reference/host/get

Examplesを見ると色々プロパティを指定できそうですが、まずはFilterなど使わずに1つのホストの情報を全て取得してみます。
1.と同様、事前準備としてまずはRequest Bodyのファイルを作成しておきます。

zabbix_02_hostget_03.json
{
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
        "filter": {
          "host": [
            "hon-nfs-01"
          ]
        }
    },
    "id": 2,
    "auth": "3f1f54fe798da1311e857ade0b602c3a"
}

実行結果はこちら。

[root@hon-cent8-01 ~]# curl -X POST -H "Content-Type: application/json-rpc" -d '@zabbix_02_hostget_03.json' http://192.168.0.220/zabbix/api_json
rpc.php |jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1049    0   840  100   209  16800   4180 --:--:-- --:--:-- --:--:-- 20980
{
  "jsonrpc": "2.0",
  "result": [
    {
      "hostid": "10632",
      "proxy_hostid": "0",
      "host": "hon-nfs-01",
      "status": "0",
      "disable_until": "0",
      "error": "",
      "available": "0",
      "errors_from": "0",
      "lastaccess": "0",
      "ipmi_authtype": "-1",
      "ipmi_privilege": "2",
      "ipmi_username": "",
      "ipmi_password": "",
      "ipmi_disable_until": "0",
      "ipmi_available": "0",
      "snmp_disable_until": "0",
      "snmp_available": "1",
      "maintenanceid": "0",
      "maintenance_status": "0",
      "maintenance_type": "0",
      "maintenance_from": "0",
      "ipmi_errors_from": "0",
      "snmp_errors_from": "0",
      "ipmi_error": "",
      "snmp_error": "",
      "jmx_disable_until": "0",
      "jmx_available": "0",
      "jmx_errors_from": "0",
      "jmx_error": "",
      "name": "hon-nfs-01",
      "flags": "0",
      "templateid": "0",
      "description": "",
      "tls_connect": "1",
      "tls_accept": "1",
      "tls_issuer": "",
      "tls_subject": "",
      "tls_psk_identity": "",
      "tls_psk": "",
      "proxy_address": "",
      "auto_compress": "1",
      "inventory_mode": "-1"
    }
  ],
  "id": 2
}
[root@hon-cent8-01 ~]#

このレスポンスはどこに書かれているのか??
答えはHost objectに記載されています。
https://www.zabbix.com/documentation/5.0/en/manual/api/reference/host/object#host

もし、hostidだけほしい!!となったらoutputを指定します。

zabbix_02_hostget_03.json
{
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
        "output": "hostid",
        "filter": {
          "host": [
            "hon-nfs-01"
          ]
        }
    },
    "id": 2,
    "auth": "3f1f54fe798da1311e857ade0b602c3a"
}

実行結果はこちら。

[root@hon-cent8-01 ~]# curl -X POST -H "Content-Type: application/json-rpc" -d '@zabbix_02_hostget_03.json' http://192.168.0.220/zabbix/api_jsonrpc.php |jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   290    0    54  100   236   1058   4627 --:--:-- --:--:-- --:--:--  5686
{
  "jsonrpc": "2.0",
  "result": [
    {
      "hostid": "10632"
    }
  ],
  "id": 2
}

まとめ

Zabbix APIを叩いてみました。
今回はhost.getのみでしたが、createやdelete、updateなどはよく使いそうです。
次回余裕があればvmware vCenter ServerのAPIを実行してみたいと思います。

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