1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
| cat > nacos_check_status.py << EOF
import requests import json import time import logging from logging.handlers import RotatingFileHandler import sys logging.basicConfig(level=logging.DEBUG)
log_handle = RotatingFileHandler('./log.txt', maxBytes=1024*1024, backupCount=5)
formatter = logging.Formatter("format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s-%(funcName)s',")
log_handle.setFormatter(formatter)
logging.getLogger().addHandler(log_handle)
def progress_bar(timmer): for i in range(1, 101): print("\r", end="") print("Waiting: {}%: ".format(i), "▋" * (i // 2), end="") sys.stdout.flush() time.sleep(timmer) class Nacos(): def __init__(self): self.webhookurl="https://oapi.dingtalk.com/robot/send?access_token=1968dd11647dfd686c4e1107cf1ad3d0d21c3813a824ee632728327722d9fa82" self.login_url = "/nacos/v1/auth/users/login" self.accessToken=None self.service_url = "/nacos/v1/ns/catalog/services" self.instance_url = "/nacos/v1/ns/instance" @staticmethod def dingding(self, content): data = { "msgtype": "text", "text": { "content": "Nacos-" + content, }, } headers = {'Content-Type': 'application/json;charset=utf-8'} response = requests.post(self.webhookurl, data=json.dumps(data), headers=headers) print(response.content) def get_access_token(self, nacos_server): logging.info("\n\n************** NacosServer: {0} **************".format(nacos_server)) "登录获取nacos accessToken" params= "username=nacos&password=nacos" try: r = json.loads(requests.post(params=params, url="http://" + nacos_server + self.login_url, timeout=5).text) accessToken = str((r['accessToken'])) return {"accessToken": accessToken, "result": True, "server": nacos_server, "msg": "Login Nacos Success...[{0}]".format(nacos_server)} except Exception as e: return {"accessToken": None, "result": False, "server": nacos_server, "msg": "Login Nacos Falied...[{0}] Error: {1}".format(nacos_server, str(e))} def register_test(self, accessToken, nacos_server): ''' 1. 创建实例 2. 注册服务 3. 删除实例 4. 删除服务 ''' data={ "serviceName": "test_service_instance", "namespaceId": "public", "accessToken": accessToken, "ip": nacos_server.split(":")[0], "port": nacos_server.split(":")[1], "ephemeral": False } try: r = (requests.post(params=data, url="http://" + nacos_server + self.instance_url, timeout=5).text) if r == "ok": msg = "[注册实例&创建服务成功] Service: {0} NameSpace: {1} {2} InstanceIP: {3}".format(data['serviceName'], data['namespaceId'], nacos_server, data['ip']) logging.info(msg) else: msg = "[注册实例&创建服务失败] Service: {0} NameSpace: {1} {2} {3}".format(data['serviceName'], data['namespaceId'], nacos_server, r) logging.error(msg) self.dingding(self, content=msg) except Exception as e: msg = "[注册实例&创建服务失败] Service: {0} NameSpace: {1} {2} {3}".format(data['serviceName'], data['namespaceId'], nacos_server, r) logging.error(msg) self.dingding(self, content=msg) def delete_instance_service(self, accessToken, nacos_server): data={ "serviceName": "test_service_instance", "namespaceId": "public", "accessToken": accessToken, "ip": nacos_server.split(":")[0], "port": nacos_server.split(":")[1], "ephemeral": False } try: r = (requests.delete(params=data, url="http://" + nacos_server + self.instance_url, timeout=5).text) if r == "ok": msg = "[注销实例成功] Service: {0} NameSpace: {1} {2} ".format(data['serviceName'], data['namespaceId'], nacos_server) logging.info(msg) else: msg = "[注销实例失败] Service: {0} NameSpace: {1} {2} {3}".format(data['serviceName'], data['namespaceId'], nacos_server, r) logging.error(msg) self.dingding(self, content=msg) except Exception as e: msg = "[注销实例失败] Service: {0} NameSpace: {1} {2} {3}".format(data['serviceName'], data['namespaceId'], nacos_server, r) logging.error(msg) self.dingding(self, content=msg) progress_bar(timmer=0.08) try: r = (requests.delete(params=data, url="http://" + nacos_server + "/nacos/v1/ns/service", timeout=5).text) if r == "ok": msg = "[删除服务成功] Service: {0} NameSpace: {1} {2} ".format(data['serviceName'], data['namespaceId'], nacos_server) logging.info(msg) else: msg = "[删除服务失败] Service: {0} NameSpace: {1} {2} {3}".format(data['serviceName'], data['namespaceId'], nacos_server, r) logging.error(msg) self.dingding(self, content=msg) except Exception as e: msg = "[删除服务失败] Service: {0} NameSpace: {1} {2} {3}".format(data['serviceName'], data['namespaceId'], nacos_server, r) logging.error(msg) self.dingding(self, content=msg) progress_bar(timmer=0.1) def get_service_list(self, accessToken, nacos_server): "请求访问某个环境的某一个服务列表, 检查返回数据是否为空,为空则为不正常" payload={ "accessToken": accessToken, "pageNo": 1, "pageSize": 10, "namespaceId": "stage" } try: r = json.loads(requests.get(params=payload, url="http://" + nacos_server + self.service_url).text) if r['count'] == 0: msg = "[获取注册服务数据失败] Nacos Server {0} 当前Stage服务列表为空!".format(nacos_server) logging.info(msg) self.dingding(self, content=msg) else: logging.info("[获取注册服务数据成功] ServiceTotal {1} on {0} - SercieName: stage ".format(nacos_server,r['count'])) except Exception as e: pass def letsgo(self, nacos_server): login_nacos_res = (self.get_access_token(nacos_server)) if login_nacos_res["result"]: logging.info("[登录获取accessToken 成功] Nacos Server {0} ".format((login_nacos_res['server']))) self.get_service_list(login_nacos_res['accessToken'], login_nacos_res['server'] ) self.register_test(login_nacos_res['accessToken'], login_nacos_res['server'] ) progress_bar(timmer=0.1) self.delete_instance_service(login_nacos_res['accessToken'], login_nacos_res['server'] ) else: msg = "[登录获取accessToken 失败] {0} ".format((login_nacos_res['msg'])) logging.info(msg) self.dingding(self, content=msg) if __name__ == "__main__": while True: nacos_server_list=[ "192.168.18.75:80", "192.168.18.75:8848", "192.168.18.73:8858", "192.168.18.74:8858", "192.168.18.75:8858", ] for i in nacos_server_list: Nacos().letsgo(i) EOF
|