Skip to content

Instantly share code, notes, and snippets.

@yourdesigncoza
Created April 19, 2013 15:59
Show Gist options
  • Save yourdesigncoza/5421294 to your computer and use it in GitHub Desktop.
Save yourdesigncoza/5421294 to your computer and use it in GitHub Desktop.
Monit is a utility for managing and monitoring, processes, programs, files, directories and file systems on a UNIX system. Monit conducts automatic maintenance and repair and can execute meaningful causal actions in error situations ::: " a great little utility that monitors your daemons " In Short it lets you know when your Apache server goes d…
# Monit is a utility for managing and monitoring, processes, programs, files, directories and file systems on a UNIX system. Monit conducts automatic maintenance and repair and can execute meaningful causal actions in error situations ::: " a great little utility that monitors your daemons "
# in Short it lets you know when your Apache server goes down, when you reach your pre-determined memory usage. It will also restart key components should they stop working, for instance your web server ( apache ) might "crash" due to a DDOS attack or similar, monit will attempt to restart said services notifying you of whats going on, depending on your mmonit configuration :::
# Official site : http://mmonit.com/
# IMPORTANT : add your own data or parameters, I make use of double segments [[ your variable ]]. eg. ssh root@[[ 96.172.44.11 ]] should be replaced with ssh [email protected] where "888.88.88.88" is your value, variable etc. I have a habit of using ":::" to indicate line ending and end of paragraph, crazy I know but be warned its just how I write ::: All notes are for my own use & should you use any it's at your own risk, it's NOT a Tutorial :::
# I'm new to server side setup so if I've erred or you know of a better way PLS. contribute :::
# mail me at [email protected]
# I installed this on my Ubuntu 12.04 VPS server, the setup might differ depending on your server configuration :::
# Install Monit
sudo apt-get install monit
# Configure Monit
# Open
sudo nano /etc/monit/monitrc
# Confirm that it's at end of file
include /etc/monit/conf.d/*
# Add Mail Format
### Mail Format START
set mail-format {
from: monit@$HOST
subject: monit alert -- $EVENT $SERVICE
message: $EVENT Service $SERVICE
Date: $DATE
Action: $ACTION
Host: WP-Beta
Description: $DESCRIPTION
}
### Mail Format END
# Change permission on the folder /etc/monit/conf.d as it will include your email password stored in plain text.
sudo chmod 0700 -R /etc/monit/conf.d/
# Check if "START=" is set to "yes"
sudo nano /etc/default/monit
# Monit’s settings are devided into three files:
# basic.conf
# apache.conf
# mysql.conf
# Create a basic config file
sudo nano /etc/monit/conf.d/basic.conf
# Add the following save and close
### BASIC.CONF START
# default deamon is set to 120 ( 2 minutes ) you can override if necessary
# set daemon 60
set logfile syslog facility log_daemon
#mail server
set mailserver smtp.gmail.com port 587
username "[[ [email protected] ]]" password "[[ gmail_password ]]"
using tlsv1
with timeout 60 seconds
set alert [[ [email protected] ]]
#Following settings for web interface
set httpd port 2812 and
use address 198.xxx.xx.xxx
allow 0.0.0.0/0.0.0.0
allow [[ user ]]:[[ userpassword]]
check system localhost
if loadavg (1min) > 4 then alert
if loadavg (5min) > 3 then alert
if memory usage > 75% then alert
if cpu usage (user) > 70% then alert
if cpu usage (system) > 30% then alert
if cpu usage (wait) > 20% then alert
check process sshd with pidfile /var/run/sshd.pid
start program "/etc/init.d/ssh start"
stop program "/etc/init.d/ssh stop"
if failed port 2042 protocol ssh then restart
if 5 restarts within 5 cycles then timeout
### BASIC.CONF END
# Create Apache file
sudo nano /etc/monit/conf.d/apache.conf
### APACHE.CONF START
check process apache2 with pidfile /var/run/apache2.pid
# (for ubuntu, debian)
group www-data
start program = "/etc/init.d/apache2 start" with timeout 60 seconds
stop program = "/etc/init.d/apache2 stop"
if children > 250 then restart
if loadavg(5min) greater than 10 for 8 cycles then stop
if totalmem > 512.0 MB for 5 cycles then alert
# if totalmem > 512.0 MB for 5 cycles then restart
if 3 restarts within 5 cycles then timeout
### APACHE.CONF END
# Create the Mysql conf file
sudo nano /etc/monit/conf.d/mysql.conf
### MYSQL.CONF START
check process mysqld with pidfile /var/run/mysqld/mysqld.pid
start program = "/etc/init.d/mysql start"
stop program = "/etc/init.d/mysql stop"
if failed host 127.0.0.1 port 3306 then restart
if failed host 127.0.0.1 port 3306 protocol mysql then alert
if 5 restarts within 5 cycles then timeout
### MYSQL.CONF END
# Create the Postfix conf file
sudo nano /etc/monit/conf.d/postfix.conf
### POSTFIX START
# Check if you have group mail first
groups ( should print mail if available )
check process postfix with pidfile /var/run/postfix.pid
group mail
start program = "/etc/init.d/postfix start"
stop program = "/etc/init.d/postfix stop"
if failed port 25 protocol smtp then restart
if 5 restarts within 5 cycles then timeout
depends on postfix_rc
check file postfix_rc with path /etc/init.d/postfix
group mail
if failed checksum then unmonitor
if failed permission 755 then unmonitor
if failed uid root then unmonitor
if failed gid root then unmonitor
### POSTFIX END
# Start and Stop Monit
sudo /etc/init.d/monit stop
sudo /etc/init.d/monit start
# verify that Monit by checking /var/log/syslog
# You should also receive an email that says that Monit is now running.
# If alls well kill apache and monitor if it starts again ( will take 2 minutes unless you changed "set daemon")
sudo killall apache2
# Monit Web interface
# Monit Web interface will run on the port number 2812. If you have any firewall in your network setup you need to enable this port.
# Now point your browser to http://yourserverip:2812/ (make sure port 2812 isn’t blocked by your firewall)
# log in with username and password as set in "allow [[ user ]]:[[ user_password ]]" .If you want a secure login you can use https check
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment