linux 定制系统进程监控脚本
- 该脚本主要用于linux系统时刻监控自己定制的进程运行情况,一旦进程检测不到,会第一时间以邮件报告的形式发送到相关人员。脚本代码如下:
#!/bin/bash
export mailto0="abc@163.com" 建立相关人员邮箱地址
mailfile="/users/output/ccmail/bak/program-check.`date +%m%d%y`.txt" 生成发送邮件的报告
touch $mailfile
chmod 666 $mailfile
echo "Hostname:`hostname`" > $mailfile
echo "Program ID:programcheck" >> $mailfile
check_list="/usr/local/etc/programcheck.lst" 按格式定制需要监控的进程列表
i=1
line=`cat $check_list |wc -l`
for((i=2;i<=$line;i++));
do
server_name=`awk 'NR=='$i' {print $1}' $check_list`
key1=`awk 'NR=='$i' {print $2}' $check_list`
key2=`awk 'NR=='$i' {print $3}' $check_list`
key3=`awk 'NR=='$i' {print $4}' $check_list`
key4=`awk 'NR=='$i' {print $5}' $check_list`
key5=`awk 'NR=='$i' {print $6}' $check_list`
ps aux | grep -e "$key1" -i | grep -e "$key2" -i | grep -e "$key3" -i | grep -e "$key4" -i |grep -e "$key5" -i > /dev/null
if [[ $? = 1 ]]
then echo "!!!$server_name : Process not found!" >> $mailfile
else
echo "$server_name : Process running normally!" >> $mailfile
fi
done
echo "Finish Check: `date`" >> $mailfile
if [[ $mailto0 = "" ]]
then mailto0="abc@163.com"
fi
echo "Mail to:$mailto0" >> $mailfile
echo "**********************************************************" >> $mailfile
echo "`cat $mailfile`">>/tmp/programcheck.log 生成监控日志
for a in `cat $mailfile |grep "Process not found"|wc -l`;do
if [ ${a} -gt 0 ];then
/usr/local/bin/mail.pl -f "`hostname`" -s "`hostname`: Server process check report" $mailto0< $mailfile 1>>$mailfile.error 2>>$mailfile.error 使用预先建立的邮件系统发送邮件
fi
done
/usr/local/etc/programcheck.lst的配置格式如下:
[root@vmgpe ~]cat /usr/local/etc/programcheck.lst
Server Name Key1 Key2 Key3 Key4 Key5
java java jmxri.jar mgmt_agent.jar mgmt_client.jar tcp
根据需求在里面Server Name 到Key5对应的列添加进程关键,条件是and,即如有其中一个关键字没有发现,就会发邮件报警。
该脚本基于Redhat 5编写,仅供分享参考。