Commit fda329da by JR Dalrymple

It still works I think - I added the ability to list all the interfaces if you…

It still works I think - I added the ability to list all the interfaces if you just omit the '-i' argument, example:

[jrdalrymple@mn-lx9 pf_interface_usage]$ ./check_snmp_usage.py -H 192.168.0.1
No interface specified - here are your options:
pflow0
vlan301
vlan300
vlan1
vlan202
pflog0
vlan204
vlan200
vlan201
lo0
vlan203
enc0
em0
em3
em2
em1
vlan105
pppoe0
pflow1
parent fc236cec
...@@ -55,6 +55,37 @@ unitMultipliers = { 'b' : 1, ...@@ -55,6 +55,37 @@ unitMultipliers = { 'b' : 1,
limitMatch = re.compile(r"([0-9]+)([bBkKmMgGtT]{0,1})") limitMatch = re.compile(r"([0-9]+)([bBkKmMgGtT]{0,1})")
# Find snmpwalk and snmpget
sp = subprocess.Popen(['which', 'snmpget'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = sp.communicate()
if sp.returncode == 0:
snmpget = stdout.rstrip()
else:
print('I couldn\'t find snmpget on your system, unable to finish')
sys.exit(3)
sp = subprocess.Popen(['which', 'snmpwalk'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = sp.communicate()
if sp.returncode == 0:
snmpwalk = stdout.rstrip()
else:
print('I couldn\'t find snmpwalk on your system, unable to finish')
sys.exit(3)
# Functions
def getInterfaces():
interfaceDict = {}
walkIfsCmd = [snmpwalk, '-v2c', '-c', communityString, host, OIDIfDescr]
sp = subprocess.Popen(walkIfsCmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = sp.communicate()
walkOut = stdout.rstrip().split('\n')
for line in walkOut:
interfaceDict[line.split()[-1]] = line.split()[0].split('.')[-1]
return interfaceDict
#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')
...@@ -86,6 +117,13 @@ if not 'host' in locals(): ...@@ -86,6 +117,13 @@ if not 'host' in locals():
print('No host specified') print('No host specified')
sys.exit(3) sys.exit(3)
if not 'interface' in locals():
interfaceDict = getInterfaces()
print('No interface specified - here are your options:')
for interface in interfaceDict:
print(interface)
sys.exit(3)
if not 'bytesLimit' in locals(): if not 'bytesLimit' in locals():
bytesLimit = 0 bytesLimit = 0
if not limitMatch.match(bytesLimit): if not limitMatch.match(bytesLimit):
...@@ -96,39 +134,10 @@ else: ...@@ -96,39 +134,10 @@ else:
if multiplier: if multiplier:
bytesLimit = int(scalar) * int(unitMultipliers[multiplier]) bytesLimit = int(scalar) * int(unitMultipliers[multiplier])
# Find snmpwalk and snmpget
sp = subprocess.Popen(['which', 'snmpget'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = sp.communicate()
if sp.returncode == 0:
snmpget = stdout.rstrip()
else:
print('I couldn\'t find snmpget on your system, unable to finish')
sys.exit(3)
sp = subprocess.Popen(['which', 'snmpwalk'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = sp.communicate()
if sp.returncode == 0:
snmpwalk = stdout.rstrip()
else:
print('I couldn\'t find snmpwalk on your system, unable to finish')
sys.exit(3)
# Not optimized - we'll be getting all of the interfaces from the remote system and match up the indexes
# Start by getting the list of available interfaces interfaceDict = getInterfaces()
walkIfsCmd = [snmpwalk, '-v2c', '-c', communityString, host, OIDIfDescr]
sp = subprocess.Popen(walkIfsCmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = sp.communicate()
walkOut = stdout.rstrip().split('\n')
for line in walkOut:
if line.split()[-1] == interface:
index = line.split()[0].split('.')[-1]
if not 'index' in locals():
print('No matching interfaces found!')
sys.exit(3)
# Now let's grab our counters and the date of the remote system # Now let's grab our counters and the date of the remote system
...@@ -146,8 +155,8 @@ current['hour'] = time.split(':')[0] ...@@ -146,8 +155,8 @@ current['hour'] = time.split(':')[0]
current['minute'] = time.split(':')[1] current['minute'] = time.split(':')[1]
current['second'] = time.split(':')[2].split('.')[0] current['second'] = time.split(':')[2].split('.')[0]
getInBytesCmd = [snmpget, '-v2c', '-c', communityString, host, OIDIfInBytes + index] getInBytesCmd = [snmpget, '-v2c', '-c', communityString, host, OIDIfInBytes + interfaceDict[interface]]
getOutBytesCmd = [snmpget, '-v2c', '-c', communityString, host, OIDIfOutBytes + index] getOutBytesCmd = [snmpget, '-v2c', '-c', communityString, host, OIDIfOutBytes + interfaceDict[interface]]
sp = subprocess.Popen(getInBytesCmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) sp = subprocess.Popen(getInBytesCmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = sp.communicate() stdout, stderr = sp.communicate()
......
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