Monitor PHP database connectivity using SAM

By | December 12, 2014

When setting up custom application monitors I wanted to be able to monitor any and every connection point the application made in the same way the application made it. Since I didn’t have permission to modify the codebase and catch errors, I simply wrote some custom Server Application Monitor scripts to monitor the connection for me. For this particular one, I wanted to be able to make a connection to a postgres database utilizing the PHP PDO.

 

First, you need to get the credentials setup. Since I would be making the connection to the database from the webserver itself I needed an SSH account to log into the Linux Server and execute the script. We already had a least privileged user account we were using for monitoring so this was already setup for me, for you, make sure you have one set up.

Then you wanted to make sure you had permissions to that database. I won’t get into this part but have your DBA set you up with a users with access to the necessary database, from the webserver you are running the script from.

From there, you want to create the application Template.
From the homepage, in the upper right hand corner, click on Settings
Then on the right side in the second group of links, click SAM Settings
Under Application Monitor Templates, click Create a New Template
Click Add Component Monitors
Choose Linux/Unix Script Monitor and click add
Select your Authentication Type and select your Credentials for Monitoring
Leave Script Working Directory empty
Add php5 ${Script} to Command Line
Under Script Body, click Edit Script
In the Script Body section, add the following code:

——————————-Copy Everything Below this Line——————————-

<?php
#ini_set (“display_errors”, “1”);
error_reporting(0);
   $host        = “host=db-hostname“;
   $port        = “port=5432”;
   $dbname      = “dbname=dbname“;
   $credentials = “user=db_user password=user_password“;

   $db = pg_connect( “$host $port $dbname $credentials”  )
# or die(‘Could not connect’)
;;
   if(!$db){
      echo “Statistic:0\n”;
      echo “Message: unable to open database”;
   } else {
      echo “Statistic:1\n”;
      echo “Message: database connection successful”;
   }
?>

——————————-Copy Everything Above this Line——————————-

 From here, you’ll want to change the highlighted fields such as your database server host name, database name, database user and database user password to fit your environment. It will not work with the values I provided.

Once your done with that, click Get Script Output, it will prompt you for the server which you want to run it on, which would be your web server, then it will as you for SSH credentials, provide your ssh credentials and it should come back with a simple statistic of either 1 which is SUCCESSFUL or 0 which is UNSUCCESSFUL

Once you get the output, click Save.

Then under Script Output #1 you’ll want to really set a threshold for both warning and critical to 0

Once that is complete, name your application template and click submit, this will save the template. From there, all that’s left is assigning the template to one of your Linux servers.

Here is a picture of my Component Monitor interface for this monitor:

There you have it. A simple php database connection script monitor for Solarwinds Server Application Monitor (SAM)

Leave a Reply

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