Setup a website monitoring service on Synology® Diskstation NAS

For the time being, I wanted a simple site monitoring service. I was looking around when I have remembered I have a Synology Diskstation in my LAN, which is always ON, of course. So why not put the service in there?

For now I do not intend to make the monitoring service available throughout the internet, so that will not be covered in this blog post.

Searched for a php monitor service and found PHP Server Monitor which seemed a good candidate. I managed to put it to work and did not need to telnet to the Diskstation even once.

This service allows to configure several servers to watch for and alerts using email and/or SMS, supporting several gateways. It has a web front end to configure and view and also allows for different users that can see different servers! Seemed perfect. It also has a feature to confirm that the site that is live is the one we expect by testing the returned HTML for a match of a string of characters or a regular expression. Although a simple test, this is a first approach for testing if the site was hacked besides server down. For some sites it would make sense to test for changes in the length of the HTML response, also.

Requisites

My Synology is a DS212j with DSM 5.0 and 256MB of RAM. The version of the PHP Server Monitor I used was the release 3.0.1.

So let’s start. The first thing to do is to prepare the Diskstation, so login to your Synology DSM as Admin.

Enable Web Station

For the Diskstation to host web sites, like PHP Server Monitor and myPHPAdmin bellow, the “Web Station” service must be enabled.

This also enables websites with PHP support:

image

Leave the PHP Settings with their defaults:

image

After hitting “Apply” a default shared folder called “web” will be created. You will need to place files in this share from a PC on your network, so make sure you have access to it. Check the folder permissions using the File Station. I access the diskstation using my personal user that is part of the Administrators of the Diskstation, so I’ve got a green pass.

Install MariaDB

The PHP Server Monitor service uses a MySQL database for storing its data. Note that on DSM V5.0 and up the MySQL was excluded from the base software. If you’re using a lower version of 5.0 you probably already have MySQL and you can skip this step. I used the package MariaDB V5.5.37, a fork of MySQL that is available on the Synology Package Center. Just install it.

Install myPHPAdmin

The phpMyAdmin is a free software tool to manage MySQL Databases, including the MariaDB we have just installed. The installation of myPHPAdmin is straigtforward. It is a package on the Package Center and I have installed version 4.1.14.

After phpMyAdmin is successfully installed it is a good practice to change the root password. Point your browser to http://diskstation/phpMyAdmin (note that your diskstation may have a different network name) and login with “root” username and blank password. On the “General Settings” area select “Change password” and make your self more confortable (make sure you write down the password somewhere).

Software Installation

Create Database and User

