Introduction :
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(S) web server. Monit is able to do automatic maintenance, repair, and run meaningful causal actions in error situations.
Here's an overview of Monit and how it works:
What is Monit?
Monit is a small, open-source, and lightweight monitoring tool used primarily on Unix-based systems (Linux, macOS, BSD). It’s designed to:
- Monitor system processes (e.g., web servers, databases).
- Check resource usage (e.g., CPU, memory).
- Watch files and directories for changes.
- Restart failed services automatically to ensure high availability.
- Send alerts via email or other methods when a service fails or when certain thresholds are exceeded.
Why Use Monit on AWS EC2?
AWS EC2 instances can run various services and applications that need constant monitoring. While AWS offers CloudWatch for monitoring resources like CPU, memory, and disk usage, Monit provides more granular control for monitoring services like web servers (Nginx, Apache), databases (MySQL, PostgreSQL), and other background processes. Some key features include:
using monit automatic service restarts when services fail or stop.
Customizable monitoring thresholds for CPU, memory, disk space, and more.
Real-time alerts for failures or resource usage violations.
Prerequisites
- An AWS EC2 instance running any supported Linux AMI (Amazon Linux 2, Ubuntu, etc.).
- Basic familiarity with Linux command-line tools and SSH access to the EC2 instance.
Step 1: Launch an EC2 Instance with a Suitable AMI
- Log in to your AWS Management Console and navigate to EC2.
- Select Launch Instance.
- Choose an Amazon Machine Image (AMI), such as Amazon Linux 2023 or Ubuntu (based on your preference).
- Configure instance details, add storage, configure security groups (ensure SSH access), and launch the instance.
- After the instance is launched, make a note of the Public IP or Public DNS to SSH into the instance.
Step 2: SSH into Your EC2 Instance
- Once your EC2 instance is running, SSH into the instance using the following command (replace your-key.pem and your-ec2-ip with your actual private key and EC2 IP address):
ssh -i your-key.pem ec2-user@your-ec2-ip
Step 3: Install Monit on Your EC2 Instance
- Monit can be installed using your system's package manager. Follow the steps based on your EC2 instance's AMI.
- Monit Packages : https://mmonit.com/monit/dist/
wget https://mmonit.com/monit/dist/monit-5.34.3.tar.gz
- You can also create an RPM package for CentOS/RHEL/Fedora from the source code directly using rpmbuild:
rpmbuild -tb monit-x.y.z.tar.gz
$ tar zxvf monit-x.y.z.tar.gz (where x.y.z denotes version numbers)
$ cd monit-x.y.z
$ ./configure (use ./configure --help to view available options)
$ make && make install
- ./configure output :
[root@ip-172-31-11-168 monit-5.34.3]# ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a race-free mkdir -p... /usr/bin/mkdir -p
........
........
config.status: executing libtool commands
+------------------------------------------------------------+
| License: |
| This is Open Source Software and use is subject to the GNU |
| AFFERO GENERAL PUBLIC LICENSE version 3, available in this |
| distribution in the file COPYING. |
| |
| By continuing this installation process, you are bound by |
| the terms of this license agreement. If you do not agree |
| with the terms of this license, you must abort the |
| installation process at this point. |
+------------------------------------------------------------+
| Libmonit is configured as follows: |
| |
| Optimized: DISABLED |
| Profiling: DISABLED |
| Compression: ENABLED |
+------------------------------------------------------------+
Monit Build Information:
Architecture: LINUX
SSL include directory: /usr/include
SSL library directory: /lib64
Compiler flags: -g -O2 -Wextra -fstack-protector-all -D_GNU_SOURCE -Wall -Wunused -std=c11 -D _REENTRANT -I/usr/include -I/usr/include
Linker flags: -lpam -lz -lpthread -lcrypt -lresolv -L/lib64 -lssl -lcrypto -L/lib64
pid file location: /run
Install directory: /usr/local
+------------------------------------------------------------+
| License: |
| This is Open Source Software and use is subject to the GNU |
| AFFERO GENERAL PUBLIC LICENSE version 3, available in this |
| distribution in the file COPYING. |
| |
| By continuing this installation process, you are bound by |
| the terms of this license agreement. If you do not agree |
| with the terms of this license, you must abort the |
| installation process at this point. |
+------------------------------------------------------------+
| Monit has been configured with the following options: |
| |
| Compression: ENABLED |
| PAM support: ENABLED |
| SSL support: ENABLED |
| Large files support: ENABLED |
| ASAN support: DISABLED |
| IPv6 support: ENABLED |
| Optimized: DISABLED |
| Profiling: DISABLED |
+------------------------------------------------------------+
- make output :
[root@ip-172-31-11-168 monit-5.34.3]# make
make all-recursive
make[1]: Entering directory '/root/rpmbuild/BUILD/monit-5.34.3'
Making all in libmonit
make[2]: Entering directory '/root/rpmbuild/BUILD/monit-5.34.3/libmonit'
Making all in .
make[3]: Entering directory '/root/rpmbuild/BUILD/monit-5.34.3/libmonit'
make[3]: Nothing to be done for 'all-am'.
make[3]: Leaving directory '/root/rpmbuild/BUILD/monit-5.34.3/libmonit'
Making all in test
make[3]: Entering directory '/root/rpmbuild/BUILD/monit-5.34.3/libmonit/test'
make[3]: Nothing to be done for 'all'.
make[3]: Leaving directory '/root/rpmbuild/BUILD/monit-5.34.3/libmonit/test'
make[2]: Leaving directory '/root/rpmbuild/BUILD/monit-5.34.3/libmonit'
make[2]: Entering directory '/root/rpmbuild/BUILD/monit-5.34.3'
make[2]: Nothing to be done for 'all-am'.
make[2]: Leaving directory '/root/rpmbuild/BUILD/monit-5.34.3'
make[1]: Leaving directory '/root/rpmbuild/BUILD/monit-5.34.3'
- make install output :
[root@ip-172-31-11-168 monit-5.34.3]# make install
make install-recursive
make[1]: Entering directory '/root/rpmbuild/BUILD/monit-5.34.3'
Making install in libmonit
make[2]: Entering directory '/root/rpmbuild/BUILD/monit-5.34.3/libmonit'
Making install in .
make[3]: Entering directory '/root/rpmbuild/BUILD/monit-5.34.3/libmonit'
make[4]: Entering directory '/root/rpmbuild/BUILD/monit-5.34.3/libmonit'
make[4]: Nothing to be done for 'install-exec-am'.
make[4]: Nothing to be done for 'install-data-am'.
make[4]: Leaving directory '/root/rpmbuild/BUILD/monit-5.34.3/libmonit'
make[3]: Leaving directory '/root/rpmbuild/BUILD/monit-5.34.3/libmonit'
Making install in test
make[3]: Entering directory '/root/rpmbuild/BUILD/monit-5.34.3/libmonit/test'
make[4]: Entering directory '/root/rpmbuild/BUILD/monit-5.34.3/libmonit/test'
make[4]: Nothing to be done for 'install-exec-am'.
make[4]: Nothing to be done for 'install-data-am'.
make[4]: Leaving directory '/root/rpmbuild/BUILD/monit-5.34.3/libmonit/test'
make[3]: Leaving directory '/root/rpmbuild/BUILD/monit-5.34.3/libmonit/test'
make[2]: Leaving directory '/root/rpmbuild/BUILD/monit-5.34.3/libmonit'
make[2]: Entering directory '/root/rpmbuild/BUILD/monit-5.34.3'
make[3]: Entering directory '/root/rpmbuild/BUILD/monit-5.34.3'
/usr/bin/mkdir -p '/usr/local/bin'
/bin/sh ./libtool --mode=install /usr/bin/install -c monit '/usr/local/bin'
libtool: install: /usr/bin/install -c monit /usr/local/bin/monit
/usr/bin/mkdir -p '/usr/local/share/man/man1'
/usr/bin/install -c -m 644 monit.1 '/usr/local/share/man/man1'
make[3]: Leaving directory '/root/rpmbuild/BUILD/monit-5.34.3'
make[2]: Leaving directory '/root/rpmbuild/BUILD/monit-5.34.3'
make[1]: Leaving directory '/root/rpmbuild/BUILD/monit-5.34.3'
- Monit basic command
Optional commands are as follows:
start all - Start all services
start <name> - Only start the named service
stop all - Stop all services
stop <name> - Stop the named service
restart all - Stop and start all services
restart <name> - Only restart the named service
monitor all - Enable monitoring of all services
monitor <name> - Only enable monitoring of the named service
unmonitor all - Disable monitoring of all services
unmonitor <name> - Only disable monitoring of the named service
reload - Reinitialize monit
status [name] - Print full status information for service(s)
summary [name] - Print short status information for service(s)
report [up|down|..] - Report state of services. See manual for options
quit - Kill the monit daemon process
validate - Check all services and start if not running
procmatch <pattern> - Test process matching pattern
Output:
1. monit --version
[root@ip-172-31-11-168 tmp]# monit --version
This is Monit version 5.34.3
Built with ssl, with ipv6, with compression, with pam and with large files
Copyright (C) 2001-2024 Tildeslash Ltd. All Rights Reserved.
2.Test Monit
[root@ip-172-31-11-168 monit-5.34.3]# monit status
Monit 5.34.3 uptime: 57m
System 'ip-172-31-11-168.ap-south-1.compute.internal'
status OK
monitoring status Monitored
monitoring mode active
on reboot start
load average [0.00] [0.00] [0.00]
cpu 0.2%usr 0.1%sys 0.0%nice 0.1%iowait 0.0%hardirq 0.0%softirq 3.6%steal 0.0%guest 0.0%guestnice
memory usage 375.2 MB [39.5%]
swap usage 0 B [0.0%]
uptime 1h 8m
boot time Sun, 05 Jan 2025 03:28:13
filedescriptors 1248 [0.0% of 9223372036854775807 limit]
data collected Sun, 05 Jan 2025 04:37:04
Step 4: Troubleshooting
- If Monit is not behaving as expected, you can check the logs to troubleshoot:
sudo tail -f /var/log/monit.log
Conclusion
By setting up Monit on your AWS EC2 AMI, you gain the ability to monitor critical services and system resources in real time. Monit’s lightweight nature and easy-to-configure alerting system make it an excellent tool for ensuring that your AWS infrastructure remains healthy and operational. Additionally, with features like service restarts and resource usage thresholds, Monit will help automate the maintenance of your EC2 instance and keep services running smoothly.
Top comments (0)