Install and Use Monit on AlmaLinux 8 | Easy Monitoring Tool

Posted on

Install and Use Monit on AlmaLinux 8 | Easy Monitoring Tool

Install and Use Monit on AlmaLinux 8 | Easy Monitoring Tool

This tutorial will guide you through the process of how to Install and Use Monit on AlmaLinux 8. Monit is a powerful, free, and open-source tool that allows you to manage and monitor your system’s resources and services. It provides a web interface for easy access and control. With Monit, you can automatically restart failing services, monitor system resources like CPU usage, memory consumption, and disk space, and receive alerts when issues arise.

You can follow this guide on the Install and Use Monit on AlmaLinux 8 and begin utilizing its features by adding services to your Monit manager and observing comprehensive information about your system’s hardware.

Before you begin the process to Install and Use Monit on AlmaLinux 8, ensure you have the following prerequisites: access to your server as a non-root user with sudo privileges and a basic firewall configured. Refer to the Initial Server Setup with AlmaLinux 8 guide for assistance with these initial steps.

Now, let’s proceed with the steps to Install and Use Monit on AlmaLinux 8.

Step 1 – Install Required Packages for Monit

First, update your system’s package repositories using the following command:

sudo dnf update -y

Next, install the necessary packages and dependencies required for Monit to function correctly. Run the following command:

sudo dnf install zlib-devel pam-devel openssl-devel libtool bison flex autoconf gcc make git epel-release -y

This command installs essential development tools and libraries, including zlib-devel, pam-devel, openssl-devel, libtool, bison, flex, autoconf, gcc, make, git, and enables the Extra Packages for Enterprise Linux (EPEL) repository.

Step 2 – Install and Start Monit on AlmaLinux 8

With the prerequisites installed, you can now install Monit on your AlmaLinux 8 system. Execute the following command:

sudo dnf install monit -y

Once the installation is complete, start the Monit service using the following command:

sudo monit

You should see output similar to the following:

**Output**
New Monit id: c1e1960b7fe7b88cbbd8949636394945
Stored in '/root/.monit.id'
Starting Monit 5.30.0 daemon with http interface at [localhost]:2812

To verify the status of the Monit service, use the following command:

sudo monit status

The output will display detailed information about the system’s status, including CPU usage, memory consumption, uptime, and more:

**Output**
Monit 5.30.0 uptime: 1m

System 'alma.linux'
  status                       OK
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  load average                 [0.06] [0.09] [0.04]
  cpu                          0.2%usr 0.4%sys 0.0%nice 0.0%iowait 0.6%hardirq 0.8%softirq 2.2%steal 0.0%guest 0.0%guestnice
  memory usage                 405.7 MB [10.9%]
  swap usage                   0 B [0.0%]
  uptime                       21h 45m
  boot time                    Sat, 05 Aug 2023 05:42:34
  filedescriptors              832 [0.2% of 377358 limit]
  data collected               Sun, 06 Aug 2023 03:27:20

To ensure that the Monit service starts automatically on boot, enable it using the following command:

sudo systemctl enable monit

Step 3 – How To Configure Monit on AlmaLinux 8?

The primary configuration file for Monit is located at /etc/monitrc. You’ll need to modify this file to configure Monit according to your specific needs.

Enable Monit HTTPD Port

By default, the Monit web interface is accessible only from localhost on port 2812. To enable remote access, you need to modify the /etc/monitrc file. Open the file using your preferred text editor (e.g., vi):

sudo vi /etc/monitrc

Locate the line starting with set httpd port 2812. Modify the use address and allow values to enable remote access. It’s crucial to understand the security implications of opening up your Monit interface to remote access. Restrict access only to trusted networks or IP addresses. You can also change the default username and password for the admin user.

set httpd port 2812 and
use address 0.0.0.0 # only accept connection from localhost (drop if you use M/M)
allow 0.0.0.0/0 # allow localhost to connect to the server and
allow admin:monit # require user 'admin' with password 'monit'

Save the changes and close the file. To verify the Monit configuration, use the following command:

sudo monit -t

If the configuration is valid, you’ll see the following output:

**Output**
Control file syntax OK

Step 4 – Configure Firewall for Monit

Allow traffic on port 2812 through the firewall. This is necessary to access the Monit web interface from remote locations. Run the following commands:

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

Finally, restart the Monit service to apply the changes:

sudo systemctl restart monit

Step 5 – Access Monit Service Manager through Web Interface

Now, you can access the Monit web interface by navigating to your server’s IP address followed by port 2812 in your web browser:

http://<your-server-ip-address>:2812

You’ll be prompted to enter the username and password you configured in the /etc/monitrc file. After successful authentication, you’ll be presented with the Monit service manager interface.

