服务器监控shell脚本

实现:

 监控功能服务器脚本,达到阈值告警

#!/bin/bash
source /etc/profile
#磁盘告警上限值
disklimit=85
#内存告警上限值
memlimit=90
#cpu告警上限值
cpulimit=95
tokenurl="http://xxx/token"
mailurl="http://xxxx/sendEmail"
service=""
arr=([email protected])
# 获取系统版本
sys_os=`cat /etc/redhat-release|awk '{print $1 "_"$4}'`
echo "系统版本:"$sys_os
# 获取系统内核
sys_version=`uname -r`
echo "Linux内核版本:"$sys_version

# 获取要监控的本地服务器IP地址
IP=`ifconfig | grep inet | grep -vE 'inet6|127.0.0.1' | awk '{print $2}'`
echo "IP地址:"$IP

# 获取cpu总核数
cpu_num=`grep -c "model name" /proc/cpuinfo`
echo "cpu总核数:"$cpu_num

# 获取占用CPU百分比
cpu_use=`top -b -n 1 | grep Cpu | awk '{print $2}' | cut -f 1 -d "%"`
echo "CPU使用百分比:"$cpu_use
# 获取空闲CPU百分比
cpu_idle=`top -b -n 1 | grep Cpu | awk '{print $8}' | cut -f 1 -d "%"`
echo "空闲CPU百分比:"$cpu_idle
cpuradio=$(awk 'BEGIN{printf "%.2f\n",100-'$cpu_idle'}')
echo "CPU使用率:"$cpuradio" %"
# 获取物理内存总量
mem_total=`free | grep Mem | awk '{print $2}'`
mem_total_g=$(printf "%.4f" `echo "scale=4;($mem_total/1024/1024)"|bc`)
echo "物理内存总量:"$mem_total

# 获取操作系统已使用内存总量
mem_used=`free | grep Mem | awk '{print $3}'`
echo "已使用内存总量(操作系统):"$mem_used
# 获取操作系统未使用内存总量
mem_free=`free | grep Mem | awk '{print $4}'`
echo "剩余内存总量(操作系统):"$mem_free
mem_available=`free | grep Mem | awk '{print $7}'`
echo "可用内存:"$mem_available
memradio=$(printf "%.4f" `echo "scale=4;($mem_used/$mem_total)"|bc`)
memradio=$(awk 'BEGIN{printf "%.2f\n",'$memradio'*100}')
echo "内存使用率:"$memradio" %"
#disk_total=`df / | grep '/' | awk '{print $2}' `
#echo "磁盘总数:"$disk_total
#disk_use=`df / | grep '/' | awk '{print $3}' `
#echo "磁盘已使用:"$disk_use
#disk_use_rate=`df / | grep '/' | awk '{print $5}' `
#echo "磁盘使用率:"$disk_use_rate

DEV=`df -Ph | grep '^/dev/*' | cut -d' ' -f1 | sort`
disk_total=0
disk_used=0
for I in $DEV 
do 
  dev=`df -h | grep $I | awk '{print $1}'`
  size=`df  | grep $I | awk '{print $2}'`
  use=`df  |grep $I |awk '{print $3}'`
  disk_total=$(($disk_total+$size))
  disk_used=$(($disk_used+$use))
  #echo $disk_used
  #echo -e "$I:\tsize:$size\tuse:$use"
done
echo "磁盘总数:"$disk_total
disk_total_g=$(printf "%.4f" `echo "scale=4;($disk_total/1024/1024)"|bc`)
echo "磁盘使用量:"$disk_used
dsikradio=$(printf "%.4f" `echo "scale=4;($disk_used/$disk_total)"|bc`)
dsikradio=$(awk 'BEGIN{printf "%.2f\n",'$dsikradio'*100}')
echo "磁盘使用率:"$dsikradio" %"
#邮件告警
#磁盘告警上限
if [ `echo "$dsikradio > $disklimit"|bc` -eq 1 ] || [ `echo "$memradio > $memlimit"|bc` -eq 1 ]
then
addr=`ip a|grep -w 'inet'|grep 'global'|sed 's/^.*inet //g'|sed 's/\/[0-9][0-9].*$//g'`
           for i in "${arr[@]}"; do
                params='{"email":"'$i'","theme":"服务器监控告警异常","content":服务器'$IP'磁盘使用率: '$dsikradio' %,内存使用率: '$memradio' %,cpu使用率: '$cpuradio' %,请检查服务是否正常.服务器配置如下:\n cpu总核数:'$cpu_num' 核 \n 物理内存总量:'$mem_total_g' g \n 磁盘总数: '$disk_total_g' g \n "}'
                echo "$params"
                result=`curl -s -H "Content-Type: application/json" -XPOST -d '{"client_id": "imas-monitoryt", "client_secret": "rUHib0bAqUOBtPsu", "grant_type": "client_credentials"}' $tokenurl`
                echo $result > requestresult.txt
                token=$(cat requestresult.txt | sed 's/,/\n/g' | grep "access_token" | sed 's/:/\n/g' | sed '1d' |sed '1d' |  sed 's/}//g' |  sed 's/"//g' )
                curl   -XPOST -H  "Content-Type: application/json"  -H "Authorization: Bearer ${token}"  ${mailurl}  -d "$params"
                rm -f requestresult.txt
           done
else
  echo "$IP服务器指标正常"
fi

监控后的内容:

猜你喜欢

转载自blog.csdn.net/qq_35008624/article/details/130465255
今日推荐