#!/usr/bin/env python ###################################################################################### # check_openvpn - check to see how many connections are made to your OpenVPN server. # connects to the management OpenVPN server, and try to give status # of all connections. # # # License Information: # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. # # $Id: check_openvpn 2009-04-23 00:35:00 MEZGANI Ali $ # ###################################################################################### import socket import re import sys import getopt class connexion: """class connexion that create a socket, and connects to management openvpn server""" def __init__(self, hostname, port, password, request): self.hostname=hostname self.port=port self.password=password self.request=request def interact(self): """function that interacte with the server and try to get out some data""" try: sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM) sock.connect((self.hostname,self.port)) data = sock.recv(1024) sock.send(self.password) sock.send('\r\n') data = sock.recv(4096) line=re.findall('SUCCESS:',data) if line != []: sock.send('\r\n') sock.recv(1024) sock.send(self.request) sock.send('\r\n') data = sock.recv(409600) if self.request.find('kill')!=0: data += sock.recv(409600) sock.send('exit') sock.send('\r\n') sock.close() return data else: print ("CHECKOPENVPN WARNING: ") sys.exit(1) except Exception, e: print ("CHECKOPENVPN CRITICAL: %s" % e) sys.exit(2) def usage(): print """Usage: check_openvpn [-h|--help] [-t|--target] \ [-p|--port] [-n|--pass]""" sys.exit(3) if __name__=="__main__": if len(sys.argv) != 7: usage() try: options, args = getopt.getopt(sys.argv[1:], "h:t:p:n:", "--help --target= --port= --pass=", ) except getopt.GetoptError: usage() sys.exit(3) for name, value in options: if name in ("-h", "--help"): usage() if name in ("-t", "--target"): host=value if name in ("-p", "--port"): port=value if name in ("-n", "--pass"): password=value sock=connexion(host, int(port), password, 'status') data=sock.interact() tab1=re.findall("(.+),(\d+\.\d+\.\d+\.\d+\:\d+),(\d+),(\d+),(.+)", data) tab2=re.findall("(\d+\.\d+\.\d+\.\d+),(.+),(\d+\.\d+\.\d+\.\d+\:\d+),(.+)", data) num=(len(tab1)+len(tab2))/2 print ("CHECKOPENVPN OK: %d connected" % num) sys.exit(0)