check_openvpn: Update to 20160803

This commit is contained in:
Jan Wagner 2016-12-23 23:20:59 +01:00
parent b064dda772
commit 6af6c5412a
2 changed files with 54 additions and 37 deletions

View file

@ -1,10 +1,11 @@
#! /usr/bin/python #!/usr/bin/env python
# Check if an OpenVPN server runs on a given UDP port. # Check if an OpenVPN server runs on a given UDP or TCP port.
# #
# Copyright 2013 Roland Wolters # Copyright 2013 Roland Wolters
# Copyright 2016 Alarig Le Lay
# #
# Version 20151106 # Version 20160803
# #
# Permission is hereby granted, free of charge, to any person obtaining # Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the # a copy of this software and associated documentation files (the
@ -26,6 +27,7 @@
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import os import os
import sys
import time import time
import hmac import hmac
import hashlib import hashlib
@ -37,12 +39,15 @@ import binascii
HMAC_CLIENT_KEY_START = 192 HMAC_CLIENT_KEY_START = 192
BUFFER_SIZE = 1024 BUFFER_SIZE = 1024
ALGORITHMS_AVAILABLE = hashlib.algorithms_available \
if hasattr(hashlib, "algorithms_available") else hashlib.algorithms
def ok(msg): def ok(msg):
print 'OK: %s' % msg print('OK: %s' % msg)
return 0 return 0
def critical(msg): def critical(msg):
print 'CRIT: %s' % msg print('CRIT: %s' % msg)
return 2 return 2
def buildpacket(tcp, key, digestmod): def buildpacket(tcp, key, digestmod):
@ -55,14 +60,14 @@ def buildpacket(tcp, key, digestmod):
h = hmac.new(key, digestmod=digestmod) h = hmac.new(key, digestmod=digestmod)
h.update(struct.pack('>I', packet)) # packet id h.update(struct.pack('>I', packet)) # packet id
h.update(struct.pack('>I', ts)) # net time h.update(struct.pack('>I', ts)) # net time
h.update('\x38') # type h.update(b'\x38') # type
h.update(session) # session id h.update(session) # session id
h.update(struct.pack('>B', 0)) # message packet id array length h.update(struct.pack('>B', 0)) # message packet id array length
h.update(struct.pack('>I', 0)) # message packet id h.update(struct.pack('>I', 0)) # message packet id
# packet # packet
result = '' result = b''
result += '\x38' # type result += b'\x38' # type
result += session # session id result += session # session id
if key: result += h.digest() # hmac if key: result += h.digest() # hmac
result += struct.pack('>I', packet) # packet id result += struct.pack('>I', packet) # packet id
@ -74,12 +79,18 @@ def buildpacket(tcp, key, digestmod):
def checkserver(host, port, tcp, timeout, key, digest): def checkserver(host, port, tcp, timeout, key, digest):
packet = buildpacket(tcp, key, digest) packet = buildpacket(tcp, key, digest)
if tcp: checkserver_tcp(host, port, timeout, packet) check = checkserver_tcp if tcp else checkserver_udp
else: checkserver_udp(host, port, timeout, packet) return check(host, port, timeout, packet)
def checkserver_udp(host, port, timeout, packet): def checkserver_udp(host, port, timeout, packet):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # thanks to glucas for the idea
try:
af, socktype, proto, canonname, sa = socket.getaddrinfo(host, port, \
socket.AF_UNSPEC, socket.SOCK_DGRAM)[0]
s = socket.socket(af, socktype, proto)
s.settimeout(timeout) s.settimeout(timeout)
except socket.error:
return critical('Unable to create UDP socket')
try: try:
s.sendto(packet, (host, port)) s.sendto(packet, (host, port))
@ -92,8 +103,13 @@ def checkserver_udp(host, port, timeout, packet):
s.close() s.close()
def checkserver_tcp(host, port, timeout, packet): def checkserver_tcp(host, port, timeout, packet):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try:
af, socktype, proto, canonname, sa = socket.getaddrinfo(host, port, \
socket.AF_UNSPEC, socket.SOCK_STREAM)[0]
s = socket.socket(af, socktype, proto)
s.settimeout(timeout) s.settimeout(timeout)
except socket.error:
return critical('Unable to create TCP socket')
try: try:
s.connect((host, port)) s.connect((host, port))
@ -121,20 +137,20 @@ def readkey(path):
key = key[index_start:index_end].replace('\n', '').replace('\r', '') key = key[index_start:index_end].replace('\n', '').replace('\r', '')
return key return key
def optionsparser(args=None): def optionsparser(argv=None):
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('-p', '--port', help='set port number (default is %%default)', type=int, dest='port', default='1194') parser.add_argument('-p', '--port', help='set port number (default is %(default)d)', type=int, default=1194)
parser.add_argument('-t', '--tcp', help='use tcp instead of udp', action='store_true') parser.add_argument('-t', '--tcp', help='use tcp instead of udp', action='store_true')
parser.add_argument('--timeout', help='set timeout (default is %%default)', type=int, default='5') parser.add_argument('--timeout', help='set timeout (default is %(default)d)', type=int, default=5)
parser.add_argument('--digest', help='set HMAC digest (default is %%default)', type=str, default='sha1') parser.add_argument('--digest', help='set HMAC digest (default is "%(default)s")', default='sha1')
parser.add_argument('--digest-size', help='set HMAC digest size', type=int) parser.add_argument('--digest-size', help='set HMAC digest size', type=int)
parser.add_argument('--digest-key', help='set HMAC key', type=str, default=None) parser.add_argument('--digest-key', help='set HMAC key')
parser.add_argument('--tls-auth', help='set tls-auth file', type=str, default=None) parser.add_argument('--tls-auth', help='set tls-auth file')
parser.add_argument('host', type=str, help='the OpenVPN host name or ip') parser.add_argument('host', help='the OpenVPN host name or IP')
return parser.parse_args(args) return parser.parse_args(argv)
def main(): def main(argv=None):
args = optionsparser() args = optionsparser(argv)
if args.digest_size and args.digest_size < 0: if args.digest_size and args.digest_size < 0:
critical('digest size must be positive') critical('digest size must be positive')
@ -146,7 +162,7 @@ def main():
digest_size = args.digest_size digest_size = args.digest_size
digest = digest.lower() digest = digest.lower()
if digest not in hashlib.algorithms: if digest not in ALGORITHMS_AVAILABLE:
return critical('digest not available') return critical('digest not available')
try: try:
digest = getattr(hashlib, digest) digest = getattr(hashlib, digest)
@ -166,4 +182,5 @@ def main():
return checkserver(args.host, args.port, args.tcp, args.timeout, key, digest) return checkserver(args.host, args.port, args.tcp, args.timeout, key, digest)
if __name__ == '__main__': if __name__ == '__main__':
main() code = main()
sys.exit(code)

View file

@ -1,6 +1,6 @@
Homepage: https://raw.github.com/liquidat/nagios-icinga-checks/master/check_openvpn Homepage: https://raw.github.com/liquidat/nagios-icinga-checks/master/check_openvpn
Watch: https://raw.github.com/liquidat/nagios-icinga-checks/master/check_openvpn Version\ ([0-9.]+) Watch: https://raw.github.com/liquidat/nagios-icinga-checks/master/check_openvpn Version\ ([0-9.]+)
Recommends: libpython-stdlib Recommends: libpython-stdlib
Version: 20151106 Version: 20160803
Uploaders: Jan Wagner <waja@cyconet.org> Uploaders: Jan Wagner <waja@cyconet.org>
Description: plugin to check if an OpenVPN server runs on a given port Description: plugin to check if an OpenVPN server runs on a given port