如何配置monit监控某个进程?
假设我们要监控 myproc 进程
创建一个新文件/etc/monit/conf-available/myproc:
$ sudo vi /etc/monit/conf-available/myproc
内容如下:
check process myproc matching "myproc" start program = "/etc/init.d/myproc start" stop program = "/usr/bin/killall myproc"
启用该监控项:
$ sudo ln -s /etc/monit/conf-available/myproc /etc/monit/conf-enabled/
检查monit配置文件的语法:
$ monit -t
重新加载monit,使新加配置文件生效:
$ sudo /etc/init.d/monit reload
或者
$ sudo systemctl reload monit
当myproc异常终止后,monit将重启它,并发送邮件。
如何在系统启动时启用monit服务?
执行以下命令:
$ sudo systemctl enable monit
如何查看Monit信息?
查看Monit总览信息
$ sudo monit summary
输出示例:
The Monit daemon 5.16 uptime: 1h 9m Process 'openvpn' Running File 'openvpn_bin' Accessible System 'blr-onitroad-do-0001' Running
查看monit运行的状态:
$ sudo monit status
查看nginx进程的信息
$ sudo monit status nginx
Monit
Monit是一个免费的开源软件,可用于监控进程。它可以重启失败服务。
Monit是用于管理和监视Unix系统的小型开源实用程序。 Monit会进行自动维护和修复,并且可以在错误情况下执行有意义的操作。如果发生错误情况,例如,Monit可以采取行动。如果sendmail未运行,则Monit可以自动再次启动sendmail,或者apache使用的资源过多(例如,正在进行DoS攻击),Monit可以停止或重启apache并向您发送警报消息。
如何配置Monit来监控Apache服务器?
配置文件:
$ cat /etc/monit/conf-available/apache2
输出示例:
check process apache with pidfile /var/run/apache2/apache2.pid group www group apache start program = "/etc/init.d/apache2 start" stop program = "/etc/init.d/apache2 stop" if 4 restarts within 20 cycles then timeout if failed host localhost port 80 with protocol http and request "/server-status" with timeout 25 seconds for 4 times within 5 cycles then restart depend apache_bin depend apache_rc check file apache_bin with path /usr/sbin/apache2 group apache include /etc/monit/templates/rootbin check file apache_rc with path /etc/init.d/apache2 group apache include /etc/monit/templates/rootbin
配置monit,监控MariaDB/MySQL服务
新建配置文件:
$ vi /etc/monit/conf-available/mysql
内容如下:
check process mysqld with pidfile /var/run/mysqld/mysqld.pid group database group mysql start program = "/etc/init.d/mysql start" stop program = "/etc/init.d/mysql stop" if failed host localhost port 3306 protocol mysql with timeout 15 seconds for 3 times within 4 cycles then restart if failed unixsocket /var/run/mysqld/mysqld.sock protocol mysql for 3 times within 4 cycles then restart if 5 restarts with 5 cycles then timeout depend mysql_bin depend mysql_rc check file mysql_bin with path /usr/sbin/mysqld group mysql include /etc/monit/templates/rootbin check file mysql_rc with path /etc/init.d/mysql group mysql include /etc/monit/templates/rootbin
在Debian或Ubuntu Linux上如何监控Nginx/Apache这些服务,当它们由问题时自动重启?
如何使用Monit Debian服务,并在服务崩溃后重启服务?
如何在Debian或Ubuntu Linux上安装monit来监控进程?
如何配置Monit来监控OpenSSH SSHD服务器?
配置文件
$ cat /etc/monit/conf-available/openssh-server
输出示例:
check process sshd with pidfile /var/run/sshd.pid group system group sshd start program = "/etc/init.d/ssh start" stop program = "/etc/init.d/ssh stop" if failed host localhost port 22 with proto ssh then restart if 5 restarts with 5 cycles then timeout depend on sshd_bin depend on sftp_bin depend on sshd_rc depend on sshd_rsa_key depend on sshd_dsa_key check file sshd_bin with path /usr/sbin/sshd group sshd include /etc/monit/templates/rootbin check file sftp_bin with path /usr/lib/openssh/sftp-server group sshd include /etc/monit/templates/rootbin check file sshd_rsa_key with path /etc/ssh/ssh_host_rsa_key group sshd include /etc/monit/templates/rootstrict check file sshd_dsa_key with path /etc/ssh/ssh_host_dsa_key group sshd include /etc/monit/templates/rootstrict check file sshd_rc with path /etc/ssh/sshd_config group sshd include /etc/monit/templates/rootrc
必须使用ln命令链接这些文件,然后重新加载monit服务器。
$ cd /etc/monit/conf-enabled/ $ sudo ln -s /etc/monit/conf-available/nginx $ sudo ln -s /etc/monit/conf-available/mysql $ sudo ln -s /etc/monit/conf-available/apache2 $ sudo monit -t $ sudo /etc/init.d/monit reload
如何查看Monit的日志?
monit 默认日志文件为/var/log/monit.log。
$ sudo tail -f /var/log/monit.log
通过浏览器查看Monit:
http://server1.onitroad.local:2812
安装monit
执行以下apt-get命令/apt命令:
$ sudo apt-get install monit
配置monit
- 主配置文件:/etc/monit/monitrc
- 进程/服务器对应配置文件的目录:/etc/monit/conf-available/和/etc/monit/conf-enabled /
编辑/etc/monit/monitrc:
$ sudo vi /etc/monit/monitrc
首先设置警报接收者的电子邮件地址:
set alert admin@your-domain-name-here
或者这样设置,只获取有关安全性的警报:
set alert security@your-domain-name-here on { checksum, permission, uid, gid }
请注意,默认情况下,monit在服务失败时只发送一封电子邮件通知,
而在服务恢复时则只发送一封电子邮件通知。
下面的设置,在服务失败时每五个周期就发送一次通知:
alert jack@server1.onitroad.local with reminder on 5 cycles
启用嵌入式HTTP接口。
用于查看monit的服务状态,并且可以通过Web界面管理服务:
set httpd port 2812 and use address localhost # only accept connection from localhost allow localhost # allow localhost to connect to the server and allow admin:monit # require user 'admin' with password 'monit'
如何启动/停止/重启monit服务?
语法为:
$ sudo systemctl status monit $ sudo systemctl stop monit $ sudo systemctl restart monit $ sudo systemctl start monit
如何配置Monit来监控Nginx服务器?
配置文件:
$ vi /etc/monit/conf-available/nginx
内容:
check process nginx with pidfile /var/run/nginx.pid group www group nginx start program = "/etc/init.d/nginx start" stop program = "/etc/init.d/nginx stop" if 5 restarts with 5 cycles then timeout depend nginx_bin depend nginx_rc check file nginx_bin with path /usr/sbin/nginx group nginx include /etc/monit/templates/rootbin check file nginx_rc with path /etc/init.d/nginx group nginx include /etc/monit/templates/rootbin