[Image of Monit Sign-in Screen]

[Image of Monit Service Manager Interface]

Step 6 – Add Services In Monit on AlmaLinux 8

To monitor specific services, you need to create configuration files for them in the /etc/monit.d directory.

For example, to monitor the Nginx web server, create a file named /etc/monit.d/nginx-monitor:

sudo vi /etc/monit.d/nginx-monitor

Add the following configuration to the file:

check process nginx with pidfile /run/nginx.pid
start program "/usr/bin/systemctl start nginx.service"
stop program "/usr/bin/systemctl stop nginx.service"
if failed port 80 protocol http then restart

This configuration tells Monit to monitor the Nginx process based on its PID file, define commands to start and stop the service, and restart the service if it fails to respond to HTTP requests on port 80.

Save the changes and close the file. Then, check the Monit configuration syntax:

sudo monit -t
**Output**
Control file syntax OK

Finally, reload the Monit service to apply the new configuration:

sudo monit reload
**Output**
Reinitializing monit daemon

The newly added service will now be displayed in the Monit service manager interface.

[Image of Monit Service List with Nginx Added]

For more detailed information and advanced configurations, refer to the Monit Docs.

Conclusion

You have successfully learned how to Install and Use Monit on AlmaLinux 8 and add services to your Monit Manager. This provides a robust foundation for monitoring and managing your server environment.

You might also be interested in these articles:

How to install Virtualizor on AlmaLinux

Set up ProFTPD on AlmaLinux 9

Install Latest LibreOffice on AlmaLinux 8 and RHEL 8

Alternative Solutions for System Monitoring on AlmaLinux 8

While Monit is a solid choice, here are two alternative solutions for system monitoring on AlmaLinux 8, along with explanations and code examples:

1. Systemd Built-in Monitoring:

Systemd, the system and service manager, has built-in capabilities that can perform basic monitoring and service management tasks. While not as feature-rich as Monit, it’s already integrated into AlmaLinux 8, eliminating the need for additional software installation.

Explanation:

Systemd can automatically restart services that fail. This is achieved using the Restart= and RestartSec= directives in the service unit file. The Restart= directive specifies under which conditions a service should be restarted (e.g., on-failure, on-abort, always). The RestartSec= directive defines the delay before the restart attempt. Systemd also keeps logs of service status, which can be reviewed with journalctl. This can be sufficient for basic error monitoring.

Code Example:

Edit the service unit file for the service you want to monitor (e.g., /etc/systemd/system/nginx.service):

sudo vi /etc/systemd/system/nginx.service

Add the following lines to the [Service] section:

[Service]
Restart=on-failure
RestartSec=5s

This configuration will restart the Nginx service automatically if it fails, with a 5-second delay between restart attempts. After modifying the file, reload the systemd daemon and restart the service:

sudo systemctl daemon-reload
sudo systemctl restart nginx

To check the logs for nginx, use the following command:

journalctl -u nginx.service

2. Prometheus and Grafana:

Prometheus is a powerful open-source monitoring and alerting toolkit designed for collecting and processing metrics from various sources. Grafana is an open-source data visualization and monitoring suite that works seamlessly with Prometheus to create dashboards and visualizations.

Explanation:

Prometheus excels at collecting time-series data from your system and applications. You can install exporters (like node_exporter for system metrics or specialized exporters for specific applications) on your AlmaLinux 8 server. These exporters expose metrics in a format Prometheus can scrape. Grafana connects to the Prometheus server and allows you to create highly customizable dashboards to visualize the collected data. Alerting can be configured in Prometheus based on metric thresholds.

Code Example:

First, install Prometheus and Grafana. A comprehensive guide is beyond the scope of this article, but you can find detailed instructions on the official Prometheus and Grafana websites.

After installing Prometheus, install node_exporter to collect system metrics:

sudo dnf install prometheus-node-exporter
sudo systemctl enable prometheus-node-exporter
sudo systemctl start prometheus-node-exporter

Configure Prometheus to scrape the node_exporter by adding the following to your prometheus.yml configuration file (usually located in /etc/prometheus/):

scrape_configs:
  - job_name: 'node'
    static_configs:
      - targets: ['localhost:9100']

Restart Prometheus:

sudo systemctl restart prometheus

Now, you can import a pre-built dashboard for node_exporter in Grafana (search for "Node Exporter Full" dashboard). This dashboard will display various system metrics, such as CPU usage, memory consumption, disk I/O, and network traffic. You can customize the dashboard to suit your specific monitoring needs. This is a more complex solution, but provides far more flexible and powerful monitoring than Monit.