一个用于 MRTG 自动告警的脚本 (ZT)

下面是 MRTG 中一个对输入对象的值进行判断并自动输出告警的脚本的例子

    -)1、首先 MRTG 在发现某个输入对象的值小于/大于某个预定的阀值时,就会触发指定的脚本

          这个脚本是由 ThreshProgI[<target>] 指定的,而阀值则是由 ThreshMaxI 和

          ThreshMinI 指定的。

    -)2、同理,输出对象也可以设定最大/小值/触发脚本,把上面的命令中的 I 改为 O 就是了

    -)3、MRTG 在调用脚本的同时会附带传递若干个变量给脚本,分别是告警的 target($1)、

         设定的阀值($2)、当前告警值 ($3)。

    -)4、为了区分告警的严重程度,我们可以通过查询 Target 是在那个配置文件里面的,因为之前

         我把所有的配置文件都按照类型进行了归类,并对每个配置文件中所包含的情况都分了一个

         告警级别,所以只要查出是那个文件就可以知道是属于什么告警级别的。

    -)5、为了便于存档,会每天保存1个日志

#!/bin/bash

cd /etc/mrtg/
alert_target=$1
threshold_value=$2
current_value=$3
host_ip=`grep "\[${alert_target}\]" /etc/mrtg/target_info |cut -d ':' -f 3` >/dev/null 2>&1
cfg_file=`grep "\[${alert_target}\]" /etc/mrtg/target_info|cut -d ':' -f 1` >/dev/null 2>&1
if [ -z "${host_ip}" ] || [ -z "${cfg_file}" ];then
 
    subject=" Unknown Target [ ${alert_target} ] Alert "

    mail -s "${subject}" n7css < /dev/null >/dev/null 2>&1
    
    exit 1;

fi

echo "告警对象 : ${alert_target}"
echo "配置文件 :${cfg_file}"
LegendI=$(grep ^LegendI ${cfg_file}|cut -d ':' -f 2)
LegendI=$(echo ${LegendI% \&nbsp\;*})
echo "监测项目1 : $LegendI"
min_input=$(grep ^ThreshMinI ${cfg_file} |cut -d ':' -f 2|tr -d '[:blank:]')
max_input=$(grep ^ThreshMaxI ${cfg_file} |cut -d ':' -f 2|tr -d '[:blank:]')
case ${threshold_value} in
        ${min_input}) alert_type="< ${LegendI} > 最小值告警" ;;
        ${max_input}) alert_type="< ${LegendI} > 最大值告警" ;;
esac
echo "告警类型 :$alert_type"
ShortLegend=$(grep ^ShortLegend ${cfg_file} |cut -d ':' -f 2) && ShortLegend=$(echo ${ShortLegend% *})
if [ -z "$ShortLegend" ];then
    ShortLegend="无"
fi
echo "单位 :${ShortLegend}"
current_time=`date +"%Y-%m-%d %H:%M:%S"`
alert_id=$(date +%s)
if grep "${cfg_file}" /etc/mrtg/important_status.cfg >/dev/null 2>&1 ; then
    alert_level='<b> !!! 严重 !!! </b>'
    alert_color='red'
elif grep "${cfg_file}" /etc/mrtg/io_perfermance.cfg >/dev/null 2>&1; then
    alert_level='<b> !!! 紧急 !!! </b>'
    alert_color='#FF00FF'
elif grep "${cfg_file}" /etc/mrtg/normal_status.cfg >/dev/null 2>&1;then
    alert_level='<b> !!! 注意 !!! </b>'
    alert_color='brown'
elif grep "${cfg_file}" /etc/mrtg/usage_detect.cfg > /dev/null 2>&1; then
    alert_level='<b> !!! 注意 !!!</b>'
    alert_color='blue'
else
    alert_level='<b> !!! 注意 !!! </b>'
    alert_color='green'
fi

