Monit monitor http status with 404 page

Solution 1:

Since version 5.8, Monit has the status option:

STATUS option can be used to explicitly test the HTTP status code returned by the HTTP server. If not used, the http protocol test will fail if the status code returned is greater than or equal to 400. You can override this behaviour by using the status qualifier.

For example to test that a page does not exist (404 should be returned in this case):

if failed
   port 80
   protocol http
   request "/non/existent.php"
   status = 404
then alert

Solution 2:

The status didn't work for me (monit 5.6). I think it's supported from 5.8?

I ended up with a script which uses curl:

#!/bin/bash
# source: /etc/monit/bin/http-check.sh

url="http://user:[email protected]/test_link/index.php"

response=$(curl -sL -w "%{http_code}\\n" $url | grep 404)

if [ "$response" = "404" ]
then
  exit 0
else
  exit 1
fi

Then I added the following monit configuration

check program http-check with path "/etc/monit/bin/http-check.sh"
  if status != 0
  then alert