Support different time formats for nagios last_check parameter

Change-Id: Ia18a6bdfb4516ee5732718200b2aa0076b2df98d
This commit is contained in:
Ifat Afek 2017-11-21 14:47:06 +02:00
parent cb5b6c8a30
commit 3d9ad8f6e7
3 changed files with 3 additions and 4 deletions

View File

@ -40,7 +40,6 @@ class NagiosTransformer(AlarmTransformerBase):
update_timestamp = datetime_utils.change_to_utc_time_and_format( update_timestamp = datetime_utils.change_to_utc_time_and_format(
entity_event[NagiosProperties.LAST_CHECK], entity_event[NagiosProperties.LAST_CHECK],
'%Y-%m-%d %H:%M:%S',
tbase.TIMESTAMP_FORMAT) tbase.TIMESTAMP_FORMAT)
vitrage_sample_timestamp = entity_event[DSProps.SAMPLE_DATE] vitrage_sample_timestamp = entity_event[DSProps.SAMPLE_DATE]
update_timestamp = \ update_timestamp = \

View File

@ -134,7 +134,6 @@ class NagiosTransformerTest(base.BaseTest):
self.assertEqual(event[NagiosProperties.SERVICE], vertex[VProps.NAME]) self.assertEqual(event[NagiosProperties.SERVICE], vertex[VProps.NAME])
self.assertEqual(datetime_utils.change_to_utc_time_and_format( self.assertEqual(datetime_utils.change_to_utc_time_and_format(
event[NagiosProperties.LAST_CHECK], event[NagiosProperties.LAST_CHECK],
'%Y-%m-%d %H:%M:%S',
tbase.TIMESTAMP_FORMAT), tbase.TIMESTAMP_FORMAT),
vertex[VProps.UPDATE_TIMESTAMP]) vertex[VProps.UPDATE_TIMESTAMP])
event_type = event.get(DSProps.EVENT_TYPE, None) event_type = event.get(DSProps.EVENT_TYPE, None)

View File

@ -16,6 +16,7 @@ from __future__ import absolute_import
from datetime import datetime from datetime import datetime
from datetime import timedelta from datetime import timedelta
from dateutil import parser
from oslo_utils import timeutils from oslo_utils import timeutils
@ -32,8 +33,8 @@ def change_time_str_format(timestamp_str, old_format, new_format):
return utc.strftime(new_format) return utc.strftime(new_format)
def change_to_utc_time_and_format(timestamp_str, old_format, new_format): def change_to_utc_time_and_format(timestamp_str, new_format):
timestamp = datetime.strptime(timestamp_str, old_format) timestamp = parser.parse(timestamp_str)
timestamp = timestamp - timedelta(seconds=( timestamp = timestamp - timedelta(seconds=(
datetime.now() - datetime.utcnow()).total_seconds()) datetime.now() - datetime.utcnow()).total_seconds())
return timestamp.strftime(new_format) return timestamp.strftime(new_format)