Created
June 15, 2015 08:04
-
-
Save olavmrk/2da82d22902b4b9b071b to your computer and use it in GitHub Desktop.
Parse /proc/net/snmp from python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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