Wednesday 4 March 2015

Curl command to monitor the status of a website

We can use curl command to check whether a website is live or not. Curl is similar to wget with some added features. Curl is very useful, while writing scripts or nagios plugins to check the status of a website.

A normal curl output looks as below:
[root@MANINMANOJ]#curl -s --head
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Expires: Wed, 04 Mar 2015 11:59:42 GMT
Date: Wed, 04 Mar 2015 11:59:42 GMT
Cache-Control: private, max-age=0
Last-Modified: Wed, 04 Mar 2015 11:58:24 GMT
ETag: "b2fe1e94-cefd-4323-8297-f4ac64161236"
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Content-Length: 0
Server: GSE
Alternate-Protocol: 80:quic,p=0.08,80:quic,p=0.08

I will explain each parameter of this output in a separate post. Here we will concentrate on website availability. Considering our case, the important parameter to notice is "HTTP/1.1 200 OK".   
The "200 OK" status code is the one which confirms that a website is live.

Now, we can develop a script using the status code as below :

if curl -s --head | grep "200 OK" > /dev/null
echo "The HTTP server on is up!" > /dev/null
echo "The HTTP server on is down!"

The above script is self explanatory and it tells us whether the website is live or not.

Kool :)

No comments:

Post a Comment

Note: only a member of this blog may post a comment.