一、安装mail
yum install mailx -y
[root@1 ~]# cat /etc/mail.rc
set from=2249902650@qq.com
set smtp=smtps://smtp.qq.com:465
set smtp-auth-user=2249902650@qq.com
set smtp-auth-password=owgotyoamyzgeabg
set smtp-auth=login
set ssl-verify=ignore
set nss-config-dir=/etc/pki/nssdb/
二、脚本
[root@1 ~]# cat test.sh
#!/bin/bash
#user:liangzeyu
#2020-60-20
[ -f /etc/init.d/functions ] && . /etc/init.d/functionsDirfile=/home/xunjian.txt
[ -e ${Dirfile} ] || touch ${Dirfile}
>${Dirfile}#磁盘空间
echo '##############disk spase######################' >>${Dirfile}
Df=`df -h|awk 'NR!=1{print $0}'|wc -l`
for i in `seq 1 ${Df}`
do
df=`df -h|awk -F '[ |%]+' 'NR!=1{print $5}'|tail -${i}|head -1`
web=`df -h|awk -F '[ ]+' 'NR!=1{print $6}'|tail -${i}|head -1`
if [ $df -ge 80 ];then
echo "${web}已使用${df}%,磁盘空间已经超过80%,请及时处理" >>${Dirfile}
else
echo "${web}已使用${df}%,目前正常" >>${Dirfile}
fi
done
#用户登陆详情
echo '##############login user######################' >>${Dirfile}
Date=`date|awk '{print $1,$2}'`
last|grep "$Date"|awk '{a[$1]+=1}END{for(i in a){print "用户:"i,"登陆次数:"a[i]"次"}}' >>${Dirfile}#网站运行情况
echo '##############website state######################' >>${Dirfile}
Curl=`curl -I -s http://www.casl.org.cn|awk 'NR==1{print $2}'`
if [ ${Curl} -ne 200 ];then
echo "网站运行异常,状态码:${Curl},请及时排查" >>${Dirfile}
else
echo "网站运行正常,状态码:${Curl}" >>${Dirfile}
fi#内存
echo '################memory space######################' >>${Dirfile}
gross=`free -m|awk 'NR==2{print $2}'`
usage=`free -m|awk 'NR==2{print $4}'`
Free=`awk 'BEGIN{print '"$usage"'/'"$gross"'*100}'`
echo "内存剩余${Free}%" >>${Dirfile}#端口详情
echo '##############post state######################' >>${Dirfile}
tomcat=`netstat -lntp|grep 80|wc -l`
tomcat1=`netstat -lntp|grep 8080|wc -l`
mysql=`netstat -lntp|grep 3306|wc -l`if [ $tomcat -eq 0 ];then
echo "tomcat 80 端口异常,请及时排查." >>${Dirfile}
else
echo "tomcat 80 端口正常," >>${Dirfile}
fiif [ $tomcat1 -eq 0 ];then
echo "tomcat 8080 端口异常,请及时排查." >>${Dirfile}
else
echo "tomcat 8080 端口正常," >>${Dirfile}
fiif [ $mysql -eq 0 ];then
echo "mysql 3306 端口异常,请及时排查." >>${Dirfile}
else
echo "mysql 3306 端口正常," >>${Dirfile}
fi#邮件发送
Time=`date +"%Y-%m-%d"`
mail -s "${Time}巡检报告" 2249902650@qq.com < ${Dirfile}
三、手动执行
[root@1 ~]# cat 1.sh
#!/bin/bash
#user:liangzeyu
#2020-60-20
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
#磁盘空间
echo '##############disk spase######################'
Df=`df -h|awk 'NR!=1{print $0}'|wc -l`
for i in `seq 1 ${Df}`
do
df=`df -h|awk -F '[ |%]+' 'NR!=1{print $5}'|tail -${i}|head -1`
web=`df -h|awk -F '[ ]+' 'NR!=1{print $6}'|tail -${i}|head -1`
if [ $df -ge 80 ];then
action "${web}已使用${df}%,磁盘空间已经超过80%,请及时处理" /bin/false
else
action "${web}已使用${df}%,目前正常" /bin/true
fi
done
#用户登陆详情
echo '##############login user######################'
Date=`date|awk '{print $1,$2}'`
last|grep "$Date"|awk '{a[$1]+=1}END{for(i in a){print "用户:"i,"登陆次数:"a[i]"次"}}'#网站运行情况
echo '##############website state######################'
Curl=`curl -I -s http://www.casl.org.cn|awk 'NR==1{print $2}'`
if [ ${Curl} -ne 200 ];then
action "网站运行异常,状态码:${Curl},请及时排查" /bin/false
else
action "网站运行正常,状态码:${Curl}" /bin/true
fi#内存
echo '################memory space######################'
gross=`free -m|awk 'NR==2{print $2}'`
usage=`free -m|awk 'NR==2{print $4}'`
Free=`awk 'BEGIN{print '"$usage"'/'"$gross"'*100}'`
echo "内存剩余${Free}%"#端口详情
echo '##############post state######################'
tomcat=`netstat -lntp|grep 80|wc -l`
tomcat1=`netstat -lntp|grep 8080|wc -l`
mysql=`netstat -lntp|grep 3306|wc -l`if [ $tomcat -eq 0 ];then
action "tomcat 80 端口异常,请及时排查." /bin/false
else
action "tomcat 80 端口正常," /bin/true
fiif [ $tomcat1 -eq 0 ];then
action "tomcat 8080 端口异常,请及时排查." /bin/false
else
action "tomcat 8080 端口正常," /bin/true
fiif [ $mysql -eq 0 ];then
action "mysql 3306 端口异常,请及时排查." /bin/false
else
action "mysql 3306 端口正常," /bin/true
fi#邮件发送
Time=`date +"%Y-%m-%d"`
#mail -s "${Time}巡检报告" 2249902650@qq.com < ${Dirfile}
------------------------------
遇到的坑:
注意在发送邮箱的时候,发送的内容千万不要有这样的
如果有,抱歉,那你发送的内容是bin文件。
-----
有问题请加博主微信进行沟通!
全部评论