|
| 1 | +# Use PowerShell Arrays within Icinga Config |
| 2 | + |
| 3 | +For the [Icinga Director](https://icinga.com/docs/director/latest/) we do provide a config generator, allowing users to easily import `CheckCommands` directly into the Icinga infrastructure by using the `Director Baskets`. |
| 4 | + |
| 5 | +Some environments how ever do not make use of the Icinga Director and therefor require to handle PowerShell arrays as well. |
| 6 | + |
| 7 | +## PowerShell Arrays |
| 8 | + |
| 9 | +PowerShell arrays are usually defined as `@()` or together with `[array]`. In the end, the PowerShell is now expecting a comma separated input with your values. |
| 10 | + |
| 11 | +```powershell |
| 12 | +-ArrayArgument 'value1', 'value2', 'value3' |
| 13 | +``` |
| 14 | + |
| 15 | +By default Icinga is not able to push arguments in this way and therefor we require a workaround |
| 16 | + |
| 17 | +## Icinga DSL for PowerShell arrays |
| 18 | + |
| 19 | +As Icinga is a very powerful solution and understands certain object types like arrays, we can make use of this. |
| 20 | + |
| 21 | +The easiest approach for integrating arguments into the PowerShell array handling, we can use the internal array handling of Icinga itself. This means from a configuration point we do not need to re-think our configuration or approach. |
| 22 | + |
| 23 | +Let's assume we have an argument called `-Services` within our check plugin which expects the `array` type. As we just learned that Icinga does understand the array data type and we can use it inside our configuration, we will continue with this approach. |
| 24 | + |
| 25 | +For this we will now write an Icinga DSL function and apply it to our `-Services` argument. We will use the custom variable `Icinga_Windows_Services` to store all our services we want to monitor on our service checks: |
| 26 | + |
| 27 | +```powershell |
| 28 | +"-Services" = { |
| 29 | + value = {{ |
| 30 | + var arr = macro("$Icinga_Windows_Services$"); |
| 31 | +
|
| 32 | + if (len(arr) == 0) { |
| 33 | + return "$null"; |
| 34 | + } |
| 35 | +
|
| 36 | + return arr.join(","); |
| 37 | + }} |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +What we do here is to load the `Icinga_Windows_Services` into the variable `arr`. After that we will check if there are elements set inside the `arr` variable with `len(arr)`. If no value is inside, we will return `$null` to always set a value to the PowerShell. |
| 42 | + |
| 43 | +Last but not least we will glue every single value inside the array together by using `arr.join(",")` and separate it with a comma. |
| 44 | + |
| 45 | +For example if we are doing this inside your service configuration: |
| 46 | + |
| 47 | +``` |
| 48 | +vars.Icinga_Windows_Services = [ "icinga2" ] |
| 49 | +vars.Icinga_Windows_Services += [ "w32time" ] |
| 50 | +``` |
| 51 | + |
| 52 | +It will automatically be rendered during the runtime as this: |
| 53 | + |
| 54 | +``` |
| 55 | +icinga2, w32time |
| 56 | +``` |
0 commit comments