From 05c0e7177366f1824bcdc24b3b4b5efe4817310a Mon Sep 17 00:00:00 2001 From: Cort Buffington Date: Mon, 15 May 2017 15:29:16 -0500 Subject: [PATCH] More cleanup... --- confbridge.py | 23 +++--- dmrlink.py | 45 ++++++------ get_ids.sh | 35 --------- html_confbridge_stats.py | 150 --------------------------------------- html_stats.py | 139 ------------------------------------ pickle_stat_reader.py | 110 ---------------------------- 6 files changed, 35 insertions(+), 467 deletions(-) delete mode 100755 get_ids.sh delete mode 100755 html_confbridge_stats.py delete mode 100755 html_stats.py delete mode 100755 pickle_stat_reader.py diff --git a/confbridge.py b/confbridge.py index ef4fbb4..67acf90 100755 --- a/confbridge.py +++ b/confbridge.py @@ -58,7 +58,7 @@ import sys from dmr_utils.utils import hex_str_3, hex_str_4, int_id from dmrlink import IPSC, systems, config_reports, reportFactory, REPORT_OPCODES # hmac_new, sha1, report -from ipsc.ipsc_const import BURST_DATA_TYPE +from DMRlink.ipsc_const import BURST_DATA_TYPE __author__ = 'Cortney T. Buffington, N0MJS' @@ -418,13 +418,13 @@ class confReportFactory(reportFactory): if __name__ == '__main__': - #import argparse - #import os - #import signal - #from dmr_utils.utils import try_download, mk_id_dict + import argparse + import os + import signal + from dmr_utils.utils import try_download, mk_id_dict - import dmrlink_log - import dmrlink_config + from DMRlink.dmrlink_config import build_config + from DMRlink.dmrlink_log import config_logging # Change the current directory to the location of the application os.chdir(os.path.dirname(os.path.realpath(sys.argv[0]))) @@ -440,30 +440,31 @@ if __name__ == '__main__': cli_args.CFG_FILE = os.path.dirname(os.path.abspath(__file__))+'/dmrlink.cfg' # Call the external routine to build the configuration dictionary - CONFIG = dmrlink_config.build_config(cli_args.CFG_FILE) + CONFIG = build_config(cli_args.CFG_FILE) # Call the external routing to start the system logger if cli_args.LOG_LEVEL: CONFIG['LOGGER']['LOG_LEVEL'] = cli_args.LOG_LEVEL if cli_args.LOG_HANDLERS: CONFIG['LOGGER']['LOG_HANDLERS'] = cli_args.LOG_HANDLERS - logger = dmrlink_log.config_logging(CONFIG['LOGGER']) + logger = config_logging(CONFIG['LOGGER']) config_reports(CONFIG) logger.info('DMRlink \'confbridge.py\' (c) 2016 N0MJS & the K0USY Group - SYSTEM STARTING...') + # Shut ourselves down gracefully with the IPSC peers. def sig_handler(_signal, _frame): logger.info('*** DMRLINK IS TERMINATING WITH SIGNAL %s ***', str(_signal)) - + for system in systems: this_ipsc = systems[system] logger.info('De-Registering from IPSC %s', system) de_reg_req_pkt = this_ipsc.hashed_packet(this_ipsc._local['AUTH_KEY'], this_ipsc.DE_REG_REQ_PKT) this_ipsc.send_to_ipsc(de_reg_req_pkt) reactor.stop() - + # Set signal handers so that we can gracefully exit if need be for sig in [signal.SIGTERM, signal.SIGINT, signal.SIGQUIT]: signal.signal(sig, sig_handler) diff --git a/dmrlink.py b/dmrlink.py index d2bbe13..8674101 100755 --- a/dmrlink.py +++ b/dmrlink.py @@ -43,13 +43,12 @@ from twisted.internet.protocol import DatagramProtocol, Factory, Protocol from twisted.protocols.basic import NetstringReceiver from twisted.internet import reactor, task -# Imports from our static file directory -from dmrlink.ipsc_const import * -from dmrlink.ipsc_mask import * -from dmrlink.reporting_const import * -from dmrlink.dmrlink_config import build_config -from dmrlink.dmrlink_log import config_logging +# Imports files in the dmrlink subdirectory (these things shouldn't change often) +from DMRlink.ipsc_const import * +from DMRlink.ipsc_mask import * +from DMRlink.reporting_const import * +# Imports from DMR Utilities package from dmr_utils.utils import hex_str_2, hex_str_3, hex_str_4, int_id @@ -65,18 +64,6 @@ __email__ = 'n0mjs@me.com' systems = {} -# Shut ourselves down gracefully with the IPSC peers. -# -def sig_handler(_signal, _frame): - logger.info('*** DMRLINK IS TERMINATING WITH SIGNAL %s ***', str(_signal)) - - for system in systems: - this_ipsc = systems[system] - logger.info('De-Registering from IPSC %s', system) - de_reg_req_pkt = this_ipsc.hashed_packet(this_ipsc._local['AUTH_KEY'], this_ipsc.DE_REG_REQ_PKT) - this_ipsc.send_to_ipsc(de_reg_req_pkt) - reactor.stop() - # Timed loop used for reporting IPSC status # # REPORT BASED ON THE TYPE SELECTED IN THE MAIN CONFIG FILE @@ -1008,6 +995,9 @@ if __name__ == '__main__': import os import signal + from DMRlink.dmrlink_config import build_config + from DMRlink.dmrlink_log import config_logging + # Change the current directory to the location of the application os.chdir(os.path.dirname(os.path.realpath(sys.argv[0]))) @@ -1025,7 +1015,6 @@ if __name__ == '__main__': # Call the external routine to build the configuration dictionary CONFIG = build_config(cli_args.CFG_FILE) - # Call the external routing to start the system logger if cli_args.LOG_LEVEL: CONFIG['LOGGER']['LOG_LEVEL'] = cli_args.LOG_LEVEL @@ -1035,6 +1024,20 @@ if __name__ == '__main__': logger.info('DMRlink \'dmrlink.py\' (c) 2013 - 2015 N0MJS & the K0USY Group - SYSTEM STARTING...') + # Set signal handers so that we can gracefully exit if need be + def sig_handler(_signal, _frame): + logger.info('*** DMRLINK IS TERMINATING WITH SIGNAL %s ***', str(_signal)) + + for system in systems: + this_ipsc = systems[system] + logger.info('De-Registering from IPSC %s', system) + de_reg_req_pkt = this_ipsc.hashed_packet(this_ipsc._local['AUTH_KEY'], this_ipsc.DE_REG_REQ_PKT) + this_ipsc.send_to_ipsc(de_reg_req_pkt) + reactor.stop() + + for sig in [signal.SIGTERM, signal.SIGINT, signal.SIGQUIT]: + signal.signal(sig, sig_handler) + # INITIALIZE THE REPORTING LOOP config_reports(CONFIG) @@ -1061,8 +1064,6 @@ if __name__ == '__main__': systems[system] = IPSC(system, CONFIG, logger, report_server) reactor.listenUDP(CONFIG['SYSTEMS'][system]['LOCAL']['PORT'], systems[system], interface=CONFIG['SYSTEMS'][system]['LOCAL']['IP']) - # Set signal handers so that we can gracefully exit if need be - for sig in [signal.SIGTERM, signal.SIGINT, signal.SIGQUIT]: - signal.signal(sig, sig_handler) + reactor.run() diff --git a/get_ids.sh b/get_ids.sh deleted file mode 100755 index 59d18b6..0000000 --- a/get_ids.sh +++ /dev/null @@ -1,35 +0,0 @@ -#! /bin/sh - -################################################### -# # -# Download Subscriber and Peer ID's from DMR-MARC # -# # -################################################### - -# To provide more readable output from DMRlink with current subscriber and repeater IDs, we download the CSV files from DMR-MARC -# If you are going to use this in a cron task, don't run it more then once a day. -# It might be good to find alternale a source as a backup. - -# - -# wget -O users.csv -q "http://www.dmr-marc.net/cgi-bin/trbo-database/datadump.cgi?table=users&format=csv&header=0" - -# Options are: - -# table { users | repeaters } - -# format { table | csv | csvq | json } - -# header { 0 | 1 } (only applies to table and csv formats) - -# id { nnnnnn } (query an individual record) - -# Get the user IDs. -wget -O subscriber_ids.csv -q "http://www.dmr-marc.net/cgi-bin/trbo-database/datadump.cgi?table=users&format=csv&header=0" - -# Get the peer IDs -wget -O peer_ids.csv -q "http://www.dmr-marc.net/cgi-bin/trbo-database/datadump.cgi?table=repeaters&format=csv&header=0" - -# Tell ambe_audio to re-read the files -echo -n "reread_subscribers" | nc 127.0.0.1 31002 - diff --git a/html_confbridge_stats.py b/html_confbridge_stats.py deleted file mode 100755 index d983093..0000000 --- a/html_confbridge_stats.py +++ /dev/null @@ -1,150 +0,0 @@ -#!/usr/bin/env python -# -############################################################################### -# Copyright (C) 2017 Cortney T. Buffington, N0MJS -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of tde GNU General Public License as published by -# the Free Software Foundation; eitder version 3 of the License, 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 -############################################################################### - -from __future__ import print_function -from cPickle import load -from pprint import pprint -from time import time, strftime, localtime -from twisted.internet import reactor -from twisted.internet import task -from binascii import b2a_hex as h -from dmr_utils.utils import int_id, get_alias -from os.path import getmtime - -__autdor__ = 'Cortney T. Buffington, N0MJS' -__copyright__ = 'Copyright (c) 2017 Cortney T. Buffington, N0MJS' -__license__ = 'GNU GPLv3' -__maintainer__ = 'Cort Buffington, N0MJS' -__email__ = 'n0mjs@me.com' - - -# This is the only user-configuration necessary -# Tell the program where the pickle file is -# Tell the program where to write the html table file -# Tell the program how often to print a report -- should match dmrlink report period -stat_file = '../confbridge_stats.pickle' -html_table_file = '../confbridge_stats.html' -frequency = 10 - -def read_dict(): - try: - with open(stat_file, 'rb') as file: - BRIDGES = load(file) - return BRIDGES - except IOError as detail: - print('I/O Error: {}'.format(detail)) - except EOFError: - print('EOFError') - -def write_file(_string): - try: - with open(html_table_file, 'w') as file: - file.write(_string) - file.close() - except IOError as detail: - print('I/O Error: {}'.format(detail)) - except EOFError: - print('EOFError') - -def build_table(): - _now = time() - _last_update = strftime('%Y-%m-%d %H:%M:%S', localtime(getmtime(stat_file))) - _cnow = strftime('%Y-%m-%d %H:%M:%S', localtime(_now)) - BRIDGES = read_dict() - if BRIDGES != 'None': - - stuff = 'Table Generated: {}
'.format(_cnow) - stuff += 'Last Stat Data Recieved: {}
'.format(_last_update) - - #style="font: 10pt arial, sans-serif;" - - for bridge in BRIDGES: - stuff += '' - stuff += '' - stuff += '\ - \ - \ - \ - \ - \ - \ - \ - \ - \ - ' - stuff += ''.format(bridge) - stuff += '\ - \ - \ - \ - \ - \ - \ - ' - - - for system in BRIDGES[bridge]: - on = '' - off = '' - active = '' - - if system['TO_TYPE'] == 'ON' or system['TO_TYPE'] == 'OFF': - if system['TIMER'] - _now > 0: - exp_time = int(system['TIMER'] - _now) - else: - exp_time = 'Expired' - if system['TO_TYPE'] == 'ON': - to_action = 'Turn OFF' - else: - to_action = 'Turn ON' - else: - exp_time = 'N/A' - to_action = 'None' - - if system['ACTIVE'] == True: - active = '' - elif system['ACTIVE'] == False: - active = '' - - for trigger in system['ON']: - on += str(int_id(trigger)) + ' ' - - for trigger in system['OFF']: - off += str(int_id(trigger)) + ' ' - - stuff += ' {} '.format(\ - system['SYSTEM'],\ - system['TS'],\ - int_id(system['TGID']),\ - active,\ - exp_time,\ - to_action,\ - on,\ - off) - - stuff += '
{}
SystemSlotTGIDStatusTimeoutTimeout ActionON TriggersOFF Triggers
UnknownConnectedDisconnected
{} {} {}{} {} {} {}

