#!/bin/bash
###############################################
#监控系统负载与CPU、内存、硬盘,超出警戒值则发告警#
###############################################
######配置######
#FTP的ip
FTP_URL_LOAD=192.168.72.130
#FTP的登陆名
FTP_USER_LOAD=test
#FTP的登陆密码
FTP_PASS_LOAD=test123
#log路径
FTP_LOAD_LOCAL=/home/monitor/log/
#上传文件路径
FTP_LOAD_REMOTE=/home/monitor/

#告警阈值
threshold=80

#磁盘
data_name=(/dev/sr0 /dev/sda2)

######配置信息######

######采集系统状态#####
#当前系统时间
now_time=date '+%F %T'
DATE=$(date +%Y%m%d)

#监控日志
logFile=$(hostname)_$DATE.log
echo "" >> $FTP_LOAD_LOCAL/$logFile
echo "" >> $FTP_LOAD_LOCAL/$logFile
echo "" >> $FTP_LOAD_LOCAL/$logFile
echo "采集时间:${now_time}" >> $FTP_LOAD_LOCAL/$logFile

#获取服务器1分钟、5分钟、15分钟的负载情况
load_average=uptime | awk '{print $8,$9,$10,$11,$12}'
echo '#系 统 负 载: 1分钟,5分钟,15分钟' >> $FTP_LOAD_LOCAL/$logFile
echo '#系 统 负 载:' $load_average >> $FTP_LOAD_LOCAL/$logFile

#获取当前系统15分钟的平均负载值
load_15=uptime | awk '{print $NF}'
load_15_val=$(echo "scale=0; $load_15/1"|bc)

#获取cpu使用率
cpuUsage=top -n 1 | awk -F '[ %]+' 'NR==3 {print $2}'
echo '# CPU 使 用 率:'$cpuUsage'%' >> $FTP_LOAD_LOCAL/$logFile

#获取内存情况
mem_total=free -m | awk -F '[ :]+' 'NR==2{print $2}'
mem_used=free -m | awk -F '[ :]+' 'NR==3{print $3}'
echo '#系 统 总 内 存:'$mem_total'M' >> $FTP_LOAD_LOCAL/$logFile
echo '#程序已使用内存:'$mem_used'M' >> $FTP_LOAD_LOCAL/$logFile

#获取内存使用率
mem_used_persent=awk 'BEGIN{printf "%.0f\n",('$mem_used'/'$mem_total')*100}'
echo '#内 存 已 使 用:'$mem_used_persent'%' >> $FTP_LOAD_LOCAL/$logFile

#获取IO使用率
iowait=iostat -c | grep -v avg | grep '[^/s]' | grep -v Li | awk '{print $4}'
iowait_val=$(echo "scale=0; $iowait/1"|bc)
echo '# IO 已 使 用:'$iowait_val'%' >> $FTP_LOAD_LOCAL/$logFile

#获取磁盘使用率
for disk in ${data_name[@]};do
diskUsage=df -h | grep $disk | awk -F '[ %]+' '{print $5}'
diskRoute=df -h | grep $disk | awk '{print $6}'
echo "" >> $FTP_LOAD_LOCAL/$logFile
echo '#磁 盘 使 用 率:'$diskUsage'%' >> $FTP_LOAD_LOCAL/$logFile
echo '#磁 盘 挂载目录:'$diskRoute >> $FTP_LOAD_LOCAL/$logFile
done

#告警
if [[ "$cpuUsage" > "$threshold" ]];then
echo "" >> $FTP_LOAD_LOCAL/$logFile
echo "告警时间:${now_time}" >> $FTP_LOAD_LOCAL/$logFile
echo "-------警告-------" >> $FTP_LOAD_LOCAL/$logFile
echo "CPU 使用率已经超过:${cpuUsage}%" >> $FTP_LOAD_LOCAL/$logFile
echo "-------警告-------" >> $FTP_LOAD_LOCAL/$logFile
fi

if [[ "$mem_used_persent" > "$threshold" ]];then
echo "" >> $FTP_LOAD_LOCAL/$logFile
echo "告警时间:${now_time}" >> $FTP_LOAD_LOCAL/$logFile
echo "-------警告-------" >> $FTP_LOAD_LOCAL/$logFile
echo "内存使用率已经超过:${mem_used_persent}%" >> $FTP_LOAD_LOCAL/$logFile
echo "-------警告-------" >> $FTP_LOAD_LOCAL/$logFile
fi

if [[ "$load_15_val" > "$threshold" ]];then
echo "" >> $FTP_LOAD_LOCAL/$logFile
echo "告警时间:${now_time}" >> $FTP_LOAD_LOCAL/$logFile
echo "-------警告-------" >> $FTP_LOAD_LOCAL/$logFile
echo "负载使用率已经超过:${load_15_val}%" >> $FTP_LOAD_LOCAL/$logFile
echo "-------警告-------" >> $FTP_LOAD_LOCAL/$logFile
fi

if [[ "$iowait_val" > "$threshold" ]];then
echo "" >> $FTP_LOAD_LOCAL/$logFile
echo "告警时间:${now_time}" >> $FTP_LOAD_LOCAL/$logFile
echo "-------警告-------" >> $FTP_LOAD_LOCAL/$logFile
echo "IO 使用率已经超过:${iowait_val}%" >> $FTP_LOAD_LOCAL/$logFile
echo "-------警告-------" >> $FTP_LOAD_LOCAL/$logFile
fi

for disk2 in ${data_name[@]};do
diskUsage2=df -h | grep $disk2 | awk -F '[ %]+' '{print $5}'
diskRoute2=df -h | grep $disk2 | awk '{print $6}'
if(($diskUsage2 > $threshold)); then
echo "" >> $FTP_LOAD_LOCAL/$logFile
echo "告警时间:${now_time}" >> $FTP_LOAD_LOCAL/$logFile
echo "-------警告-------" >> $FTP_LOAD_LOCAL/$logFile
echo '磁 盘 挂 载 目 录:'$diskRoute2 >> $FTP_LOAD_LOCAL/$logFile
echo "磁盘使用率已经超过:${diskUsage2}%" >> $FTP_LOAD_LOCAL/$logFile
echo "-------警告-------" >> $FTP_LOAD_LOCAL/$logFile
fi
done

######采集系统状态#####

#上传到FTP服务器
ftp -i -v -n<<EOF
open $FTP_URL_LOAD
user $FTP_USER_LOAD $FTP_PASS_LOAD
binary
cd $FTP_LOAD_REMOTE
lcd $FTP_LOAD_LOCAL
prompt
mput $logFile
close
bye
EOF