监控redis多实例的负载情况

单个服务器上创建多实例,对其重要参数的监控是非常重要的,以下是监控服务器上多实例的负载情况:主要包含:redis实例的QPS、内存使用情况、客户端连接数,服务

   单个服务器上创建多实例,对其重要参数的监控是非常重要的,以下是监控服务器上多实例的负载情况:主要包含:redis实例的qps、内存使用情况、客户端连接数,服务器的内存使用率、cpu使用率、cpu load值、网卡流量等,脚本把采集到的数据显示并且写入到数据库中,方便查看多个服务器的多实例情况以及数据的汇总等,写的优点仓促,有兴趣的同学可以整理一下脚本使其模块化、简洁化。脚本如下:

#!/usr/bin/env python#-*-coding:UTF-8-*-import os,threading,time,sys,sigar,MySQLdb”’安装python的sigar模块apt-get install libtool automake gettext python-MySQLdb screen pkg-config gitgit clone git://github.com/hyperic/sigar.git sigar.git./autogen.sh./configuremakemake installcd bindings/python/python setup.py install”””建库sqlCREATE TABLE `redis_stats` ( `id` int(11) NOT NULL AUTO_INCREMENT, `host_name` varchar(50) NOT NULL, `qps` int(11) NOT NULL, `clients` int(11) NOT NULL, `redis_mem_used` varchar(50) NOT NULL, `sys_mem_used_pers` float NOT NULL, `cpu_used` float NOT NULL, `cpu_load` varchar(50) NOT NULL, `netband` varchar(255) NOT NULL, `uptime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`), KEY `host_name` (`host_name`), KEY `uptime` (`uptime`)) ENGINE=InnoDB DEFAULT CHARSET=utf8查询每个实例的最新记录select host_name,qps,clients,redis_mem_used,concat(sys_mem_used_pers,’%’) as sys_mem_used,concat(cpu_used,’%’) as cpu_used,cpu_load,netband from DBA.redis_stats group by host_name desc order by qps desc;”’def log2db(check_log):log_host = ‘192.168.56.101’log_user = ‘root’log_pass = ‘1q2w3e4r’try:conn = MySQLdb.connect(host = log_host,port = 3306,user = log_user,passwd = log_pass,charset=’utf8’,connect_timeout=20)cursor = conn.cursor()#cursor.execute(insert_sql)cursor.executemany(“INSERT INTO redis.redis_stats (`host_name`,`qps`,`clients`,`redis_mem_used`,`sys_mem_used_pers`,`cpu_used`,`cpu_load`,`netband`) VALUES (%s,%s,%s,%s,%s,%s,%s,%s)”,tuple(check_log))conn.commit()cursor.close()conn.close()except Exception,e:print edef redis_info(host,port,res):var = []var.append(host)var.append(port)aaa = os.popen(“redis-cli -h %s -p %s info|grep -v ‘#’|tr -s ‘rn'” % (host,port)).readlines()dirc = {}for i in aaa:if i != ‘rn’:a = i.strip()aa = a.split(“:”)dirc[aa[0]]=aa[1]var.append(dirc[“connected_clients”])var.append(dirc[“instantaneous_ops_per_sec”])var.append(dirc[“used_memory_human”])res.append(var)def main():netband = {}stime = 5while True:try:sg = sigar.open()mem = sg.mem()#内存mem_percent = “%.2f” % mem.used_percent()cpu = sg.cpu()#CPU总的使用率cpu_idle = “%.2f” % ((1-float(cpu.idle())/cpu.total())*100)loadavg = sg.loadavg()#CPU load值cpu_loadavg = ‘,’.join([str(i) for i in loadavg])#nets = [i for i in sg.net_interface_list() if ‘dum’ not in i and i != ‘lo’]#网卡流量统计nets = [i.strip() for i in os.popen(“/bin/ip a|grep global|awk ‘{print $7}'”).readlines() if i.strip() != ”]if len(netband) != 0:for net in nets:netband[net+’_Out’] = “%.2f” % (float((sg.net_interface_stat(net).tx_bytes()) – int(netband[net+’_Out’]))/stime/1024/1024)netband[net+’_In’] = “%.2f” % (float((sg.net_interface_stat(net).rx_bytes()) – int(netband[net+’_In’]))/stime/1024/1024)else:for net in nets:netband[net+’_Out’] = “%.2f” % (float(sg.net_interface_stat(net).tx_bytes())/stime/1024/1024)netband[net+’_In’] = “%.2f” % (float(sg.net_interface_stat(net).rx_bytes())/stime/1024/1024)redis_list = [‘192.168.56.101:6379′,’192.168.1.87:16379’]text = “*”*20 + ” Redis Status %s ” % time.strftime(“%Y-%m-%d %H:%M:%S”) + “*”*20print “33[1;31;40m%s33[0m” % textthreads = []res = []for i in redis_list:aa = i.split(‘:’)host = aa[0]port = aa[1]t = threading.Thread(target=redis_info,args=(host,port,res))threads.append(t)for i in range(len(threads)):threads[i].start()for i in range(len(threads)):threads[i].join()print “33[1;35;40m%s33[0m” % (“Redis_host:port”.ljust(23)+”Redis:QPS”.ljust(10)+”Redis:Clients”.ljust(15)+”Redis:Mem_used”.ljust(15)+”Mem_percent”.ljust(12)+”Cpu_used”.ljust(10)+”Cpu_loadavg”.ljust(17))All_qps = 0All_clients = 0res.sort()check_log = []for i in res:log = [i[0]+’:’+i[1],int(i[3]),int(i[2]),i[4],float(mem_percent),float(cpu_idle),cpu_loadavg,str(netband)]check_log.append(log)print (i[0]+’:’+i[1]).ljust(23)+i[3].ljust(10)+i[2].ljust(15)+i[4].ljust(15)+mem_percent.ljust(12)+cpu_idle.ljust(10)+cpu_loadavg.ljust(17)+str(netband)All_qps = All_qps + int(i[3])All_clients = All_clients + int(i[2])log2db(check_log)print “33[1;35;40m%s33[0m” % (“Summary All host:”.ljust(23)+str(All_qps).ljust(10)+str(All_clients).ljust(10))netband = {}for net in nets:netband[net+’_Out’] = sg.net_interface_stat(net).tx_bytes()netband[net+’_In’] = sg.net_interface_stat(net).rx_bytes()time.sleep(stime)except KeyboardInterrupt :sys.exit(0)printbreakif __name__ == “__main__”:main()

有图有真相:

wKiom1LXUSeTq5ufAAXw-D3QjCY771.jpg

ViiTor实时翻译 ViiTor实时翻译

AI实时多语言翻译专家!强大的语音识别、AR翻译功能。

ViiTor实时翻译 116 查看详情 ViiTor实时翻译

本文出自 “王伟” 博客,,请务必保留此出处

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/538855.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年11月9日 14:16:27
下一篇 2025年11月9日 14:21:23

相关推荐

发表回复

登录后才能评论
关注微信