In a previous post we talked about Monit and how to install it on our Centos and/or Ubuntu server in order to control the services we have installed. This time we bring you a web application that allows you to graph the data collected by Monit. This tutorial works for both Centos and Ubuntu, although some paths and commands may vary slightly.
Requirements and dependencies
For Monit Graph to work correctly, we need to have an Apache or Nginx server with PHP installed on our server. If you need to know how to install an Apache server you can check “How to install in Linux: Apache MariaDB and PHP (LAMP)” or if you prefer Nginx you can go to “How to install in Linux: NGINX MariaDB and PHP (LEMP)“.
It is also important to have the following PHP packages installed:
Ubuntu:
# apt-get install libapache2-mod-php7.0 php7.0-cli php7.0-mysql php7.0-xml php7.0-curl
Centos:
# yum install php-cli php-mysql php-xml php-curl
Additionally, you can create a VirtualHost or Server (depending on whether you use Apache or Nginx) to access via subdomain if you prefer. Remember that it is important to protect the directory where you are going to install Monit Graph since it does not have a login panel. Since this is a tutorial, we will work with Apache's default path which is /var/www/html and we will add the web there. If you have any questions, you can always contact Clouding Support for help.
Download Monit-Graph
To download Monit-Graph we can use two methods: either access via SSH and use wget, or copy it via FTP. We will use the first method as it is the most convenient for us.
Once connected via SSH, we need to go to the path configured in our DocumentRoot or Root, in our case:
# cd /var/www/html
Download Monit-Graph with wget:
# wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/monit-graph/monit-graph.tar.gz
Unpack:
# tar zxf monit*.tar.gz
Delete the tar.gz:
# rm monit*.tar.gz
Set directory and file permissions:
chown www-data.www-data -R * chmod 755 -R * chmod 777 data/logs chmod 644 data/index.php chmod 644 data/logs/index.php
Edit the configuration file:
# nano config.php
And configure it like this:
$monit_url = "46.183.XXX.XXX:2812"; // The IP:Port or domain:port of our Monit $monit_http_username = "admin"; // User to access Monit $monit_http_password = "monit"; // Password to access Monit $monit_url_ssl = false; // Change true to false
Add the following to cron so that every minute it collects data from our Monit:
# crontab -e
* * * * * php /var/www/html/cron.php >> /var/log/monit-graph.log
Make sure to adjust the path to the cron.php file.
Once these changes are made, restart the Apache/Nginx server and access your Monit Graph:
Monit-Graph does not display graphs
If it does not work correctly and does not display the graphs, a simple way to debug configuration errors is to run the cron.php file from the terminal like this:
# php cron.php
When executed, it will show errors that we can easily fix. For example, the following error:
[MonitGraph] /var/www/html/monit-graph.class.php line 122: cURL Error (27): SSL: couldn't create a context: error:140A90C4:SSL routines:SSL_CTX_new:null ssl method passed
This means that in our configuration file we have the value $monit_url_ssl set to true instead of false.
Another example:
PHP Warning: file_get_contents(/var/www/html/data/server.xml): failed to open stream: No such file or directory in /var/www/html/monit-graph.class.php on line 148
This means that it cannot find the file /var/www.html/data.server.xml, which likely means that Monit Graph has not been able to connect with Monit and has not yet automatically created that file. In this case, check your configuration file again and make sure that the value of $monit_url is correct (don’t forget to specify Monit’s ports).
Sources: DreamCenception, Code Google