def main(): """Main to start the telegraf service """ ctx = cfg.ConfigMgr() app_cfg = ctx.get_app_config() app_name = ctx.get_app_name() dev_mode = ctx.is_dev_mode() cfg_inst = os.getenv('ConfigInstance', app_name) log = configure_logging(os.environ['PY_LOG_LEVEL'].upper(), __name__, dev_mode) log.info("=============== STARTING telegraf ===============") try: command = None if len(sys.argv) > 1: command = str(sys.argv[1]) read_config(app_cfg, dev_mode, log) if command is None: if dev_mode: telegraf_conf = "/etc/Telegraf/" \ + cfg_inst \ + "/" \ + cfg_inst \ + "_devmode.conf" else: telegraf_conf = "/etc/Telegraf/"+cfg_inst+"/"+cfg_inst+".conf" subprocess.call(["telegraf", "-config=" + telegraf_conf]) else: subprocess.call(shlex.split(command)) except subprocess.CalledProcessError as err: log.error(err, exc_info=True) sys.exit(1)
def start_subscriber(): subscriber = None try: ctx = cfg.ConfigMgr() if ctx.get_num_subscribers() is -1: raise "No subscriber instances found, exiting..." sub_ctx = ctx.get_subscriber_by_index(0) msgbus_cfg = sub_ctx.get_msgbus_config() print('[INFO] Initializing message bus context') msgbus_sub = mb.MsgbusContext(msgbus_cfg) topics = sub_ctx.get_topics() print(f'[INFO] Initializing subscriber for topic {topics[0]}') subscriber = msgbus_sub.new_subscriber(topics[0]) print('[INFO] Running...') while True: msg, _ = subscriber.recv() if msg is not None: print(f'[INFO] RECEIVED by subscriber : {msg}') else: print('[INFO] Receive interrupted') except KeyboardInterrupt: print('[INFO] Quitting...') finally: if subscriber is not None: subscriber.close()
def main(): """Main method to FactoryControl App """ config_client = cfg.ConfigMgr() dev_mode = config_client.is_dev_mode() log = configure_logging(os.environ['PY_LOG_LEVEL'].upper(), __name__, dev_mode) log.info("=============== STARTING factoryctrl_app ===============") try: factory_control_app = FactoryControlApp(dev_mode, config_client, log) factory_control_app.main() except Exception as ex: log.exception(ex)
def start_publisher(): publisher = None try: ctx = cfg.ConfigMgr() if ctx.get_num_publishers() == -1: raise "No publisher instances found, exiting..." pub_ctx = ctx.get_publisher_by_index(0) msgbus_cfg = pub_ctx.get_msgbus_config() print('[INFO] Initializing message bus context') msgbus_pub = mb.MsgbusContext(msgbus_cfg) topics = pub_ctx.get_topics() print(f'[INFO] Initializing publisher for topic \'{topics[0]}\'') publisher = msgbus_pub.new_publisher(topics[0]) app_cfg = ctx.get_app_config() print(f'App Config is \'{app_cfg}\'') print('[INFO] Running...') while True: blob = b'\x22' * 10 meta = { 'integer': 123, 'floating': 55.5, 'string': 'test', 'boolean': True, 'empty': None, 'obj': { 'test': { 'test2': 'hello' }, 'test3': 'world' }, 'arr': ['test', 123] } publisher.publish(( meta, blob, )) print(f'[INFO] Msg published by publisher : \'{meta}\'') time.sleep(int(app_cfg["loop_interval"])) except KeyboardInterrupt: print('[INFO] Quitting...') finally: if publisher is not None: publisher.close()
def main(): """Main to start kapacitor service """ try: ctx = cfg.ConfigMgr() app_cfg = ctx.get_app_config() config = app_cfg.get_dict() app_name = ctx.get_app_name() dev_mode = ctx.is_dev_mode() except Exception as e: logger = configure_logging( os.getenv('PY_LOG_LEVEL', 'info').upper(), __name__, dev_mode) logger.exception( "Fetching app configuration failed, Error: {}".format(e)) sys.exit(1) logger = configure_logging(os.environ['PY_LOG_LEVEL'].upper(), __name__, dev_mode) kapacitor_classifier = KapacitorClassifier(logger) logger.info("=============== STARTING kapacitor ==============") host_name = shlex.quote(os.environ["KAPACITOR_SERVER"]) if not host_name: error_log = ('Kapacitor hostname is not Set in the container. ' 'So exiting...') kapacitor_classifier.exit_with_failure_message(error_log) msg, status = kapacitor_classifier.start_udfs(config) if status is FAILURE: kapacitor_classifier.exit_with_failure_message(msg) kapacitor_started = False if (kapacitor_classifier.start_kapacitor(config, host_name, dev_mode, app_name) is True): kapacitor_started = True else: error_log = "Kapacitor is not starting. So Exiting..." kapacitor_classifier.exit_with_failure_message(error_log) msg, status = kapacitor_classifier.enable_tasks(config, kapacitor_started, host_name, dev_mode) if status is FAILURE: kapacitor_classifier.exit_with_failure_message(msg)
def main(args): """Main method. """ # Initializing Etcd to set env variables ctx = cfg.ConfigMgr() num_of_subscribers = ctx.get_num_subscribers() dev_mode = ctx.is_dev_mode() logger = configure_logging(os.environ['PY_LOG_LEVEL'].upper(), __name__, dev_mode) window_name = 'EII Visualizer App' visualizer_config = ctx.get_app_config() # Validating config against schema with open('./schema.json', "rb") as infile: schema = infile.read() if not (Util.validate_json(schema, json.dumps(visualizer_config.get_dict()))): sys.exit(1) image_dir = os.environ["IMAGE_DIR"] # If user provides image_dir, create the directory if don't exists if image_dir: if not os.path.exists(image_dir): os.mkdir(image_dir) # Initializing required variables queue_dict = {} topic_config_list = [] topics_list = [] for index in range(num_of_subscribers): # Fetching subscriber element based on index sub_ctx = ctx.get_subscriber_by_index(index) # Fetching msgbus config of subscriber msgbus_cfg = sub_ctx.get_msgbus_config() # Fetching topics of subscriber topic = sub_ctx.get_topics()[0] # Adding topic & msgbus_config to # topic_config tuple topic_config = (topic, msgbus_cfg) topic_config_list.append(topic_config) topics_list.append(topic) queue_dict[topic] = queue.Queue(maxsize=10) try: root_win = tkinter.Tk() button_dict = {} image_dict = {} window_width = 600 window_height = 600 window_geometry = str(window_width) + 'x' + str(window_height) root_win.geometry(window_geometry) root_win.title(window_name) column_value = len(topics_list) // 2 row_value = len(topics_list) % 2 height_value = int(window_height / (row_value + 1)) width_value = int(window_width / (column_value + 1)) blank_image_shape = (300, 300, 3) blank_image = np.zeros(blank_image_shape, dtype=np.uint8) text = 'Disconnected' text_position = (20, 250) text_font = cv2.FONT_HERSHEY_PLAIN text_color = (255, 255, 255) cv2.putText(blank_image, text, text_position, text_font, 2, text_color, 2, cv2.LINE_AA) blankimg = Image.fromarray(blank_image) for button_count in range(len(topics_list)): button_str = "button{}".format(button_count) image_dict[button_str] = ImageTk.PhotoImage(image=blankimg) button_count, row_count, column_count = 0, 0, 0 if len(topics_list) == 1: height_value = window_height width_value = window_width button_dict[str(button_count)] = \ tkinter.Button(root_win, text=topics_list[0]) button_dict[str(button_count)].grid(sticky='NSEW') tkinter.Grid.rowconfigure(root_win, 0, weight=1) tkinter.Grid.columnconfigure(root_win, 0, weight=1) else: for key in queue_dict: button_dict[str(button_count)] = tkinter.Button(root_win, text=key) if column_count > column_value: row_count = row_count + 1 column_count = 0 if row_count > 0: height_value = int(window_height / (row_count + 1)) for key2 in button_dict: button_dict[key2].config(height=height_value, width=width_value) else: for key2 in button_dict: button_dict[key2].config(height=height_value, width=width_value) button_dict[str(button_count)].grid(row=row_count, column=column_count, sticky='NSEW') tkinter.Grid.rowconfigure(root_win, row_count, weight=1) tkinter.Grid.columnconfigure(root_win, column_count, weight=1) button_count = button_count + 1 column_count = column_count + 1 root_win.update() msg_bus_subscriber(topic_config_list, queue_dict, logger, visualizer_config) while True: button_count = 0 for key in queue_dict: if not queue_dict[key].empty(): logger.info('Preparing frame for visualization') frame = queue_dict[key].get_nowait() img = Image.fromarray(frame) del frame if len(img.split()) > 3: blue, green, red, _ = img.split() else: blue, green, red = img.split() img = Image.merge("RGB", (red, green, blue)) imgwidth, imgheight = img.size aspect_ratio = (imgwidth / imgheight) + 0.1 resized_width = button_dict[str( button_count)].winfo_width() resized_height = round( button_dict[str(button_count)].winfo_width() / aspect_ratio) resized_img = img.resize((resized_width, resized_height)) del img image_dict["button" + str(button_count)] = ImageTk.PhotoImage( image=resized_img) button_dict[str(button_count)].config( image=image_dict["button" + str(button_count)], compound=tkinter.BOTTOM) del resized_img else: try: button_dict[str(button_count)].config( image=image_dict["button" + str(button_count)], compound=tkinter.BOTTOM) except Exception: logger.exception("Tkinter exception") button_count = button_count + 1 root_win.update() except KeyboardInterrupt: logger.info('Quitting...') except Exception: logger.exception('Error during execution:') finally: logger.exception('Destroying EII databus context') sys.exit(1)
# Notifying caller once all samples are received self.done_receiving = True return def threadRunner(topics_list, msgbus_cfg, config_dict): """ To run TimeSeriesCalculator for each topic """ tsc_app = TimeSeriesCalculator(topics_list, msgbus_cfg, config_dict) tsc_app.eiiSubscriber() if __name__ == "__main__": try: ctx = cfg.ConfigMgr() config_dict = ctx.get_app_config() app_name = ctx.get_app_name() dev_mode = ctx.is_dev_mode() logger.info("app name is{}".format(app_name)) # Fetching total number of subscribers num_of_elements = ctx.get_num_subscribers() except Exception as e: logger.error("{}".format(e)) if dev_mode: fmt_str = ( '%(asctime)s : %(levelname)s : {} : %(name)s : [%(filename)s] :'. format("Insecure Mode") + '%(funcName)s : in line : [%(lineno)d] : %(message)s') else:
def main(): try: ctx = cfg.ConfigMgr() app_name = ctx.get_app_name() dev_mode = ctx.is_dev_mode() app_cfg = ctx.get_app_config() except Exception as e: logger.error("{}".format(e)) if dev_mode: fmt_str = ('%(asctime)s : %(levelname)s : {} : %(name)s \ : [%(filename)s] :'.format("Insecure Mode") + '%(funcName)s : in line : [%(lineno)d] : %(message)s') else: fmt_str = ('%(asctime)s : %(levelname)s : %(name)s \ : [%(filename)s] :' + '%(funcName)s : in line : [%(lineno)d] : %(message)s') logging.basicConfig(format=fmt_str, level=logging.DEBUG) query = app_cfg["query"] img_handle_queue = queue.Queue(maxsize=10000) # TODO: This is a temporary fix to wait until influx container # binds 8086 port. num_retries = 5 retry_count = 0 sock_retries = 2 sock_retry_count = 0 influx_up = False while (retry_count < num_retries): try: influx_host = os.getenv("INFLUX_SERVER", "") influx_port = os.getenv("INFLUXDB_PORT", 8086) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) if not (sock.connect_ex((influx_host, int(influx_port)))): influx_up = True break else: logger.info('Checking 8086 port of ' 'ia_influxdbconnector') retry_count += 1 time.sleep(10) except socket.gaierror as e: if (sock_retry_count <= sock_retries): logger.info('Waiting for ia_influxdbconnector ' 'container to start') sock_retry_count += 1 time.sleep(5) else: break if influx_up: time.sleep(5) else: logger.info('Please check ia_influxdbconnector container, ' 'then restart ia_discover_history container') os._exit(-1) # This thread will retrieve the image from imagestore service # and will store into frames directory condition = threading.Condition() img_query_thread = threading.Thread( target=imagestore_client.retrieve_image_frames, args=(ctx, query, img_handle_queue, condition)) img_query_thread.start() influx_query_thread = threading.Thread(target=retrieve_measurement_data, args=(ctx, query, img_handle_queue, condition)) influx_query_thread.start() influx_query_thread.join() logger.info("Influx query processing done") img_query_thread.join() logger.info("Retrieved all the frames exiting....") sys.exit(0)