From a55e6267282d47edd07d6e5c6e7141dc625f2c3c Mon Sep 17 00:00:00 2001 From: Konstantin Shalygin Date: Sun, 28 Jun 2015 12:30:45 +0600 Subject: [PATCH 1/2] Add timeout option, unk answer --- check_openvpn | 47 +++++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/check_openvpn b/check_openvpn index 054809b..798d7a3 100755 --- a/check_openvpn +++ b/check_openvpn @@ -1,10 +1,10 @@ -#! /usr/bin/python +#!/usr/bin/python2 # Check if an OpenVPN server runs on a given UDP port. # # Copyright 2013 Roland Wolters, credativ GmbH # -# Version 20130904 +# Version 20150628 # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the @@ -27,37 +27,41 @@ import sys import socket -import argparse import binascii +from optparse import OptionParser def ok(msg): print "OK: %s" % msg sys.exit(0) def critical(msg): - print "CRIT: %s" % msg + print "CRITICAL: %s" % msg sys.exit(2) -def checkserver(host,port,proto): +def unknown(msg): + print "UNKNOWN: %s" % msg + sys.exit(3) + +def checkserver(host,port,tcp,timeout): byte_stream = "\x38\x01\x00\x00\x00\x00\x00\x00\x00" - if proto: + if tcp: ovpn_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) else: ovpn_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - ovpn_sock.settimeout(5) + ovpn_sock.settimeout(timeout) try: ovpn_sock.sendto(byte_stream,(host,port)) data, addr = ovpn_sock.recvfrom(1024) # buffer size is 1024 bytes reply = binascii.hexlify(data) - if proto: + if tcp: ok("OpenVPN tcp port reachable.") else: ok("OpenVPN server response (hex): %s" % reply) except socket.timeout: - critical("Request timed out") + unknown("Request timed out") ovpn_sock.close() except (socket.error, Exception): critical("OpenVPN server not responding") @@ -66,20 +70,19 @@ def checkserver(host,port,proto): ovpn_sock.close() return data -def optionsparser(): - parser = argparse.ArgumentParser() - parser.add_argument("-p","--port", help="set port number", - type=int, dest="port", default="1194") - parser.add_argument("-t","--tcp", help="use tcp instead of udp", - action="store_true") - parser.add_argument("host", type=str, help="the OpenVPN host name or ip") - return parser.parse_args() - def main(): - arguments = optionsparser() - - data = checkserver(arguments.host,arguments.port,arguments.tcp) + usage = 'usage: %prog -H vpn.example.com' + parser = OptionParser(usage=usage) + parser.add_option('-p', '--port', type='int', dest='port', default='1194', help='Set port number (default is %default)') + parser.add_option('-t', '--timeout', type='int', dest='timeout', default='5', help='Set timeout (default is %default)') + parser.add_option('--tcp', action='store_true', dest='tcp', help='Use TCP instead of UDP') + parser.add_option('-H', '--host', type='str', dest='host', help='OpenVPN host name or IP') + (options, args) = parser.parse_args() + if not options.host: + parser.print_help() + sys.exit(0) + data = checkserver(options.host,options.port,options.tcp,options.timeout) if __name__ == "__main__": main() - + \ No newline at end of file From 024fdf97976d3347aa566dbd70085988acc7ca0e Mon Sep 17 00:00:00 2001 From: Konstantin Shalygin Date: Sun, 28 Jun 2015 12:35:11 +0600 Subject: [PATCH 2/2] Add to readme --- README.md | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 24d0164..dbacc62 100644 --- a/README.md +++ b/README.md @@ -8,18 +8,19 @@ The usage of the checks depends on what kind of checks they are: * normal checks must be run on the monitoring server ## How to use check_openvpn -The plugin is a normal check which must be run on the monitoring server. It queries the target OpenVPN server and outputs OK, etc. +The plugin is a normal check which must be run on the monitoring server. It queries the target OpenVPN server and outputs +OK, UNKNOWN and CRITICAL states. ``` $ python check_openvpn --help -usage: check_openvpn [-h] [-p PORT] [-t] host +Usage: check_openvpn -H vpn.example.com -positional arguments: - host the OpenVPN host name or ip - -optional arguments: +Options: -h, --help show this help message and exit - -p PORT, --port PORT set port number - -t, --tcp use tcp instead of udp + -p PORT, --port=PORT Set port number (default is 1194) + -t TIMEOUT, --timeout=TIMEOUT + Set timeout (default is 5) + --tcp Use TCP instead of UDP + -H HOST, --host=HOST OpenVPN host name or IP ``` ## How to use check_puppetagent