-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-gw.py
executable file
·213 lines (180 loc) · 6.26 KB
/
check-gw.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/python -t
# -------------------------------------------------------------------------------
# Copyright: 2011 (c) Mauro Ferrigno
# License: GPL2
#
# 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 (or with Nagios); if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA
# -------------------------------------------------------------------------------
# Credits go to Ethan Galstad for coding Nagios
# If any changes are made to this script, please mail me a copy of the changes
#
from optparse import OptionParser
import os
import subprocess
import re
import time
import string
import sys
__author__="Mauro Ferrigno mauro@flink.it"
#listhost=['151.1.1.1','www.google.it','193.43.2.1','dns.nic.it','151.1.1.1']
listhost=['222.222.222.221','111.222.222.221'] # PING HOST
listgw=['10.0.1.250','10.0.1.254'] # GATEWAY HOST
class ping:
def __init__(self,lifeline="NONE",report="NONE",verbose=None):
self.lifeline = re.compile(r"(\d) received")
self.report = ("No response","Partial Response","Alive")
self.countok=[]
self.verbose=verbose
def pinghost(self,listhost):
''' RITORNA L' IP DEGLI HOST RAGGIUNGIBILE'''
self.listhost=listhost
for host in self.listhost:
ip = str(host)
pingaling = os.popen("ping -w2 -q -c2 "+ip,"r")
if self.verbose :
print "Testing ",ip,
sys.stdout.flush()
while 1:
line = pingaling.readline()
if not line: break
igot = re.findall(self.lifeline,line)
if igot:
if self.verbose :
print self.report[int(igot[0])]
if int(igot[0]) == 2:
self.countok.append(host)
return self.countok
def calcavaibility(self,al,warn,crit,verbose="NONE"):
"""CALCOLA LA PERCENTUALE PER LE SOGLIE WARN E CRIT"""
self.al = al
self.crit = crit
self.warn = warn
pingok = float(len(self.al))
ghost = float(len(self.listhost))
result = '%.0f' %(round((pingok/ghost) *100))
if self.verbose :
print "WARN "+str(warn)
print "CRIT "+str(crit)
print "RESULT "+result
if int(result) < int(crit):
return " CRITICAL "+result+"% PACKET RECEIVED"
if int(result) < int(warn):
return " WARNING "+result+"% PACKET RECEIVED"
return " OK "+result+"% PACKET RECEIVED"
################# END CLASS PING ########
class parseinput:
def __init__(self,options=None,args=None):
parser = OptionParser()
parser.add_option("--warn",dest="warn",default="None",help="Warning threshold")
parser.add_option("--crit",dest="crit",default="None",help="Critical threshold")
parser.add_option("--state",dest="state",default="OK",help="State returned by host-alive")
parser.add_option("--verb",action="store_true", dest="verb",help="Verbose ping")
(options, args) = parser.parse_args()
self.options=options
self.args=args
options.fargs=' '.join(args)
self.fargs = options.fargs
if len(sys.argv)==1:
parser.error("-h for help")
if options.warn == "None":
parser.error("options --wan not found")
if options.crit == "None":
parser.error("options --crit not found")
################ END CLASS PARSEINPUT ##############
class managegw:
def __init__(self,listgw,verbose=None):
self.verbose=verbose
self.listgw=listgw
self.al=[]
for elem in os.popen("route -n").read().split("\n"):
if re.match("^[0-9]", elem.strip()):
self.al.append(elem)
self.activegw = self.al[-1].split()[1]
self.othergw = filter(lambda x: x!=self.activegw,listgw)
def getactivegw(self):
return self.activegw
def getothergw(self):
return str(self.othergw[0])
@staticmethod
def _add_delete_cmd(command,gateway,ip):
''' Returns proper iproute command arguments to add and delete routes '''
cmd = 'route %s default gw %s' % (command, gateway)
if ip is not None:
cmd = 'route %s -host %s gw %s' % (command, ip, gateway)
return cmd
@staticmethod
def add(gateway=None, ip=None):
''' Adds a new route with corresponding properties. '''
cmd = managegw._add_delete_cmd('add',gateway,ip)
iproute(cmd)
return cmd
@staticmethod
def delete(gateway=None,ip=None):
''' Removes a route with corresponding properties. '''
cmd = managegw._add_delete_cmd('del',gateway,ip)
iproute(cmd)
return cmd
################ END CLASS managegw ##############
############### MISC FUNC ###############
def statuscode(checkval):
if re.match(r'^.*OK',checkval,re.IGNORECASE):
return 0
elif re.match(r'^.*WARNING',checkval,re.IGNORECASE):
return 1
elif re.match(r'^.*CRITICAL',checkval,re.IGNORECASE):
return 2
else:
return 3
################ END FUNCTION STATUSCODDE ##############
def iproute(args):
'''An iproute wrapper'''
args_list = args.split()
cmd = args_list
proc = subprocess.Popen(cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
close_fds=True)
stdout_value, stderr_value = proc.communicate()
if stderr_value:
raise NameError(stderr_value)
if stdout_value:
return stdout_value
################ END FUNCTION IPROUTE ##############
def main():
pi = parseinput()
checkalive=pi.options.state
verbmode = pi.options.verb
a = ping(verbose=verbmode)
if checkalive == 'CRITICAL': # UTILIZZATO PER LE VERIFICHE DI CAMBIO GW
''' PING TEST FAIL SET OTHERGW AND TEST OTHERGW'''
activegw = managegw(listgw).getactivegw()
othergw = managegw(listgw).getothergw()
managegw.add(othergw)
managegw.delete(activegw)
# listhost1=['151.1.1.1','193.43.2.1','173.203.44.122','151.1.1.1']
pingresults = a.pinghost(listhost)
resultcode = a.calcavaibility(pingresults,pi.options.warn,pi.options.crit)
if statuscode(resultcode) > 1:
''' PING TEST FOR OTHERGW RESTORE CONFIG AND EXIT CRITICAL ERROR'''
managegw.add(activegw)
managegw.delete(othergw)
print resultcode
sys.exit(statuscode(' CRITICAL'))
print " OK"
sys.exit(0)
if __name__=="__main__":
main()