#monit安装笔记
##linux进程监控插件monit安装
-
安装
方法一:yum 安装 yum -y install epel-release yum -y install monit 方法二:压缩包安装 tar -xzvf monit-5.25.2.tar.gz cd monit-5.25.2 ./configure --prefix=/usr --without-pam make make install cp monitrc /etc/monitrc
-
配置
修改文件: vim /etc/monitrc 或 vim /etc/monit.conf
set daemon 60 #set log syslog set logfile /var/log/monit.log set httpd port 2812 and use address 172.20.10.11 allow 0.0.0.0/0.0.0.0 allow admin:monit #with ssl { # enable SSL/TLS and set path to server certificate # pemfile: /etc/ssl/certs/monit.pem #} include /etc/monit.d/*
-
命令
启动服务:systemctl start monit 或 service monit start 或 /usr/bin/monit -c /etc/monitrc 停止服务:systemctl stop monit 重启服务:systemctl restart monit 查看服务运行状态:systemctl status monit -l 查看日志: tailf /var/log/monit.log
-
验证启动是否成功
访问: http://ip:2812
##开机启动monit服务
-
将服务启动命令加入到/etc/rc.d/rc.local (命令: vim /etc/rc.d/rc.local)中,在文件的最后添加:
/usr/bin/monit -c /etc/monitrc /usr/bin/monit reload /usr/bin/monit start all
注意monit服务开机自启动,需要将完整路径写入,否则不起作用
-
给/etc/rc.d/rc.local文件附加上可执行权限
chmod 755 /etc/rc.d/rc.local
监控tomcat
-
修改$Tomcat_HOME/bin目录下的catalina.sh脚本(命令: vim $Tomcat_HOME/bin/catalina.sh),添加一下环境变量:
CATALINA_PID=/usr/local/tomcat/tomcat8080.pid JAVA_HOME=/web/soft/jdk
-
创建pid目录和监控tomcat配置文件目录:
mkdir /usr/local/tomcat/ mkdir /etc/monit.d
-
创建监控tomcat配置文件,在/etc/monit.d目录下面,创建一个tomcatrc的文件:
vi /etc/monit.d/tomcatrc
-
向tomcatrc中添加:
check process tomcat8080 with pidfile /usr/local/tomcat/tomcat8080.pid #需将tomcat添加为linux服务 #start program = "/etc/init.d/tomcat start" with timeout 60 seconds #需将tomcat添加为linux服务 #stop program = "/etc/init.d/tomcat stop" #不用将tomcat添加为linux服务,需在catalina.sh中添加CATALINA_PID、JAVA_HOME变量,如: #CATALINA_PID=/usr/local/tomcat/tomcat8080.pid #JAVA_HOME=/web/soft/jdk start program = "/web/soft/apache-tomcat-7.0.94/bin/startup.sh" with timeout 60 seconds stop program = "/web/soft/apache-tomcat-7.0.94/bin/shutdown.sh" #设置在10个监视周期内重启了9次则超时,不再监视 if 9 restarts within 10 cycles then timeout #如果在5个周期内该服务的cpu使用率都超过90%,则提示 if cpu usage > 90% for 5 cycles then alert #若连续5个周期连接都失败,则重启服务 if failed host 127.0.0.1 port 8090 protocol http and request "/CISOpenAPI2.1/" for 5 cycles then restart #https请求 if failed host weixin.iptv.gd.cn port 443 protocol https and request "/CISOpenAPI2.1/" and for 5 cycles then restart
-
添加tomcatrc 脚本为可执行权限
chmod 755 /etc/monit.d/tomcatrc
-
重启monit
systemctl restart monit
监控nginx
-
创建监控nginx配置文件,在/etc/monit.d目录下面,创建一个nginx的文件:
mkdir /etc/monit.d vi /etc/monit.d/nginx
-
向nginx中添加:
check process nginx with pidfile /var/run/nginx.pid start program = "/web/soft/nginx/sbin/nginx -c /web/soft/nginx/conf/nginx.conf" stop program = "/web/soft/nginx/sbin/nginx -s stop" if failed host 127.0.0.1 port 80 protocol http for 5 cycles then restart
-
添加nginx脚本为可执行权限
chmod 755 /etc/monit.d/nginx
-
重启monit
systemctl restart monit
添加tomcat为linux系统服务
-
创建pid存储目录:
mkdir /usr/local/tomcat
-
修改$Tomcat_HOME/bin目录下的catalina.sh脚本(命令: vim $Tomcat_HOME/bin/catalina.sh),添加一下环境变量:
CATALINA_PID=/usr/local/tomcat/tomcat8080.pid
-
将$Tomcat_HOME/bin目录下的catalina.sh脚本复制到目录/etc/init.d中,重命名为tomcat
cp $Tomcat_HOME/bin/catalina.sh /etc/init.d/tomcat
-
修改刚才复制的tomcat脚本
a. 在脚本的第三行后面插入下面两行 # chkconfig: 2345 10 90 # description:Tomcat service 第一行是服务的配置:第一个数字是服务的运行级,2345表明这个服务的运行级是2、3、4和5级(Linux的运行级为0到6);第二个数字是启动优先级,数值从0到99;第三个数是停止优先级,数值也是从0到99。 第二行是对服务的描述 b. 在脚本中设置 CATALINA_HOME 和 JAVA_HOME 这两个脚本必需的环境变量,如: CATALINA_PID=/usr/local/tomcat/tomcat8080.pid CATALINA_HOME=/usr/share/tomcat JAVA_HOME=/usr/share/java/jdk
-
添加tomcat 脚本为可执行权限
chmod 755 /etc/init.d/tomcat
-
最后用chkconfig设置服务运行
chkconfig --add tomcat
-
查看自定义的服务状态信息
chkconfig --list