Last active
December 28, 2017 15:22
-
-
Save jceloria/0f1822d47cdf467201398690fc561129 to your computer and use it in GitHub Desktop.
Telegraf input plugin for Motorola Surfboard 6141
This file contains 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 | |
# -*- coding: utf-8 -*- | |
######################################################################################################################## | |
""" | |
Telegraf input plugin for Motorola Surfboard 6141 | |
Copyright © 2017 by John Celoria. | |
This program is free software; you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation; either version 2, or (at your option) | |
any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program; if not, write to the Free Software Foundation, | |
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
""" | |
######################################################################################################################## | |
# Library imports | |
import argparse | |
import json | |
import moto | |
import re | |
import sys | |
######################################################################################################################## | |
# Set some defaults | |
__author__ = 'John Celoria' | |
__version__ = '0.1' | |
######################################################################################################################## | |
def normalize(obj): | |
# Replace spaces w/ underscores, lowercase dictionary keys and strip/convert values to integers | |
return [{k.replace(" ", "_").lower():int(re.sub("\D", "", v)) for k,v in d.items()} for d in obj] | |
def normalize_values(obj): | |
return [{k:int(re.sub("\D", "", v)) for k,v in d.items()} for d in obj] | |
def main(arguments): | |
parser = argparse.ArgumentParser(description=__doc__, | |
formatter_class=argparse.RawDescriptionHelpFormatter) | |
parser.add_argument("-d", "--downstream", help="Return downstream information", action="store_true") | |
parser.add_argument("-u", "--upstream", help="Return upstream information", action="store_true") | |
parser.add_argument("-r", "--raw", help="Display unmodified raw data", action="store_true") | |
args = parser.parse_args(arguments) | |
if not (args.downstream or args.upstream): | |
parser.error('Please specify one of -d or -u options') | |
# Return a list of dictionaries | |
modem = moto.MotorolaModem() | |
downstream_list = modem.getDownstreamInformation() | |
upstream_list = modem.getUpstreamInformation() | |
if not (args.raw): | |
# Normalize keys and values | |
downstream_list = normalize(downstream_list) | |
upstream_list = normalize(upstream_list) | |
# Add telegraf tag | |
[d.update({'stream':'upstream'}) for d in upstream_list] | |
[d.update({'stream':'downstream'}) for d in downstream_list] | |
# JSON-ify our lists | |
downstream_json = json.dumps(downstream_list, indent=2, sort_keys=True) | |
upstream_json = json.dumps(upstream_list, indent=2, sort_keys=True) | |
if args.upstream: | |
print(upstream_json) | |
if args.downstream: | |
print(downstream_json) | |
if __name__ == '__main__': | |
sys.exit(main(sys.argv[1:])) | |
######################################################################################################################## |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment