def _get_environment_vars(self): """ If the OS level environment variables 'env_vars_config' exists, then reads the json config file and converts it to a dictionary. Then loops through the keys in the config dictionary and for every key sets a class instance attribute using the key and its related value. A key can exists but doesn't have to contain a value(i.e. "key": "" or "key": null), in both cases then the value for the attribute will be set to None. :return: Nothing. class instance attributes are created. """ if self.environ_vars_config_path is None: raise RuntimeError( "Environment variable config file is not present, environment variable 'env_vars_config' " "is missing.") else: env_config_rc, env_config_dict = Common.read_json_file( self.environ_vars_config_path) if not env_config_rc: raise RuntimeError( "Environment variable config dictionary could not be obtained." " Path to file specified: {}".format( self.environ_vars_config_path)) else: for env_var_key, env_var_value in env_config_dict.items(): if env_var_value is None or env_var_value == "": setattr(self, env_var_key, None) else: setattr(self, env_var_key, env_var_value)
def get(self): """ :return: Renders the html page with all substituted content needed. """ # INFO: For now requirement to login is not needed. Open access is fine, if needed uncomment below code to # INFO: require authentication via JWT from login app. # if request.cookies.get('access_token_cookie') is None: # self.redirect_to_uscc_login() # return self.login_redirect_response while True: try: rc, nh_status_dict = Common.read_json_file(os.environ.get('neh_status')) break except json.JSONDecodeError: time.sleep(3) continue if rc: if os.environ.get('exec_env') != 'local': self.auto_tracker_resp = requests.post(url=self.auto_tracker_url, data={'automation': self.auto_tracker_id}, auth=(self.auto_tracker_user, self.auto_tracker_pass), verify=False) return render_template('network_health/nh_dashboard.html', neh_status=nh_status_dict) return render_template('network_health/nh_dashboard.html')
def establish_logging(self, logger_config_json=None): """ Establishes a logger instance. :param logger_config_json: Path to a JSON configuration file :return: """ if logger_config_json is not None: rc, logger_config_dict = Common.read_json_file(logger_config_json) if rc: logging.config.dictConfig(logger_config_dict) self.nh_logger = logging.getLogger(self.logger_name) else: numerical_level = getattr(logging, os.environ.get('log_level').upper(), None) if not isinstance(numerical_level, int): raise ValueError("Invalid log level: {}".format( os.environ.get('log_level'))) logging.basicConfig(level=numerical_level) self.nh_logger = logging.getLogger(self.logger_name)
def main(): """ :return: """ # carrier_smtp_syntax = dict(att='mms.att.net', verizon='vtext.com', uscc='email.uscc.net') # svt_rc, svt_dict = Common.read_json_file('svt_tests') # if svt_rc: # for test_name in svt_dict.get('test_names'): # print(test_name) nh_base = NhBase() nh_base.establish_logging(logger_config_json=nh_base.neh_log_config_file) if nh_base.exec_env == 'dev': nh_status_path = os.environ.get('neh_status') nh_lcc_map_path = os.environ.get('neh_lcc_map') else: nh_status_path = '/neh_status_files/neh_status/neh_test_status' nh_lcc_map_path = '/neh_status_files/neh_lcc_config' nh_status_rc, nh_status_dict = Common.read_json_file(nh_status_path) nh_lcc_rc, nh_lcc_dict = Common.read_json_file(nh_lcc_map_path) if not nh_status_rc: nh_base.nh_logger.critical( "Network Health Status file couldn't be obtained. Path specified: {}" .format(nh_status_path)) return False if not nh_lcc_rc: nh_base.nh_logger.critical( "Network Health LCC configuration map file couldn't be obtained. Path specified: {}" .format(nh_lcc_map_path)) return False netcool_db = QueryOracle() con = cx_Oracle.connect( "automation_ro/[email protected]:1521/netcoold") cursor = con.cursor() cursor.execute( """SELECT c_alarmsource, c_lastoccurrence, c_summary, c_alertkey, c_suppressescl, c_ttnumber FROM netcoold.alerts_status_t WHERE c_lastoccurrence > (SYSTIMESTAMP - .0833) AND c_alertgroup = 'ASCOM Availibility Alarms' ORDER BY 1,4,2 """) # Get all the column names for the result set column_name_list = [x[0] for x in cursor.description] # Join the column names and the data for each row into a dictionary for easier processing. result_dicts = [ dict(zip(column_name_list, row)) for row in cursor.fetchall() ] # return False # result_dicts = [dict(zip(column_name_list, cursor.fetchone()))] # Initialize a dictionary of lists by SVT test name that contains list of LCCs. These lists are used to keep track # of what LCCs we updated by removing the LCC for the SVT test name in the result set. At the end of processing the # result set any LCCs that didn't get their status updated will be left in the lists and are assumed to have a # "COMM AlARM" status that indicates the test didn't run within the last hour. In the status config dictionary this # will be replaced with a 'warning' status. lcc_tracking_results_dict = dict() for test_name in nh_status_dict.keys(): if test_name != 'last_refreshed': temp_list = [] for lcc in nh_lcc_dict.values(): if lcc_tracking_results_dict.get(test_name) is None: temp_list.append(lcc) lcc_tracking_results_dict[test_name] = temp_list else: if lcc not in lcc_tracking_results_dict.get(test_name): lcc_tracking_results_dict.get(test_name).append(lcc) for test_name in nh_status_dict.keys(): for result in result_dicts: if test_name in result.get('C_ALERTKEY'): if result.get('C_ALARMSOURCE') in nh_lcc_dict.keys(): if nh_lcc_dict.get( result.get('C_ALARMSOURCE') ) in lcc_tracking_results_dict.get(test_name): lcc_tracking_results_dict.get(test_name).remove( nh_lcc_dict.get(result.get('C_ALARMSOURCE'))) if 'Success' in result.get('C_SUMMARY'): nh_status_dict[test_name][nh_lcc_dict.get( result.get('C_ALARMSOURCE'))] = "success" elif 'Failure' in result.get('C_SUMMARY'): nh_status_dict[test_name][nh_lcc_dict.get( result.get('C_ALARMSOURCE'))] = "failure" elif 'Comm Alarm' in result.get('C_SUMMARY'): nh_status_dict[test_name][nh_lcc_dict.get( result.get('C_ALARMSOURCE'))] = "warning" else: nh_status_dict[test_name][nh_lcc_dict.get( result.get('C_ALARMSOURCE'))] = "" for tracking_test_name, no_lcc_status_list in lcc_tracking_results_dict.items( ): for location in no_lcc_status_list: nh_status_dict.get(tracking_test_name)[location] = 'warning' nh_status_dict['last_refreshed'] = datetime.datetime.now().strftime( "%I:%M%p on %B %d, %Y") with open(nh_status_path, 'w') as wfh: json.dump(nh_status_dict, wfh) print("status file updated") # time.sleep(300) return True