def capture_data(self, peer, sender, bus, topic, headers, message): # Grab the timestamp string from the message (we use this as the # value in our readings at the end of this method) _log.debug("In capture data") timestamp_string = headers.get(headers_mod.DATE, None) data = message try: if isinstance(data, dict): data = data elif isinstance(data, int) or \ isinstance(data, float) or \ isinstance(data, long): data = data except ValueError as e: log_message = "message for {topic} bad message string:" \ "{message_string}" _log.error(log_message.format(topic=topic, message_string=message[0])) raise topic = self.get_renamed_topic(topic) if self.gather_timing_data: add_timing_data_to_header( headers, self.core.agent_uuid or self.core.identity, "collected") payload = {'headers': headers, 'message': data} self._event_queue.put({'source': "forwarded", 'topic': topic, 'readings': [(timestamp_string, payload)]})
def capture_data(self, peer, sender, bus, topic, headers, message): # Grab the timestamp string from the message (we use this as the # value in our readings at the end of this method) _log.debug("In capture data") timestamp_string = headers.get(headers_mod.DATE, None) data = message try: if isinstance(data, dict): data = data elif isinstance(data, int) or isinstance(data, float): data = data except ValueError as e: log_message = "message for {topic} bad message string: {message_string}" _log.error(log_message.format(topic=topic, message_string=message[0])) raise topic = self.get_renamed_topic(topic) if self.gather_timing_data: add_timing_data_to_header(headers, self.core.agent_uuid or self.core.identity, "collected") payload = {'headers': headers, 'message': data} self._event_queue.put({'source': "forwarded", 'topic': topic, 'readings': [(timestamp_string, payload)]})
def capture_data(self, peer, sender, bus, topic, headers, message): # Grab the timestamp string from the message (we use this as the # value in our readings at the end of this method) _log.debug("In capture data") timestamp_string = headers.get(headers_mod.DATE, None) data = message try: # 2.0 agents compatability layer makes sender = pubsub.compat # so we can do the proper thing when it is here _log.debug("message in capture_data {}".format(message)) if sender == 'pubsub.compat': # data = jsonapi.loads(message[0]) data = compat.unpack_legacy_message(headers, message) _log.debug("data in capture_data {}".format(data)) if isinstance(data, dict): data = data elif isinstance(data, int) or \ isinstance(data, float) or \ isinstance(data, long): data = data # else: # data = data[0] except ValueError as e: log_message = "message for {topic} bad message string:" \ "{message_string}" _log.error( log_message.format(topic=topic, message_string=message[0])) raise if topic_replace_list: if topic in self._topic_replace_map.keys(): topic = self._topic_replace_map[topic] else: self._topic_replace_map[topic] = topic temptopics = {} for x in topic_replace_list: if x['from'] in topic: new_topic = temptopics.get(topic, topic) temptopics[topic] = new_topic.replace( x['from'], x['to']) for k, v in temptopics.items(): self._topic_replace_map[k] = v topic = self._topic_replace_map[topic] if gather_timing_data: add_timing_data_to_header( headers, self.core.agent_uuid or self.core.identity, "collected") payload = {'headers': headers, 'message': data} self._event_queue.put({ 'source': "forwarded", 'topic': topic, 'readings': [(timestamp_string, payload)] })
def capture_data(self, peer, sender, bus, topic, headers, message): # Grab the timestamp string from the message (we use this as the # value in our readings at the end of this method) _log.debug("In capture data") timestamp_string = headers.get(headers_mod.DATE, None) data = message try: # 2.0 agents compatability layer makes sender = pubsub.compat # so we can do the proper thing when it is here _log.debug("message in capture_data {}".format(message)) if sender == 'pubsub.compat': # data = jsonapi.loads(message[0]) data = compat.unpack_legacy_message(headers, message) _log.debug("data in capture_data {}".format(data)) if isinstance(data, dict): data = data elif isinstance(data, int) or \ isinstance(data, float) or \ isinstance(data, long): data = data # else: # data = data[0] except ValueError as e: log_message = "message for {topic} bad message string:" \ "{message_string}" _log.error(log_message.format(topic=topic, message_string=message[0])) raise if topic_replace_list: if topic in self._topic_replace_map.keys(): topic = self._topic_replace_map[topic] else: self._topic_replace_map[topic] = topic temptopics = {} for x in topic_replace_list: if x['from'] in topic: new_topic = temptopics.get(topic, topic) temptopics[topic] = new_topic.replace( x['from'], x['to']) for k, v in temptopics.items(): self._topic_replace_map[k] = v topic = self._topic_replace_map[topic] if gather_timing_data: add_timing_data_to_header(headers, self.core.agent_uuid or self.core.identity, "collected") payload = {'headers': headers, 'message': data} self._event_queue.put({'source': "forwarded", 'topic': topic, 'readings': [(timestamp_string, payload)]})
def publish_to_historian(self, to_publish_list): _log.debug("publish_to_historian number of items: {}".format(len(to_publish_list))) current_time = self.timestamp() last_time = self._last_timeout _log.debug('Last timeout: {} current time: {}'.format(last_time, current_time)) if self._last_timeout: # if we failed we need to wait 60 seconds before we go on. if self.timestamp() < self._last_timeout + 60: _log.debug('Not allowing send < 60 seconds from failure') return if not self.rmq_to_rmq_comm: if not self._target_platform: self.historian_setup() if not self._target_platform: _log.debug('Could not connect to target') return to_send = [] for x in to_publish_list: topic = x['topic'] headers = x['value']['headers'] message = x['value']['message'] if self.gather_timing_data: add_timing_data_to_header(headers, self.core.agent_uuid or self.core.identity, "forwarded") to_send.append({'topic': topic, 'headers': headers, 'message': message}) with gevent.Timeout(30): try: _log.debug("Sending to destination historian.") self.report_all_handled() # If local and destination platforms are using RMQ message bus, # then shovel will be used to setup the connection and forwarding # of data. All we need to do is perform normal RPC and specify # destination instance name if self.rmq_to_rmq_comm: kwargs = {"external_platform": self.destination_instance_name} self.vip.rpc.call(self.destination_historian_identity, 'insert', to_send, **kwargs).get(timeout=10) else: self._target_platform.vip.rpc.call(self.destination_historian_identity, 'insert', to_send).get( timeout=10) except gevent.Timeout: self._last_timeout = self.timestamp() if self._target_platform: self._target_platform.core.stop() self._target_platform = None _log.error("Timeout when attempting to publish to target.") self.vip.health.set_status( STATUS_BAD, "Timeout occurred")
def publish_to_historian(self, to_publish_list): _log.debug("publish_to_historian number of items: {}".format( len(to_publish_list))) current_time = self.timestamp() last_time = self._last_timeout _log.debug('Last timeout: {} current time: {}'.format( last_time, current_time)) if self._last_timeout: # if we failed we need to wait 60 seconds before we go on. if self.timestamp() < self._last_timeout + 60: _log.debug('Not allowing send < 60 seconds from failure') return if not self._target_platform: self.historian_setup() if not self._target_platform: _log.debug('Could not connect to target') return to_send = [] for x in to_publish_list: topic = x['topic'] headers = x['value']['headers'] message = x['value']['message'] if self.gather_timing_data: add_timing_data_to_header( headers, self.core.agent_uuid or self.core.identity, "forwarded") to_send.append({ 'topic': topic, 'headers': headers, 'message': message }) with gevent.Timeout(30): try: _log.debug("Sending to destination historian.") self._target_platform.vip.rpc.call( self.destination_historian_identity, 'insert', to_send).get(timeout=10) self.report_all_handled() except gevent.Timeout: self._last_timeout = self.timestamp() self._target_platform.core.stop() self._target_platform = None _log.error("Timeout when attempting to publish to target.") self.vip.health.set_status(STATUS_BAD, "Timeout occurred")
def publish_to_historian(self, to_publish_list): _log.debug("publish_to_historian number of items: {}" .format(len(to_publish_list))) current_time = self.timestamp() last_time = self._last_timeout _log.debug('Last timeout: {} current time: {}'.format(last_time, current_time)) if self._last_timeout: # if we failed we need to wait 60 seconds before we go on. if self.timestamp() < self._last_timeout + 60: _log.debug('Not allowing send < 60 seconds from failure') return if not self._target_platform: self.historian_setup() if not self._target_platform: _log.debug('Could not connect to target') return to_send = [] for x in to_publish_list: topic = x['topic'] headers = x['value']['headers'] message = x['value']['message'] if self.gather_timing_data: add_timing_data_to_header( headers, self.core.agent_uuid or self.core.identity, "forwarded") to_send.append({'topic': topic, 'headers': headers, 'message': message}) with gevent.Timeout(30): try: _log.debug("Sending to destination historian.") self._target_platform.vip.rpc.call( self.destination_historian_identity, 'insert', to_send).get(timeout=10) self.report_all_handled() except gevent.Timeout: self._last_timeout = self.timestamp() self._target_platform.core.stop() self._target_platform = None _log.error("Timeout when attempting to publish to target.") self.vip.health.set_status( STATUS_BAD, "Timeout occurred")
def capture_data(self, peer, sender, bus, topic, headers, message): # Grab the timestamp string from the message (we use this as the # value in our readings at the end of this method) _log.debug("In capture data") timestamp_string = headers.get(headers_mod.DATE, None) data = message try: # 2.0 agents compatability layer makes sender = pubsub.compat # so we can do the proper thing when it is here _log.debug("message in capture_data {}".format(message)) if sender == 'pubsub.compat': data = compat.unpack_legacy_message(headers, message) _log.debug("data in capture_data {}".format(data)) if isinstance(data, dict): data = data elif isinstance(data, int) or \ isinstance(data, float) or \ isinstance(data, long): data = data except ValueError as e: log_message = "message for {topic} bad message string:" \ "{message_string}" _log.error( log_message.format(topic=topic, message_string=message[0])) raise topic = self.get_renamed_topic(topic) if self.gather_timing_data: add_timing_data_to_header( headers, self.core.agent_uuid or self.core.identity, "collected") payload = {'headers': headers, 'message': data} self._event_queue.put({ 'source': "forwarded", 'topic': topic, 'readings': [(timestamp_string, payload)] })
def publish_to_historian(self, to_publish_list): for item in to_publish_list: if self._gather_timing_data: turnaround_time = add_timing_data_to_header(item["headers"], self.core.agent_uuid or self.core.identity, "published") self._turnaround_times.append(turnaround_time) if len(self._turnaround_times) > 10000: # Test is now over. Button it up and shutdown. mean = math_utils.mean(self._turnaround_times) stdev = math_utils.stdev(self._turnaround_times) _log.info("Mean time from collection to publish: " + str(mean)) _log.info("Std dev time from collection to publish: " + str(stdev)) self._turnaround_times = [] #_log.debug("publishing {}".format(item)) _log.debug("recieved {} items to publish" .format(len(to_publish_list))) self.report_all_handled()
def publish_to_historian(self, to_publish_list): for item in to_publish_list: if self._gather_timing_data: turnaround_time = add_timing_data_to_header(item["headers"], self.core.agent_uuid or self.core.identity, "published") self._turnaround_times.append(turnaround_time) if len(self._turnaround_times) > 10000: # Test is now over. Button it up and shutdown. mean = math_utils.mean(self._turnaround_times) stdev = math_utils.stdev(self._turnaround_times) _log.info("Mean time from collection to publish: " + str(mean)) _log.info("Std dev time from collection to publish: " + str(stdev)) self._turnaround_times = [] #_log.debug("publishing {}".format(item)) _log.debug("recieved {} items to publish" .format(len(to_publish_list))) self.report_all_handled()
def publish_to_historian(self, to_publish_list): handled_records = [] _log.debug("publish_to_historian number of items: {}" .format(len(to_publish_list))) parsed = urlparse(self.core.address) next_dest = urlparse(destination_vip) current_time = self.timestamp() last_time = self._last_timeout _log.debug('Lasttime: {} currenttime: {}'.format(last_time, current_time)) timeout_occurred = False if self._last_timeout: # if we failed we need to wait 60 seconds before we go on. if self.timestamp() < self._last_timeout + 60: _log.debug('Not allowing send < 60 seconds from failure') return if not self._target_platform: self.historian_setup() if not self._target_platform: _log.debug('Could not connect to target') return for vip_id in required_target_agents: try: self._target_platform.vip.ping(vip_id).get() except Unreachable: skip = "Skipping publish: Target platform not running " \ "required agent {}".format(vip_id) _log.warn(skip) self.vip.health.set_status( STATUS_BAD, skip) return except Exception as e: err = "Unhandled error publishing to target platform." _log.error(err) _log.error(traceback.format_exc()) self.vip.health.set_status( STATUS_BAD, err) return for x in to_publish_list: topic = x['topic'] value = x['value'] # payload = jsonapi.loads(value) payload = value headers = payload['headers'] headers['X-Forwarded'] = True try: del headers['Origin'] except KeyError: pass try: del headers['Destination'] except KeyError: pass if gather_timing_data: add_timing_data_to_header(headers, self.core.agent_uuid or self.core.identity,"forwarded") if timeout_occurred: _log.error( 'A timeout has occurred so breaking out of publishing') break with gevent.Timeout(30): try: _log.debug('debugger: {} {} {}'.format(topic, headers, payload)) self._target_platform.vip.pubsub.publish( peer='pubsub', topic=topic, headers=headers, message=payload['message']).get() except gevent.Timeout: _log.debug("Timeout occurred email should send!") timeout_occurred = True self._last_timeout = self.timestamp() self._num_failures += 1 # Stop the current platform from attempting to # connect self._target_platform.core.stop() self._target_platform = None self.vip.health.set_status( STATUS_BAD, "Timeout occured") except Unreachable: _log.error("Target not reachable. Wait till it's ready!") except ZMQError as exc: if exc.errno == ENOTSOCK: # Stop the current platform from attempting to # connect _log.error("Target disconnected. Stopping target platform agent") self._target_platform = None self.vip.health.set_status( STATUS_BAD, "Target platform disconnected") except Exception as e: err = "Unhandled error publishing to target platfom." _log.error(err) _log.error(traceback.format_exc()) self.vip.health.set_status( STATUS_BAD, err) # Before returning lets mark any that weren't errors # as sent. self.report_handled(handled_records) return else: handled_records.append(x) _log.debug("handled: {} number of items".format( len(to_publish_list))) self.report_handled(handled_records) if timeout_occurred: _log.debug('Sending alert from the ForwardHistorian') status = Status.from_json(self.vip.health.get_status()) self.vip.health.send_alert(FORWARD_TIMEOUT_KEY, status) else: self.vip.health.set_status( STATUS_GOOD,"published {} items".format( len(to_publish_list)))
def publish_to_historian(self, to_publish_list): handled_records = [] _log.debug("publish_to_historian number of items: {}".format( len(to_publish_list))) parsed = urlparse(self.core.address) next_dest = urlparse(destination_vip) current_time = self.timestamp() last_time = self._last_timeout _log.debug('Lasttime: {} currenttime: {}'.format( last_time, current_time)) timeout_occurred = False if self._last_timeout: # if we failed we need to wait 60 seconds before we go on. if self.timestamp() < self._last_timeout + 60: _log.debug('Not allowing send < 60 seconds from failure') return if not self._target_platform: self.historian_setup() if not self._target_platform: _log.debug('Could not connect to target') return for vip_id in required_target_agents: try: self._target_platform.vip.ping(vip_id).get() except Unreachable: skip = "Skipping publish: Target platform not running " \ "required agent {}".format(vip_id) _log.warn(skip) self.vip.health.set_status(STATUS_BAD, skip) return except Exception as e: err = "Unhandled error publishing to target platform." _log.error(err) _log.error(traceback.format_exc()) self.vip.health.set_status(STATUS_BAD, err) return for x in to_publish_list: topic = x['topic'] value = x['value'] # payload = jsonapi.loads(value) payload = value headers = payload['headers'] headers['X-Forwarded'] = True try: del headers['Origin'] except KeyError: pass try: del headers['Destination'] except KeyError: pass if gather_timing_data: add_timing_data_to_header( headers, self.core.agent_uuid or self.core.identity, "forwarded") if timeout_occurred: _log.error( 'A timeout has occurred so breaking out of publishing') break with gevent.Timeout(30): try: _log.debug('debugger: {} {} {}'.format( topic, headers, payload)) self._target_platform.vip.pubsub.publish( peer='pubsub', topic=topic, headers=headers, message=payload['message']).get() except gevent.Timeout: _log.debug("Timeout occurred email should send!") timeout_occurred = True self._last_timeout = self.timestamp() self._num_failures += 1 # Stop the current platform from attempting to # connect self._target_platform.core.stop() self._target_platform = None self.vip.health.set_status(STATUS_BAD, "Timeout occured") except Unreachable: _log.error( "Target not reachable. Wait till it's ready!") except ZMQError as exc: if exc.errno == ENOTSOCK: # Stop the current platform from attempting to # connect _log.error( "Target disconnected. Stopping target platform agent" ) self._target_platform = None self.vip.health.set_status( STATUS_BAD, "Target platform disconnected") except Exception as e: err = "Unhandled error publishing to target platfom." _log.error(err) _log.error(traceback.format_exc()) self.vip.health.set_status(STATUS_BAD, err) # Before returning lets mark any that weren't errors # as sent. self.report_handled(handled_records) return else: handled_records.append(x) _log.debug("handled: {} number of items".format( len(to_publish_list))) self.report_handled(handled_records) if timeout_occurred: _log.debug('Sending alert from the ForwardHistorian') status = Status.from_json(self.vip.health.get_status()) self.vip.health.send_alert(FORWARD_TIMEOUT_KEY, status) else: self.vip.health.set_status( STATUS_GOOD, "published {} items".format(len(to_publish_list)))