Error in alarm evaluation when timezone in time_constraints is specified

Bug #1342676 reported by Rikimaru Honjo
14
This bug affects 2 people
Affects Status Importance Assigned to Milestone
Ceilometer
Fix Released
Medium
Rikimaru Honjo

Bug Description

Define a alarm with timezone in time_constraints is specified as follws:

curl -v -X POST -H 'X-Auth-Token:5093deed81f9418a9efcecccf9cf2e4a' -H 'Content-type: application/json' -d '{"alarm_actions": ["http://site:8000/alarm"], "name": "TestAlarm3", "threshold_rule": {"meter_name":"image.size", "period":10, "comparison_operator":"gt", "threshold":1.0, "statistic": "avg" }, "type": "threshold", "time_constraints": [{"description": "nightly build every night at 23h for 3 hours", "duration": 10800, "name": "SampleConstraint", "start": "0 23 * * *", "timezone": "Europe/Ljubljana"}]}' http://localhost:8777/v2/alarms

The following error occurs when Alarm evaluator tries to evaluate the alarm:

2014-06-27 15:06:36.017 7694 ERROR ceilometer.alarm.service [-] alarm evaluation cycle failed
2014-06-27 15:06:36.017 7694 TRACE ceilometer.alarm.service Traceback (most recent call last):
2014-06-27 15:06:36.017 7694 TRACE ceilometer.alarm.service File "/usr/local/lib/python2.7/dist-packages/ceilometer/alarm/service.py", line 93, in _evaluate_assigned_alarms
2014-06-27 15:06:36.017 7694 TRACE ceilometer.alarm.service self._evaluate_alarm(alarm)
2014-06-27 15:06:36.017 7694 TRACE ceilometer.alarm.service File "/usr/local/lib/python2.7/dist-packages/ceilometer/alarm/service.py", line 105, in _evaluate_alarm
2014-06-27 15:06:36.017 7694 TRACE ceilometer.alarm.service self.evaluators[alarm.type].obj.evaluate(alarm)
2014-06-27 15:06:36.017 7694 TRACE ceilometer.alarm.service File "/usr/local/lib/python2.7/dist-packages/ceilometer/alarm/evaluator/threshold.py", line 175, in evaluate
2014-06-27 15:06:36.017 7694 TRACE ceilometer.alarm.service if not self.within_time_constraint(alarm):
2014-06-27 15:06:36.017 7694 TRACE ceilometer.alarm.service File "/usr/local/lib/python2.7/dist-packages/ceilometer/alarm/evaluator/__init__.py", line 95, in within_time_constraint
2014-06-27 15:06:36.017 7694 TRACE ceilometer.alarm.service now_tz = now_utc.astimezone(tz) if tz else now_utc
2014-06-27 15:06:36.017 7694 TRACE ceilometer.alarm.service ValueError: astimezone() cannot be applied to a naive datetime
2014-06-27 15:06:36.017 7694 TRACE ceilometer.alarm.service

The error says that astimezone() cannot be applied to a native datetime (now_utc).
Thus, making now_utc to non-native (aware object) will solve the problem.
The following code will do.

>>> import datetime
>>> now_utc = datetime.datetime.utcnow()
>>> print now_utc.tzinfo
None
>>> import pytz
>>> tz = pytz.timezone('Europe/Ljubljana')
>>> now_tz = now_utc.astimezone(tz)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: astimezone() cannot be applied to a naive datetime
>>>
>>> utc = pytz.timezone('UTC')
>>> now_tz = now_utc.replace(tzinfo=utc).astimezone(tz)
>>>

Changed in ceilometer:
assignee: nobody → Rikimaru Honjo (honjo-rikimaru-c6)
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to ceilometer (master)

Fix proposed to branch: master
Review: https://review.openstack.org/107332

Changed in ceilometer:
status: New → In Progress
Eoghan Glynn (eglynn)
Changed in ceilometer:
milestone: none → juno-3
importance: Undecided → Medium
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to ceilometer (master)

Reviewed: https://review.openstack.org/107332
Committed: https://git.openstack.org/cgit/openstack/ceilometer/commit/?id=667e4643122761b899a0824662a644ba7253cef8
Submitter: Jenkins
Branch: master

commit 667e4643122761b899a0824662a644ba7253cef8
Author: Rikimaru Honjo <email address hidden>
Date: Thu Jul 17 19:59:23 2014 +0900

    Replace datetime of time_constraints by aware object

    Error occurs in alarm evaluation when timezone in time_constraints is specified,
    because astimezone() cannot be applied to a native datetime.

    Making datetime to non-native (aware object) will solve the problem.

    Change-Id: Ic0de4a5b32865bbe330c2936f14adf59168eefe5
    Closes-bug: #1342676

Changed in ceilometer:
status: In Progress → Fix Committed
Thierry Carrez (ttx)
Changed in ceilometer:
status: Fix Committed → Fix Released
Thierry Carrez (ttx)
Changed in ceilometer:
milestone: juno-3 → 2014.2
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.