大家好,欢迎来到IT知识分享网。
二、Monit的安装
yum install -y monit
也可以通过源码安装
可以通过如下命令查看monit版本:
monit -V
三、常用命令
monit -t #配置文件检测 monit #启动monit daemon monit -c /var/monit/monitrc # 启动monit daemon 时 指定配置文件 monit reload # 当更新了配置文件需要重载 monit status # 查看所有服务状态 monit status nginx # 查看nginx服务状态 monit stop all # 停止所有服务 monit stop nginx # 停止nginx服务 monit start all # 启动所有服务 monit start nginx # 启动nginx服务 monit -V # 查看版本
/etc/monitrc # 主配置文件 /etc/monit.d/ # 单独配置各项服务
注意:为了保护控制文件和密码的安全性,monitrc必须具有读写权限不超过0700(u=rwx,g=0,o=0)
1、主配置文件主要配置全局
cat /etc/monitrc
Global section set daemon 30 // 设置monit作为守护进程运行,每30秒监视一次 set logfile syslog // 设置日志文件为syslog # 邮箱服务器设置 set mailserver xxx@xxx username "xxx" password "xxx" # using ssl set alert xxx@xxx set alert xxx@xxx #可以设置多个 set mail-format {
from: xxx@xxx subject: [$SERVICE] $EVENT message: [$SERVICE] $EVENT Date: $DATE Action: $ACTION Host: $HOST Description: $DESCRIPTION Your faithful employee, Monit } # 设置web服务认证 set httpd port 2812 and # ssl enable # pemfile /etc/certs/monit.pem # use address all # only accept connection from localhost allow 127.0.0.1 # 允许localhost连接 allow admin:monit # web登录的用户名和密码 Services allow // 允许连接的主机IP,或网段 allow username:password // 设置页面监控访问的用户名和密码 Includes include /etc/monit.d/*
vi /etc/monit.d/nginx.monit
check process nginx with pidfile /usr/local/nginx/nginx.pid start program = "/usr/local/nginx/sbin/nginx" stop program = "/usr/local/nginx/sbin/nginx -s stop" if failed host 192.168.1.11 port 8011 protocol http then restart
检查配置是否正常
monit -t
出现如图所示,Control file syntax OK,说明正常
3、启动monit
monit
4、启动所有服务或者单个服务
monit start all
5、若修改了配置文件,重载配置
monit reload
6、使用下面命令查看监控状态
monit status
控制台输出:
$ monit status The Monit daemon 5.17.1 uptime: 4d 15h 45m Process 'nginx' status Running monitoring status Monitored pid 20563 parent pid 1 uid 0 effective uid 0 gid 0 uptime 3d 22h 36m threads 1 children 2 memory 820 kB memory total 7.3 MB memory percent 0.0% memory percent total 0.2% cpu percent 0.0% cpu percent total 0.0% data collected Sat, 18 Feb 2017 10:09:56 System 'iZ28s4jxu17Z' status Running monitoring status Monitored load average [0.03] [0.06] [0.06] cpu 8.4%us 0.8%sy 0.2%wa memory usage 1.5 GB [41.2%] swap usage 0 B [0.0%] data collected Sat, 18 Feb 2017 10:09:56
或者浏览器输入http://localhost:2812登录网页版查看实时状态。
alert foo@bar
如果您希望在服务保持处于失败状态时每十个周期通知一次,您可以使用:
alert foo@bar with reminder on 10 cycles
同样,如果您想在每个失败的周期获得通知,您可以使用:
alert foo@bar with reminder on 1 cycle
要禁止某些用户和服务的警报,可以在服务检查的局部配置里添加语句:
noalert mail-address
CHECK PROCESS <unique name> <PIDFILE <path> | MATCHING <regex>>
2、文件
CHECK FILE <unique name> PATH <path>
3、Fifo
CHECK FIFO <unique name> PATH <path>
4、文件系统
CHECK FILESYSTEM <unique name> PATH <path>
5、目录
CHECK DIRECTORY <unique name> PATH <path>
6、远程主机
CHECK HOST <unique name> ADDRESS <host address>
7、系统
CHECK SYSTEM <unique name>
8、网络
CHECK NETWORK <unique name> <ADDRESS <ipaddress> | INTERFACE <name>>
9、自定义
CHECK PROGRAM <unique name> PATH <executable file> [TIMEOUT <number> SECONDS]
check host <service> address <address or ip> if failed xxx then alert alert xx@xxx
if failed xxx then alert
示例演示:
cat /etc/monit.d/test
system check system $HOST if loadavg (1min) > 4 then alert if loadavg (5min) > 2 then alert if cpu usage > 95% for 10 cycles then alert if memory usage > 75% then alert if swap usage > 25% then alert file check file apache_bin with path /usr/local/apache/bin/httpd if failed checksum and expect the sum 8f7fcefa0b33a2ba316cba3659 then unmonitor if failed permission 755 then unmonitor if failed uid root then unmonitor if failed gid root then unmonitor alert security@foo.bar on {
checksum, permission, uid, gid, unmonitor } with the mail-format {
subject: Alarm! } group server process check process apache with pidfile /usr/local/apache/logs/httpd.pid start program = "/etc/init.d/httpd start" with timeout 60 seconds stop program = "/etc/init.d/httpd stop" if cpu > 60% for 2 cycles then alert if cpu > 80% for 5 cycles then restart if totalmem > 200.0 MB for 5 cycles then restart if children > 250 then restart if loadavg(5min) greater than 10 for 8 cycles then stop if failed host www.tildeslash.com port 80 protocol http and request "/somefile.html" then restart if failed port 443 protocol https with timeout 15 seconds then restart if 3 restarts within 5 cycles then unmonitor depends on apache_bin group server filesystem check filesystem datafs with path /dev/sdb1 start program = "/bin/mount /data" stop program = "/bin/umount /data" if failed permission 660 then unmonitor if failed uid root then unmonitor if failed gid disk then unmonitor if space usage > 80% for 5 times within 15 cycles then alert if space usage > 99% then stop if inode usage > 30000 then alert if inode usage > 99% then stop group server file's timestamp check file database with path /data/mydatabase.db if failed permission 700 then alert if failed uid data then alert if failed gid data then alert if timestamp > 15 minutes then alert if size > 100 MB then exec "/my/cleanup/script" as uid dba and gid dba directory permission check directory bin with path /bin if failed permission 755 then unmonitor if failed uid 0 then unmonitor if failed gid 0 then unmonitor remote host check host myserver with address 192.168.1.1 if failed ping then alert if failed port 3306 protocol mysql with timeout 15 seconds then alert if failed port 80 protocol http and request /some/path with content = "a string" then alert network link status check network public with interface eth0 if failed link then alert if changed link then alert if saturation > 90% then alert if download > 10 MB/s then alert if total upload > 1 GB in last hour then alert custom program status output check program myscript with path /usr/local/bin/myscript.sh if status != 0 then alert
启动
monit
echo "moni:2345:respawn:/usr/local/monit-5.10/bin/monit -Ic /etc/monitrc" >> /etc/inittab
monit stop tomcat
要停止全部monit所监视的服务可以使用:
monit stop all
启动全部,则是
monit start all
End~
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/112011.html


