9 Jun 2010

Nagios plugin

1st time I try to write plugin for nagios, it's really easy and simple with python :-). If you dont know how to use, please read this
First, check if we can reach website, then count response time and return nagios status.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Hung Nguyen Van
# 09-06-2010
from urllib2 import urlopen
import time, sys, socket

def timer(_dest):
    timeout = 10
    socket.setdefaulttimeout(timeout)    
    try:
        _response = urlopen(_dest)
    except:
        print "CRITICAL: Cannot connect to server!"
        sys.exit(3)
    start_time = time.time()
    _response = urlopen(_dest)
    end_time = time.time()
    final = end_time - start_time
    return final

if __name__ == '__main__':
    _dest = "https://" + sys.argv[1]
    seconds = timer(_dest)
    if seconds >= 10.0:
        print "CRITICAL: Server took %.2f seconds to response" % (seconds)
        sys.exit(2)
    if seconds >= 7.0:
        print "WARNING: Server took %.2f seconds to response" % (seconds)
        sys.exit(1)
    else:
        print "OK: Server took %.2f seconds to response" % (seconds)
        sys.exit(0)

No comments:

Disqus