How to install and configure monit in Rocky Linux 8

Monit is a free, open-source process supervision tool for Unix and Linux. With Monit, system status can be viewed directly from the command line, or via the native HTTP web server. Monit is able to do automatic maintenance, repair, and run meaningful causal actions in error situations. 

In this guide we will learn how to install and configure monit on a Rocky Linux 8 server. If you are interested in setting up M/Monit, checkout How to install and set up M/Monit in Linux.

These are some of the features of Monit.

# Proactive

Monit can act if an error situation should occur, e.g.; if sendmail is not running, Monit can start sendmail again automatically or if apache is using too much resources (e.g. if a DoS attack is in progress) Monit can stop or restart apache and send you an alert message. Monit can also monitor process characteristics, such as; how much memory or cpu cycles a process is using.

# Processes

You can use Monit to monitor daemon processes or similar programs running on localhost. Monit is particularly useful for monitoring daemon processes, such as those started at system boot time from /etc/init/ For instance sendmail, sshd, apache and mysql.

# Files, Dirs and Filesystems

You can also use Monit to monitor files, directories and filesystems on localhost. Monit can monitor these items for changes, such as timestamps changes, checksum changes or size changes. This is also useful for security reasons – you can monitor the md5 or sha1 checksum of files that should not change and get an alert or perform an action if they should change.

# Cloud and Hosts

Monitor network connections to various servers, either on localhost or on remote hosts. TCP, UDP and Unix Domain Sockets are supported. Network tests can be performed on a protocol level; Monit has built-in tests for the main Internet protocols, such as HTTP, SMTP etc. Even if a protocol is not supported you can still test the server as you can configure Monit to send any data and test the response from the server.

# Programs and scripts

Monit can be used to test programs or scripts at certain times, much like cron, but in addition, you can test the exit value of a program and perform an action or send an alert if the exit value indicates an error. This means that you can use Monit to perform any type of check you can write a script for.

# System

Finally, Monit can be used to monitor general system resources on localhost such as overall CPU usage, Memory and Load Average.

# Step 1: Installing Monit

Monit is available in the default Rocky Linux repositories. Use this command to install?

sudo dnf install -y monit

If you want to install the latest version, use the downloads page here.

# Step 2: Starting and enabling service

Once installed, we need to start the monit service. Use this command to start:

sudo systemctl start monit

Check the status to confirm that the service is running using this command:

