You can use several solution. One of them is monit
.
From command line, just run the following command:
sudo apt-get install monit
Monit should now be installed and accessible through one of the following URLs:
http://localhost:2812
http://IPADDRESS:2812 (local network IP)
http://domain.com:2812 (if you have domain name pointing to your server)
Monit Configuration
Before you can start using Monit
for automatic server monitoring, you have to do some basic configuration. First backup the existing default Monit
configuration using the following command:
sudo mv /etc/monit/monitrc /etc/monit/monitrc.bak
Next, create a new monitrc
file using the command
sudo /etc/monit/monitrc
and copy the following contents to it.
# How often in seconds should monit check your services.
set daemon 120
set logfile /var/log/monit.log
set idfile /var/lib/monit/id
set statefile /var/lib/monit/state
# Configure your SMTP out server.
set mailserver smtp-server.columbus.rr.com port 25,localhost
set eventqueue
basedir /var/lib/monit/events # set the base directory where events will be stored
# optionally limit the queue size
slots 100
# Use one of the following 2 lines. The second line alerts on every little change and can be annoying.
set alert admin@domain.com but not on { instance, pid, ppid } #does not send alert on pid changes
#set alert admin@domain.com
set httpd port 2812 and
#Change username and password
allow Username:Password
# To enable SSL for WebUI uncomment the next 2 lines
#ssl enable
#pemfile /path/to/unified/certificate.pem
# To restrict access to localhost only uncomment the following line
#allow localhost
include /etc/monit/conf.d/*
Lines that begin with a # are comments to help you customize the configuration. Make sure you have at least the admin email, SMTP server, and SMTP port configured correctly. You should also consider changing Monit WebUI username and password. If you have an SSL certificate you can enable that as well for HTTPS access
For now use the following commands to ensure Monit is working well.
To test Monit
configuration for syntax errors:
sudo monit -t
To start Monit:
sudo monit
To check Monit Status:
sudo monit status
System Load Monitoring with Monit
It is required that you have a working Monit instance with a proper /etc/monit/monitrc
file. Monit configurations for various services are loaded from /etc/monit/conf.d
folder. To monitor server load with Monit
, create a Monit
configuration file using the following command:
sudo /etc/monit/conf.d/systemload
Copy the following contents to it, save, and exit
# domain.com could be IP, hostname, or localhost
check System domain.com
if loadavg (1min) > 4 then alert
if loadavg (5min) > 2 then alert
if memory usage > 75% then alert
if swap usage > 25% then alert
if cpu usage (user) > 80% then alert
if cpu usage (system) > 30% then alert
if cpu usage (wait) > 20% then alert
This code will make Monit
send you an email alert when one of the above conditions (eg. average load is >4
for at least 1 min or when more than 75% RAM
is full) are met. You can customize the above rules as you please. Below is an example email alert sent by Monit
along with a description of what condition caused the alert.
Test and Reload Monit
Once you make any changes you have to test Monit
configuration:
sudo monit -t
You should see the following message: Control File Syntax OK.
Then, check to see if Monit
is already running using the following command:
sudo /etc/init.d/monit status
If Monit
is running, reload Monit
configurations using the following command:
sudo /etc/init.d/monit reload
Now, fire up your web browser and visit one of the following URLs depending on how your Monit is configured (be sure to use the correct port number):
http://localhost:2812
http://IPADDRESS:2812 (local network IP)
http://domain.com:2812 (if you have domain name pointing to your server)
You should see the system status, load, CPU load, Memory Load, and Swap load
Storage Monitoring with Monit
Next, it is required that you have a working Monit
instance with a proper /etc/monit/monitrc
file. Monit
configurations for various services are loaded from /etc/monit/conf.d
folder. For drive space monitoring with Monit, create a
Monit` configuration file using the following command:
sudo /etc/monit/conf.d/storagespace
Copy the following contents to it, save, and exit
# add each drive you want to monitor below
check filesystem Ubuntu with path /dev/sda1
if space usage > 90% then alert
check filesystem Home with path /dev/sda3
if space usage > 90% then alert
check filesystem Media with path /dev/sdb1
if space usage > 90% then alert
The code above code will monitor hard drive space in 3 partitions in 2 drives. If any of them is more than 90% full you will got alert.
Also you can monitoring a specific services.
Monitor Apache Server with Monit
Setting up Apache server monitoring with Monit
is with Monit’s
pre-made configuration templates. All you have to do is copy the existing template from /etc/monit/monitrc.d
to /etc/monit/conf.d
folder.
sudo cp /etc/monit/monitrc.d/apache2 /etc/monit/conf.d/
Instead of copying, you may also create a symbolic link. Apache web server process creates apache2.pid
. The above code monitor apache2.pid
file and if does not exist, Monit
will try to restart Apache. A restart will trigger an email alert. If restart fails multiple times then Monit stops monitoring Apache server.
...