目录
1、Monit 安装与配置
1.1、自动安装
yum install -y epel-release && yum install -y monit
1.2、手动安装
下载路径:
https://dl.fedoraproject.org/pub/epel/
https://mmonit.com/monit/dist/
1.3、启动命令:
systemctl restart monit
1.4、随系统自动启动:
chkconfig mysqld on
1.5、添加配置文件
/etc/monitrc会include 下面的配置文件
配置文件目录:/etc/monit.d/
1.6、配置文件检测
monit –t
monit -t -c /etc/monitrc
如果有提示如下语句,那么就是可用的:
Control file syntax OK
重启生效:
systemctl restart monit
2、各种应用程序本地监控
2.1、Tomcat
2.1.1、Tomcat 启动生成PID.
# Get standard environment variables
PRGDIR=`dirname "$PRG"`
CATALINA_PID=/var/run/tomcat.pid
2.1.2、Monit 配置文件Tomcatrc
check process tomcat with pidfile /var/run/tomcat.pid
start program = "/home/connect/apache-tomcat-8.5.41/bin/startup.sh" with timeout 60 seconds
stop program = "/home/connect/apache-tomcat-8.5.41/bin/shutdown.sh"
2.3、普通Java应用进程
public static void main(String[] args) throws Exception {
int pid = getPid();
}
private static int getPid() {
RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
String name = runtime.getName(); // format: "pid@hostname"
int pid = -1;
try {
pid = Integer.parseInt(name.substring(0, name.indexOf('@')));
} catch (Exception e) {
pid = -1;
}
return pid;
}
2.4、Spring boot
配置文件application.properties文件内容如下,该配置指定了pid文件的位置:
spring.pid.file=/xxx/syb.pid
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication sa = new SpringApplication(App.class);
sa.addListeners(new ApplicationPidFileWriter());
sa.run(args);
}
}
2.5、Mysql
在 my.cnf 中配置 PID
[mysqld]
pid-file= /var/run/mysqld/mysqld.pid
配置脚本
check process mysql with pidfile /run/mysqld/mysqld.pid
start program = "/usr/sbin/service mysql start" with timeout 60 seconds
stop program = "/usr/sbin/service mysql stop"
if failed unixsocket /var/run/mysqld/mysqld.sock then restart