Permalink
Browse files

Merge pull request #2 from pcarrier/master

linux_netconfig.py progress, linux_netstat.py
  • Loading branch information...
thatch45 committed Nov 27, 2011
2 parents 9b22afa + 23c905d commit 4bc6a66eca26ae381c2e9ed7a1f4f149abddecb7
Showing with 122 additions and 17 deletions.
  1. +93 −17 modules/linux_netconfig.py
  2. +29 −0 modules/linux_netstat.py
View
@@ -11,6 +11,7 @@ def __virtual__():
"""
return 'netconfig' if __grains__['kernel'] == 'Linux' else False
+# num name flags extra link addr brd
LINK_MATCHER = re.compile(r"""
^
(?P<num> [0-9] +?) :
@@ -25,6 +26,7 @@ def __virtual__():
""", re.X | re.M)
+# num name type addr brd? scope alias? extra?
ADDR_MATCHER = re.compile(r"""
^
(?P<num> [0-9] +?) :
@@ -46,6 +48,20 @@ def __virtual__():
$
""", re.X | re.M)
+# addr dev lladdr? state?
+NEIGH_MATCHER = re.compile(r"""
+ ^
+ (?P<addr> [^ ] +)
+ \ dev\ (?P<dev> [^ ] +)
+ (?:
+ \ lladdr\ (?P<lladdr> [^ ]+)
+ )?
+ \ (?P<state> [A-Z]+)?
+ $
+ """, re.X | re.M)
+
+
+
def _int_if_possible(string):
"""
PRIVATE METHOD
@@ -108,6 +124,21 @@ def _structured_addr(match):
return res
+def _structured_neigh(match):
+ """
+ PRIVATE METHOD
+ Turns a NEIGH_MATCHER match into structured data
+ """
+ identifier = (match.group('addr'), match.group('dev'))
+ infos = {}
+ state = match.group('state')
+ lladdr = match.group('lladdr')
+ if state:
+ infos['state'] = state
+ if lladdr:
+ infos['lladdr'] = lladdr
+ return identifier, infos
+
def _structured_links_output(output):
"""
PRIVATE METHOD
@@ -119,6 +150,7 @@ def _structured_links_output(output):
if link_match:
name, infos = _structured_link(link_match)
res[name] = infos
+
return res
def _structured_addresses_output(output):
@@ -136,7 +168,21 @@ def _structured_addresses_output(output):
return res
-def all_links():
+def _structured_neigh_output(output):
+ """
+ PRIVATE METHOD
+ Return a dictionary mapping address and device to neighborhood information from the ip output
+ """
+ res = {}
+ for line in iter(output.splitlines()):
+ neigh_match = NEIGH_MATCHER.match(line)
+ if neigh_match:
+ identifier, infos = _structured_neigh(neigh_match)
+ res[identifier] = infos
+
+ return res
+
+def links():
"""
Return information about all network links on the system
"""
@@ -152,30 +198,60 @@ def link(name):
if match:
return _structured_link(LINK_MATCHER.match(output))
-def all_addresses():
+def addresses_with_options(options):
"""
- Return information about addresses for all network links on the system
+ Return information about addresses for a given "ip addr show" set of options
+ eg netconfig.addresses_with_options 'scope host'
"""
- output = __salt__['cmd.run']('ip -o addr show')
+ output = __salt__['cmd.run']('ip -o addr show {0}'.format(options))
return _structured_addresses_output(output)
-def addresses(name):
+def addresses():
+ """
+ Return information about addresses for all network links on the system
+ """
+ return addresses_with_options('')
+
+def addresses_for(name):
"""
Return information about addresses for a given network link on the system
"""
- output = __salt__['cmd.run']('ip -o addr show {0}'.format(name))
- parsed = _structured_addresses_output(name)
+ parsed = addresses_with_options('dev {0}'.format(name))
if parsed.has_key(name):
- return res[name]
+ return parsed[name]
+def neighbours_with_options(options):
+ """
+ Return information about neighbours for a given "ip neigh show" set of options
+ eg netconfig.neighbours_with_options 'nud noarp'
+ """
+ output = __salt__['cmd.run']('ip -o neigh show {0}'.format(options))
+ return _structured_neigh_output(output)
+
+def neighbours():
+ """
+ Return information about all known neighbours
+ """
+ return neighbours_with_options('')
+
+def neighbours_for(name):
+ """
+ Return information about neighbours for a given network link on the system
+ """
+ return neighbours_with_options('dev {0}'.format(name))
+
+def all_neighbours():
+ """
+ Return information about all attempted neighboors, including failed ones
+ """
+ return neighbours_with_options('nud all')
-# TODO: ip addr show
-# TODO: ip link show
-# TODO: ip neigh show
-# TODO: ip tunnel show
-# TODO: ip route show table all
-# TODO: ip maddr show
-# TODO: ip mroute show
-# TODO: ifenslave -a
-# TODO: netstat -s
# TODO: brctl show
+# TODO: ip maddr show
+# TODO: ifenslave -a (not sure how parseable this is)
+
+# For networking nerds:
+# TODO: brctl showmacs name
+# TODO: ip tunnel show
+# TODO: ip route show table all (looks like hell)
+# TODO: ip mroute show
View
@@ -0,0 +1,29 @@
+def __virtual__():
+ """
+ Only run on Linux systems
+ """
+ return 'netstat' if __grains__['kernel'] == 'Linux' else False
+
+def s():
+ """
+ Return the statistics available in netstat -s.
+ The netstat command is not needed: we use kernel-provided files directly.
+ """
+ stats = {}
+ lines = open('/proc/net/netstat').readlines() + \
+ open('/proc/net/snmp').readlines()
+
+ currently_in_header_line = True
+ for line in lines:
+ sections = line.split(': ')
+ prefix, list = sections[0], sections[1].strip()
+ stats.setdefault(prefix, {})
+ items = list.split(' ')
+ if currently_in_header_line:
+ headers = items
+ else:
+ for pos in range(len(headers)):
+ stats[prefix][headers[pos]] = int(items[pos])
+ currently_in_header_line = not currently_in_header_line
+
+ return stats

0 comments on commit 4bc6a66

Please sign in to comment.