' - - write_file(stuff) - - -if __name__ == '__main__': - output_stats = task.LoopingCall(build_table) - output_stats.start(frequency) - reactor.run() \ No newline at end of file diff --git a/html_stats.py b/html_stats.py deleted file mode 100755 index 99de401..0000000 --- a/html_stats.py +++ /dev/null @@ -1,139 +0,0 @@ -#!/usr/bin/env python -# -############################################################################### -# Copyright (C) 2017 Cortney T. Buffington, N0MJS -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of tde GNU General Public License as published by -# the Free Software Foundation; eitder version 3 of the License, 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 -############################################################################### - -from __future__ import print_function -from cPickle import load -from pprint import pprint -from time import time, strftime, localtime -from twisted.internet import reactor -from twisted.internet import task -from binascii import b2a_hex as h -from dmr_utils.utils import int_id, get_alias - -__autdor__ = 'Cortney T. Buffington, N0MJS' -__copyright__ = 'Copyright (c) 2017 Cortney T. Buffington, N0MJS' -__license__ = 'GNU GPLv3' -__maintainer__ = 'Cort Buffington, N0MJS' -__email__ = 'n0mjs@me.com' - - -# This is the only user-configuration necessary -# Tell the program where the pickle file is -# Tell the program where to write the html table file -# Tell the program how often to print a report -- should match dmrlink report period -stat_file = '../dmrlink_stats.pickle' -html_table_file = '../stats.html' -frequency = 30 - -def read_dict(): - try: - with open(stat_file, 'rb') as file: - NETWORK = load(file) - return NETWORK - except IOError as detail: - print('I/O Error: {}'.format(detail)) - except EOFError: - print('EOFError') - -def write_file(_string): - try: - with open(html_table_file, 'w') as file: - file.write(_string) - file.close() - except IOError as detail: - print('I/O Error: {}'.format(detail)) - except EOFError: - print('EOFError') - -def build_table(): - NETWORK = read_dict() - if NETWORK != 'None': - _cnow = strftime('%Y-%m-%d %H:%M:%S', localtime(time())) - stuff = 'Last Update: {}'.format(_cnow) - stuff += '' - - for ipsc in NETWORK: - stat = NETWORK[ipsc]['MASTER']['STATUS'] - master = NETWORK[ipsc]['LOCAL']['MASTER_PEER'] - - stuff += '' - - stuff += '\ - \ - \ - \ - \ - \ - \ - \ - ' - - stuff += '' - - stuff += '\ - \ - \ - \ - \ - ' - - if not master: - stuff += ''.format(\ - str(int_id(NETWORK[ipsc]['MASTER']['RADIO_ID'])).rjust(8,'0'),\ - NETWORK[ipsc]['MASTER']['IP'],\ - stat['CONNECTED'],\ - stat['KEEP_ALIVES_SENT'],\ - stat['KEEP_ALIVES_RECEIVED'],\ - stat['KEEP_ALIVES_MISSED'],) - - if master: - for peer in NETWORK[ipsc]['PEERS']: - stat = NETWORK[ipsc]['PEERS'][peer]['STATUS'] - stuff += ''.format(\ - str(int_id(peer)).rjust(8,'0'),\ - NETWORK[ipsc]['PEERS'][peer]['IP'],\ - stat['CONNECTED'],\ - stat['KEEP_ALIVES_RECEIVED']) - - else: - for peer in NETWORK[ipsc]['PEERS']: - stat = NETWORK[ipsc]['PEERS'][peer]['STATUS'] - if peer != NETWORK[ipsc]['LOCAL']['RADIO_ID']: - stuff += ''.format(\ - str(int_id(peer)).rjust(8,'0'),\ - NETWORK[ipsc]['PEERS'][peer]['IP'],\ - stat['CONNECTED'],\ - stat['KEEP_ALIVES_SENT'],\ - stat['KEEP_ALIVES_RECEIVED'],\ - stat['KEEP_ALIVES_MISSED']) - stuff += '
{} '.format(ipsc) - if master: - stuff += '(master)' - else: - stuff += '(peer)' - stuff +='
TypeRadio IDIP AddressConnectedKeep Alives
SentReceivedMissed
Master{}{}{}{}{}{}
Peer{}{}{}n/a{}n/a
Peer{}{}{}{}{}{}

' - - write_file(stuff) - - -if __name__ == '__main__': - output_stats = task.LoopingCall(build_table) - output_stats.start(frequency) - reactor.run() \ No newline at end of file diff --git a/pickle_stat_reader.py b/pickle_stat_reader.py deleted file mode 100755 index 17a60d8..0000000 --- a/pickle_stat_reader.py +++ /dev/null @@ -1,110 +0,0 @@ -#!/usr/bin/env python -# -############################################################################### -# Copyright (C) 2016 Cortney T. Buffington, N0MJS -# -# 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 3 of the License, 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 -############################################################################### - -from __future__ import print_function -from cPickle import load -from pprint import pprint -from time import ctime -from twisted.internet import reactor -from twisted.internet import task -from binascii import b2a_hex as h - -__author__ = 'Cortney T. Buffington, N0MJS' -__copyright__ = 'Copyright (c) 2015 Cortney T. Buffington, N0MJS and the K0USY Group' -__credits__ = 'Adam Fast, KC0YLK, Dave Kierzkowski, KD8EYF' -__license__ = 'GNU GPLv3' -__maintainer__ = 'Cort Buffington, N0MJS' -__email__ = 'n0mjs@me.com' - - -# This is the only user-configuration necessary -# Tell the program where the pickle file is -# Tell the program how often to print a report -stat_file = '../dmrlink_stats.pickle' -frequency = 30 - - -def int_id(_hex_string): - return int(h(_hex_string), 16) - -def read_dict(): - try: - with open(stat_file, 'rb') as file: - NETWORK = load(file) - return NETWORK - except IOError as detail: - print('I/O Error: {}'.format(detail)) - except EOFError: - print('EOFError') - -def print_stats(): - NETWORK = read_dict() - if NETWORK != "None": - print('NETWORK STATISTICS REPORT:', ctime()) - - for ipsc in NETWORK: - stat = NETWORK[ipsc]['MASTER']['STATUS'] - master = NETWORK[ipsc]['LOCAL']['MASTER_PEER'] - - print(ipsc) - - if master: - print(' MASTER Information:') - print(' RADIO ID: {} (self)'.format(str(int_id(NETWORK[ipsc]['LOCAL']['RADIO_ID'])).rjust(8,'0'))) - else: - print(' MASTER Information:') - print(' RADIO ID: {} CONNECTED: {}, KEEP ALIVES: SENT {} RECEIVED {} MISSED {} ({})'.format(\ - str(int_id(NETWORK[ipsc]['MASTER']['RADIO_ID'])).rjust(8,'0'),\ - stat['CONNECTED'],stat['KEEP_ALIVES_SENT'],\ - stat['KEEP_ALIVES_RECEIVED'],\ - stat['KEEP_ALIVES_MISSED'],\ - NETWORK[ipsc]['MASTER']['IP'])) - - - print(' PEER Information:') - - if master: - for peer in NETWORK[ipsc]['PEERS']: - stat = NETWORK[ipsc]['PEERS'][peer]['STATUS'] - print(' RADIO ID: {} CONNECTED: {}, KEEP ALIVES: RECEIVED {} ({})'.format(\ - str(int_id(peer)).rjust(8,'0'),\ - stat['CONNECTED'],\ - stat['KEEP_ALIVES_RECEIVED'],\ - NETWORK[ipsc]['PEERS'][peer]['IP'])) - else: - for peer in NETWORK[ipsc]['PEERS']: - stat = NETWORK[ipsc]['PEERS'][peer]['STATUS'] - if peer == NETWORK[ipsc]['LOCAL']['RADIO_ID']: - print(' RADIO ID: {} (self)'.format(str(int_id(peer)).rjust(8,'0'))) - else: - print(' RADIO ID: {} CONNECTED: {}, KEEP ALIVES: SENT {} RECEIVED {} MISSED {} ({})'.format(\ - str(int_id(peer)).rjust(8,'0'),\ - stat['CONNECTED'],\ - stat['KEEP_ALIVES_SENT'],\ - stat['KEEP_ALIVES_RECEIVED'],\ - stat['KEEP_ALIVES_MISSED'],\ - NETWORK[ipsc]['PEERS'][peer]['IP'])) - print() - print() - -if __name__ == '__main__': - output_stats = task.LoopingCall(print_stats) - output_stats.start(frequency) - reactor.run() \ No newline at end of file