Commit 4abd5421 by JR Dalrymple

Pulls in the data and prints out some useful junk - gotta add a couple of…

Pulls in the data and prints out some useful junk - gotta add a couple of metrics to the tmp file so that it can handle situations where the snmp counter rolls or is reset
parent 22689d9f
......@@ -29,6 +29,7 @@ import sys
import getopt
import re
import subprocess
import datetime
host = ''
communityString = 'public'
......@@ -100,28 +101,79 @@ if not 'index' in locals():
# Now let's grab our counters and the date of the remote system
current = {}
getDateTimeCmd = [snmpget, '-v2c', '-c', communityString, host, OIDDateTime]
sp = subprocess.Popen(getDateTimeCmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = sp.communicate()
dateTime = stdout.rstrip().split()[-1]
date = dateTime.split(',')[0]
time = dateTime.split(',')[1]
year = date.split('-')[0]
month = date.split('-')[1]
day = date.split('-')[2]
hour = time.split(':')[0]
minute = time.split(':')[1]
second = time.split(':')[2]
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 + index]
getOutBytesCmd = [snmpget, '-v2c', '-c', communityString, host, OIDIfOutBytes + index]
sp = subprocess.Popen(getInBytesCmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = sp.communicate()
inBytes = stdout.rstrip().split()[-1]
current['inbytes'] = stdout.rstrip().split()[-1]
sp = subprocess.Popen(getOutBytesCmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = sp.communicate()
outBytes = stdout.rstrip().split()[-1]
current['outbytes'] = stdout.rstrip().split()[-1]
# We have all the data we'll need to recourd the new line in our tmp file
outLine = current['year'] + '|' + current['month'] + '|' + current['day'] + '|' + current['hour'] + '|' + current['minute'] + '|' + current['second'] + '|' + current['inbytes'] + '|' + current['outbytes']
# Time to see if we have a temp file in place or not and read it if we do
tmpFile = '/tmp/check_snmp_usage_' + host + '_' + interface + '.tmp'
if os.path.isfile(tmpFile):
f = open(tmpFile, 'r')
fileLines = f.readlines()
f.close
lastList = fileLines[-1].rstrip().split('|')
last = {}
last['year'] = lastList[0]
last['month'] = lastList[1]
last['day'] = lastList[2]
last['hour'] = lastList[3]
last['minute'] = lastList[4]
last['second'] = lastList[5]
last['inbytes'] = lastList[6]
last['outbytes'] = lastList[7]
else:
f = open(tmpFile, 'a')
f.write(outLine)
f.close()
print('First run - no status update until the next run')
sys.exit(3)
# Get all of the differences
format = '%Y %m %d %H %M %S'
lastDateStr = last['year'] + ' ' + last['month'] + ' ' + last['day'] + ' ' + last['hour'] + ' ' + last['minute'] + ' ' + last['second']
currentDateStr = current['year'] + ' ' + current['month'] + ' ' + current['day'] + ' ' + current['hour'] + ' ' + current['minute'] + ' ' + current['second']
lastDateTime = datetime.datetime.strptime(lastDateStr, format)
currentDateTime = datetime.datetime.strptime(currentDateStr, format)
timeDifference = currentDateTime - lastDateTime
inBytesDifference = int(current['inbytes']) - int(last['inbytes'])
outBytesDifference = int(current['outbytes']) - int(last['outbytes'])
print(timeDifference.total_seconds())
print(inBytesDifference)
print(outBytesDifference)
inBytesPerSec = int(inBytesDifference) / int(timeDifference.total_seconds())
outBytesPerSec = int(outBytesDifference) / int(timeDifference.total_seconds())
print(str(inBytesPerSec) + ' bytes in per second')
print(str(outBytesPerSec) + ' bytes out per second')
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