Get_time_period() not working

Hello I want a “backup” range for our load checks. My problem is this one returns always true

object TimePeriod "backup" {
  ranges = {
    monday = "02:00-03:00"
    tuesday = "02:00-03:00"
    wednesday = "02:00-03:00"
    thursday = "02:00-03:00"
    friday = "02:00-03:00"
    saturday = "02:00-03:00"
    sunday = "02:00-03:00"
  }
}

if (get_time_period("backup").is_inside) {
  vars.testbackup = "true"
} else {
  vars.testbackup = "false"
}

This one returns “Object of type ‘Function’

  vars.testbackup = {{
    if (get_time_period("backup").is_inside) {
      return ["true"]
    } else {
      return ["false"]
    }
  }}

What can I do?

Hello,
I have an idea to confirm the condition is working properly or not.

  1. Connect to icinga console, ex :

ICINGA2_API_PASSWORD=mypassword icinga2 console --connect ‘https://root@localhost:5665/

  1. run get_time_period("backup")
  2. run get_time_period("backup").is_inside
  3. manually change your server time to a value between 2 and 3 AM, also temporarly disable ntp services if you have (chronyd, ntpd, etc), confirm date is in good range :

systemctl stop chronyd ntpd
timedatectl set-time 2020-10-06 2:30:00
date

  1. run get_time_period("backup").is_inside

Can you post commands outputs or each steps ?

This is working, I did a test before. I changed the range not the time of the server (I don’t want to chenge server time)

The Problem is in the if condition maybee?

<1> => get_time_period("backup")
{
__name = "backup"
active = true
display_name = "backup"
excludes = [ ]
extensions = {
	DbObject = {
		type = "Object"
	}
}
ha_mode = 0.000000
includes = [ ]
is_inside = false
name = "backup"
original_attributes = null
package = "_etc"
pause_called = false
paused = false
prefer_includes = true
ranges = {
	friday = "03:00-05:00"
	monday = "03:00-05:00"
	saturday = "03:00-05:00"
	sunday = "03:00-05:00"
	thursday = "03:00-05:00"
	tuesday = "03:00-05:00"
	wednesday = "03:00-05:00"
}
resume_called = true
segments = [ {
	begin = 1602032400.000000
	end = 1602039600.000000
} ]
source_location = {
	first_column = 0.000000
	first_line = 1.000000
	last_column = 25.000000
	last_line = 1.000000
	path = "/etc/icinga2/conf.d/wy/notifications/backup.conf"
}
start_called = true
state_loaded = true
stop_called = false
templates = [ "backup", "legacy-timeperiod" ]
type = "TimePeriod"
update = {
	arguments = [ "tp", "begin", "end" ]
	deprecated = false
	name = "Internal#LegacyTimePeriod"
	side_effect_free = false
	type = "Function"
}
valid_begin = 1601972661.818574
valid_end = 1602062661.818574
vars = null
version = 0.000000
zone = ""
}

Result A

<2> => get_time_period("backup").is_inside
false

changed range

<2> => get_time_period("backup")
{
	__name = "backup"
	active = true
	display_name = "backup"
	excludes = [ ]
	extensions = {
		DbObject = {
			type = "Object"
		}
	}
	ha_mode = 0.000000
	includes = [ ]
	is_inside = true
	name = "backup"
	original_attributes = null
	package = "_etc"
	pause_called = false
	paused = false
	prefer_includes = true
	ranges = {
		friday = "03:00-05:00"
		monday = "03:00-05:00"
		saturday = "03:00-05:00"
		sunday = "03:00-05:00"
		thursday = "03:00-05:00"
		tuesday = "11:00-13:00"
		wednesday = "03:00-05:00"
	}
	resume_called = true
	segments = [ {
		begin = 1601974800.000000
		end = 1601982000.000000
	}, {
		begin = 1602032400.000000
		end = 1602039600.000000
	} ]
	source_location = {
		first_column = 0.000000
		first_line = 1.000000
		last_column = 25.000000
		last_line = 1.000000
		path = "/etc/icinga2/conf.d/wy/notifications/backup.conf"
	}
	start_called = true
	state_loaded = true
	stop_called = false
	templates = [ "backup", "legacy-timeperiod" ]
	type = "TimePeriod"
	update = {
		arguments = [ "tp", "begin", "end" ]
		deprecated = false
		name = "Internal#LegacyTimePeriod"
		side_effect_free = false
		type = "Function"
	}
	valid_begin = 1601972961.817979
	valid_end = 1602063098.971921
	vars = null
	version = 0.000000
	zone = ""
}

Result B

<3> => get_time_period("backup").is_inside
true

Thanks for the input, it works indeed.

After rereading the whole thing, i’m asking myself if the problem is not about how it is evaluated.
Are you expecting true/false as boolean or “true”/“false” as string when you reuse vars.testbackup value ?

I would also need more context about in which objects this snippets are to know how they are called and how do you validate their values, if you want to know their value at runtime while you cant check it from console you can use the log function to get value from icinga log, it’s especially true for the part that is shown to you as “Object of type ‘Function’” to know what’s going on inside.

This is my complete check.

I never get the else statement

object CheckCommand "by_ssh_load" {
  import "by_ssh"
  vars.by_ssh_logname = "icinga"
  vars.by_ssh_identity= "/var/spool/icinga2/.ssh/icinga"
  vars.by_ssh_command = "/usr/lib64/nagios/plugins/check_load -r -w $warn$ -c $crit$"
}

apply Service "load" {
  import "generic-service"
  check_command = "by_ssh_load"

  if (host.vars.check_load_is_highload_system) {
    vars.warn = "4,3,2"
    vars.crit = "5,4,3"
  } else {
    vars.warn = "1,.9,.8"
    vars.crit = "1.1,1,.9"
  }

  if (get_time_period("backup").is_inside) {
    vars.warn = "5,4,3"
    vars.crit = "6,5,4"
  } else {
    vars.testbackup = "no"
  }

  assign where host.vars.check_load=="1"

See https://icinga.com/docs/icinga2/latest/doc/08-advanced-topics/
#time-dependent-thresholds for an example of how to set these variables.

Antony.

@Pooh thanx, but I know this #time-dependent-thresholds example. That’s my problem it does not work.

@Pooh thanx, but I know this #time-dependent-thresholds example. That’s my
problem it does not work.

Have you tried it?

The code you posted previously is not what’s in the example:

Your code:

 if (get_time_period("backup").is_inside) {
   vars.warn = "5,4,3"
   vars.crit = "6,5,4"
 }

Example code:

vars.load_wload1 = {{
if (get_time_period(“backup”).is_inside) {
return 20
} else {
return 5
}
}}

Antony.

yes I tested both.

Then I get: “Object of type ‘Function’”

The problem is indeed in the different syntax if I remember correct.

if (get_time_period("backup").is_inside) {
   vars.warn = "5,4,3"
   vars.crit = "6,5,4"
}

Is evaluated during load of configuration, so only once and dynamically.

vars.load_wload1 = {{
  if (get_time_period(“backup”).is_inside) {
    return 20
  } else {
    return 5
  }
}}

Causes the function behind the variable to be executed every time the variable is accessed, so it is dynamic.

I had created an issue to include such functions in the ITL as global function which was rejected, but perhaps helps you to create a nicer configuration: https://github.com/Icinga/icinga2/issues/4890
This could be improved further by having the function return a function, but I can not remember the syntax for this at the moment.