linux下对服务器性能监控shell脚本

时间:2023-01-31 10:59:09
 #!/bin/bash

 #提取本服务器的IP地址信息
ENO1=`ifconfig | sed -n '1,1p' | awk -F ' ' '{print $1}'`
IP=`ifconfig ${ENO1}| grep "inet addr" | cut -f -d ":" | cut -f -d " "`
echo 'eno: '${ENO1}
echo 'IP: '${IP} # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# 各个监控警告值
WARN_LOAD=1.1
WARN_CPU=0.95
WARN_MEM=0.93
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
CPU=0.0
LOAD1=0.0
LOAD5=0.0
LOAD15=0.0
MEM=0.0
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # send_mail() {
echo 'send mail ' "${1}" "${2}"
} # 、监控系统负载的变化情况.
system_load_func()
{
cpu_num=`grep -c 'model name' /proc/cpuinfo` load=$(uptime | awk -F 'load average: ' '{print $2}')
load_1=$(echo $load | awk -F ', ' '{print $1}')
load_5=$(echo $load | awk -F ', ' '{print $2}')
load_15=$(echo $load | awk -F ', ' '{print $3}') #计算当前系统单个核心平均负载值,结果小于1.0时前面个位数补0。
LOAD1=`echo "scale=2;a=${load_1}/${cpu_num};if(length(a)==scale(a)) print 0;print a" | bc`
LOAD5=`echo "scale=2;a=${load_5}/${cpu_num};if(length(a)==scale(a)) print 0;print a" | bc`
LOAD15=`echo "scale=2;a=${load_15}/${cpu_num};if(length(a)==scale(a)) print 0;print a" | bc` if [ `echo "${LOAD15} > ${WARN_LOAD}" | bc` -eq ]
then
send_mail "${IP}服务器15分钟的系统平均负载为${LOAD15}, 超过警戒值${WARN_LOAD}, 请立即处理!" "$IP 服务器系统负载告警!"
fi
} # 、监控系统cpu的情况.
cpu_func()
{
#取当前空闲cpu百份比值(只取整数部分)
cpu_idle=`top -b -d 0.1 -n | grep Cpu | tail -n | awk '{print $8}' | cut -f -d "."`
CPU=`echo "scale=2; (100 - ${cpu_idle}) / 100" | bc` if [ `echo "${CPU} > ${WARN_CPU}" | bc` -eq ]
then
send_mail "${IP}服务器cpu使用${CPU},请及时处理." "$IP 服务器CPU告警"
fi
} #. 监控物理内存
mem_func()
{
mem=$(free -m | sed -n '2,2p')
mem_total=`echo ${mem} | awk -F ' ' '{print $2}'`
mem_used=`echo ${mem} | awk -F ' ' '{print $3}'`
# mem_free=`echo ${mem} | awk -F ' ' '{print $4}'` if [ $mem_used -ne ]
then
MEM=`echo "scale=2; ${mem_used} / ${mem_total}" | bc`
if [ `echo "${MEM} > ${WARN_MEM}" | bc` -eq ]
then
send_mail "$IP服务器物理内存已使用 ${MEM},请及时处理." "$IP 服务器内存告警"
fi
fi
} CNT=
print_info() {
echo "# # # # # # # # # # # [${CNT}] # # # # # # # # # # # # # #"
echo cpu: ${CPU}
echo load1: ${LOAD1}
echo load5: ${LOAD5}
echo load15: ${LOAD15}
echo mem: ${MEM}
echo "# # # # # # # # # # # # # # # # # # # # # # # # # # #"
(( CNT += ))
# exit
} while true
do
system_load_func
cpu_func
mem_func
print_info
sleep
done

注: 需要下载bc命令:

sudo apt-get install bc

这里我只在脚本里面写了获取各个监控信息的数据, 发送邮件和数据写入数据库这些我省略掉了. 发送邮件我使用的是python, 通过该shell脚本调用python发送邮件.(因为用linux自带的我发现会出现很多垃圾邮件, 怎么解决我没有去研究). 得到的数据我会写入数据库记录, 在这里为了通用性, 我把写入数据库部分省略掉了.