ZabbixAPIを使用したトリガー有効化

# 解決したい問題
* Active-StandbyのHA構成にしているシステムでF/Oのトリガーを任意のプロセスに指定していて、Zabbixのトリガーもそのプロセスの監視にしている場合、通常時はPrimaryでは正常だが、Secondaryでは異常として検知し続けてしまう。


#方法
* Secondaryではトリガーを無効にしておき、F/O時にzabbix apiでトリガーIDを指定して有効にする。
    * https://www.zabbix.com/documentation/2.2/manual/api

#イメージ
![cloudcraft - zabbix API.png](https://qiita-image-store.s3.amazonaws.com/0/58788/2213fa56-89bd-0501-565c-bf1923104205.png)

#zabbix api
## user.loginメソッドで認証
* リクエスト

```bash
$ curl -X GET -H "Content-Type:application/json-rpc" -d '{"auth":null,"method":"user.login","id":1,"params":{"user":"*****","password":"*****"},"jsonrpc":"2.0"}' http[s]://*****/api_jsonrpc.php
```
* レスポンス

```bash
{"jsonrpc":"2.0","result":"****************************","id":1}
```

## host.getメソッドによるホスト一覧取得
* リクエスト
    * 取得したキーを記述してリクエストする

```bash
$ curl -X GET -H "Content-Type:application/json-rpc" -d '{"auth":"****************************","method":"host.get","id":1,"params":{},"jsonrpc":"2.0"}' http[s]://*****/api_jsonrpc.php
```

## trigger.getメソッドによるトリガーID取得

```bash
$ curl -X GET -H "Content-Type:application/json-rpc" -d '{"auth":"****************************","method":"trigger.get","id":1,"params":{},"jsonrpc":"2.0"}' http[s]://*****/api_jsonrpc.php | jq '.' > zabbix-api-trigger_get
```

## trigger.updateメソッドによるトリガー有効化

```bash
$ curl -X GET -H "Content-Type:application/json-rpc" -d '{"auth":"****************************","method":"trigger.update","id":1,"params":{"triggerid":"****","status":0},"jsonrpc":"2.0"}' http[s]://*****/api_jsonrpc.php | jq '.'
```


# 設定
* ホストIDでFilterし目的のトリガーIDを取得
    * paramsにtriggeridとdescriptionを併せて記述しておくと分かりやすい

```bash

$ curl -X GET -H "Content-Type:application/json-rpc" -d '{"auth":"****************************","method":"trigger.get","id":1,"params":{"output":["triggerid","description"],"filter":{"hostid":"****"}},"jsonrpc":"2.0"}' http[s]://*****/api_jsonrpc.php | jq '.'

{
  "id": 1,
  "result": [
    {
      "description": "Host name of zabbix_agentd was changed on {HOST.NAME}",
      "triggerid": "*****"
    },
    {
      "description": "Zabbix agent on {HOST.NAME} is unreachable for 1 minutes",
      "triggerid": "*****"
    },
    {
      "description": "Version of zabbix_agent(d) was changed on {HOST.NAME}",
      "triggerid": "*****"
    },
省略
  ],
  "jsonrpc": "2.0"
}

```

* descriptionを指定した検索が便利

```bash
$ curl -X GET -H "Content-Type:application/json-rpc" -d '{"auth":"****************************","method":"trigger.get","id":1,"params":{"output":["triggerid","description"],"filter":{"hostid":"*****"},"search":{"description":"Zabbix trapper processes more than 75% busy"}},"jsonrpc":"2.0"}' http[s]://*****/api_jsonrpc.php | jq '.'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   353  100   117  100   236    104    209  0:00:01  0:00:01 --:--:--   209
{
  "id": 1,
  "result": [
    {
      "description": "Zabbix trapper processes more than 75% busy",
      "triggerid": "*****"
    }
  ],
  "jsonrpc": "2.0"
}

```



## トリガーIDを指定して有効化

```bash

$ curl -X GET -H "Content-Type:application/json-rpc" -d '{"auth":"****************************","method":"trigger.update","id":1,"params":{"triggerid":"*****","status":0},"jsonrpc":"2.0"}' http[s]://*****/api_jsonrpc.php | jq '.'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   192  100    58  100   134    602   1391 --:--:-- --:--:-- --:--:--  1395
{
  "id": 1,
  "result": {
    "triggerids": [
      "*****"
    ]
  },
  "jsonrpc": "2.0"
}
```
## トリガーIDを指定して無効化

```bash

$ curl -X GET -H "Content-Type:application/json-rpc" -d '{"auth":"****************************","method":"trigger.update","id":1,"params":{"triggerid":"*****","status":1},"jsonrpc":"2.0"}' http[s]://*****/api_jsonrpc.php | jq '.'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   192  100    58  100   134     50    117  0:00:01  0:00:01 --:--:--   117
{
  "id": 1,
  "result": {
    "triggerids": [
      "*****"
    ]
  },
  "jsonrpc": "2.0"
}

```

* 有効化コマンドを自動アクションに組み込んでアクションの実行条件に”F/O発生時”とし、実行内容を作成する
![Screenshot from 2016-02-08 23:28:48.png](https://qiita-image-store.s3.amazonaws.com/0/58788/534b34b9-e34b-9160-a531-7f70ec3cb7bc.png)