Skip to navigation
Monitoring FreeBSD with Monit
11.11.24
``` pkg install -y monit sysrc monit_enable=yes ``` Create and edit /usr/local/etc/monitrc All production servers need monitoring with email alerts so you know when the server is having trouble or is under heavy load. In the IT solution industry there is no shortage of monitoring and management tools out in the wild. Monit is a small Open Source utility for managing and monitoring Unix systems. Monit conducts automatic maintenance, repair, and can execute meaningful causal actions in error situations. This tutorial will cover locally installing, configuring, and testing Monit. Installing Monit # pkg install -y monit Set Monit to run on startup # sysrc monit_enable=yes Create the monit config. By default the below config example file will monitor CPU, Disk, and Memory usage and trigger an email when the resources are exhausted. Monit can also detect when services such as apache, mysql, postfix, and nginx have stopped and can automatically restart them for you. uncomment the lines in the below configuration file for services that you want to actively monitor. Don’t forget to configure your smtp email settings under #email settings for email alerts and set your hostname on line “check system” # nano /usr/local/etc/monitrc ############################################################################### ## Global section ############################################################################### ## ## Start Monit in the background (run as a daemon): set daemon 30 # check services at 30 seconds intervals with start delay 240 # optional: delay the first check by 4-minutes (by # # default Monit check immediately after Monit start) # # ## Set syslog logging. If you want to log to a standalone log file instead, ## specify the full path to the log file # set logfile /var/log/monit.log # # ## Set the location of the Monit lock file which stores the process id of the ## running Monit instance. By default this file is stored in $HOME/.monit.pid # set pidfile /var/run/monit.pid #email settings set mailserver mail.emailserver.com port 587 username "monit@emailserver.com" password "password" using tlsv1 set alert support@emailserver.com with reminder on 500 cycle #apache # check process apache with pidfile /var/run/apache2/apache2.pid # group www-data # start program = "/usr/local/etc/rc.d/apache24 start" # stop program = "/usr/local/etc/rc.d/apache24 stop" # if failed host example.com port 80 protocol http then restart # if 5 restarts within 5 cycles then timeout #nginx # check process nginx with pidfile /var/run/nginx.pid # start program = "/usr/local/etc/rc.d/nginx start" with timeout 60 seconds # stop program = "/usr/local/etc/rc.d/ nginx stop" # if failed host 127.0.0.1 port 80 protocol http for 3 cycles then restart #mysql # check process mysql with pidfile /var/run/mysqld/mysqld.pid # start program = "/usr/local/etc/rc.d/mysql-server start" # stop program = "/usr/local/etc/rc.d/mysql-server stop" # if failed host 192.168.1.41 port 3306 protocol mysql then restart # if 5 restarts within 5 cycles then timeout #CPU, DISK, Memory, load check system hostname.example.com if loadavg (5min) > 3 for 4 cycles then alert if loadavg (15min) > 1 for 4 cycles then alert if memory usage > 80% for 4 cycles then alert if swap usage > 20% for 4 cycles then alert if cpu usage (user) > 80% for 4 cycles then alert if cpu usage (system) > 20% for 4 cycles then alert if cpu usage (wait) > 20% for 4 cycles then alert check filesystem "root" with path /dev/mfid0s1a if space usage > 80% for 8 cycles then alert Note: Make sure the “check filesystem” line has the correct mount point for your HDD. You can use the below command to determine what if anything to put in place of /dev/sda1. # df -h Filesystem Size Used Avail Capacity Mounted on /dev/sda1 Start Monit # service monit start you can verify everything is running properly by reviewing the Monit log # tail -f /var/log/monit.log [EST Dec 19 14:14:36] info : Starting Monit 5.19.0 daemon [EST Dec 19 14:14:36] info : Monit start delay set to 240s [EST Dec 19 14:18:36] info : 'hostname.example.com' Monit 5.19.0 started Testing: Now lets see if it works, for resource monitor testing we can install stress # pkg install -y stress use stress to exhaust the CPU # stress --cpu 8 -v --timeout 100s If monit is setup correctly you will see this when viewing the Monit log and an email should have also been sent to the address defined on the set alert line You can also use stress to exhaust system memory for Monit testing as well # stress --vm 2 --vm-bytes 1028M If you are monitoring a service such as nginx with monit you can test the reaction by stopping the service # service nginx stop Once monit detects the service has stopped it will start it backup. You can also have monit monitor any log for a sting of text and send an email alert when said string is logged. For example let’s assume that nginx is configured to log errors to /var/log/nginx/access.log, we can set this monitor by adding the below snippet to the end of /usr/local/etc/monitrc. set alert email@emailserver.com with reminder on 500 cycle check file nginxreports.log with path /var/log/nginx/access.log if match "upstream server temporarily disabled" for 2 cycles then alert hope you found this tutorial helpful and informative. Please share and if you are a Medium subscriber clap to support my content.
https://medium.com/@tdebarbora/monitoring-freebsd-with-monit-9ab9493d69bf
Reply
Anonymous
Information Epoch 1770458986
Data dominates.
Home
Notebook
Contact us