I’d like to get an alert if a service dies or is unreachable.
One way of doing this would be to set up a cron
job to periodically ping MariaDB and send an e-mail to a specified address based on that ping status.
Allow outgoing emails in environment configuration (if necessary)
Email support can be set up in each environment, and by default it is already enabled on master
and disabled elsewhere. You can enable email through the environment configuration settings in the Web UI, or by using the Platform.sh CLI:
$ platform environment:info enable_smtp true
You can find more information about email on Platform.sh in the documentation.
Set up a cron job in .platform.app.yaml
to ping MariaDB
In your .platform.app.yaml
set up a cron
job for the service you want to check the status of. Here an exit code 0
means our ping was successful and MariaDB is running fine.
# proj_dir/.platform.app.yaml
...
crons:
# Check MySQL/MariaDB status
mysql_status:
# Runs every 10 minutes
spec: '*/10 * * * *'
cmd: |
# Pings MySQL/MariaDB, retrieves status message
status=$(mysqladmin -h mysql.internal ping)
# Saves the exit code
check=$?
if [ $check = 0 ]; then
# Run mail php file, passing service and status message
php notifications/service_notify.php --service "MySQL" --status "$status"
fi
relationships:
mysql: "mysql:mysql"
...
If you named your relationships differently, you will need to modify mysql.internal
to the correct name. (For example, database.internal
, etc.)
ping()
may not be the most robust test case, and is only used here for a simple example. Check the mysqladmin
documentation to find the test that best suits your needs.
Write a php file that handles mail()
for the ping
status
In the project directory, create a subdirectory called notifications
, and within that subdirectory write a service_notify.php
file:
// proj_dir/notifications/service_notify.php
<?php
// Retrieve opts
$shortopts = "";
$longopts = array(
"service:",
"status:",
);
$options = getopt($shortopts, $longopts);
$service = $options["service"];
$status = $options["status"];
// Project environment variables
$projectid = getenv('PLATFORM_PROJECT');
$environment = getenv('PLATFORM_ENVIRONMENT');
$app = getenv('PLATFORM_APPLICATION_NAME');
// Mail Contents
$to = 'you@example.com';
$subject = sprintf('Platform.sh Service Status Update: %s DOWN on Project %s', $service, $projectid);
$headers = 'From: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// Create the message
$message = <<<EOT
There has been a change in a service status:
* Service: $service
* Status: $status
on
* Project: $projectid
* Application: $app
* Environment: $environment
EOT;
// Send it
mail($to, $subject, $message, $headers);
?>
Verify
Our project is set up to email you@example.com
with the status of a successful ping
on mysql.internal
every 10 minutes. That status email will look like this.
webmaster@example.com
Platform.sh Service Status Update: MySQL DOWN on Project <project ID>
To: you@example.com
--------------------------------------------------------------------------
There has been a change in a service status:
* Service: MySQL
* Status: mysqld is alive
on
* Project: <project ID>
* Application: app
* Environment: master-7rqtwti
Set up MySQL down notifications
The preceeding steps send out an e-mail that includes the status message of a successful ping on mysql.internal
. That is, it will e-mail us every ten minutes to let us know everything is fine.
To modify the notification to let us know that MySQL has died or is unreachable, modify the if statement in .platform.app.yaml
cron
job to:
# If not successful (1)
if [ $check != 0 ]; then
# Run mail php file, passing service and status message
php notifications/service_notify.php --service "MySQL" --status "$status"
fi
This way, when a ping delivers an exit code of 1
, an email is sent!