echo "告警等级:${alert_level}"
alert_level_string=$(echo ${alert_level
#'<b> !!! '})
alert_level_string=$(echo ${alert_level_string%' !!! </b>'})
subject="告警对象 [ $alert_target ] 告警时间 [ ${current_time} ] 告警类型 [ ${alert_type} ]"
subject_detail="级别: ${alert_level_string} ; 对象: <${alert_target}> ; 时间: <${current_time}> ; 类型 [${alert_type}] ; 阀值: <${threshold_value}> ; 当前值: <${current_value}> ID: <${alert_id}>"

echo "${subject_detail}" >> /var/www/html/mrtg/total_alert

##########################################################################################################

# 现在开始生成 html 格式的告警历史

##########################################################################################################

echo '<pre>' > /var/www/html/mrtg/alert_subject.new
echo '<font size=6>告警时间 &nbsp;:'${current_time}'</font><p>' >> /var/www/html/mrtg/alert_subject.new
echo '<font color='${alert_color}'>主 &nbsp; &nbsp;题 &nbsp;:&nbsp;'${subject}'</font><p>' >> /var/www/html/mrtg/alert_subject.new
echo '<font color='${alert_color}'>告警 ID &nbsp;&nbsp;:'${alert_id}'</font><p>' >> /var/www/html/mrtg/alert_subject.new
echo '<font color='${alert_color}'>告警级别&nbsp; :'${alert_level}'</font><p>' >> /var/www/html/mrtg/alert_subject.new
echo '<font color='${alert_color}'>告警主机 :'${host_ip}'</font><p>' >> /var/www/html/mrtg/alert_subject.new
echo '<font color='${alert_color}'>告警对象 :'${alert_target}'</font><p>' >> /var/www/html/mrtg/alert_subject.new
echo '<font color='${alert_color}'>告警类型 :'${alert_type}'</font><p>' >> /var/www/html/mrtg/alert_subject.new
echo '<font color='${alert_color}'>告警阀值 :'${threshold_value}'</font><p>' >> /var/www/html/mrtg/alert_subject.new
echo '<font color='${alert_color}'>单 位 :'$ShortLegend'</font><p>' >> /var/www/html/mrtg/alert_subject.new
echo '<font color='${alert_color}'>当前数值 :'${current_value}'</font><p>' >> /var/www/html/mrtg/alert_subject.new
echo '<hr>' >> /var/www/html/mrtg/alert_subject.new
echo '</pre>' >> /var/www/html/mrtg/alert_subject.new
cd /var/www/html/mrtg
if [ ! -e alert_subject ];then
        touch alert_subject
fi
last_alert_time=$(grep -m 1 '告警 ID' alert_subject)
last_alert_time=$(echo ${last_alert_time%'</font>'*})
last_alert_time=$(echo ${last_alert_time
##*:})
last_alert_date=$(date -d "1970-01-01 ${last_alert_time} sec utc" +%Y%m%d )
last_alert_day=$(date -d "1970-01-01 ${last_alert_time} sec utc" +%d)
current_day=$(date +%d)
echo "最后告警日期 : ${last_alert_day}"
echo "当前日期 :${current_day}"
if [ ${last_alert_day} -ne ${current_day} ];then
    echo "现在需要清空 alert_subject"
    cp alert_history.html alert_hist/alert_history_${last_alert_date}.html >/dev/null 2>&1
    > alert_subject
else
    echo "现在还没有过0点,不需要清空 alert_subject"
fi

rm -f /var/www/html/mrtg/alert_history.html
cat html.header alert_subject.new alert_subject html.footer > /var/www/html/mrtg/alert_history.html
content=$(tail -n +13 alert_history.html)
content=$(echo "${content%'</BODY>'*}")
echo "$content" > alert_subject
mail -s "${subject}" n7css << EOF

*******************************************************

告警时间 : < ${current_time} >

告警主机 :< ${host_ip} >

告警对象 :< ${alert_target} >

告警类型 : ${alert_type}

告警阀值 :< ${threshold_value} >

单 位 :< $ShortLegend >

当前数值 :< ${current_value} >

*******************************************************


以上摘自:http://blog.chinaunix.net/u/12066/showart.php?id=488258

原文地址:https://www.cnblogs.com/d9394/p/10611781.html