對於運維來講,監控是一個重要的工做,若是作好了監控能夠解決如下問題: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,具體如何寫就看各位了。
個人腳本在附件。