Commit 857b910a by JR Dalrymple

Unit conversions are working for the usage limit argument

parent 796dde7c
...@@ -38,6 +38,18 @@ OIDIfDescr = '.1.3.6.1.2.1.2.2.1.2' ...@@ -38,6 +38,18 @@ OIDIfDescr = '.1.3.6.1.2.1.2.2.1.2'
OIDIfInBytes = '.1.3.6.1.2.1.2.2.1.10.1.' OIDIfInBytes = '.1.3.6.1.2.1.2.2.1.10.1.'
OIDIfOutBytes = '.1.3.6.1.2.1.2.2.1.16.1.' OIDIfOutBytes = '.1.3.6.1.2.1.2.2.1.16.1.'
unitMultipliers = { 'k' : 1024,
'K' : 1024,
'm' : 1024 * 1024,
'M' : 1024 * 1024,
'g' : 1024 * 1024 * 1024,
'G' : 1024 * 1024 * 1024,
't' : 1024 * 1024 * 1024 * 1024,
'T' : 1024 * 1024 * 1024 * 1024,
}
limitMatch = re.compile(r"([0-9]+)([kKmMgGtT]{0,1})")
#USAGE #USAGE
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')
...@@ -61,10 +73,22 @@ for opt, arg in opts: ...@@ -61,10 +73,22 @@ for opt, arg in opts:
usage() usage()
sys.exit(3) sys.exit(3)
# Argument processing
if not 'host' in locals(): if not 'host' in locals():
print('No host specified') print('No host specified')
sys.exit(3) sys.exit(3)
if not 'bytesLimit' in locals():
bytesLimit = 0
if not limitMatch.match(bytesLimit):
print('Could not validate limit argument')
else:
scalar = limitMatch.search(bytesLimit).group(1)
multiplier = limitMatch.search(bytesLimit).group(2)
if multiplier:
bytesLimit = int(scalar) * int(unitMultipliers[multiplier])
# Find snmpwalk and snmpget # Find snmpwalk and snmpget
sp = subprocess.Popen(['which', 'snmpget'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) sp = subprocess.Popen(['which', 'snmpget'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
...@@ -219,5 +243,5 @@ monthComplete = ( float(secondsIntoMonth.total_seconds()) / float(secondsInMonth ...@@ -219,5 +243,5 @@ monthComplete = ( float(secondsIntoMonth.total_seconds()) / float(secondsInMonth
totalDataUsed = int(inBytesTotal) + int(outBytesTotal) totalDataUsed = int(inBytesTotal) + int(outBytesTotal)
percentageDataUsed = ( float(totalDataUsed) / float(bytesLimit) ) * 100 percentageDataUsed = ( float(totalDataUsed) / float(bytesLimit) ) * 100
print(monthComplete)
print(percentageDataUsed) print percentageDataUsed
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