Skip to content

Adding code to control network devices monitored #7

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
13 changes: 9 additions & 4 deletions bin/net_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def get_sys_net(iface, sys):
return (p.returncode, stdout.strip())

class NetMonitor():
def __init__(self, hostname, diag_hostname):
def __init__(self, hostname, diag_hostname, dev_regex):
self._diag_pub = rospy.Publisher('/diagnostics', DiagnosticArray, queue_size = 100)
self._mutex = threading.Lock()
self._net_level_warn = rospy.get_param('~net_level_warn', net_level_warn)
Expand All @@ -117,6 +117,7 @@ def __init__(self, hostname, diag_hostname):
value = 'N/A') ]
self._last_usage_time = 0
self._last_publish_time = 0
self._dev_regex = dev_regex
self.check_usage()

def cancel_timers(self):
Expand Down Expand Up @@ -154,7 +155,7 @@ def check_network(self):
(retcode, cmd_out) = get_sys_net(ifaces[i], 'operstate')
if retcode == 0:
values.append(KeyValue(key = 'State', value = cmd_out))
ifacematch = re.match('eth[0-9]+', ifaces[i])
ifacematch = re.match(self._dev_regex, ifaces[i])
if ifacematch and (cmd_out == 'down' or cmd_out == 'dormant'):
level = DiagnosticStatus.ERROR
values.append(KeyValue(key = 'Input Traffic',
Expand Down Expand Up @@ -239,19 +240,23 @@ def publish_stats(self):
import optparse
parser =\
optparse.OptionParser(
usage="usage: net_monitor.py [--diag-hostname=cX]")
usage="usage: net_monitor.py [--diag-hostname=cX] [--dev-regex=ethX]")
parser.add_option("--diag-hostname", dest="diag_hostname",
help="Computer name in diagnostics output (ex: 'c1')",
metavar="DIAG_HOSTNAME",
action="store", default = hostname)
parser.add_option("--dev-regex", dest="dev_regex",
help="Device regular expression to match (ex: 'eth[1,2]')",
metavar="DEV_REGEX",
action="store", default = 'eth[0-9]+')
options, args = parser.parse_args(rospy.myargv())
try:
rospy.init_node('net_monitor_%s' % hostname)
except rospy.exceptions.ROSInitException:
print >> sys.stderr,\
'Network monitor is unable to initialize node. Master may not be running.'
sys.exit(0)
net_node = NetMonitor(hostname, options.diag_hostname)
net_node = NetMonitor(hostname, options.diag_hostname, options.dev_regex)
rate = rospy.Rate(1.0)
try:
while not rospy.is_shutdown():
Expand Down
3 changes: 2 additions & 1 deletion launch/system_monitor.launch
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<arg name="machine_name" default="$(optenv HOSTNAME localhost)"/>
<arg name="config_file" default="$(find system_monitor)/config/system_monitor.yaml"/>
<arg name="output" default="log"/>
<arg name="net_dev_regex" default="eth[0-9]+"/>

<group ns="system_monitor/$(arg machine_name)">
<node name="cpu_monitor" pkg="system_monitor" type="cpu_monitor.py"
Expand All @@ -13,7 +14,7 @@
<node name="ntp_monitor" pkg="system_monitor" type="ntp_monitor.py"
output="$(arg output)" respawn="true"/>
<node name="net_monitor" pkg="system_monitor" type="net_monitor.py"
output="$(arg output)" respawn="true"/>
output="$(arg output)" respawn="true" args="--dev-regex='$(arg net_dev_regex)'"/>
</group>

<rosparam command="load" file="$(arg config_file)"
Expand Down