-
Notifications
You must be signed in to change notification settings - Fork 0
/
cumulus-show-vrf.py
52 lines (47 loc) · 1.86 KB
/
cumulus-show-vrf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Add @VRF to the process name if not in default.
# Use this on hosts where you have VRFs as l3mdev cgroups
# (like on Cumulus switches).
import os
try:
# If you have a l3mdev cgroup ...
os.stat('/sys/fs/cgroup/l3mdev')
except OSError as e:
if e.args[0] == 2: # ENOENT
pass
else:
raise
else:
# ... then add an adjust class to add "@VRF" to processes outside of
# default VRFs.
class ProcessFormatterMixin(object):
def adjust(self, process):
super(ProcessFormatterMixin, self).adjust(process)
try:
with open('/proc/{}/cgroup'.format(process.pid)) as fp:
groups = fp.read()
except IOError:
pass
else:
l3mdev = [
i for i in groups.split('\n') if ':l3mdev:' in groups]
if l3mdev:
l3mdev = l3mdev[0].split(':', 2)
assert len(l3mdev) == 3, l3mdev
assert l3mdev[2].startswith('/'), l3mdev
vrf = l3mdev[2][1:]
if vrf:
try:
name, rest = process.cmdline.split(' ', 1)
except ValueError:
process.cmdline = '{}@{}'.format(
process.cmdline, vrf)
else:
process.cmdline = '{}@{} {}'.format(
name, vrf, rest)
def include(self, process):
# We want to monitor these daemons, but not their
# (grand)children, as they come and go:
if process.has_parent(include_self=False, cmdline__startswith=(
'sshd:@', '/usr/sbin/sshd@')):
return False
return super(ProcessFormatterMixin, self).include(process)