对于运维来说,监控是一个重要的工作,如果做好了监控可以解决以下问题:

1、做了硬件监控,如果服务器出现硬件问题可以提前知晓,提前安排好解决方案,避免突然出现问题造成损失;

2、做了系统与服务的监控,如果系统资源与服务出现问题,可以及时知晓并解决,同时可以根据周期内监控数据,做好调优;

如果仅完成以上事情的话,只是对运维本身工作有所帮忙,如何对其他部门做支持,以及让公司领导看出运维团队的重要性,就需要多下一份功夫,毕竟如果出现问题,就是运维工作不到位,如果不出问题,是运维应该做的。

为了提供运维团队对其他部门的支持,以及为运维争取话语权,我除了对以上2个工作更好、快速的完成外,还对于监控数据充分利用起来,通过监控数据实现报表功能,实现以下工作:

1、运维无法直接创造利益,就只能开源节流,节省服务器数量,合并压力小的业务,以便节省服务器数量与成本;

2、通过监控数据,计算出各项目使用的资源量与成本,方便各项目负责人的知晓业务使用情况与成本;

3、方便财务统计,并知晓各项目使用机房流量带宽百分百。

所以我使用shell+mysql,写了个统计报表功能,能定时的统计每个月(我默认是每月,可以自定义时间)以下信息的信息(我统计是平均值,是概数,供参考):

1、主机资源使用

功能:包括查询时间、主机所属组、主机ip、cpu逻辑核数、cpu平均空闲值、cpu平均最小值、可用平均内存、可用最小内存、总内存、cpu最小wio、cpu最大wio、进入最大流量、出去最大流量、进入平均流量、出去平均流量、进入最小流量、出去最小流量;

作用:使用这个表可以帮忙我们对浪费服务器资源的项目适当减少服务器数量,以便节省资源与成本。

wKioL1Ny5BijI3mNAAI5J81_F2c750.jpg

2、各项目网络流量

功能:包括查询时间、主机所属组、进入最大流量、出去最大流量、进入平均流量、出去平均流量、进入最小流量、出去最小流量;

作用:方便各项目查看自己使用网络流量与计算成本。

如下图

wKiom1Ny49vTD2TKAAE1q9fxaeQ967.jpg

3、机房网络流量

功能:包括查询时间、机房、进入最大流量、出去最大流量、进入平均流量、出去平均流量、进入最小流量、出去最小流量;

作用:方便运维了解机房网络使用量与每月机房带宽成本计算(一般机房计算机房带宽成本都使用cacti)。

如下图

wKioL1Ny5K2iObngAAEIkoQuhRg096.jpg

4、各项目占机房总流量百分比

功能:包括查询时间、所属组、进入最大流量、出去最大流量、进入平均流量、出去平均流量、进入最小流量、出去最小流量;

作用:能及时满足财务的智能带宽分配需求。

如下图

wKioL1Ny5hHifFWsAAEqMNs_45Q367.jpg

下面是介绍如何实现:

1、脚本运行时间

wKiom1Ny5rSw-10CAACYa6f5_8A246.jpg

可以看到51秒后就能完成。

完成后会在/tmp/zabbix_log目录里有4个文件生成

1
2
3
4
5
6
7
8
[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

2、txt转为excel

请参看“http://jingyan.baidu.com/article/359911f5108f3757fe0306fb.html”,我就不介绍了,很简单。

3、脚本内容

由于脚本内容过多,我就简单介绍前几行

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/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).

4、搜索cpu资源sql

1
2
3
4
5
6
7
#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、搜索cpu等待sql

1
2
3
4
5
6
7
#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

6、搜索5分钟最大负载sql

1
2
3
4
5
6
7
#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

7、搜索平均内存sql

1
2
3
4
5
6
7
#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

8、搜索总共内存值sql

1
2
3
4
5
6
7
#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

9、搜索平均em2网卡进入与出去流量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#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 5、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,具体如何写就看各位了。

我的脚本在附件。