近期有個工作上的需求,需要監控Linux主機上的某個服務是否有異常,並且需要在異常時發出告警通知,所以寫了shell script來監控,下面的範例是監控Smokeping的Process是否還有在正常運作,並在異常時發出Slack的通知訊息
範例如下,存檔後放到排程每五分鐘監控一次即可
#!/bin/bash
smokeping_status=`ps aux | grep FPing | grep -v grep | wc -l `
if [ ${smokeping_status} = 0 ]; then
echo "Smokeping-Services 服務異常,請檢查"
data='{
"username": "'${HOSTNAME}'",
"icon_url": "https://www.iconninja.com/files/916/202/326/ping-icon.png",
"attachments": [
{
"title": "Smokeping服務狀態 : 狀態異常",
"color": "#ff0000",
"text": " 請檢查'${HOSTNAME}' Smokeping Process狀態"
}
],
"channel": "#alert-monitor"
}'
echo $data
curl -X POST -H "Content-type: application/json" --data "$data" https://hooks.slack.com/services/A8CKZ8G2C/A6FJBJKTYM/GaFDyZpDc79U3LABX79a3e
exit 2
else
echo "Smokeping-Services 服務正常運作中"
exit 0
fi
(繼續閱讀...)