1. API
1.1 对 host 创建
# curl -i -X POST -H 'Content-Type:application/json' -d '
> {
> "jsonrpc": "2.0",
> "method": "maintenance.create",
> "params": {
> "name": "Maintenance API test 01",
> "active_since": "1560970800",
> "active_till": "1560972600",
> "hostids": [
> 10185,
> 10184
> ],
> "timeperiods": [{
> "timeperiod_type": 0,
> "every": 1,
> "dayofweek": 64,
> "start_time": 64800,
> "period": 3600
> }]
> },
> "auth": "aab4f7f7be4778f2fab229980f4a95c4",
> "id": "1"
> }' http://192.168.4.142/api_jsonrpc.php
HTTP/1.1 200 OK
Date: Wed, 19 Jun 2019 07:08:10 GMT
Server: Apache/2.2.15 (CentOS)
X-Powered-By: PHP/5.3.3
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: POST
Access-Control-Max-Age: 1000
Content-Length: 61
Connection: close
Content-Type: application/json
{"jsonrpc":"2.0","result":{"maintenanceids":["72"]},"id":"1"}
1.2 对 group 创建
# curl -i -X POST -H 'Content-Type:application/json' -d '
> {
> "jsonrpc": "2.0",
> "method": "maintenance.create",
> "params": {
> "name": "Maintenance API test group",
> "active_since": "1560927600",
> "active_till": "1560929400",
> "groupids": [
> 10
> ],
> "timeperiods": [{
> "timeperiod_type": 0,
> "every": 1,
> "dayofweek": 64,
> "start_time": 64800,
> "period": 3600
> }]
> },
> "auth": "aab4f7f7be4778f2fab229980f4a95c4",
> "id": "1"
> }' http://192.168.4.142/api_jsonrpc.php
HTTP/1.1 200 OK
Date: Wed, 19 Jun 2019 07:31:03 GMT
Server: Apache/2.2.15 (CentOS)
X-Powered-By: PHP/5.3.3
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: POST
Access-Control-Max-Age: 1000
Content-Length: 61
Connection: close
Content-Type: application/json
{"jsonrpc":"2.0","result":{"maintenanceids":["74"]},"id":"1"}
登录到页面中查看,创建完成。
***
注意
1. auth
2. 时间为timestamp格式,需要转化时间格式。
3. name 必须为唯一,如果重复,无法创建
{"jsonrpc":"2.0","error":{"code":-32602,"message":"Invalid params.","data":"Maintenance \"Maintenance API test\" already exists."},"id":"1"}
***
2. ansible
2.1 安装包
pip install zabbix-api
2.2 playbook
---
- hosts: localhost
remote_user: root
vars:
dbs:
- "dbw01mb"
- "dbr01mb"
tasks:
- name: Create maintenance window for host dbw01mb & dbr01mb for 10 minutes
zabbix_maintenance:
name: test ansible module zabbix_maintenance {{ item + ' ' + ansible_date_time.date + ' ' + ansible_date_time.time }}
host_names: "{{ item }}"
state: present
minutes: 15
server_url: http://192.168.4.142/
login_user: Admin
login_password: zabbix
with_items:
- "{{ dbs }}"
执行结果
TASK [Create maintenance window for host dbw01mb & dbr01mb for 10 minutes] *******************************************************************************
changed: [localhost] => (item=dbw01mb) => {
"ansible_loop_var": "item",
"changed": true,
"invocation": {
"module_args": {
"collect_data": true,
"desc": "Created by Ansible",
"host_groups": null,
"host_names": [
"dbw01mb"
],
"http_login_password": null,
"http_login_user": null,
"login_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"login_user": "Admin",
"minutes": 15,
"name": "test ansible module ********_maintenance dbw01mb 2019-06-19 05:17:26",
"server_url": "http://192.168.4.142/",
"state": "present",
"timeout": 10,
"validate_certs": true
}
},
"item": "dbw01mb"
}
changed: [localhost] => (item=dbr01mb) => {
"ansible_loop_var": "item",
"changed": true,
"invocation": {
"module_args": {
"collect_data": true,
"desc": "Created by Ansible",
"host_groups": null,
"host_names": [
"dbr01mb"
],
"http_login_password": null,
"http_login_user": null,
"login_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"login_user": "Admin",
"minutes": 15,
"name": "test ansible module ********_maintenance dbr01mb 2019-06-19 05:17:26",
"server_url": "http://192.168.4.142/",
"state": "present",
"timeout": 10,
"validate_certs": true
}
},
"item": "dbr01mb"
}
***
注意
1. host_names 和 host_name 都可以。zabbix 会通过 host_name 找到 hostid
2. 执行完成之后,开始,minutes 默认为 10分钟
3. 之所以在 name 中增加时间参数,是因为如果 name 重复,不会报错,也不会创建。加一个唯一的标签加以区分。如果不加这种唯一标签,最好在停机之后,删除该 Maintenance window。
***