def process_message(self, message): (alarm_id, metrics, new_state, old_state, link, lifecycle_state, state_change_reason, sub_alarms_json_snake_case, tenant_id, time_stamp) = parse_alarm_state_hist_message( message) ts = time_stamp / 1000.0 data = {"measurement": 'alarm_state_history', "time": datetime.fromtimestamp(ts, tz=pytz.utc).strftime( '%Y-%m-%dT%H:%M:%S.%fZ'), "fields": { "tenant_id": tenant_id.encode('utf8'), "alarm_id": alarm_id.encode('utf8'), "metrics": json.dumps(metrics, ensure_ascii=False).encode( 'utf8'), "new_state": new_state.encode('utf8'), "old_state": old_state.encode('utf8'), "link": link.encode('utf8'), "lifecycle_state": lifecycle_state.encode('utf8'), "reason": state_change_reason.encode('utf8'), "reason_data": "{}".encode('utf8'), "sub_alarms": sub_alarms_json_snake_case.encode('utf8') }, "tags": { "tenant_id": tenant_id.encode('utf8') }} LOG.debug(data) return data
def process_message(self, message): (alarm_id, metrics, new_state, old_state, link, lifecycle_state, state_change_reason, sub_alarms_json_snake_case, tenant_id, time_stamp) = parse_alarm_state_hist_message(message) name = 'alarm_state_history' fields = [] fields.append('tenant_id=' + line_utils.escape_value(tenant_id)) fields.append('alarm_id=' + line_utils.escape_value(alarm_id)) fields.append( 'metrics=' + line_utils.escape_value(json.dumps(metrics, ensure_ascii=False))) fields.append('new_state=' + line_utils.escape_value(new_state)) fields.append('old_state=' + line_utils.escape_value(old_state)) fields.append('link=' + line_utils.escape_value(link)) fields.append('lifecycle_state=' + line_utils.escape_value(lifecycle_state)) fields.append('reason=' + line_utils.escape_value(state_change_reason)) fields.append('reason_data=' + line_utils.escape_value("{}")) fields.append('sub_alarms=' + line_utils.escape_value(sub_alarms_json_snake_case)) line = name + ',tenant_id=' + line_utils.escape_tag(tenant_id) line += ' ' + ','.join(fields) line += ' ' + str(int(time_stamp)) LOG.debug(line) return line, tenant_id
def process_message(self, message): (alarm_id, metrics, new_state, old_state, link, lifecycle_state, state_change_reason, sub_alarms_json_snake_case, tenant_id, time_stamp) = parse_alarm_state_hist_message(message) ts = time_stamp / 1000.0 data = { "measurement": 'alarm_state_history', "time": datetime.fromtimestamp( ts, tz=pytz.utc).strftime('%Y-%m-%dT%H:%M:%S.%fZ'), "fields": { "tenant_id": tenant_id.encode('utf8'), "alarm_id": alarm_id.encode('utf8'), "metrics": json.dumps(metrics, ensure_ascii=False).encode('utf8'), "new_state": new_state.encode('utf8'), "old_state": old_state.encode('utf8'), "link": link.encode('utf8'), "lifecycle_state": lifecycle_state.encode('utf8'), "reason": state_change_reason.encode('utf8'), "reason_data": "{}".encode('utf8'), "sub_alarms": sub_alarms_json_snake_case.encode('utf8') }, "tags": { "tenant_id": tenant_id.encode('utf8') } } LOG.debug(data) return data
def process_message(self, message): (alarm_id, metrics, new_state, old_state, link, lifecycle_state, state_change_reason, sub_alarms_json_snake_case, tenant_id, time_stamp) = parse_alarm_state_hist_message( message) name = u'alarm_state_history' fields = [] fields.append(u'tenant_id=' + line_utils.escape_value(tenant_id)) fields.append(u'alarm_id=' + line_utils.escape_value(alarm_id)) fields.append(u'metrics=' + line_utils.escape_value( json.dumps(metrics, ensure_ascii=False))) fields.append(u'new_state=' + line_utils.escape_value(new_state)) fields.append(u'old_state=' + line_utils.escape_value(old_state)) fields.append(u'link=' + line_utils.escape_value(link)) fields.append(u'lifecycle_state=' + line_utils.escape_value( lifecycle_state)) fields.append(u'reason=' + line_utils.escape_value( state_change_reason)) fields.append(u'reason_data=' + line_utils.escape_value("{}")) fields.append(u'sub_alarms=' + line_utils.escape_value( sub_alarms_json_snake_case)) line = name + u',tenant_id=' + line_utils.escape_tag(tenant_id) line += u' ' + u','.join(fields) line += u' ' + str(int(time_stamp)) LOG.debug(line) return line
def process_message(self, message): (alarm_id, metrics, new_state, old_state, link, lifecycle_state, state_change_reason, sub_alarms_json_snake_case, tenant_id, time_stamp) = parse_alarm_state_hist_message(message) alarm_state_hist = (self._retention, json.dumps(metrics, ensure_ascii=False).encode('utf8'), old_state.encode('utf8'), new_state.encode('utf8'), sub_alarms_json_snake_case.encode('utf8'), state_change_reason.encode('utf8'), "{}".encode('utf8'), tenant_id.encode('utf8'), alarm_id.encode('utf8'), time_stamp) return alarm_state_hist
def process_message(self, message): (alarm_id, metrics, new_state, old_state, link, lifecycle_state, state_change_reason, sub_alarms_json_snake_case, tenant_id, time_stamp) = parse_alarm_state_hist_message(message) alarm_state_hist = (self._retention, json.dumps(metrics, ensure_ascii=False).encode('utf8'), old_state.encode('utf8'), new_state.encode('utf8'), sub_alarms_json_snake_case.encode('utf8'), state_change_reason.encode('utf8'), "{}".encode('utf8'), tenant_id.encode('utf8'), alarm_id.encode('utf8'), time_stamp) return alarm_state_hist
def test_parse_alarm_state_hist_message(self): message = mock.Mock() message.value.return_value = """{ "alarm-transitioned": { "alarmId": "dummyid", "metrics": "dummymetrics", "newState": "dummynewState", "oldState": "dummyoldState", "link": "dummylink", "lifecycleState": "dummylifecycleState", "stateChangeReason": "dummystateChangeReason", "tenantId": "dummytenantId", "timestamp": "dummytimestamp", "subAlarms": { "subAlarmExpression": "dummy_sub_alarm", "currentValues": "dummy_values", "metricDefinition": "dummy_definition", "subAlarmState": "dummy_state" } } }""" output = { 'sub_alarm_expression': 'dummy_sub_alarm', 'current_values': 'dummy_values', 'metric_definition': 'dummy_definition', 'sub_alarm_state': 'dummy_state' } data = utils.parse_alarm_state_hist_message(message) self.assertEqual(data[0], 'dummyid') self.assertEqual(data[1], 'dummymetrics') self.assertEqual(data[2], 'dummynewState') self.assertEqual(data[3], 'dummyoldState') self.assertEqual(data[4], 'dummylink') self.assertEqual(data[5], 'dummylifecycleState') self.assertEqual(data[6], 'dummystateChangeReason') sub_alarms_data = json.loads(data[7]) for elemKey, elemValue in output.items(): self.assertIn(elemValue, sub_alarms_data[elemKey]) self.assertEqual(data[8], 'dummytenantId') self.assertEqual(data[9], 'dummytimestamp')
def test_parse_alarm_state_hist_message(self): message = Mock() message.value.return_value = """{ "alarm-transitioned": { "alarmId": "dummyid", "metrics": "dummymetrics", "newState": "dummynewState", "oldState": "dummyoldState", "link": "dummylink", "lifecycleState": "dummylifecycleState", "stateChangeReason": "dummystateChangeReason", "tenantId": "dummytenantId", "timestamp": "dummytimestamp", "subAlarms": { "subAlarmExpression": "dummy_sub_alarm", "currentValues": "dummy_values", "metricDefinition": "dummy_definition", "subAlarmState": "dummy_state" } } }""" output = [ '"sub_alarm_expression":"dummy_sub_alarm"', '"current_values":"dummy_values"', '"metric_definition":"dummy_definition"', '"sub_alarm_state":"dummy_state"' ] data = utils.parse_alarm_state_hist_message(message) self.assertEqual(data[0], 'dummyid') self.assertEqual(data[1], 'dummymetrics') self.assertEqual(data[2], 'dummynewState') self.assertEqual(data[3], 'dummyoldState') self.assertEqual(data[4], 'dummylink') self.assertEqual(data[5], 'dummylifecycleState') self.assertEqual(data[6], "dummystateChangeReason") for elem in output: self.assertIn(elem, data[7]) self.assertEqual(data[8], 'dummytenantId') self.assertEqual(data[9], 'dummytimestamp')
def test_parse_alarm_state_hist_message(self): message = Mock() message.message.value = """{ "alarm-transitioned": { "alarmId": "dummyid", "metrics": "dummymetrics", "newState": "dummynewState", "oldState": "dummyoldState", "link": "dummylink", "lifecycleState": "dummylifecycleState", "stateChangeReason": "dummystateChangeReason", "tenantId": "dummytenantId", "timestamp": "dummytimestamp", "subAlarms": { "subAlarmExpression": "dummy_sub_alarm", "currentValues": "dummy_values", "metricDefinition": "dummy_definition", "subAlarmState": "dummy_state" } } }""" output = ['"sub_alarm_expression":"dummy_sub_alarm"', '"current_values":"dummy_values"', '"metric_definition":"dummy_definition"', '"sub_alarm_state":"dummy_state"'] data = utils.parse_alarm_state_hist_message(message) self.assertEqual(data[0], 'dummyid') self.assertEqual(data[1], 'dummymetrics') self.assertEqual(data[2], 'dummynewState') self.assertEqual(data[3], 'dummyoldState') self.assertEqual(data[4], 'dummylink') self.assertEqual(data[5], 'dummylifecycleState') self.assertEqual(data[6], "dummystateChangeReason") for elem in output: self.assertIn(elem, data[7]) self.assertEqual(data[8], 'dummytenantId') self.assertEqual(data[9], 'dummytimestamp')