How to get a value in an array as String?

Hi,

i need to get one item out of an array as string. In this example the array only has one entry.
What I want to do, is to restart a service when it crashed with an event command.

The dummy (not working :wink: ) version inside the event command:

var restart_service = ($service.vars.IcingaCheckService_Array_Service$).ConvertFirstValueFoundInArrayToString

You want that within Powershell or Icinga DSL?

Icinga DSL. Sry my dummy code looks more like powershell, as I’am better with that (read: i have no clue about icinga DSL advanced syntax) :slight_smile:

Try something like this:

$ icinga2 console
Icinga 2 (version: v2.11.0-330-g1a503b554)
Type $help to view available commands.
<1> => var arr = [ 1, 2, 3 ]
null
<2> => var res; if (len(arr) > 0) { res = arr[0].to_string() }; res
"1"

All types provide a to_string method. If you’re not certain about this, you could always use typeof() for additional sanity checks.

var res; if (len(arr) > 0) { var r = arr[0]; if (typeof(r) == Number) { res = r.to_string() } }; res

References

4 Likes