DateTime function with timezone


(Chris) #1

Hello

is there currently a possibility to add a timezone to the DateTime function ?

in this case i try to montor a file with a date in the filename, but the server is in another Timezone.
The monitoring checks the last write to a file (to check if a application is dead).

Here my example:

vars.someattribute = {{ return "C:\\folder\\log\\filename_" + DateTime().format("%Y-%m-%d") + ".log" }}

Is there currently a possibility to do this?

I did not find anything in the docs about this:
https://icinga.com/docs/icinga2/latest/doc/18-library-reference/#datetime-type

Would it be better to create a feature request ?

Regards & Thanks


#2

You can print the timezone using “%z” but you can not change it.

<11> => DateTime().format("%H:%m%z")
"16:01+0100"

(Michael Friedrich) #3

This won’t work reliably, since the custom attribute is evaluated on the master/satellite, and not the agent which actually executes the check. So you’ll need to know about the agent’s timezone in advance and construct a suitable DateTime object exclusively for this agent host and timezone (which won’t work since the constructor cannot provide such an offset). The relevant code parts are located here: https://github.com/Icinga/icinga2/blob/master/lib/base/datetime.cpp in case you want to look into a patch here.