#!/bin/bash
# 系统监控,记录cpu、memory、load average,当超过规定数值时发电邮通知管理员
# *** config start ***
# 当前目录路径
ROOT=$(cd "$(dirname "$0")"; pwd)
# 当前服务器名
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 记录上一次发送通知电邮时间
CPU_REMARK='/tmp/servermonitor_cpu.remark'
MEM_REMARK='/tmp/servermonitor_mem.remark'
LOAD_REMARK='/tmp/servermonitor_loadaverage.remark'
# 发通知电邮间隔时间
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
}
# 获取内存使用情况
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})
# 检查内存占用是否超过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})
# 检查是否负载是否有压力
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
}
# 获取上一次发送电邮时间
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
#!/bin/bash
SITES=("http://web01.example.com" "http://web02.example.com") # 要监控的网站
NOTICE_EMAIL='me@example.com' # 管理员电邮
MAXLOADTIME=10 # 访问超时时间设置
REMARKFILE='/tmp/monitor_load.remark' # 记录时否发送过通知电邮,如发送过则一小时内不再发送
ISSEND=0 # 是否有发送电邮
EXPIRE=3600 # 每次发送电邮的间隔秒数
NOW=$(date +%s)
if [ -f "$REMARKFILE" ] [ -s "$REMARKFILE" ]; then
REMARK=$(cat $REMARKFILE)
# 删除过期的电邮发送时间记录文件
if [ $(( $NOW - $REMARK )) -gt "$EXPIRE" ]; then
rm -f ${REMARKFILE}
REMARK=""
fi
else
REMARK=""
fi
# 循环判断每个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
# 发送电邮后记录发送时间
if [ "$ISSEND" = "1" ]; then
echo "$(date +%s)" > $REMARKFILE
fi
exit 0