生产实践:
连接数据库查找id,然后调用接口屏蔽报警
脚本内容:
PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb。PyMySQL 遵循 Python 数据库 API v2.0 规范,并包含了 pure-Python MySQL 客户端库。脚本内容如下
#!/usr/bin/env python3 #coding: utf-8 import http.client import sys import json import pymysql hostname=sys.argv[1] # 连接数据库 try: db = pymysql.connect( host="checklist.mysql01.chegva.com", user="anzhihe_r", passwd="Jxpxxxxxx2", db="falcon_portal", port=3350, charset="utf8") cursor = db.cursor() print('>> 已连接数据表,处理中...') sql = "SELECT id FROM host WHERE HOSTNAME = '%s';" % (hostname) # 执行SQL语句 cursor.execute(sql) id = cursor.fetchone() print(id[0]) except: print("Error: unable to fetch data") # 提交并关闭链接 cursor.close() db.close() print('>> Done.'); conn = http.client.HTTPSConnection("alarm.chegva.com") payload = {"hostIds":[id[0]],"begin":1560760220,"end":1561883420,"reason":"报警屏蔽"} headers = { 'User-Agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.90 Safari/537.36", 'Content-Type': "application/json", 'Accept': "application/json", 'Referer': "https://chegva.com/tree", 'Cookie': "xxx", 'Connection': "keep-alive" } conn.request("PATCH", "/api/v1/hydra/hosts/maintain", json.dumps(payload), headers) res = conn.getresponse() data = res.read() print(data.decode("utf-8"))
参考: