49 lines
1.5 KiB
Python
Executable file
49 lines
1.5 KiB
Python
Executable file
#!/usr/bin/python3
|
|
|
|
import sys
|
|
|
|
with open('/proc/meminfo', 'r') as f:
|
|
lines = f.read().splitlines()
|
|
|
|
mem = {x.split(':',1)[0] : int( x.split(':',1)[1].replace('kB', '').lstrip().rstrip() ) for x in lines}
|
|
|
|
mem['MemUsed'] = mem['MemTotal'] - mem['MemFree'] - mem ['Cached'] - mem['SReclaimable'] - mem['Buffers']
|
|
mem['SwapUsed'] = mem['SwapTotal'] - mem['SwapFree']
|
|
|
|
mem['OverallUsed'] = mem['MemUsed'] + mem['SwapUsed']
|
|
|
|
ratio = float(mem['OverallUsed']) / float(mem['MemTotal']) * 100.0
|
|
|
|
|
|
#print('MemTotal: %8.2f MB' % (mem['MemTotal']/1024) )
|
|
#print('OverallUsed: %8.2f MB' % (mem['OverallUsed']/1024) )
|
|
#print('Ratio: %8.2f %%' % ratio )
|
|
#print('MemUsed: %8.2f MB' % (mem['MemUsed']/1024) )
|
|
#print('SwapUsed: %8.2f MB' % (mem['SwapUsed']/1024) )
|
|
|
|
out = 'MemRatio: %.1f%%/%.1fGB of %.1fGB RAM (and %.1fGB Swap) used' % ( ratio, (mem['OverallUsed']/1024.0/1024.0), (mem['MemTotal']/1024.0/1024.0), (mem['SwapTotal']/1024.0/1024.0) )
|
|
|
|
if ratio > 150.0:
|
|
exitcode = 2
|
|
exitstr = 'CRITICAL'
|
|
elif ratio > 100.0:
|
|
exitcode = 1
|
|
exitstr = 'WARNING'
|
|
else:
|
|
exitcode = 0
|
|
exitstr = 'OK'
|
|
|
|
multiout = []
|
|
for label in ['MemTotal', 'OverallUsed', 'MemUsed', 'SwapTotal', 'SwapUsed']:
|
|
multiout.append('%s: %.4fGB' % (label, float(mem[label])/1024.0/1024.0))
|
|
|
|
perfdata = []
|
|
for label in ['MemTotal', 'OverallUsed', 'MemUsed', 'SwapTotal', 'SwapUsed']:
|
|
perfdata.append('%s=%dB;;;;' % (label, mem[label]*1024) )
|
|
|
|
|
|
print(exitstr + ' - ' + out + '|' + ' '.join(perfdata))
|
|
print('\n'.join(multiout))
|
|
sys.exit(exitcode)
|
|
|