Skip to content

Instantly share code, notes, and snippets.

@0xa
Created March 8, 2013 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0xa/5117848 to your computer and use it in GitHub Desktop.
Save 0xa/5117848 to your computer and use it in GitHub Desktop.
OpenVPN ping plugin for Nagios
#!/usr/bin/env python
# OpenVPN plugin for Nagios
from optparse import OptionParser
import socket
from datetime import datetime
def main():
senddata= "\x38\x01\x00\x00\x00\x00\x00\x00\x00"
usage = 'usage: %prog -H HOST [options]'
parser = OptionParser(usage=usage)
parser.add_option('-H', '--host', dest='host',
help='host to check')
parser.add_option('-p', '--port', dest='port', type='int',
default=1194, help='port to check')
parser.add_option('-t', '--tcp', dest='use_tcp', action='store_true',
default=False, help='use TCP instead of UDP')
parser.add_option('-w', '--timeout', dest='timeout', type='int',
default=5, help='set the timeout')
parser.add_option('-v', dest='verbose', action='store_true')
(options, args) = parser.parse_args()
if not options.host:
parser.error('provide a --host/-H')
exit(3) # unknown
host = options.host
port = options.port
proto = (socket.SOCK_STREAM, 'tcp') if options.use_tcp \
else (socket.SOCK_DGRAM, 'udp')
if options.verbose:
print "checking %s:%d %s"%(host, port, proto[1])
sock = socket.socket(socket.AF_INET, proto[0])
status = 0
try:
sock.settimeout(options.timeout)
ts = datetime.now()
sock.connect((host, port))
sock.send(senddata)
reply = sock.recv(100)
te = datetime.now()
if options.verbose:
hreply = ''.join(['%02x'%ord(byte) for byte in reply])
print 'received(hex): %s'%(hreply)
td = te - ts
print 'ok %.3fms'%(td.total_seconds()*1000)
status = 0 # ok
except socket.gaierror, e:
print 'error: '+e.args[1]
status = 2 # critical
except:
print 'server not responding'
status = 2 # critical
sock.close()
exit(status)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment