python监控es状态 发表于 2019-04-28 | 分类于 python | 浏览 次 需求用python写一个监控es状态的脚本 实例监控es的机器1234567891011配置文件[root@Ops-script monitor_elasticsearch]# cat escluster_ip.ini[mail]name = sy@xxx.com[tax_es]cluster_ip = 192.168.50.7:9200,192.168.50.8:9200,192.168.50.9:9200name_pass = none,none[analysis_es]cluster_ip = 192.168.50.24:9200,192.168.50.25:9200name_pass = none,none 脚本12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455[root@Ops-script monitor_elasticsearch]# cat monitor_elastic_cluster.pyimport urllib,socket,time,jsonimport logging,requests,configparser,os# 定义日志格式读取配置文件cur_path = os.path.dirname(os.path.realpath(__file__))logging.basicConfig(format='[ %(asctime)s ] -- %(message)s', datefmt='%a, %d %b %Y %H:%M:%S', filename=cur_path+"/scripts.log", level=logging.INFO)config_path = os.path.join(cur_path,"escluster_ip.ini")conf = configparser.ConfigParser()conf.read(config_path)esgroup = conf.sections()def GetClusterIp(): # 按集群名循环检查,集群名为配置文件中的标题 for group in esgroup: # 获取到 mail 中的邮件地址不再继续循环 if group == "mail": mailname = conf.get(group,"name") continue # 获取到一个集群 ip 后进行 get 请求 iplist = conf.get(group,"cluster_ip").split(",") for esip in iplist: url = "http://%s/_cat/health"%esip try: # 超时时间为 3 秒, request 的请求值不是200的将全部置为400 # 如果是请求成功,则获取 status 的状态和 status_num 的百分比 response = requests.get(url,timeout=3) request = response.status_code status = response.text.split()[3] status_num = response.text.split()[-1] except: request = 400 # 请求值为 200 后,检查 status 值非 green 状态发送报警邮件,并不在继续检查本集群内的剩余节点 if request == 200: if status != "green": mailtitle = "elasticsearch集群 [ %s ], ip为:%s ,查状态: %s, 状态百分比: %s"%(group,esip,status,status_num) mailtxt = "elasticsearch集群 [ %s ]\n\nip为: [ %s ]\n\n检查状态为: %s\n\n状态百分比: %s"%(group,esip,status,status_num) command = "echo -e %s%s%s | mail -s %s%s%s %s"%('"',mailtxt,'"','"',mailtitle,'"',mailname) logging.info(mailtitle) os.system(command) break # 请求值不是 200 ,报警后继续检查集群内剩余 ip else: mailtitle = "elasticsearch集群 [ %s ], ip为:%s, 请求超时,请检查端口"%(group,esip) mailtxt = "elasticsearch集群 [ %s ]\n\nip: [ %s ]\n\n请求超时,请检查端口"%(group,esip) command = "echo -e %s%s%s | mail -s %s%s%s %s"%('"',mailtxt,'"','"',mailtitle,'"',mailname) logging.info(mailtitle) os.system(command)if __name__ == "__main__": GetClusterIp() Donate 打赏 微信支付 支付宝 本文作者: Shu Yu 本文链接: https://shenshengkun.github.io/posts/71abe607.html 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处!