3

I know there are tools like pingdom.com and uptimerobot.com that will monitor your site and let you know if they're down, but I find that my WordPress site is experiencing the dreaded "error establishing a database connection" more often than desired, and I'd like to get some sort of notification when that happens (but not in the form of a user complaint!). Pingdom and Uptimerobot do not consider that an error, and thus do not push any notifications.

Are there any simple tools or methods that will monitor my site for that specific problem (and perhaps other problems?) that may not be caught otherwise.

[Edit] 1. I'm referring specifically to the the well-known WordPress error that often occurs due to various DB connectivity reasons. When the error occurs, visiting the site returns the default white page with text shown below.

  1. For the purposes of this question, I'm not interested in why it occurs or how to fix it, but instead, I'd simply like to figure out how to automatically monitor it and get notified. Thanks.

enter image description here

  • THis sounds like a server-related issue with your database. Have you contacted your host provider? – Ethan Jinks O'Sullivan Sep 15 '16 at 4:23
  • No I haven't, typically my quick and easy solution (for now) is to just reboot server and it works. However, my question is about to monitor and get notified first-hand this error. – fortuneRice Sep 15 '16 at 4:34
  • My experience with Uptime Robot is that it reports The monitor ... is currently DOWN (HTTP 500 - Internal Server Error). when the database connection is out. – birgire Sep 15 '16 at 13:50
2

You can actually use Pingdom to check for this kind of error, rather than a straight forward check for a response from the server you can configure the check to look for a particular string on the page. For instance, if your page has some kind of consistent bit of text on the homepage then you can look for that.

This Pingdom post has some info about checking for strings:

Make the most of your HTTP check: best practice for optional settings

You can also check that it doesn't have particular string, so you should be able to make it flag it as down if it shows the text "Error establishing a database connection".

2

You can override the message with a file wp-content/db-error.php.

Then, in this file you can log the error or send a e-mail to alert there is a problem. I recommand you to not send a e-mail on every error because you can receive a lot of e-mails in a few seconds.

  • hmmm forgot about that – Mark Kaplun Sep 15 '16 at 7:11
  • 1
    Cool. Didn't know that - do you know whether wp_mail is available at that point? – Andy Macaulay-Brook Sep 15 '16 at 8:59
  • 1
    @AndyMacaulay-Brook That depends on what you put in the file -- you might need to do a require_once('../wp-load.php'); at the start of your file. – FaCE Sep 15 '16 at 11:10
1

How are you connecting to the database? Could you not catch an exception that sends an email to yourself?

With WordPress I assume it's PHP, so...

            try {
                    // connect to database
            } catch(PDOException $ex) {
                    $this->output_error_string .= "$ex";
                    mail(/* send mail */);
            }
  • Yes WordPress is php - I'm not exactly sure how it connects to DB, but it would be part of the WP core files, and I'd like to refrain from modifying core, as any changes are lost during upgrades. Plus, if I monitor via running code on my server then the monitoring tool won't work if the server runs into other problems - so I would prefer an external monitoring solution. – fortuneRice Sep 15 '16 at 2:15
  • 1
    Sorry, I think I was thinking you were connecting to the database with SQL using something like wpdb (codex.wordpress.org/Class_Reference/wpdb). – sdexp Sep 15 '16 at 2:28
  • I'll edit my question to make it more clear. – fortuneRice Sep 15 '16 at 4:34
0

There are probably several external ways to do it which are not wordpress specific, like doing an http get once a minute (or more) of the home page and check its content. can even be done from a local host or from a different one.

Wordpress side solution, in a twist on what @sdexp suggests, is to override the wpdb class by providing your own implementation of it (in a wpdb.php file placed in /wp-content) which differ from the core one just by the action being done when connection fails. This is obviously a soft fork of wpdb but that class do not change a lot and with smart use of the php magic methods call, set and get in your implementation you can minimize the possibility of future failure even further.

Still your best path is to use external service. Monitoring is surprisingly something that not very easy to get right if you haven't done such a thing before. Which service? StackOverflow or ServerFault are probably much better places to ask as it is really not a wordpress specific issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.