单个服务器上创建多实例,对其重要参数的监控是非常重要的,以下是监控服务器上多实例的负载情况:主要包含: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 '\r\n' % (host,port)).readlines()dirc = {}for i in aaa:if i != '\r\n':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 \033[1;31;40m%s\033[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 \033[1;35;40m%s\033[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 \033[1;35;40m%s\033[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()有图有真相:
本文出自 “王伟” 博客,,请务必保留此出处
