Skip to content

Instantly share code, notes, and snippets.

@olavmrk
Created June 15, 2015 08:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olavmrk/2da82d22902b4b9b071b to your computer and use it in GitHub Desktop.
Save olavmrk/2da82d22902b4b9b071b to your computer and use it in GitHub Desktop.
Parse /proc/net/snmp from python
#!/usr/bin/env python
from __future__ import print_function
from __future__ import unicode_literals
values = {}
field_headers_type = None
field_headers = None
with open('/proc/net/snmp') as fh:
for line in fh:
line = line.strip()
if not line:
continue # Trailing empty line
field_type, field_values = line.split(': ', 1)
field_values = field_values.split(' ')
if field_headers_type != field_type:
# We have a new header line.
field_headers_type = field_type
field_headers = field_values
else:
# This is a line with data values
field_values = [ long(v) for v in field_values ]
values[field_type] = dict(zip(field_headers, field_values))
import pprint
pprint.pprint(values)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment