Dummy check status + variable do not calculate correctly, but it works in console?

So i have attempted to produce a dummy service generator + dependency generator

I start out with this :

var klastry_backup["lubssbl03_full"] = {
  nodes = [ "lubssbl01", "lubssbl02" ]
  schedules = [ "sqlv2_sql99_full_week" ]
}
 var klastry_backup["CHV09_WAWSDEVSQL"] = {
  nodes = [ "lubshvh52", "lubshvh53" ]
  schedules = [ "chv09_wawsdevsql" ]
}

(...)

and then i invoke a function that parses those structures and generates services with dummy check

  wygeneruj_checki_tsm = function(klastry_backup) {
        for (cluster => config in klastry_backup) {
          for (schedule in config.schedules) {
            log("making schedule " + schedule + " for " + cluster)
            log("nodes are : " + string(config.nodes))
            apply Service "tsm_schedule_" + cluster + "_" + schedule use(config,cluster,schedule) {
              check_command = "dummy"
              display_name = "Sprawdzenie " + schedule + " dla " + cluster + " " + string(config.nodes) + " " + schedule
              vars.no_notifications = true
              vars.no_notification = true

// this is a dummy host for those all services
              assign where host.name == "tsmclusters"

              var cluster_nodes = config.nodes

              vars.dummy_text = datacenter.oblicz_avail_message(cluster_nodes, schedule)
              vars.dummy_state = datacenter.oblicz_avail(cluster_nodes, schedule)
            }
            var nodes = config.nodes
          } //schedule

        }
  }
} //namespace


datacenter.wygeneruj_checki_tsm(klastry_backup)

So far, it all works fine, but the calculation of dummy_text and dummy_state fails.

namespace datacenter { 
//calculate how many nodes have a matching service with OK and return 0 if at least one is found
  function oblicz_avail(nodes, schedule) {
                // obliczenie stanu z nodow
                var up_count = 0
                var stan = 0

                for (node in nodes) {

                  for (stan in get_services(node).filter(
                    (sv) use(node, schedule) => match("*" + schedule, sv.name)
                  ).map(s => s.state)) {
                    if (stan == 0 ) { up_count += 1 }
                  } //for
                }
                if (up_count > 0 ) {
                  return 0
                } else {
                  return 1
                }

  } // function

Function to gather status messages from discovered services

//gather status messages from matching services across the nodes.


  function oblicz_avail_message(nodes, schedule) {
    var message = ""
    for (node in nodes) {
      message += "checking for " + node + "\n"
      for (mesg in get_services(node).filter((sv) use (schedule) => match("*" + schedule, sv.name)).map(s => s.host_name + " " + s.state + " " + s.last_check_result.output + "\n")) {
        message += mesg
      }
      var up_count = 0
      for (stan in get_services(node).filter(
        (sv) use(node, schedule) => match("*" + schedule, sv.name)
      ).map(s => s.state)) {
        if (stan == 0 ) { up_count += 1 }
      }
      message += node + " " + schedule + " " + up_count + "\n"
    }
    return message
  } // function
} // namespace

Now the odd thing is - in icinga2 the dummy status is always set to 1 ( which means the up_count is never altered from initial value of 0 )

And the dummy_text is generated from all lines that modify message EXCEPT the for loop.

e.g. i would get a message of ( in icingaweb2 )

checking for lubshvh52
lubshvh52 chv09_wawsiis07 0
checking for lubshvh53
lubshvh53 chv09_wawsiis07 0

While, if i declare neccesary variables in icinga2 console :

<2> =>   nodes = [ "lubshvh52", "lubshvh53" ]
null
<3> => schedule = "chv09_wawsiis07"
null
<4> => datacenter.oblicz_avail_message (nodes,schedule)
"checking for lubshvh52\nlubshvh52 2 [cappo71s] backup is Failed - The operation completed with at least one error message (except for error messages for skipped files). exit code 12, start date: 2022-12-26 19:00:00\nlubshvh52 chv09_wawsiis07 0\nchecking for lubshvh53\nlubshvh53 0 [cappo71s] backup is Completed - All operations completed successfully. exit code 0, start date: 2022-12-26 19:00:00\nlubshvh53 chv09_wawsiis07 1\n"

So basically, there is plugin_output visible here, which doesn’t appear in icinga2. And the up_count is 1 this time around. So the function is finding the services this time around.

How can i proceed to debug this better? Or does anyone have a clue as to what is going on?

I approached it differently and it works this time around

namespace datacenter { 
  wygeneruj_checki_tsm = function(cluster) {
          for (schedule in cluster.schedules) {
            log("making schedule " + schedule + " for " + string(cluster))
            var node_list = ""
            var node_list_display = ""
            for (node in cluster.nodes) {
              node_list += node
              node_list_display += node + " "
            }

            apply Service "tsm_schedule_" + node_list + "_" + schedule use(node_list,node_list_display,cluster,schedule) {
              check_command = "dummy"
              display_name = "Backup " + schedule + " dla nodow " + node_list_display
              vars.no_notifications = true
              vars.no_notification = true
              assign where host.name == "tsmclusters"

              vars.cluster_nodes = cluster.nodes
              vars.schedule = schedule

              vars.dummy_text = {{
                var nodes = macro("$cluster_nodes$")
                var schedule = macro("$schedule$")
                var message = ""
                var mesg=""
                for (node in nodes) {
                  for (mesg in get_services(node).filter((sv) use (schedule) => match("*" + schedule, sv.name)).map(s => s.host_name + " " + s.state + " " + s.last_check_result.output + "\n")) {
                    message += mesg
                  }
                }
                return message
              }}

              vars.dummy_state = {{
                var up_count = 0
                var stan = 0

                var nodes = macro("$cluster_nodes$")
                var schedule = macro("$schedule$")

                for (node in nodes) {
                  for (stan in get_services(node).filter(
                    (sv) use(node, schedule) => match("*" + schedule, sv.name)
                  ).map(s => s.state)) {
                    if (stan == 0 ) { up_count += 1 }
                  } //for
                }
                if (up_count > 0 ) {
                  return 0
                }
              }}

            }
          } //schedule

  } // function
} //namespace


// function is called per-entry in dictionary
for (klaster => entry in klastry_backup) {
  log("making check for " + klaster)
  datacenter.wygeneruj_checki_tsm(entry)
}

I am still puzzled as to why it failed to work beforehand.