Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redefine run behaviour #7

Merged
merged 2 commits into from
Jun 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bundle.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "network-monitor",
"version": "0.9.3",
"version": "0.9.4",
"author": "Jimmy Chen",
"email": "jimmy.chen@moxa.com",
"description": "Monitoring network bandwidth; alert if reaching watermark.",
Expand Down
93 changes: 53 additions & 40 deletions netmon.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import subprocess
import xml.etree.ElementTree as ET
from time import sleep
from datetime import datetime, timedelta
from sanji.core import Sanji
from sanji.core import Route
from sanji.connection.mqtt import Mqtt
Expand Down Expand Up @@ -38,51 +39,63 @@ def init(self, *args, **kwargs):
self.do_start()

def read_bandwidth(self):
subprocess.call(["vnstat", "-u", "-i", self.model.db["interface"]])
tmp = subprocess.check_output("vnstat --xml -i " +
self.model.db["interface"] +
"|grep -m 1 total", shell=True)
root = ET.fromstring(tmp)
count = 0
for item in root:
count += int(item.text)
_logger.debug(
"Interface: %s Read Bandwidth %s" %
(self.model.db["interface"], count))
return count
try:
subprocess.call(["vnstat", "-u", "-i", self.model.db["interface"]])
tmp = subprocess.check_output("vnstat --xml -i " +
self.model.db["interface"] +
"|grep -m 1 total", shell=True)
root = ET.fromstring(tmp)
count = 0
for item in root:
count += int(item.text)
_logger.debug(
"Interface: %s Read Bandwidth %s" %
(self.model.db["interface"], count))
return count
except:
return 0

# This function will be executed after registered.
def run(self):

next_report_dt = datetime.utcnow()
prev_usage = None

while True:
# message of today
motd = 0
count = self.read_bandwidth()
if count < self.model.db["threshold"]:
sleep(60)
continue

while True:
if motd == 0:
_logger.debug(
"Interface: %s Reach limited threshold %s" %
(self.model.db["interface"],
self.model.db["threshold"]))

self.publish.event.put(
"/network/bandwidth/event",
data={
"info": self.read_bandwidth(),
"enable": self.model.db["enable"],
"interface": self.model.db["interface"],
"threshold": self.model.db["threshold"]
})

if motd >= 5 or self.read_bandwidth() == 0:
break

motd += 1
sleep(60)
if self.model.db["enable"]:
usage = self.read_bandwidth()
now_dt = datetime.utcnow()

if usage > self.model.db["threshold"]:
if now_dt - next_report_dt > timedelta(minutes=1):
next_report_dt = now_dt

# rapidly report if usage burst 1M under 5 seconds
if prev_usage is None or usage - prev_usage > 1024:
next_report_dt = now_dt

if next_report_dt > now_dt:
sleep(5)
continue

self.publish.event.put(
"/network/bandwidth/event",
data={
"info": self.read_bandwidth(),
"enable": self.model.db["enable"],
"interface": self.model.db["interface"],
"threshold": self.model.db["threshold"]
})

if usage >= self.model.db["threshold"]:
delta = timedelta(minutes=1)
else:
delta = timedelta(hours=1)

next_report_dt = now_dt + delta
prev_usage = usage
else:
sleep(1)

@Route(methods="get", resource="/network/bandwidth")
def get_root(self, message, response):
Expand Down