Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

How do I check if a web page contains the text "Error connecting to database" and if the text exists in the page restart the database?

Here's what I have so far but it isn't working:

check host website.com with address website.com
   group database
   start program = "/usr/bin/service mysql start"
   stop program = "/usr/bin/service mysql stop"
   if url http://website.com content == "Error connecting to database" then restart
share|improve this question
    
What you say isn't working, what do you mean? Does monit not recognize the configuration? Does it make the http request (check access.log) –  Zoredache Jun 30 '12 at 0:06
    
I get etc/monit/conf.d/mysql:13: Error: syntax error 'url' –  Tyler Jun 30 '12 at 0:14
    
Well, there is your problem then. What version of monit are you running? Are you sure that version supports checking URLs? That is a relatively new feature. Which version are you running? –  Zoredache Jun 30 '12 at 0:27
    
I'm using monit 5.3.2 and I'm certain url is valid it's the syntax I seem to be messing up, I tried if "failed url" but it's not a failed url i am trying to test it's a working url that contains that content. –  Tyler Jun 30 '12 at 0:31

1 Answer 1

up vote 5 down vote accepted

From the documentation here, failed is a required keyword (in fact, just about every monit test is of the form if failed). You have to write your test as failing to be normal:

if failed (url http://www.example.com and content != "Error connecting to database")

Thus, failure means that it can't retrieve the page or the page's content matches the regex "Error connecting to database"

share|improve this answer
    
tried your syntax and i get etc/monit/monitrc:249: Error: syntax error '' –  Tyler Jun 30 '12 at 1:00
    
i forgot to put "then restart" so monit starts now but it doesn't detect the error, i'm wondering if the <body> html tags are being seen and it's not matching because of them? –  Tyler Jun 30 '12 at 1:13
    
@Tyler check the page source and see if the actual text Error connecting to database appears, or if there are maybe extra spaces or other characters in there that make it not match? It's possible that it's doing something like trying to anchor the start and end of the regex despite the fact that it doesn't use ^ and $, in which case you'd need to make it something like ".*Error connecting to database.*" –  DerfK Jun 30 '12 at 1:23
    
Thanks @DerfK, I found two more issues. 1) I needed to pull /test.php instead of / and 2) I needed to exclude test.php from the Varnish cache, now I fixed those issues the test works and the restart works. –  Tyler Jun 30 '12 at 2:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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