zabbixAPI叩いてみた

## 勉強前イメージ

zabbixAPIって前にやったことあるからおさらい。
結構めんどくさかったイメージすごいある....
頑張る。。。

## 調査

公式ドキュメントは[こちら](https://www.zabbix.com/documentation/4.0/manual/api)
今回はzabbix4系を使います。
インストール方法は[こちら](https://qiita.com/miyuki_samitani/items/39e366e75d64d0d94506)をごらんください

### 前準備

- jqコマンドのインストール(見やすくするため)

```
yum install epel-release
yum install jq
```

- 認証トークンの取得

```
curl -X GET -H "Content-Type:application/json-rpc" -d '{ "id": "1", "params": { "user": "[ユーザ名]", "password": "[パスワード]" }, "method": "user.login", "jsonrpc": "2.0" }' http://[zabbixのIPアドレス]/zabbix/api_jsonrpc.php
==========
{"jsonrpc":"2.0","result":"XXXXXXXXXXXXXXXXXXXXXXXXX","id":"1"}
==========

※
ユーザ名 = zabbixにloginするときのID
パスワード = zabbixにloginするときのパスワード
```

### ホスト一覧取得

- zabbixの画面

以下のホスト一覧を取得します

![ホストの設定 - Google Chrome 2020-11-25 21.21.45.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/129517/f0bd1c62-69d7-b8d3-ad72-a2b0d748c9af.png)


- ホスト一覧全部取得

```
curl -X GET -H "Content-Type:application/json-rpc" -d '{
	"auth":"[認証トークン]", 
	"method":"host.get", 
	"id":1, 
	"params":{
		"output":"extend", 
		"selectInterfaces":"extend"
	}, 
	"jsonrpc":"2.0"
}' http://[zabbixのIPアドレス]/zabbix/api_jsonrpc.php | jq '.'
==========
{
  "jsonrpc": "2.0",
  "result": [
    {
      "hostid": "10084",
      "proxy_hostid": "0",
      "host": "Zabbix server",
      "status": "0",
      "disable_until": "0",
      "error": "",
      "available": "1",
      "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": "0",
      "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": "Zabbix server",
      "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",
      "interfaces": [
        {
          "interfaceid": "1",
          "hostid": "10084",
          "main": "1",
          "type": "1",
          "useip": "1",
          "ip": "127.0.0.1",
          "dns": "",
          "port": "10050",
          "bulk": "1"
        }
      ]
    },
    {
      "hostid": "10271",
      "proxy_hostid": "0",
      "host": "web server",
      "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": "0",
      "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": "web server",
      "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",
      "interfaces": [
        {
          "interfaceid": "2",
          "hostid": "10271",
          "main": "1",
          "type": "1",
          "useip": "1",
          "ip": "192.168.156.121",
          "dns": "",
          "port": "10050",
          "bulk": "1"
        }
      ]
    }
  ],
  "id": 1
}
==========
```

- ホスト名一覧取得

```
curl -X GET -H "Content-Type:application/json-rpc" -d '{
	"auth":"[認証トークン]", 
	"method":"host.get", 
	"id":1, 
	"params":{
		"output":"extend", 
		"selectInterfaces":"extend"
	}, 
	"jsonrpc":"2.0"
}' http://[zabbixのIPアドレス]/zabbix/api_jsonrpc.php | jq '.result[].host'
==========
"Zabbix server"
"web server"
==========
```

- ホストID一覧取得

```
curl -X GET -H "Content-Type:application/json-rpc" -d '{
	"auth":"[認証トークン]", 
	"method":"host.get", 
	"id":1, 
	"params":{
		"output":"extend", 
		"selectInterfaces":"extend"
	}, 
	"jsonrpc":"2.0"
}' http://[zabbixのIPアドレス]/zabbix/api_jsonrpc.php | jq '.result[].hostid'
==========
"10084"
"10271"
==========
```

### アイテム一覧取得

- 「web server」 のアイテム一覧取得

`"hostids":"[ホストID]"` は 「ホストID一覧取得」 でとってきます。

```
curl -X GET -H "Content-Type:application/json-rpc" -d '{
	"auth":"[認証トークン]", 
	"method":"item.get", 
	"id":1, 
	"params":{
		"output":"extend", 
		"selectInterfaces":"extend",
		"hostids":"[ホストID]"
	}, 
	"jsonrpc":"2.0"
}' http://[zabbixのIPアドレス]/zabbix/api_jsonrpc.php | jq '.'
==========
{
  "jsonrpc": "2.0",
  "result": [
    {
      "itemid": "28862",
      "type": "3",
      "snmp_community": "",
      "snmp_oid": "",
      "hostid": "10271",
      "name": "HTTP service is running",
      "key_": "net.tcp.service[http]",
      "delay": "1m",
      "history": "1w",
      "trends": "365d",
      "status": "0",
      "value_type": "3",
      "trapper_hosts": "",
      "units": "",
      "snmpv3_securityname": "",
      "snmpv3_securitylevel": "0",
      "snmpv3_authpassphrase": "",
      "snmpv3_privpassphrase": "",
      "formula": "",
      "error": "",
      "lastlogsize": "0",
      "logtimefmt": "",
      "templateid": "23645",
      "valuemapid": "1",
      "params": "",
      "ipmi_sensor": "",
      "authtype": "0",
      "username": "",
      "password": "",
      "publickey": "",
      "privatekey": "",
      "mtime": "0",
      "flags": "0",
      "interfaceid": "2",
      "port": "",
      "description": "",
      "inventory_link": "0",
      "lifetime": "30d",
      "snmpv3_authprotocol": "0",
      "snmpv3_privprotocol": "0",
      "state": "0",
      "snmpv3_contextname": "",
      "evaltype": "0",
      "jmx_endpoint": "",
      "master_itemid": "0",
      "timeout": "3s",
      "url": "",
      "query_fields": [],
      "posts": "",
      "status_codes": "200",
      "follow_redirects": "1",
      "post_type": "0",
      "http_proxy": "",
      "headers": [],
      "retrieve_mode": "0",
      "request_method": "0",
      "output_format": "0",
      "ssl_cert_file": "",
      "ssl_key_file": "",
      "ssl_key_password": "",
      "verify_peer": "0",
      "verify_host": "0",
      "allow_traps": "0",
      "interfaces": [
        {
          "interfaceid": "2",
          "hostid": "10271",
          "main": "1",
          "type": "1",
          "useip": "1",
          "ip": "192.168.156.121",
          "dns": "",
          "port": "10050",
          "bulk": "1"
        }
      ],
      "lastclock": "1606308845",
      "lastns": "96787888",
      "lastvalue": "0",
      "prevvalue": "0"
    }
  ],
  "id": 1
}
==========
```

- 「web server」 のアイテム名取得

```
curl -X GET -H "Content-Type:application/json-rpc" -d '{
	"auth":"[認証トークン]", 
	"method":"item.get", 
	"id":1, 
	"params":{
		"output":"extend", 
		"selectInterfaces":"extend",
		"hostids":"[ホストID]"
	}, 
	"jsonrpc":"2.0"
}' http://[zabbixのIPアドレス]/zabbix/api_jsonrpc.php | jq '.result[].name[]'
==========
"HTTP service is running"
==========
```

## 勉強後イメージ

今回取得するだけしてみた
jqコマンド使いこなせなくて難しかった....
まだめんどくさいところまで来てないな

## 参考

- [ZabbixAPIの使い方](https://qiita.com/takahito007/items/e5a9a454148f0b5b9b93)
- [zabbixのAPIを使ってホスト一覧を取得する。](https://tsunokawa.hatenablog.com/entry/2014/04/18/180141)