Skip to content

Instantly share code, notes, and snippets.

@adimania
Created December 19, 2014 13:05
Show Gist options
  • Save adimania/d465fa9c83ca6fcaf3d3 to your computer and use it in GitHub Desktop.
Save adimania/d465fa9c83ca6fcaf3d3 to your computer and use it in GitHub Desktop.
Nagios Bandwidth Check
import subprocess
import time
import json
import re
import pynsca
import sys
from pynsca import NSCANotifier
'''
We need to alert on 30 Mbps. ifconfig returns bytes.
10Mbps = 1280 KBps
20Mbps = 2560 KBps
30Mbps = 3840 KBps
'''
p=subprocess.Popen(['ifconfig','eth0'], stdout=subprocess.PIPE).communicate()
crit_flag = 0
rx_bytes_prev = long(re.compile(r'RX bytes:(\d+)').findall(p[0])[0])
tx_bytes_prev = long(re.compile(r'TX bytes:(\d+)').findall(p[0])[0])
rx_record = [0]*60
tx_record = [0]*60
notif = NSCANotifier(monitoring_server='nagios.myhost.com', password='mypassword')
notif.svc_result(sys.argv[1], 'bandwidth check', pynsca.OK, json.dumps({'download':'init', 'upload':'init'} ))
while True:
p=subprocess.Popen(['ifconfig','eth0'], stdout=subprocess.PIPE).communicate()
rx_bytes = long(re.compile(r'RX bytes:(\d+)').findall(p[0])[0])
tx_bytes = long(re.compile(r'TX bytes:(\d+)').findall(p[0])[0])
bw_rx = (rx_bytes - rx_bytes_prev)/1024
bw_tx = (tx_bytes - tx_bytes_prev)/1024
if rx_bytes < 0 or tx_bytes < 0 or bw_rx < 0 or bw_tx < 0:
continue
rx_bytes_prev = rx_bytes
tx_bytes_prev = tx_bytes
rx_record.pop(0)
tx_record.pop(0)
rx_record.append(bw_rx)
tx_record.append(bw_tx)
if sum(rx_record)/len(rx_record) > 6400 or sum(tx_record)/len(tx_record) > 6400:
notif.svc_result(sys.argv[1], 'bandwidth check', pynsca.CRITICAL, json.dumps({'download':sum(rx_record)/len(rx_record), 'upload':sum(tx_record)/len(tx_record), 'download_data':rx_record, 'upload_data':tx_record}))
crit_flag=1
time.sleep(2)
elif crit_flag == 1:
notif.svc_result(sys.argv[1], 'bandwidth check', pynsca.OK, json.dumps({'download':sum(rx_record)/len(rx_record), 'upload':sum(tx_record)/len(tx_record), 'download_data':rx_record[:20], 'upload_data':tx_record[:20] }))
crit_flag=0
time.sleep(1)
import subprocess
import time
import json
import pynsca
import sys
import re
from pynsca import NSCANotifier
'''
We need to alert on 30 Mbps. ifconfig returns bytes.
10Mbps = 1280 KBps
20Mbps = 2560 KBps
30Mbps = 3840 KBps
'''
p=subprocess.Popen(['ifconfig','eth0'], stdout=subprocess.PIPE).communicate()
crit_flag = 0
rx_bytes_prev = long(re.compile(r'RX bytes:(\d+)').findall(p[0])[0])
tx_bytes_prev = long(re.compile(r'TX bytes:(\d+)').findall(p[0])[0])
notif = NSCANotifier(monitoring_server='nagios.myhost.com', password='mypassword')
notif.svc_result(sys.argv[1], 'bandwidth check', pynsca.OK, json.dumps({'download':'init', 'upload':'init'} ))
while True:
p=subprocess.Popen(['ifconfig','eth0'], stdout=subprocess.PIPE).communicate()
rx_bytes = long(re.compile(r'RX bytes:(\d+)').findall(p[0])[0])
tx_bytes = long(re.compile(r'TX bytes:(\d+)').findall(p[0])[0])
# below is to get KBps
bw_rx = (rx_bytes - rx_bytes_prev)/1024
bw_tx = (tx_bytes - tx_bytes_prev)/1024
if rx_bytes < 0 or tx_bytes < 0 or bw_rx < 0 or bw_tx < 0:
continue
rx_bytes_prev = rx_bytes
tx_bytes_prev = tx_bytes
if bw_rx > 6400 or bw_tx > 6400:
notif.svc_result(sys.argv[1], 'bandwidth check', pynsca.CRITICAL, json.dumps({'download':bw_rx, 'upload':bw_tx}))
crit_flag=1
elif crit_flag == 1:
notif.svc_result(sys.argv[1], 'bandwidth check', pynsca.OK, json.dumps({'download':bw_rx, 'upload':bw_tx}))
crit_flag = 0
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment