def _zip_db(zip_location, sql_filename, zip_content): try: start_time = time.time() zip_files([sql_filename], [zip_content], save_type="save_to_disk", file_location=zip_location) end_time = time.time() total_zip_time = str(round(end_time - start_time, 2)) logger.network_logger.info("Zipping " + sql_filename + " took " + total_zip_time + " seconds") except Exception as error: logger.primary_logger.error("* Unable to create Database zip of " + sql_filename + ": " + str(error))
def send_db_graph_emails(email_address_list): generate_plotly_graph(None, graph_config=app_config_access.email_db_graph_config) sleep(5) while server_plotly_graph_variables.graph_creation_in_progress: sleep(5) date_time = datetime.utcnow().strftime("%Y-%m-%d_%H:%M") filename = app_cached_variables.hostname + "_" + date_time + "_KS_Graph" message = get_new_email_message("Kootnet Sensor Graph - " + app_cached_variables.hostname) message.attach( MIMEText(_get_default_email_body_text("Plotly Graph"), "plain")) try: zipped_graph = zip_files([filename + ".html"], [ get_file_content(app_config_access.email_db_graph_config. plotly_graph_saved_location, open_type="rb") ]) payload = MIMEBase("application", "zip") payload.set_payload(zipped_graph.read()) encoders.encode_base64(payload) payload.add_header("Content-Disposition", "attachment", filename=filename + ".zip") message.attach(payload) except Exception as error: logger.network_logger.error("Graph Email did not send: " + str(error)) payload = MIMEText("\n\nError Generating Zip of Plotly Graph\n\n", "plain") message.attach(payload) for email_address in email_address_list: send_email(email_address, message)
def download_all_sql_databases_zipped(): logger.network_logger.debug( "* Download Zip of All Databases Accessed by " + str(request.remote_addr)) zip_name = _get_net_and_host_name_str("All_Databases", ".zip") try: return_names = [ app_cached_variables.hostname + "_Main_Database.sqlite", app_cached_variables.hostname + "_Checkin_Database.sqlite", app_cached_variables.hostname + "_MQTT_Sub_Database.sqlite" ] return_files = [ get_file_content(file_locations.sensor_database, open_type="rb"), get_file_content(file_locations.sensor_checkin_database, open_type="rb"), get_file_content(file_locations.mqtt_subscriber_database, open_type="rb") ] return_zip = zip_files(return_names, return_files) return send_file(return_zip, attachment_filename=zip_name, as_attachment=True) except Exception as error: logger.primary_logger.error("* Download All Databases Zip: " + str(error)) return_zip = "Error Creating Zip on " + app_cached_variables.ip + " - " + app_cached_variables.hostname return send_file(return_zip + " - " + str(error), attachment_filename=zip_name + ".txt", as_attachment=True)
def _get_zipped_configurations(): main_config = app_config_access.primary_config.get_config_as_str() installed_sensors = app_config_access.installed_sensors.get_config_as_str() display_config = app_config_access.display_config.get_config_as_str() checkin_config = app_config_access.checkin_config.get_config_as_str() interval_recording_config = app_config_access.interval_recording_config.get_config_as_str( ) trigger_high_low = app_config_access.trigger_high_low.get_config_as_str() trigger_variances = app_config_access.trigger_variances.get_config_as_str() email_config = remove_line_from_text( app_config_access.email_config.get_config_as_str(), [5, 6]) mqtt_broker_config = app_config_access.mqtt_broker_config.get_config_as_str( ) mqtt_pub_config = remove_line_from_text( app_config_access.mqtt_publisher_config.get_config_as_str(), [5, 6]) mqtt_sub_config = remove_line_from_text( app_config_access.mqtt_subscriber_config.get_config_as_str(), [5, 6]) open_sense_map_config = remove_line_from_text( app_config_access.open_sense_map_config.get_config_as_str(), [2]) wu_config = remove_line_from_text( app_config_access.weather_underground_config.get_config_as_str(), [4, 5]) luftdaten_config = app_config_access.luftdaten_config.get_config_as_str() sensor_control_config = app_config_access.sensor_control_config.get_config_as_str( ) try: return_names = [ os.path.basename(file_locations.primary_config), os.path.basename(file_locations.installed_sensors_config), os.path.basename(file_locations.display_config), os.path.basename(file_locations.checkin_configuration), os.path.basename(file_locations.interval_config), os.path.basename(file_locations.trigger_high_low_config), os.path.basename(file_locations.trigger_variances_config), os.path.basename(file_locations.email_config), os.path.basename(file_locations.mqtt_broker_config), os.path.basename(file_locations.mqtt_publisher_config), os.path.basename(file_locations.mqtt_subscriber_config), os.path.basename(file_locations.osm_config), os.path.basename(file_locations.weather_underground_config), os.path.basename(file_locations.luftdaten_config), os.path.basename(file_locations.html_sensor_control_config) ] return_files = [ main_config, installed_sensors, display_config, checkin_config, interval_recording_config, trigger_high_low, trigger_variances, email_config, mqtt_broker_config, mqtt_pub_config, mqtt_sub_config, open_sense_map_config, wu_config, luftdaten_config, sensor_control_config ] blob_data = zip_files(return_names, return_files, skip_datetime=True).read() return blob_data except Exception as error: logger.primary_logger.error("* Unable to Zip Configurations: " + str(error)) return None
def create_the_big_zip(ip_list): """ Downloads everything from sensors based on provided IP or DNS addresses (as a list) then creates a single zip file for download off the local web portal. """ new_name = "TheBigZip_" + app_cached_variables.hostname + "_" + str( time.time())[:-8] + ".zip" rm_cached_variables.sc_big_zip_name = new_name if len(ip_list) > 0: try: return_names = ["ReportCombo.html"] generate_html_reports_combo(ip_list) return_files = [rm_cached_variables.html_combo_report] _queue_name_and_file_list( ip_list, network_commands.download_zipped_everything) ip_name_and_data = get_data_queue_items() for sensor in ip_name_and_data: current_file_name = sensor[0].split( ".")[-1] + "_" + sensor[1] + ".zip" try: if auth_error_msg_contains in str(sensor[2]): current_file_name = sensor[0].split( ".")[-1] + "_" + sensor[1] + ".txt" sensor[2] = "Incorrect Login Provided" except Exception as error: print(str(error)) return_names.append(current_file_name) return_files.append(sensor[2]) zip_location = file_locations.html_sensor_control_big_zip zip_files(return_names, return_files, save_type="to_disk", file_location=zip_location) logger.network_logger.info( "Sensor Control - The Big Zip Generation Completed") except Exception as error: logger.primary_logger.error("Sensor Control - Big Zip Error: " + str(error)) rm_cached_variables.sc_big_zip_name = "" rm_cached_variables.creating_the_big_zip = False
def _zip_and_delete_database(database_location, db_save_name): """ Creates a zip of linked database in the database backup folder then deletes the original database. """ filename_start = app_cached_variables.ip.split(".")[-1] + app_cached_variables.hostname + "_" sql_filename = filename_start + db_save_name + ".sqlite" zip_filename = filename_start + str(datetime.utcnow().strftime("%Y-%m-%d_%H_%M_%S")) + db_save_name + ".zip" zip_full_path = file_locations.database_backup_folder + "/" + zip_filename try: zip_content = get_file_content(database_location, open_type="rb") zip_files([sql_filename], [zip_content], save_type="save_to_disk", file_location=zip_full_path) os.remove(database_location) backup_db_zip_filenames = get_list_of_filenames_in_dir(file_locations.database_backup_folder) app_cached_variables.zipped_db_backup_list = backup_db_zip_filenames return True except Exception as error: print(str(error)) backup_db_zip_filenames = get_list_of_filenames_in_dir(file_locations.database_backup_folder) app_cached_variables.zipped_db_backup_list = backup_db_zip_filenames return False
def create_all_databases_zipped(ip_list): """ Downloads remote sensor databases from provided IP or DNS addresses (as a list) then creates a single zip file for download off the local web portal. """ try: _queue_name_and_file_list( ip_list, command=network_commands.sensor_sql_all_databases_zip) data_list = get_data_queue_items() database_names = [] sensors_database = [] for sensor_data in data_list: db_name = sensor_data[0].split( ".")[-1] + "_" + sensor_data[1] + ".zip" try: if auth_error_msg_contains in str(sensor_data[2]): db_name = sensor_data[0].split( ".")[-1] + "_" + sensor_data[1] + ".txt" sensor_data[2] = "Incorrect Login Provided" except Exception as error: print(str(error)) database_names.append(db_name) sensors_database.append(sensor_data[2]) rm_cached_variables.sc_databases_zip_in_memory = False zip_files( database_names, sensors_database, save_type="save_to_disk", file_location=file_locations.html_sensor_control_databases_zip) rm_cached_variables.sc_databases_zip_name = "Multiple_Databases_" + str( time.time())[:-8] + ".zip" except Exception as error: logger.network_logger.error( "Sensor Control - Databases Zip Generation Error: " + str(error)) rm_cached_variables.sc_databases_zip_name = "" rm_cached_variables.creating_databases_zip = False logger.network_logger.info( "Sensor Control - Databases Zip Generation Complete")
def _get_zipped_logs(): try: return_names = [ app_cached_variables.hostname + "_" + os.path.basename(file_locations.primary_log), app_cached_variables.hostname + "_" + os.path.basename(file_locations.network_log), app_cached_variables.hostname + "_" + os.path.basename(file_locations.sensors_log) ] return_files = [ get_file_content(file_locations.primary_log, open_type="rb"), get_file_content(file_locations.network_log, open_type="rb"), get_file_content(file_locations.sensors_log, open_type="rb") ] return zip_files(return_names, return_files) except Exception as error: logger.primary_logger.error("* Unable to Zip Logs: " + str(error)) return None
def _get_zipped_logs(): utc_now = datetime.utcnow().strftime("%Y-%m-%d_%H:%M_") try: return_names = [ utc_now + os.path.basename(file_locations.primary_log), utc_now + os.path.basename(file_locations.network_log), utc_now + os.path.basename(file_locations.sensors_log) ] return_files = [ logger.get_sensor_log(file_locations.primary_log), logger.get_sensor_log(file_locations.network_log), logger.get_sensor_log(file_locations.sensors_log) ] blob_data = zip_files(return_names, return_files).read() return blob_data except Exception as error: logger.primary_logger.error("* Unable to Zip Logs: " + str(error)) return None
def put_all_reports_zipped_to_cache(ip_list): """ Downloads ALL remote sensor reports from provided IP or DNS addresses (as a list) then creates a single zip file for download off the local web portal. """ try: hostname = app_cached_variables.hostname generate_html_reports_combo(ip_list) html_reports = [rm_cached_variables.html_combo_report] html_report_names = ["ReportCombo.html"] rm_cached_variables.sc_in_memory_zip = zip_files( html_report_names, html_reports) rm_cached_variables.sc_reports_zip_name = "Reports_from_" + hostname + "_" + str( time.time())[:-8] + ".zip" except Exception as error: logger.network_logger.error( "Sensor Control - Reports Zip Generation Error: " + str(error)) rm_cached_variables.creating_combo_reports_zip = False logger.network_logger.info( "Sensor Control - Reports Zip Generation Complete")
def create_multiple_sensor_logs_zipped(ip_list): """ Downloads remote sensor logs from provided IP or DNS addresses (as a list) then creates a single zip file for download off the local web portal. """ try: _queue_name_and_file_list( ip_list, command=network_commands.download_zipped_logs) data_list = get_data_queue_items() zip_names = [] logs_zipped = [] for sensor_data in data_list: zip_name = sensor_data[0].split( ".")[-1] + "_" + sensor_data[1] + ".zip" try: if auth_error_msg_contains in str(sensor_data[2]): zip_name = sensor_data[0].split( ".")[-1] + "_" + sensor_data[1] + ".txt" sensor_data[2] = "Incorrect Login Provided" except Exception as error: print(str(error)) zip_names.append(zip_name) logs_zipped.append(sensor_data[2]) clear_zip_names() rm_cached_variables.sc_in_memory_zip = zip_files( zip_names, logs_zipped) rm_cached_variables.sc_logs_zip_name = "Multiple_Logs_" + str( time.time())[:-8] + ".zip" except Exception as error: logger.network_logger.error( "Sensor Control - Logs Zip Generation Error: " + str(error)) rm_cached_variables.sc_logs_zip_name = "" rm_cached_variables.creating_logs_zip = False logger.network_logger.info( "Sensor Control - Multi Sensors Logs Zip Generation Complete")
def send_report_emails(email_address_list): generate_html_reports_combo( app_config_access.email_reports_config.get_raw_ip_addresses_as_list()) while rm_cached_variables.creating_combo_report: sleep(5) message = get_new_email_message("Kootnet Sensor Report - " + app_cached_variables.hostname) message.attach( MIMEText(_get_default_email_body_text("HTML Report"), "plain")) date_time = datetime.utcnow().strftime("%Y-%m-%d_%H:%M") filename = app_cached_variables.hostname + "_" + date_time + "_KS_Report" zipped_report = zip_files([filename + ".html"], [rm_cached_variables.html_combo_report]) payload = MIMEBase("application", "zip") payload.set_payload(zipped_report.read()) encoders.encode_base64(payload) payload.add_header("Content-Disposition", "attachment", filename=filename + ".zip") message.attach(payload) for email_address in email_address_list: send_email(email_address, message)