Commit 796dde7c by JR Dalrymple

Right now the plugin outputs percentages of month complete and data used.

Need to add error checking for the -b argument and lots of other stuff, coming along though
parent 4abd5421
...@@ -126,10 +126,6 @@ sp = subprocess.Popen(getOutBytesCmd, stdout=subprocess.PIPE, stderr=subprocess. ...@@ -126,10 +126,6 @@ sp = subprocess.Popen(getOutBytesCmd, stdout=subprocess.PIPE, stderr=subprocess.
stdout, stderr = sp.communicate() stdout, stderr = sp.communicate()
current['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 # 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' tmpFile = '/tmp/check_snmp_usage_' + host + '_' + interface + '.tmp'
...@@ -149,7 +145,10 @@ if os.path.isfile(tmpFile): ...@@ -149,7 +145,10 @@ if os.path.isfile(tmpFile):
last['second'] = lastList[5] last['second'] = lastList[5]
last['inbytes'] = lastList[6] last['inbytes'] = lastList[6]
last['outbytes'] = lastList[7] last['outbytes'] = lastList[7]
last['inbytestotal'] = lastList[8]
last['outbytestotal'] = lastList[9]
else: else:
outLine = current['year'] + '|' + current['month'] + '|' + current['day'] + '|' + current['hour'] + '|' + current['minute'] + '|' + current['second'] + '|' + current['inbytes'] + '|' + current['outbytes'] + '|0|0'
f = open(tmpFile, 'a') f = open(tmpFile, 'a')
f.write(outLine) f.write(outLine)
f.close() f.close()
...@@ -166,14 +165,59 @@ lastDateTime = datetime.datetime.strptime(lastDateStr, format) ...@@ -166,14 +165,59 @@ lastDateTime = datetime.datetime.strptime(lastDateStr, format)
currentDateTime = datetime.datetime.strptime(currentDateStr, format) currentDateTime = datetime.datetime.strptime(currentDateStr, format)
timeDifference = currentDateTime - lastDateTime 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()) if int(current['inbytes']) > int(last['inbytes']) and int(current['outbytes']) > int(last['outbytes']):
outBytesPerSec = int(outBytesDifference) / int(timeDifference.total_seconds()) inBytesDifference = int(current['inbytes']) - int(last['inbytes'])
outBytesDifference = int(current['outbytes']) - int(last['outbytes'])
inBytesPerSec = int(inBytesDifference) / int(timeDifference.total_seconds())
outBytesPerSec = int(outBytesDifference) / int(timeDifference.total_seconds())
inBytesTotal = int(last['inbytestotal']) + int(inBytesDifference)
outBytesTotal = int(last['outbytestotal']) + int(outBytesDifference)
outLine = current['year'] + '|' + current['month'] + '|' + current['day'] + '|' + current['hour'] + '|' + current['minute'] + '|' + current['second'] + '|' + current['inbytes'] + '|' + current['outbytes'] + '|' + str(inBytesTotal) + '|' + str(outBytesTotal)
fileLines[-1] = outLine
f = open(tmpFile, 'w')
f.write(outLine)
f.close()
else:
print('Seems the counter went whacky, we need to reset things')
print('expect results after the next check iteration.')
print('If you continue to see this message something is very wrong.')
outLine = current['year'] + '|' + current['month'] + '|' + current['day'] + '|' + current['hour'] + '|' + current['minute'] + '|' + current['second'] + '|' + current['inbytes'] + '|' + current['outbytes'] + '|' + last['inbytestotal'] + '|' + last['outbytestotal']
fileLines[-1] = outLine
f = open(tmpFile, 'w')
f.write(outLine)
f.close()
sys.exit(3)
# Files should be done, time to do Nagios logic
# Calculate number of seconds into month so far
beginningOfMonthStr = current['year'] + ' ' + current['month'] + ' 1 00 00 00'
beginningOfMonthDateTime = datetime.datetime.strptime(beginningOfMonthStr, format)
secondsIntoMonth = currentDateTime - beginningOfMonthDateTime
# Calculate number of seconds in the current month
if current['month'] == '12':
nextMonth = '1'
nextYear = int(current['year']) + 1
else:
nextMonth = int(current['month']) + 1
nextYear = current['year']
beginningOfNextMonthStr = str(nextYear) + ' ' + str(nextMonth) + ' 1 00 00 00'
beginningOfNextMonthDateTime = datetime.datetime.strptime(beginningOfNextMonthStr, format)
secondsInMonth = beginningOfNextMonthDateTime - beginningOfMonthDateTime
# Percentage of month complete
monthComplete = ( float(secondsIntoMonth.total_seconds()) / float(secondsInMonth.total_seconds()) ) * 100
# Percentage of data used
# Assume that total allotment is inbytes and outbytes combined
print(str(inBytesPerSec) + ' bytes in per second') totalDataUsed = int(inBytesTotal) + int(outBytesTotal)
print(str(outBytesPerSec) + ' bytes out per second') percentageDataUsed = ( float(totalDataUsed) / float(bytesLimit) ) * 100
print(monthComplete)
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