Point your browser to the phpMyAdmin service (mine is at http://diskstation/phpMyAdmin) and create the database and user:

  • On the “Databases” tab create a new database, give it a name of your preference and select “Create”. It will then appear on the same page on the databases list.
  • Select the “Check Privileges” of that database to go to the Privileges tab with the database already selected.
  • Select “Add user”. This is the user that PHP Server Monitor software will use to access the database.
  • Fill in the username and password (once again, make sure you write down the data somewhere).
  • Choose “local” for “Host”.
  • Make sure that “Grant all privileges on database…” is checked.
  • Leave all “Global privileges” unchecked and select “Go”.

Install of PHP Server Monitor

Head to www.phpservermonitor.org and download the latest release version to your computer storage. I downloaded release 3.0.1.

Unzip all the files to the diskstation web share. Put the folders and files into a folder with the name that will be used in the URL to access the service. I used “phpservermon”. Remember that the web server on the diskstation is case sensitive for file and folder names. In my case it looked like this:

image

And right afterwards you can access it using a browser. Mine is on http://diskstation/phpservermon. Just try to access yours. It should redirect you immediately to the install page:

image

Note that it has already found PHP and PHP cURL module successfully! Just select “Let’s go” to continue the installation.

Next step is to fill in the database info. Just fill name, user and password with the data from when you created the database. Leave host and prefix as is.

On the next screen you should see a successful connection to the database, but an error writing the config.php file:

image

The error is because of folder permissions. In fact if you check the web share using the Diskstation File Station tool, you will see that the “http” group only have read access and not write access. Now you can fix this problem in two ways:

  • You give the “http” group write permission and try again using the “Save configuration” button on the installation screen.
  • Save the configuration the installation needs your self. The installation screen provides instructions on how to do it.

I went for the second option, since the first one involves giving up a little on security:

  • Copy “config.php.sample” to “config.php” on the same folder.
  • Edit it with Notepad++ using the info provided on the installation screen. It is important to use a text editor that respects Unix line endings.
  • Select “I have saved the configuration” and the installation process will check for the config.php file.

If it can access the database correctly it will prompt to create an account for the service:

image

After hitting “Install” you should have the installation complete notice:

image

and after selecting “Go to your monitor” you can type in the account data just created.

image

After entering the service you will see the servers configured by default: “Gmail SMTP” and “SourceForge”.

The usage of the service is not covered in this blog post since it is covered on its documentation, but it is really simple to configure.

There is only one last step that needs to be made so the service is fully installed. For the service to go on its own monitoring the servers on a schedule a special PHP page must be run on an interval. This is discussed on the PHP Server Monitor documentation in “Setting up a cronjob”. You now have two options to accomplish this:

  • Following the documentation about “setting up a cronjob”. This requires telnet access to the Diskstation.
  • Schedule a task on the Diskstation Task Scheduler, under Control Panel.I did not proceed on the frist option.

The only drawback on using the second option is that the interval cannot be set less than 1 hour.

So, on the Task Scheduler use the button “Create” and “User-defined script” to create a new task as shown:

image

Note that in the third input box the text is:

/usr/bin/php /volume1/web/phpservermon/cron/status.cron.php

What separates ”…/php” from “/volume1/…” is a space and not a new line. It looks like they’re on two lines because of word-wrapping on the text box.

Then select the “Schedule” tab and setup your preferred time interval. I setup mine to every hour of every day.

For this task on Task Scheduler to work I needed a little tweak on the PHP Server Monitor code.

Comment the following lines in the file “cron/status.cron.php”:

//if(!psm_is_cli()) {
//    die('This script can only be run from the command line.');
//}

This may be corrected (maybe not that way I did!) on the version you will use, so, before any modification, just try to run the task on the Task Scheduler to see if the service updates the servers check. There is a “Run” button on the Task Scheduler so you don’t have to wait for the next scheduled run.

I have placed an issue on the project repository on GitHub about this problem.

And that’s it! You should have a server monitoring service running on your Synology Diskstation!

Support for SSL/TLS in SMTP

There is another tweak that may be corrected by the time you are reading this. At release 3.0.1 there is no support for SSL/TSL SMTP connections. This prevents using GMail as a SMTP gateway for sending messages since GMail requires TLS.

To correct this just add a few lines of code to the file “src/includes/functions.inc.php”. After the line:

$phpmailer->Port = psm_get_conf('email_smtp_port');

insert the following lines:

if ($phpmailer->Port == 587) {
    $phpmailer->SMTPSecure = 'tls';
} else if ($phpmailer->Port == 465) {
    $phpmailer->SMTPSecure = 'ssl';
}

Then when you specify the 587 as the SMTP port on the service SMTP configuration, TLS will be used automatically.

Have fun!

ztp

9 comments.

  1. You make my day! I’ve been looking about my own server monitor solution for quite a while now. Thank you!
    I am with 3.1.1 version number and as far as I can say the only adjustments I did are:
    1. define(‘PSM_CRON_TIMEOUT’, 10); have the timeout set to 10 secs in the psmconfig.inc.php.
    2. No need to alter the /cron/status.cron.php file anymore.

  2. Hi! Just for your info, with DSM 6 you have to change the cronjob to something like:

    /volume1/@appstore/PHP5.6/usr/local/bin/php56 /volume1/web/phpservermon/cron/status.cron.php

    That is, instead of just calling “php”, you need to use /volume1/@appstore/PHP5.6/usr/local/bin/php56

    If you don’t do that, you get a “database connection error”.

    BTW, the new Task Scheduler now allows to schedule tasks every minute! 🙂

    1. Doesn’t work for me –

      PHP Warning: MySQL connection failed: could not find driver in /volume1/web/monitor/src/psm/Service/Database.php on line 525

      Warning: MySQL connection failed: could not find driver in /volume1/web/monitor/src/psm/Service/Database.php on line 525

      (with config: define(‘PSM_DEBUG’, true);

    2. I should have mentioned that my problem is related to PHP7.0 and PHP5.6 still works as described above.
      PHP7.0 needs a larger workaround since it tries to use the system PHP configuration and not the one that runs on the Webstation.

  3. Hi!
    I have searching for a long time for something like this. But i cant get the cron work. The result says “normal” but the ping to my server has never been tried.

    i tried with php 7.0 and 5.6. Can anyone help me please.

    1. I guess i figured out where the problem is, but i still need help please. When i update manually i get an 500 error (?&mod=server_update)

      1. But the two default server (smtp gmail amd source forge) does update. only my servers not

Leave a Reply

Your email address will not be published. Required fields are marked *