对于运维来讲,监控是一个重要的工做,若是作好了监控能够解决如下问题:html
一、作了硬件监控,若是服务器出现硬件问题能够提早知晓,提早安排好解决方案,避免忽然出现问题形成损失;mysql
二、作了系统与服务的监控,若是系统资源与服务出现问题,能够及时知晓并解决,同时能够根据周期内监控数据,作好调优;sql
若是仅完成以上事情的话,只是对运维自己工做有所帮忙,如何对其余部门作支持,以及让公司领导看出运维团队的重要性,就须要多下一份功夫,毕竟若是出现问题,就是运维工做不到位,若是不出问题,是运维应该作的。shell
为了提供运维团队对其余部门的支持,以及为运维争取话语权,我除了对以上2个工做更好、快速的完成外,还对于监控数据充分利用起来,经过监控数据实现报表功能,实现如下工做:ubuntu
一、运维没法直接创造利益,就只能开源节流,节省服务器数量,合并压力小的业务,以便节省服务器数量与成本;windows
二、经过监控数据,计算出各项目使用的资源量与成本,方便各项目负责人的知晓业务使用状况与成本;centos
三、方便财务统计,并知晓各项目使用机房流量带宽百分百。bash
因此我使用shell+mysql,写了个统计报表功能,能定时的统计每月(我默认是每个月,能够自定义时间)如下信息的信息(我统计是平均值,是概数,供参考):服务器
一、主机资源使用网络
功能:包括查询时间、主机所属组、主机ip、cpu逻辑核数、cpu平均空闲值、cpu平均最小值、可用平均内存、可用最小内存、总内存、cpu最小wio、cpu最大wio、进入最大流量、出去最大流量、进入平均流量、出去平均流量、进入最小流量、出去最小流量;
做用:使用这个表能够帮忙咱们对浪费服务器资源的项目适当减小服务器数量,以便节省资源与成本。
二、各项目网络流量
功能:包括查询时间、主机所属组、进入最大流量、出去最大流量、进入平均流量、出去平均流量、进入最小流量、出去最小流量;
做用:方便各项目查看本身使用网络流量与计算成本。
以下图
三、机房网络流量
功能:包括查询时间、机房、进入最大流量、出去最大流量、进入平均流量、出去平均流量、进入最小流量、出去最小流量;
做用:方便运维了解机房网络使用量与每个月机房带宽成本计算(通常机房计算机房带宽成本都使用cacti)。
以下图
四、各项目占机房总流量百分比
功能:包括查询时间、所属组、进入最大流量、出去最大流量、进入平均流量、出去平均流量、进入最小流量、出去最小流量;
做用:能及时知足财务的智能带宽分配需求。
以下图
下面是介绍如何实现:
一、脚本运行时间
能够看到51秒后就能完成。
完成后会在/tmp/zabbix_log目录里有4个文件生成
[root@ip-10-10-13-8 zabbix_log]# pwd /tmp/zabbix_log [root@ip-10-10-13-8 zabbix_log]# ll 总用量 100 -rw-r--r-- 1 root root 5484 5月 14 11:19 zabbix_group_network_traffic.txt -rw-r--r-- 1 root root 78282 5月 14 11:19 zabbix_host_search.txt -rw-r--r-- 1 root root 5477 5月 14 11:19 zabbix_network_percent.txt -rw-r--r-- 1 root root 296 5月 14 11:19 zabbix_room_network.txt
下面分别介绍一下这4个文件
zabbix_group_network_traffic.txt对应“各项目网络流量”
zabbix_host_search.txt对应“主机资源使用”
zabbix_network_percent.txt对应“各项目占机房总流量百分比”
zabbix_room_network.txt对应“机房网络流量”
因为运行脚本后会生成txt文件,非技术人员仍是喜欢看excel,因此下一步介绍如何把txt转为excel
二、txt转为excel
请参看“http://jingyan.baidu.com/article/359911f5108f3757fe0306fb.html”,我就不介绍了,很简单。
三、脚本内容
因为脚本内容过多,我就简单介绍前几行
#!/bin/bash . /etc/profile logdir='/tmp/zabbix_log' mysql_host='10.10.11.12' mysql_user='zabbix' mysql_passwd='zabbix' mysql_database='zabbix' year=`date +%Y` month=`date +%m` next_month=`echo $month+1|bc` if [ ! -d $logdir ];then mkdir $logdir fi
默认会新创建个/tmp/zabbix_log目录来存放txt文件,而后定义好了mysql信息,同时搜索的日期是从本月的1日0点到下月1日的0点(好比如今是5月,那么搜索日期是从2014-05-01 00:00:00到2014-06-01 00:00:00).
四、搜索cpu资源sql
#select cpu avg idle mysql -h $mysql_host -u $mysql_user -p$mysql_passwd $mysql_database >$logdir/info_mysql_cpu_avg_idle.txt<<EOF set names utf8; select from_unixtime(hi.clock,'%Y-%m') as Date,g.name as Group_Name,h.host as Host, round(avg(hi.value_avg),1) as Cpu_Avg_Idle from hosts_groups hg join groups g on g.groupid = hg.groupid jo in items i on hg.hostid = i.hostid join hosts h on h.hostid=i.hostid join trends hi on i.itemid = hi.itemid where i.key_='system.cpu.util[,idle]' and hi.clock >= UNIX_TIMESTAMP('${year}-$ {month}-01 00:00:00') and hi.clock < UNIX_TIMESTAMP('${year}-0${next_month}-01 00:00:00') group by h.host; EOF
五、搜索cpu等待sql
#select cpu avg idle mysql -h $mysql_host -u $mysql_user -p$mysql_passwd $mysql_database >$logdir/info_mysql_cpu_avg_idle.txt<<EOF set names utf8; select from_unixtime(hi.clock,'%Y-%m') as Date,g.name as Group_Name,h.host as Host, round(avg(hi.value_avg),1) as Cpu_Avg_Idle from hosts_groups hg join groups g on g.groupid = hg.groupid jo in items i on hg.hostid = i.hostid join hosts h on h.hostid=i.hostid join trends hi on i.itemid = hi.itemid where i.key_='system.cpu.util[,idle]' and hi.clock >= UNIX_TIMESTAMP('${year}-$ {month}-01 00:00:00') and hi.clock < UNIX_TIMESTAMP('${year}-0${next_month}-01 00:00:00') group by h.host; EOF
六、搜索5分钟最大负载sql
#select cpu max load 5 minute mysql -h $mysql_host -u $mysql_user -p$mysql_passwd $mysql_database >$logdir/info_mysql_cpu_max_load5.txt<<EOF set names utf8; select from_unixtime(hi.clock,'%Y-%m') as Date,g.name as Group_Name,h.host as Host, round(max(hi.value_max),0) as Cpu_Max_Iowait from hosts_groups hg join groups g on g.groupid = hg.groupid join items i on hg.hostid = i.hostid join hosts h on h.hostid=i.hostid join trends hi on i.itemid = hi.itemid where i.key_='system.cpu.load[all,avg5]' and hi.clock >= UNIX_TIMESTAMP('${ye ar}-${month}-01 00:00:00') and hi.clock < UNIX_TIMESTAMP('${year}-0${next_month}-01 00:00:00') group by h.host; EOF
七、搜索平均内存sql
#select memory avg avaiable mysql -h $mysql_host -u $mysql_user -p$mysql_passwd $mysql_database >$logdir/info_mysql_memory_avg_avaiable.txt<<EOF set names utf8; select from_unixtime(hi.clock,'%Y-%m') as Date,g.name as Group_Name,h.host as Host,round(avg(hi.value_avg)/1024/1024/1024,1) as Memory_Avaiable from hosts_groups hg join groups g on g.groupi d = hg.groupid join items i on hg.hostid = i.hostid join hosts h on h.hostid=i.hostid join trends_uint hi on i.itemid = hi.itemid where i.key_='vm.memory.size[available]' and hi.clock >= UNIX_TIMESTAMP('${year}-${month}-01 00:00:00') and hi.clock < UNIX_TIMESTAMP('${year}-0${next_month}-01 00:00:00') group by h.host; EOF
八、搜索总共内存值sql
#select memory total mysql -h $mysql_host -u $mysql_user -p$mysql_passwd $mysql_database >$logdir/info_mysql_memory_total.txt<<EOF set names utf8; select from_unixtime(hi.clock,'%Y-%m') as Date,g.name as Group_Name,h.host as Host,round(avg(hi.value_avg)/1024/1024/1024,0) as Memory_Total from hosts_groups hg join groups g on g.groupid = hg.groupid join items i on hg.hostid = i.hostid join hosts h on h.hostid=i.hostid join trends_uint hi on i.itemid = hi.itemid where i.key_='vm.memory.size[total]' and hi.clock >= UNIX_TI MESTAMP('${year}-${month}-01 00:00:00') and hi.clock < UNIX_TIMESTAMP('${year}-0${next_month}-01 00:00:00') group by h.host; EOF
九、搜索平均em2网卡进入与出去流量
#select network em2 avg in mysql -h $mysql_host -u $mysql_user -p$mysql_passwd $mysql_database >$logdir/info_mysql_network_em2_avg_in.txt<<EOF set names utf8; select from_unixtime(hi.clock,'%Y-%m') as Date,g.name as Group_Name,h.host as Host,round(avg(hi.value_avg)/1000,0) as Network_Em2_Avg_In from hosts_groups hg join groups g on g.groupid = hg .groupid join items i on hg.hostid = i.hostid join hosts h on h.hostid=i.hostid join trends_uint hi on i.itemid = hi.itemid where i.key_='net.if.in[em2]' and hi.clock >= UNIX_TIMESTAMP('$ {year}-${month}-01 00:00:00') and hi.clock < UNIX_TIMESTAMP('${year}-0${next_month}-01 00:00:00') group by h.host; EOF sed -i '/Date*/d' $logdir/info_mysql_network_em2_avg_in.txt #select network em2 avg out mysql -h $mysql_host -u $mysql_user -p$mysql_passwd $mysql_database >$logdir/info_mysql_network_em2_avg_out.txt<<EOF set names utf8; select from_unixtime(hi.clock,'%Y-%m') as Date,g.name as Group_Name,h.host as Host,round(avg(hi.value_avg)/1000,0) as Network_Em2_Avg_Out from hosts_groups hg join groups g on g.groupid = h g.groupid join items i on hg.hostid = i.hostid join hosts h on h.hostid=i.hostid join trends_uint hi on i.itemid = hi.itemid where i.key_='net.if.out[em2]' and hi.clock >= UNIX_TIMESTAMP( '${year}-${month}-01 00:00:00') and hi.clock < UNIX_TIMESTAMP('${year}-0${next_month}-01 00:00:00') group by h.host; EOF sed -i '/Date*/d' $logdir/info_mysql_network_em2_avg_in.txt paste $logdir/info_mysql_network_em2_avg_in.txt $logdir/info_mysql_network_em2_avg_out.txt |awk '{print $1"\t"$2"\t"$3"\t"$4"\t"$NF}' >$logdir/info_mysql_network_em2_avg.txt
因为我公司服务器系统比较繁杂,rhel或者centos 五、6,ubuntu 12.04与12.04.4,windows 2003/2008/2012,这样致使不少监控项没办法所有查看,因此数据不是很是的精确。
关于外网流量,因为网卡名也不同,有的网卡是em、有的是eth、有的是Broadcom NetXtreme Gigabit Ethernet #2等,而且我这里若是网卡名是em的话,em1是内网,em2是外网;网卡名是eth的话,eth0是内网,eth1是外网。
因此若是各位想使用我脚本的话,确定得本身根据本身需求来修改,我分享脚本主要是让你们看看各个监控项的sql,具体如何写就看各位了。
个人脚本在附件。