cat raid_fzt.sh
#!/bin/bash
#data:2018.3.9
#check raid status and mail to someone
DIR="/home/s/ops/pantheon/argos/raid.check" # 脚本所在目录
cd $DIR
LOG='raid_check.2018-03-09_11:17:31'
#LOG=$(ls -t raid_check.`date +"%F"`.* | xargs echo | cut -d ' ' -f 1 )
BAD_RAID=$(python /raid.check/marshalv3.py $LOG ) #有问题Raid的列表
html_input(){
echo "<tr>
<td>$1</td>
<td>$2</td>
<td>$3</td>
<td>$4</td>
</tr>" >>$DIR/mail.html
} #构造一横行表格信息
set_info(){
echo "<head>
<meta http-equiv="Content-Type" content="text/html\;charset=UTF-8">
<p><font color="red">如下服务器硬件报警,请业务尽快报修,报修时请在报修工单中写明故障信息。</font></p>
</head>
<table border=1>
<tr>
<th>主机信息</th>
<th>Hermes信息</th>
<th>接口负责人</th>
<th>业务负责人</th>
</tr>" > $DIR/mail.html
host_list=$(grep $1 raid_info | awk '{print $1}') #主机信息列表
for html_host in $host_list
do
html_owner=$(grep $html_host raid_info | awk '{print $4}') #对应owner信息
html_hermes=$(grep $html_host raid_info | awk '{print $2}') #对应hermes信息
echo $html_host $html_hermes $1 $html_owner
html_input $html_host $html_hermes $1 $html_owner #构造每行表格信息
done
echo "</table>
<p><font>如有问题请联系: </font> <a href="mailto:[email protected]"> [email protected]</a> </p>
<p><font face="arial" color="red">此邮件为系统发送,请勿回复!!! </font></p>" >> mail.html
#cat mail.html >> all.html # 报表汇总
mutt [email protected] -s "Raid check" -e "set content_type=text/html" < $DIR/mail.html #发送邮件
#mutt $1 -s "Raid 检测报警" -e "set content_type=text/html" < $DIR/mail.html
} # 制作mail.html,并使用邮件发送
for host in $BAD_RAID
do
HERMES=$(Pwhoismycluster $host | grep "hermes:" -A 1 | sed -n '2p' | cut -d : -f 1 | sed 's/[[:space:]]//g') #
OWNER=$(curl -s "http://10.95.22.14:9093/element/findServerUserBy" -H "Content-Type: application/json" -d '{"HostnameIn":"'$host'"}' | grep -Po '"owner":".*?"' | awk -F ":" '{print $NF}' |sed 's/"//g')
OPS=$(curl -s "http://10.95.22.14:9093/element/findServerUserBy" -H "Content-Type: application/json" -d '{"HostnameIn":"'$host'"}' | grep -Po '"ops":".*?"' | awk -F ":" '{print $NF}' |sed 's/"//g')
OPS=${OPS:[email protected]}
HERMES=${HERMES:=没查到HERMES}
OWNER=${OWNER:=没查到OWNER}
# echo $host $HERMES $OPS $OWNER
echo $host $HERMES $OPS $OWNER >> $DIR/raid_info
done # 找到 host hermes ops owner 信息并写入 raid_info 这个文件中,供后续处理
OPS_LIST=$(cat $DIR/raid_info | awk '{print $3}' | sort | uniq ) # 相同邮件人去重
for html_ops in $OPS_LIST
do
set_info $html_ops # set_info 来发送邮件
done
rm $DIR/raid_info # 删除 raid_info 这个文件
原文地址:http://blog.51cto.com/19941018/2085958