zabbix公司專屬應用程式之從資料庫提取centos 6.2系統線上天數

來源:互聯網
上載者:User

標籤:zabbix查詢6.2系統線上時間   快速查看6.2系統線上時間   zabbix 報表功能   zabbix查詢   

曆史原因造成公司有很多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/profilelogdir=‘/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 $logdirfi##zabbix get centos 6.2 uptime#select os system versionmysql -h $mysql_host  -u $mysql_user -p$mysql_passwd $mysql_database >$logdir/mysql_zabbix_os_system.txt<<EOFset 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 uptimemysql -h $mysql_host  -u $mysql_user -p$mysql_passwd $mysql_database >$logdir/mysql_zabbix_os_uptime.txt<<EOFset 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;EOFsed -i ‘/Date*/d‘ $logdir/mysql_zabbix_os_system.txtsed -i ‘/Date*/d‘ $logdir/mysql_zabbix_os_uptime.txtawk ‘{print $1"\t"$3"\t"$4"\t6.2"}‘ $logdir/mysql_zabbix_os_system.txt > $logdir/mysql_zabbix_os_system_version.txtawk ‘{print $1"\t"$3"\t"$4"\t"$5}‘ $logdir/mysql_zabbix_os_uptime.txt > $logdir/mysql_zabbix_os_uptime_new.txtawk ‘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.txtsed -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、結果內容(部分展示,主機群組資訊屏蔽了)

[[email protected] zabbix_log]# cat zabbix_os_uptime_result.txt 查詢日期所屬組主機ip系統版本線上天數2014-07-23xxx機房--自研項目--xx10.10.10.466.2 2182014-07-23xxx機房--自研項目--xxx10.10.10.476.2 2182014-07-23xxx機房--自研項目--xxx192.168.10.1536.2 2022014-07-23xxx機房--自研項目--xxx192.168.10.1546.2 202

4、txt轉成excel格式

請參看“http://jingyan.baidu.com/article/359911f5108f3757fe0306fb.html”,我就不介紹了,很簡單。

如果大家想使用我的指令碼,可以從附件裡下載,認為本文對你有益,請給個好評與贊,謝謝。

本文出自 “吟—技術交流” 部落格,請務必保留此出處http://dl528888.blog.51cto.com/2382721/1529157

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.