監(jiān)控硬件運(yùn)行狀況
shell 監(jiān)控cpu,memory,load average,記錄到log,當(dāng)負(fù)載壓力時(shí),發(fā)電郵通知管理員。
原理:
1.獲取cpu,memory,load average的數(shù)值
2.判斷數(shù)值是否超過自定義的范圍,例如(CPU>90%,Memory10%,load average>2)
3.如數(shù)值超過范圍,發(fā)送電郵通知管理員。發(fā)送有時(shí)間間隔,每小時(shí)只會(huì)發(fā)送一次。
4.將數(shù)值寫入log。
5.設(shè)置crontab 每30秒運(yùn)行一次。
ServerMonitor.sh
#!/bin/bash
# 系統(tǒng)監(jiān)控,記錄cpu、memory、load average,當(dāng)超過規(guī)定數(shù)值時(shí)發(fā)電郵通知管理員
# *** config start ***
# 當(dāng)前目錄路徑
ROOT=$(cd "$(dirname "$0")"; pwd)
# 當(dāng)前服務(wù)器名
HOST=$(hostname)
# log 文件路徑
CPU_LOG="${ROOT}/logs/cpu.log"
MEM_LOG="${ROOT}/logs/mem.log"
LOAD_LOG="${ROOT}/logs/load.log"
# 通知電郵列表
NOTICE_EMAIL='admin@admin.com'
# cpu,memory,load average 記錄上一次發(fā)送通知電郵時(shí)間
CPU_REMARK='/tmp/servermonitor_cpu.remark'
MEM_REMARK='/tmp/servermonitor_mem.remark'
LOAD_REMARK='/tmp/servermonitor_loadaverage.remark'
# 發(fā)通知電郵間隔時(shí)間
REMARK_EXPIRE=3600
NOW=$(date +%s)
# *** config end ***
# *** function start ***
# 獲取CPU占用
function GetCpu() {
cpufree=$(vmstat 1 5 |sed -n '3,$p' |awk '{x = x + $15} END {print x/5}' |awk -F. '{print $1}')
cpuused=$((100 - $cpufree))
echo $cpuused
local remark
remark=$(GetRemark ${CPU_REMARK})
# 檢查CPU占用是否超過90%
if [ "$remark" = "" ] [ "$cpuused" -gt 90 ]; then
echo "Subject: ${HOST} CPU uses more than 90% $(date +%Y-%m-%d' '%H:%M:%S)" | sendmail ${NOTICE_EMAIL}
echo "$(date +%s)" > "$CPU_REMARK"
fi
}
# 獲取內(nèi)存使用情況
function GetMem() {
mem=$(free -m | sed -n '3,3p')
used=$(echo $mem | awk -F ' ' '{print $3}')
free=$(echo $mem | awk -F ' ' '{print $4}')
total=$(($used + $free))
limit=$(($total/10))
echo "${total} ${used} ${free}"
local remark
remark=$(GetRemark ${MEM_REMARK})
# 檢查內(nèi)存占用是否超過90%
if [ "$remark" = "" ] [ "$limit" -gt "$free" ]; then
echo "Subject: ${HOST} Memory uses more than 90% $(date +%Y-%m-%d' '%H:%M:%S)" | sendmail ${NOTICE_EMAIL}
echo "$(date +%s)" > "$MEM_REMARK"
fi
}
# 獲取load average
function GetLoad() {
load=$(uptime | awk -F 'load average: ' '{print $2}')
m1=$(echo $load | awk -F ', ' '{print $1}')
m5=$(echo $load | awk -F ', ' '{print $2}')
m15=$(echo $load | awk -F ', ' '{print $3}')
echo "${m1} ${m5} ${m15}"
m1u=$(echo $m1 | awk -F '.' '{print $1}')
local remark
remark=$(GetRemark ${LOAD_REMARK})
# 檢查是否負(fù)載是否有壓力
if [ "$remark" = "" ] [ "$m1u" -gt "2" ]; then
echo "Subject: ${HOST} Load Average more than 2 $(date +%Y-%m-%d' '%H:%M:%S)" | sendmail ${NOTICE_EMAIL}
echo "$(date +%s)" > "$LOAD_REMARK"
fi
}
# 獲取上一次發(fā)送電郵時(shí)間
function GetRemark() {
local remark
if [ -f "$1" ] [ -s "$1" ]; then
remark=$(cat $1)
if [ $(( $NOW - $remark )) -gt "$REMARK_EXPIRE" ]; then
rm -f $1
remark=""
fi
else
remark=""
fi
echo $remark
}
# *** function end ***
cpuinfo=$(GetCpu)
meminfo=$(GetMem)
loadinfo=$(GetLoad)
echo "cpu: ${cpuinfo}" >> "${CPU_LOG}"
echo "mem: ${meminfo}" >> "${MEM_LOG}"
echo "load: ${loadinfo}" >> "${LOAD_LOG}"
exit 0
監(jiān)控網(wǎng)站是否異常
shell 監(jiān)控網(wǎng)站是否異常的腳本,如有異常自動(dòng)發(fā)電郵通知管理員。
流程:
1.檢查網(wǎng)站返回的http_code是否等于200,如不是200視為異常。
2.檢查網(wǎng)站的訪問時(shí)間,超過MAXLOADTIME(10秒)視為異常。
3.發(fā)送通知電郵后,在/tmp/monitor_load.remark記錄發(fā)送時(shí)間,在一小時(shí)內(nèi)不重復(fù)發(fā)送,如一小時(shí)后則清空/tmp/monitor_load.remark。
#!/bin/bash
SITES=("http://web01.example.com" "http://web02.example.com") # 要監(jiān)控的網(wǎng)站
NOTICE_EMAIL='me@example.com' # 管理員電郵
MAXLOADTIME=10 # 訪問超時(shí)時(shí)間設(shè)置
REMARKFILE='/tmp/monitor_load.remark' # 記錄時(shí)否發(fā)送過通知電郵,如發(fā)送過則一小時(shí)內(nèi)不再發(fā)送
ISSEND=0 # 是否有發(fā)送電郵
EXPIRE=3600 # 每次發(fā)送電郵的間隔秒數(shù)
NOW=$(date +%s)
if [ -f "$REMARKFILE" ] [ -s "$REMARKFILE" ]; then
REMARK=$(cat $REMARKFILE)
# 刪除過期的電郵發(fā)送時(shí)間記錄文件
if [ $(( $NOW - $REMARK )) -gt "$EXPIRE" ]; then
rm -f ${REMARKFILE}
REMARK=""
fi
else
REMARK=""
fi
# 循環(huán)判斷每個(gè)site
for site in ${SITES[*]}; do
printf "start to load ${site}\n"
site_load_time=$(curl -o /dev/null -s -w "time_connect: %{time_connect}\ntime_starttransfer: %{time_starttransfer}\ntime_total: %{time_total}" "${site}")
site_access=$(curl -o /dev/null -s -w %{http_code} "${site}")
time_total=${site_load_time##*:}
printf "$(date '+%Y-%m-%d %H:%M:%S')\n"
printf "site load time\n${site_load_time}\n"
printf "site access:${site_access}\n\n"
# not send
if [ "$REMARK" = "" ]; then
# check access
if [ "$time_total" = "0.000" ] || [ "$site_access" != "200" ]; then
echo "Subject: ${site} can access $(date +%Y-%m-%d' '%H:%M:%S)" | sendmail ${NOTICE_EMAIL}
ISSEND=1
else
# check load time
if [ "${time_total%%.*}" -ge ${MAXLOADTIME} ]; then
echo "Subject: ${site} load time total:${time_total} $(date +%Y-%m-%d' '%H:%M:%S)" | sendmail ${NOTICE_EMAIL}
ISSEND=1
fi
fi
fi
done
# 發(fā)送電郵后記錄發(fā)送時(shí)間
if [ "$ISSEND" = "1" ]; then
echo "$(date +%s)" > $REMARKFILE
fi
exit 0
您可能感興趣的文章:- Ubuntu中為Android增加硬件抽象層(HAL)模塊訪問Linux內(nèi)核驅(qū)動(dòng)程序
- 基于Linux系統(tǒng)中查看硬件等信息的方法詳解
- Linux下的硬件安裝
- Linux常用查看硬件設(shè)備信息命令大全(值得收藏)