2

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
Tyler
  • 77
  • 1
  • 10
  • 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 00:06
  • I get etc/monit/conf.d/mysql:13: Error: syntax error 'url' – Tyler Jun 30 '12 at 00: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 00: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 00:31

1 Answers1

5

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"

DerfK
  • 19,313
  • 2
  • 35
  • 51
  • tried your syntax and i get etc/monit/monitrc:249: Error: syntax error '' – Tyler Jun 30 '12 at 01:00
  • i forgot to put "then restart" so monit starts now but it doesn't detect the error, i'm wondering if the html tags are being seen and it's not matching because of them? – Tyler Jun 30 '12 at 01: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 01: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 02:10