$ sudo systemctl status monit
● monit.service - Pro-active monitoring utility for unix systems
   Loaded: loaded (/usr/lib/systemd/system/monit.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2022-03-31 15:37:35 UTC; 6s ago
     Docs: man:monit(1)
 Main PID: 251683 (monit)
    Tasks: 2 (limit: 23465)
   Memory: 2.1M
   CGroup: /system.slice/monit.service
           └─251683 /usr/bin/monit -I

Mar 31 15:37:35 monitoring systemd[1]: Started Pro-active monitoring utility for unix systems.

Finally, enable monit on boot:

sudo systemctl enable monit

# Step 3: Configuring monit service

# Enable monit web interface

Monit comes with user friendly-web interface to view the system status and manage them through the web browser. By default, Monit web interface is disabled. You can enable it by editing /etc/monit/monitrc configuration file:

sudo vim /etc/monitrc

Make the following changes. The allow directive will require user ‘admin’ with password ‘monit’ for the web login.

set httpd port 2812 
    allow admin:monit

# Set up M/Monit host

M/monit provides a central view of all mmonit data. It is helpful when you are managing muliple servers and do not want to login to each of them to check the status. The format of the url is http://[username]:[password]@[host]:[port]/collector.

Use this command in monitrc to configure mmonit host:

set mmonit http://monit:monit@

Final monit configuration

set daemon 30
set log syslog
set idfile /var/
set statefile /var/.monit.state
set eventqueue
    basedir /var/lib/monit/events
    slots 100
set httpd port 2812 and
   use address #only accept connection from localhost
   allow admin:monit
set mmonit http://monit:monit@

include /etc/monit.d/*

# Restart mmonit to apply changes

Save the file, then restart Monit service to apply the changes:

sudo systemctl restart monit

# Step 4: Allow service through firewall

If you have firewalld installed and enabled, use this command to whielist monit port for external access.

sudo firewall-cmd --permanent --add-port=2812/tcp
sudo firewall-cmd --reload

# Step 5: Accessing the web interface

Open your web browser and type the URL http://[your_servr_ip]:2812. You will be redirected to the Monit login page. Provide monit username as admin and password as monit, then click on the Sign In button. You should see the Monit default dashboard.

# Step 6: Configure monit for a service with pid

The line include /etc/monit.d/* in the file /etc/monitrc includes all defined configurations in monit.d directory. It is recommended to include individual configurations there.

We will use grafana-server as a example. We will need to create the configuration file for grafana-server. You can do this with the following command:

sudo vim /etc/monit.d/grafana-server

Then add this content:

check process grafana-server with pidfile /var/run/grafana/
   start program = "/bin/systemctl start grafana-server" with timeout 15 seconds
   stop  program = "/bin/systemctl stop grafana-server"
if failed port 3000 protocol http then restart

Next, verify the Monit status with the following command:

monit -t


$ sudo monit -t
Control file syntax OK

After resolving any possible syntax errors, you can start running all of the monitored programs.

monit start all

Next, restart the Monit service to apply the changes:

sudo systemctl reload monit

You can see the new service on the Monit web interface. Or you can use the status command in the terminal:

$ sudo monit status
Monit 5.30.0 uptime: 12h 18m

Process 'grafana-server'
  status                       OK
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  pid                          43609
  parent pid                   1
  uid                          989
  effective uid                989
  gid                          986
  uptime                       22d 11h 11m
  threads                      11
  children                     0
  cpu                          0.0%
  cpu total                    0.0%
  memory                       2.7% [102.2 MB]
  memory total                 2.7% [102.2 MB]
  security attribute           system_u:system_r:unconfined_service_t:s0
  filedescriptors              13 [0.1% of 10000 limit]
  total filedescriptors        13
  read bytes                   216.2 B/s [770.4 MB total]
  disk read bytes              0 B/s [2.3 MB total]
  disk read operations         2.1 reads/s [4070127 reads total]
  write bytes                  24.8 B/s [331.1 MB total]
  disk write bytes             136.5 B/s [167.5 MB total]
  disk write operations        1.8 writes/s [3258928 writes total]
  port response time           1.485 ms to localhost:3000 type TCP/IP protocol HTTP
  data collected               Fri, 01 Apr 2022 03:57:26

System 'monitoring'
  status                       OK
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  load average                 [0.04] [0.02] [0.00]
  cpu                          1.1%usr 0.6%sys 0.0%nice 0.0%iowait 0.2%hardirq 0.1%softirq 0.1%steal 0.0%guest 0.0%guestnice
  memory usage                 1.1 GB [31.1%]
  swap usage                   0 B [0.0%]
  uptime                       22d 13h 10m
  boot time                    Wed, 09 Mar 2022 14:47:17
  filedescriptors              2496 [0.7% of 374156 limit]
  data collected               Fri, 01 Apr 2022 03:57:26

# Step 7: Monitor local system

We can also use monit to monitor and alert on local system resource usage. This file monitors for load average, memory, swap and cpu usage and alerts on thresholds defined.

check system $HOST
    if loadavg (1min) > 10 then alert
    if loadavg (5min) > 2 then alert
    if memory usage > 75% then alert
    if swap usage > 25% then alert
    if cpu usage (user) > 70% then alert
    if cpu usage (system) > 30% then alert
    if cpu usage (wait) > 20% then alert

# Conclusion

Congratulations! You have successfully installed Monit monitoring tool on your rocky linux server.

Last updated on Mar 20, 2024 17:19 +0300
comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy