Package: prometheus-xmpp-alerts
Version: 0.4.2-1
Severity: important

prometheus-xmpp-alerts is expecting its datetime to be in UTC, but that
is not what the prometheus alertmanager uses.

This means basically prometheus-xmpp-alerts does not work with
prometheus-alermanager which is its main use-case.

I suspect that one or both of the programming teams assumes the servers
run UTC which is a pretty bad assuption, not sure if it is xmpp-alerts
fault for hard-coding the timezone or alermanagers fault for not setting
it. In reality its both.

In my opinion, the xmpp-alerts should accept both a UTC timezone and a
different timezone.

The file in question is 
/usr/lib/python3/dist-packages/prometheus_xmpp/__init__.py
with the function parse_timestring

    ts = re.sub('\\.([0-9]{6})([0-9]{0,3})Z$', r'.\1Z', ts)
    return datetime.strptime(ts, '%Y-%m-%dT%H:%M:%S.%fZ')

The error message is:
Sep 08 21:26:39 constantine prometheus-xmpp-alerts[2111702]: ValueError: time 
data '2022-09-08T21:26:09.706263468+10:00' does not match format 
'%Y-%m-%dT%H:%M:%S.%fZ'

which is true, "+10:00" doesn't match "Z", but it does match "%z". Also
%z understands "Z" too. Oddly enough it doesn't understand any other
sort of timezone, e.g. K is UTC+10h but that's strptime for you.

You'll also notice that the nanoseconds have not been stripped, again
because of the assumption of the timezone for the regex.

So the answer is, do timezones properly.
1) Make the regex understand both sorts of timezones
2) Use %z because that's what its for.

The regex is we either get "Z" or +/-hh:mm so the solution on a single
line is:

>>> datetime.strptime(re.sub('\\.([0-9]{6})([0-9]{0,3})(Z|[-+][0-9]{1,2}:[0-9]{2})$',
>>>  r'.\1\3',"2022-09-08T21:26:09.706263468+10:00"), '%Y-%m-%dT%H:%M:%S.%f%z')
datetime.datetime(2022, 9, 8, 21, 26, 9, 706263, 
tzinfo=datetime.timezone(datetime.timedelta(seconds=36000)))
>>> datetime.strptime(re.sub('\\.([0-9]{6})([0-9]{0,3})(Z|[-+][0-9]{1,2}:[0-9]{2})$',
>>>  r'.\1\3',"2022-09-08T21:26:09.706263468Z"), '%Y-%m-%dT%H:%M:%S.%f%z')
datetime.datetime(2022, 9, 8, 21, 26, 9, 706263, tzinfo=datetime.timezone.utc)
>>> datetime.strptime(re.sub('\\.([0-9]{6})([0-9]{0,3})(Z|[-+][0-9]{1,2}:[0-9]{2})$',
>>>  r'.\1\3',"2022-09-08T21:26:09.706263468-10:00"), '%Y-%m-%dT%H:%M:%S.%f%z')
datetime.datetime(2022, 9, 8, 21, 26, 9, 706263, 
tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=50400)))

I can provide a patch if required.

 - Craig

-- System Information:
Debian Release: 11.4
  APT prefers stable
  APT policy: (990, 'stable'), (500, 'stable-updates'), (500, 
'stable-security'), (500, 'testing'), (1, 'experimental')
Architecture: amd64 (x86_64)

Kernel: Linux 5.10.0-10-amd64 (SMP w/1 CPU thread)
Locale: LANG=en_AU.UTF-8, LC_CTYPE=en_AU.UTF-8 (charmap=UTF-8), LANGUAGE not set
Shell: /bin/sh linked to /bin/dash
Init: systemd (via /run/systemd/system)
LSM: AppArmor: enabled

Versions of packages prometheus-xmpp-alerts depends on:
ii  python3                    3.9.2-3
ii  python3-aiohttp            3.7.4-1
ii  python3-prometheus-client  0.9.0-1
ii  python3-slixmpp            1.7.0-2
ii  python3-yaml               5.3.1-5

prometheus-xmpp-alerts recommends no packages.

Versions of packages prometheus-xmpp-alerts suggests:
ii  prometheus-alertmanager  0.21.0+ds-4+b5

-- Configuration Files:
/etc/prometheus/xmpp-alerts.yml changed [not included]

-- no debconf information

Reply via email to