#!/bin/bash
#
#**************************************************#
#Author: 陪你听风
#QQ: 286518646@qq.com
#Date: 2023-09-06
#FileName: tomcat_monitor.sh
#URL: 189102195xx
#Description: The script is init OS
#Copyright(C): 2023All rights reserved
#**************************************************#
echo -e "\033[32m监测tomcat服务运行状态,如果运行异常,发送邮件通知,并且启动服务"
export info=""
export email="286518646@qq.com"
servername=`hostname`
ip=`ifconfig|grep inet |egrep -v inet6 |awk '{print $2}'|sed ':a;N;$!ba;s/\n/,/g'`
check_time=`date +"%F %T %A"`
title=`echo -e "服务器信息检测脚本\r\n主机:$servername\r\nIP地址:$ip\r\n检测时间:$check_time"`
start_nginx(){
systemctl start nginx
}
check_nginx(){
PID=`ps aux |grep nginx |egrep -v grep |awk '{print $2}'`
PORT=`netstat -lnptu |grep :80 |egrep -v "tcp6|grep" |grep nginx|tr -s " " |cut -d ":" -f2|awk '{print $1}'`
if [[ ! -z $PID && $PORT -eq 80 ]];then
message="nginx正在运行,进程号是:\n$PID,\n监听端口是:\n$PORT"
echo -e "\033[32m$message\033[0m"
else
echo -e "\033[31m警告!未检测到nginx运行的信息"
echo -e "nginx服务出现中断,主机:$servername,IP地址:$ip ,检测时间:$check_time" | mail -s "主机:$servername nginx服务出现中断,IP地址:$ip,检测时间:$check_time" -r "$email" $email
echo -e "准备启动nginx服务......"
start_nginx
echo -e "nginx服务启动成功......"
echo -e "nginx服务已经恢复,主机:$servername, IP地址:$ip ,检测时间:$check_time,进程号是:$PID,监听端口是:$PORT" |mail -s "n主机:$servername nginx服务已经恢复,IP地址:$ip检测时间:$check_time" -r "$email" $email
check_nginx
fi
}
while :
do
echo -e "\033[32m服务器信息检测脚本\r\n主机:$servername\r\nIP地址:$ip\r\n检测时间:$check_time"
check_nginx
echo "---------------------------------------------------------------"
sleep 2s
done