历史原因造成公司有很多centos 6.2的系统,从某天6.2系统突然自动重启,排查半天发现是6.2的内核bug(超过208天后可能自动重启,可以参考https://access.redhat.com/site/solutions/68466),所以为了不影响业务,在发现6.2系统在线超过180天后就安排重启工作,下面是介绍如何使用zabbix来从数据库里获取6.2系统服务器的在线天数,然后通知业务重启,避免意外重启导致业务受到影响。
1、脚本内容
#!/bin/bash #script name check_6.2_uptime.sh . /etc/profile logdir=‘/tmp/zabbix_log‘ mysql_host=‘localhost‘ mysql_user=‘zabbix‘ mysql_passwd=‘zabbix‘ mysql_database=‘zabbix‘ year=`date +%Y` month=`date +%m-%d` next_month=`date +%m-%d -d "1 days"` if [ ! -d $logdir ];then mkdir $logdir fi ##zabbix get centos 6.2 uptime #select os system version mysql -h $mysql_host -u $mysql_user -p$mysql_passwd $mysql_database >$logdir/mysql_zabbix_os_system.txt<<EOF set names utf8; select from_unixtime(hi.clock) as Date,g.name as Group_Name,h.host as Host, hi.value as System_version 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 history_str hi on i.itemid = hi.itemid where i.key_=‘system.sw.os[name]‘ and hi.value like ‘%6.2%‘ and g.name <> ‘所有Mysql主机组‘ and hi.clock >= UNIX_TIMESTAMP(‘${year}-${month} 00:00:00‘) and hi.clock <= UNIX_TIMESTAMP(‘${year}-${next_month} 00:00:00‘) group by h.host; EOF #select uptime mysql -h $mysql_host -u $mysql_user -p$mysql_passwd $mysql_database >$logdir/mysql_zabbix_os_uptime.txt<<EOF set names utf8; select from_unixtime(hi.clock) as Date,g.name as Group_Name,h.host as Host, round(max(hi.value_max)/86400,0) as System_Uptime 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_=‘system.uptime‘ and hi.clock >= UNIX_TIMESTAMP(‘${year}-${month} 00:00:00‘) and hi.clock <= UNIX_TIMESTAMP(‘${year}-${next_month} 00:00:00‘) group by h.host; EOF sed -i ‘/Date*/d‘ $logdir/mysql_zabbix_os_system.txt sed -i ‘/Date*/d‘ $logdir/mysql_zabbix_os_uptime.txt awk ‘{print $1"\t"$3"\t"$4"\t6.2"}‘ $logdir/mysql_zabbix_os_system.txt > $logdir/mysql_zabbix_os_system_version.txt awk ‘{print $1"\t"$3"\t"$4"\t"$5}‘ $logdir/mysql_zabbix_os_uptime.txt > $logdir/mysql_zabbix_os_uptime_new.txt awk ‘NR==FNR{a[$1,$2,$3]=$4;next}{print $0,a[$1,$2,$3]}‘ $logdir/mysql_zabbix_os_uptime_new.txt $logdir/mysql_zabbix_os_system_version.txt|awk ‘{if($5 >150) {print $0}}‘|sort -k2nr > $logdir/zabbix_os_uptime_result.txt sed -i ‘1i 查询日期\t所属组\t主机ip\t系统版本\t在线天数‘ $logdir/zabbix_os_uptime_result.txt
默认结果在/tmp/zabbix_log/zabbix_os_uptime_result.txt,请自行根据自己的环境来修改脚本的数据库信息。
2、使用
直接使用下面命令
/bin/bash check_6.2_uptime.sh
3、结果内容(部分展示,主机组信息屏蔽了)
[root@puppet zabbix_log]# cat zabbix_os_uptime_result.txt 查询日期 所属组 主机ip 系统版本 在线天数 2014-07-23 xxx机房--自研项目--xx 10.10.10.46 6.2 218 2014-07-23 xxx机房--自研项目--xxx 10.10.10.47 6.2 218 2014-07-23 xxx机房--自研项目--xxx 192.168.10.153 6.2 202 2014-07-23 xxx机房--自研项目--xxx 192.168.10.154 6.2 202
4、txt转成excel格式
请参看“http://jingyan.baidu.com/article/359911f5108f3757fe0306fb.html”,我就不介绍了,很简单。
如果大家想使用我的脚本,可以从附件里下载,认为本文对你有益,请给个好评与赞,谢谢。
本文出自 “吟—技术交流” 博客,请务必保留此出处http://dl528888.blog.51cto.com/2382721/1529157