Commit 0a6b29b0 by JR Dalrymple

If the SNMP date fails the script now falls back to current system time of the monitoring host

Also added change to handle '/' in interface names - since that's how the tmp file name is decided they had to be replaced so they didn't get interpreted as a directory marker
parent fda329da
2015|11|22|08|58|14|2832992006|3784422397|9913|24918
\ No newline at end of file
...@@ -54,6 +54,8 @@ unitMultipliers = { 'b' : 1, ...@@ -54,6 +54,8 @@ unitMultipliers = { 'b' : 1,
} }
limitMatch = re.compile(r"([0-9]+)([bBkKmMgGtT]{0,1})") limitMatch = re.compile(r"([0-9]+)([bBkKmMgGtT]{0,1})")
billingPeriodMatch = re.compile(r"([dDmM]{1})([0-9]{1,2})")
snmpDateTimeMatch = re.compile(r"[0-9]+-[0-9]+-[0-9]+,[0-9]+:[0-9]+:[0-9]")
# Find snmpwalk and snmpget # Find snmpwalk and snmpget
...@@ -90,7 +92,7 @@ def getInterfaces(): ...@@ -90,7 +92,7 @@ def getInterfaces():
def usage(): def usage():
print('Usage: check_snmp_usage.py -H hostname -i interface -b bandwidth limit (bytes) -c community string \n') print('Usage: check_snmp_usage.py -H hostname -i interface -b bandwidth limit (bytes) -c community string \n')
try: try:
opts, args = getopt.getopt(sys.argv[1:], 'H:i:b:cd') opts, args = getopt.getopt(sys.argv[1:], 'H:i:b:cdp')
except getopt.GetoptError as err: except getopt.GetoptError as err:
print(err) print(err)
usage() usage()
...@@ -107,6 +109,8 @@ for opt, arg in opts: ...@@ -107,6 +109,8 @@ for opt, arg in opts:
communityString = arg communityString = arg
elif opt in ('-d'): elif opt in ('-d'):
debug = 1 debug = 1
elif opt in ('-p'):
billingPeriodArg = arg
else: else:
usage() usage()
sys.exit(3) sys.exit(3)
...@@ -133,7 +137,28 @@ else: ...@@ -133,7 +137,28 @@ else:
multiplier = limitMatch.search(bytesLimit).group(2) multiplier = limitMatch.search(bytesLimit).group(2)
if multiplier: if multiplier:
bytesLimit = int(scalar) * int(unitMultipliers[multiplier]) bytesLimit = int(scalar) * int(unitMultipliers[multiplier])
# validate billing period data
if not 'billingPeriodArg' in locals():
billingPeriod = 0
else:
if not billingPeriodMatch.match(billingPeriodArg):
print('Could not validate billing period')
usage()
sys.exit(3)
else:
billingPeriod = billingPeriodMatch.search(billingPeriodArg).group(1)
billTime = billingPeriodMatch.search(billingPeriodArg).group(2)
if billingPeriod == 'M':
billingPeriod = 'm'
elif billingPeriod == 'D':
billingPeriod = 'd'
if billingPeriod == 'm':
if int(billTime) < 1 or int(billTime) > 31:
print('When using monthly billing cycle it is necessary to specify a day between 1 and 31 inclusive.')
sys.exit(3)
elif billingPeriod == 'd':
if int(billTime) < 0 or int(billTime) > 24:
print('When using daily billing cycle it is necessary to specify an hour between 0 and 24 inclusive.')
# Not optimized - we'll be getting all of the interfaces from the remote system and match up the indexes # Not optimized - we'll be getting all of the interfaces from the remote system and match up the indexes
...@@ -146,14 +171,31 @@ getDateTimeCmd = [snmpget, '-v2c', '-c', communityString, host, OIDDateTime] ...@@ -146,14 +171,31 @@ getDateTimeCmd = [snmpget, '-v2c', '-c', communityString, host, OIDDateTime]
sp = subprocess.Popen(getDateTimeCmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) sp = subprocess.Popen(getDateTimeCmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = sp.communicate() stdout, stderr = sp.communicate()
dateTime = stdout.rstrip().split()[-1] dateTime = stdout.rstrip().split()[-1]
date = dateTime.split(',')[0]
time = dateTime.split(',')[1]
current['year'] = date.split('-')[0] # If the SNMP query returned useful data bust it out into a dictionary
current['month'] = date.split('-')[1] if snmpDateTimeMatch.search(dateTime):
current['day'] = date.split('-')[2] date = dateTime.split(',')[0]
current['hour'] = time.split(':')[0] time = dateTime.split(',')[1]
current['minute'] = time.split(':')[1] current['year'] = date.split('-')[0]
current['second'] = time.split(':')[2].split('.')[0] current['month'] = date.split('-')[1]
current['day'] = date.split('-')[2]
current['hour'] = time.split(':')[0]
current['minute'] = time.split(':')[1]
current['second'] = time.split(':')[2].split('.')[0]
# We will have to use system time - SNMP time didn't work the way we wanted
else:
dateTime = str(datetime.datetime.now())
date = dateTime.split()[0]
time = dateTime.split()[1]
current['year'] = date.split('-')[0]
current['month'] = date.split('-')[1]
current['day'] = date.split('-')[2]
current['hour'] = time.split(':')[0]
current['minute'] = time.split(':')[1]
current['second'] = time.split(':')[2].split('.')[0]
getInBytesCmd = [snmpget, '-v2c', '-c', communityString, host, OIDIfInBytes + interfaceDict[interface]] getInBytesCmd = [snmpget, '-v2c', '-c', communityString, host, OIDIfInBytes + interfaceDict[interface]]
getOutBytesCmd = [snmpget, '-v2c', '-c', communityString, host, OIDIfOutBytes + interfaceDict[interface]] getOutBytesCmd = [snmpget, '-v2c', '-c', communityString, host, OIDIfOutBytes + interfaceDict[interface]]
...@@ -170,6 +212,9 @@ current['outbytes'] = stdout.rstrip().split()[-1] ...@@ -170,6 +212,9 @@ current['outbytes'] = stdout.rstrip().split()[-1]
tmpFile = '/tmp/check_snmp_usage_' + host + '_' + interface + '.tmp' tmpFile = '/tmp/check_snmp_usage_' + host + '_' + interface + '.tmp'
# If the interface name has forward slashes (Cisco) they need to be replaced
tmpFile = tmpFile.replace('/', '_')
if os.path.isfile(tmpFile): if os.path.isfile(tmpFile):
f = open(tmpFile, 'r') f = open(tmpFile, 'r')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment