Skip to main content

CWP: How to monitor CWP Server Services using Monit on CentOS 7.6

Monit is a free open source and a very helpful program that automatically monitors and manages server process, files, directories, checksums, permissions, filesystems and services like Apache, Nginx, MySQL, FTP, SSH, Postfix and so on in a UNIX/Linux based systems and provides an excellent monitoring functionality to system administrators.

You can reduce the downtime using Monit because when any services goes down , Monit will check it and start the services automatically, example: if your dns service is down then monit will check, if found it's down then monit will start the dns service automatically. One interesting thing is that monit runs it own httpd server. If your apache server is down, monit will running using it's own server.

In this tutorial we are going to show you, how to install Monit along with CentOS Webpanel on CentOS 7.

Prerequisites:
To complete this tutorial, you will need:
1. CentOS Linux release 7.6.1810 (Core)
2. CWP7.admin , CWP version: 0.9.8.772
3. CSF Firewall

Update your YUM Repositories then Install Monit
# yum update -y
# yum install monit 
Open Monit Configuration File : /etc/monitrc , Next uncomment the following section
# vi /etc/monitrc

set daemon  30              # check services at 30 seconds intervals
set log syslog
set pidfile /var/run/monit.pid
set idfile  /var/.monit.id
set statefile /var/.monit.state

set mailserver localhost port 25

set eventqueue
basedir /var/monit  # set the base directory where events will be stored
slots 100           # optionally limit the queue size

set alert admin@datahead.com          #receive all alerts

set alert admin@datahead.com not on { instance, action } 

:x (save & close)
Add the IP address, allow anyone to connect
# vi /etc/monitrc

set httpd port 2812 and
use address 0.0.0.0
allow 0.0.0.0/0.0.0.0
allow admin:monit      # require user 'admin' with password 'monit' 

Check Monit syntax for error
# monit -t 
Control file syntax OK  
Create the below file(s) if missing :
# touch /var/run/monit.pid 
# touch /var/log/moinit.log
Now I will monitor some services like amavisd, clamd, crond, php-fpm & cwpsrv. Open monit Configuration file & add the below code at end of the line:
Monitor CWP.amavisd 
# vi /etc/monitrc 

check process amavisd with pidfile /var/run/amavisd/amavisd.pid
        start program "/usr/bin/systemctl start amavisd.service"
        stop program "/usr/bin/systemctl stop amavisd.service"
        if failed unixsocket /var/run/amavisd/amavisd.sock then restart
        if cpu > 70% for 4 cycles then alert
        if cpu > 90% for 8 cycles then restart
        if 4 restarts within 8 cycles then timeout
 
Monitor CWP.clamd
# vi /etc/monitrc 

check process clamd with pidfile /var/run/clamd.amavisd/clamd.pid
        start program "/usr/bin/systemctl start clamd.service"
        stop program "/usr/bin/systemctl stop clamd.service"
        if failed unixsocket /var/run/clamd.amavisd/clamd.sock then restart
        if cpu > 70% for 4 cycles then alert
        if cpu > 90% for 8 cycles then restart
        if 4 restarts within 8 cycles then timeout 
Monitor CWP.crond
# vi /etc/monitrc 

check process crond with pidfile /var/run/crond.pid
        start program = "/usr/bin/systemctl start crond.service"
        stop  program = "/usr/bin/systemctl stop crond.service" 
Monitor CWP.cwp-phpfpm
# vi /etc/monitrc

check process cwp-phpfpm matching "cwp-phpfpm"
        start program "/usr/bin/systemctl start cwp-phpfpm.service"
        stop program "/usr/bin/systemctl stop cwp-phpfpm.service"
        if failed unixsocket /usr/local/cwp/php71/var/sockets/cwpsrv.sock then restart
        if failed unixsocket /usr/local/cwp/php71/var/sockets/cwpsvc.sock then restart
        if failed unixsocket /usr/local/cwp/php71/var/sockets/login.sock then restart
        if cpu > 70% for 4 cycles then alert
        if cpu > 90% for 8 cycles then restart
        if 4 restarts within 8 cycles then timeout 
Monitor cwp.cwpsrv
# vi /etc/monitrc

check process cwpsrv with pidfile /usr/local/cwpsrv/var/run/nginx.pid
        start program "/usr/bin/systemctl start cwpsrv.service"
        stop program "/usr/bin/systemctl stop cwpsrv.service"
        if 4 restarts within 8 cycles then timeout 
Open 2812 port on CSF Firewall & Restart CSF Firewall
# vi /etc/csf/csf.conf
 
 # Allow incoming TCP ports
 TCP_IN = "20,21,22,2812,25,53,80,110,143,443,465,587,993,995,2030,2031,2082,2083,2086,2087,2095,2096" 

# csf -r 
Once this is configured, monit should reload and reread the configuration file, and the web interface will be available:
# monit reload  
Another way To Monitor:
Download all the file from download link (DownLoad Link) and upload to /etc/monit.d/ directory.

Now Check Monit syntax , If no error then enable & restart monit service
# monit -t 
# systemctl enable monit
# systemctl restart monit
Now check the monit log
# tail -f /var/log/monit.log
Monit Basic Command
Start monit by using the following command.
# monit
Monit daemon with PID 99008 awakened

Check the monit status.
# monit status

Reload it, to take effect of changes.
# monit reload

you can start running all of the monitored programs.
# monit start all

Restart all monit monitored service(s)
# monit restart all

Start, Stop & Restart a specific service using
# monit start httpd
# monit stop sshd  
# monit restart nginx
Check Monit Summary:

Now login to your monit server using Monit username & password that you set earlier. Login URL : http://SERVER_FQDN:2812 

Comments

Most Popular

CWP DNS Part 1 : How to Configure DNS properly for CentOS WebPanel on CentOS 7.6

After hosting my parent domain on CWP7.admin, I am getting dns error and i'm not able access my server using my server FQDN but i can access using my server IP.
So what can i do for that problem ?

Yes, you have to fix the error .

Environment Details:
Distro Name: CentOS Linux release 7.6.1810 (Core)
CentOS-Web Panel version: CWP7.admin
CWP version: 0.9.8.757
WebServer: Apache Only
FQDN: host.datahead.biz
IP: 192.120.10.3

1.Change Hostname Permanently:
# hostnamectl set-hostname host.datahead.biz# hostnamectl Static hostname: host.datahead.biz Icon name: computer-vm Chassis: vm Machine ID: 7400071490ea4f7d931374824ad4b52c Boot ID: 6e1f2d76495d4b318c25c4a1195aa130 Virtualization: vmware Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-862.14.4.el7.x86_64 Architecture: x86-64 It also writes this information to the /etc/hostname file as well.
# cat /etc/hostname host.d…

CWP DNS Part 2 : How to Configure DNS properly for CentOS WebPanel on CentOS 7.6

7.Open Main Configuration file
# vi /etc/named.conf 12 options { 13 listen-on port 53 { any; }; 14 listen-on-v6 port 53 { ::1; }; 15 directory "/var/named"; 16 dump-file "/var/named/data/cache_dump.db"; 17 statistics-file "/var/named/data/named_stats.txt"; 18 memstatistics-file "/var/named/data/named_mem_stats.txt"; 19 recursing-file "/var/named/data/named.recursing"; 20 secroots-file "/var/named/data/named.secroots"; 21 allow-query { any; }; 33 recursion no; 34 35 dnssec-enable yes; 36 dnssec-validation yes; 54 zone "." IN { 55 type hint; 56 file "named.ca"; 57 }; 58 59 include "/etc/named.rfc1912.zones"; 60 include "/etc/named.root.key"; 61 …