调用zabbix api和配合ansible-playbook 在发版过程中禁用zabbix中对应的机器

python脚本如下:

# -*- coding: utf-8 -*
import json
import requests
import pymysql as mysql
import sys

mysql_conn= mysql.connect(
    host = "127.0.0.1",
    port = 3306,
    user = "root",
    passwd = "123456",
    db = "poll",
    charset="utf8"
)

url = "http://1.1.1.1/zabbix/api_jsonrpc.php"
header = {"Content-Type": "application/json"}

##获取api 令牌,在第一次请求的时候需要获取到令牌才能访问
# data = json.dumps(
#     {
#         "jsonrpc": "2.0",
#         "method": "user.login",
#         "params": {
#             "user": "Admin",
#             "password": "123456"
#         },
#         "id": 1,
#
#     }
# )

# request = requests.post(url, data=data, headers=header)
# print(request.text)

##禁用主机
def close_host(ip):
    sql = '''SELECT hostid FROM `poll`.`hosts` WHERE `host` = '%s' LIMIT 0,1000;''' %ip
    rcursor = mysql_conn.cursor()
    rcursor.execute(sql)
    id = rcursor.fetchone()


    try:
        close_host = json.dumps(
            {
                "jsonrpc": "2.0",
                "method": "host.update",
                "params": {
                    "hostid": id[0],
                    "status": 1
                },
                "auth": "123456",
                "id": 1
            }
        )
        request_update_macro = requests.post(url, data=close_host, headers=header)
        sc = json.loads(request_update_macro.text)
        print(sc)

    except Exception as e:
        print(e)


##打开主机
def open_host(ip):
    sql = '''SELECT hostid FROM `poll`.`hosts` WHERE `host` = '%s' LIMIT 0,1000;''' % ip
    rcursor = mysql_conn.cursor()
    rcursor.execute(sql)
    id = rcursor.fetchone()

    try:
        open_host = json.dumps(
            {
                "jsonrpc": "2.0",
                "method": "host.update",
                "params": {
                    "hostid": id[0],
                    "status": 0
                },
                "auth": "123456",
                "id": 1
            }
        )
        request_update_macro = requests.post(url, data=open_host, headers=header)
        sc = json.loads(request_update_macro.text)
        print(sc)

    except Exception as e:
        print(e)


if __name__ == '__main__':
    status = sys.argv[1].strip()
    ip_update = sys.argv[2]
    try:
        if status == 'close':
            print("在zabbix禁用主机%s!!" %ip_update)
            close_host(ip_update)
        elif status == 'open':
            print("在zabbix上打开主机%s!!" %ip_update)
            open_host(ip_update)

        else:
            print("输入参数有误!!")

    except Exception as e:
        print(e)

ansible-playbook 发版过程中加入对应的在ansible server 端执行python脚本,并将IP传入进去:
关闭zabbix中的机器:

  - name: close machine on zabbix
    hosts: 127.0.0.1
    connection: local
    shell:  /home/appsvr/data/anaconda3/bin/python3 /usr/local/operations_scripts/public/control-zabbix.py close {{ ansible_eth0['ipv4']['address'] }}
    ignore_errors: True

打开zabbix中的机器:

  - name: open machine on zabbix
    hosts: 127.0.0.1
    connection: local
    shell:  /home/appsvr/data/anaconda3/bin/python3 /usr/local/operations_scripts/public/control-zabbix.py open {{ ansible_eth0['ipv4']['address'] }}
    ignore_errors: True

你可能感兴趣的:(调用zabbix api和配合ansible-playbook 在发版过程中禁用zabbix中对应的机器)