Iterate over numbers


we have a bunch of mounts on a bunch of servers. Mounts are numbered (/mnt/mount01 … /mnt/mount80).

I need to monitor them, but don’t want to define all the mounts by hand. I was hoping for a bash-like function like "for MOUNT in {00 … 80}; do “create service”; done. Or similar…

If you use icinga director you could run a script using icinga director api to create these services individually.
[REST API - Icinga Director]

If you are using icinga2 conf files you could use icinga2 api to create these objects individually
[Icinga2 Api - Icinga 2]

write a script → run on host → host knows mounts → host tells icinga what to monitor

or you write your own check that checks all mountpoints

I do not use the director, plain config files. Meanwhile, I tried to solve this by using ansible. And while it is fun to learn ansible and stuff, your idea to just have one check script that just checks everything is maybe easier.

You can do one of the following:

for (mount in range(10)) {
	object Service "mount-" + mount use (mount) {
		vars.mount = "/mnt/mount" + mount

Or alternatively:

apply Service "mount-" for (mount in range(10)) {
	vars.mount = "/mnt/mount" + mount
	assign where [...]

The latter makes for sense if you use the same apply rule for many hosts (you can also access host vars instead of hard-coding a number like 10 in the example.

1 Like

This looks like the ansible approach I’m working on. My issue here:

  • every server has an unknown number of NFS Servers
  • every server has an unknown number of mounts from each of that servers
  • every mountpoint has a format of 4 digits (0001 - 0020) - that means i need someting like format %4d
    unknown means they are not equal everywhere and could change over time. But they are known of course and I could put some “vars.nfsserver=server1”

How I would usually solve this:


vars.nfs-servers = ["server1","server2"]
vars.nfs-mount-min = 1
vars.nfs-mount-max = 20


apply Service "NFS Server Stale " for (nfs-server in host.vars.nfs-servers) {
/* another for loop for mountpoints per server
  for (mount in range(host.vars.nfs-mount-min,host.vars.nfs-mount.max)) {
    check_command = "nfs-stale"
    vars.nfs_path = "/mnt/"+nfs-server+mount
assign where vars.nfs-servers

So, the big question: how to put a loop into a loop?

Another approach would be to fix the NFS Servers and only iterate over the exports.


vars.nfs-server1_min = 1
vars.nfs-server1_max = 20


apply Service "NFS-SERVER1 NFS Stale " for (MOUNT in range(format("%04d",host.vars.nfs-server1_min),format("%04d",host.vars.nfs-server1_max))) {
   import "generic-service"
   command_endpoint = host.vars.agent_endpoint
   vars.grafana_graph_disable = true
   check_command = "nfs-stale"
   vars.nfs_path="/mnt/NFS-SERVER1/" + MOUNT

   assign where host.vars.nfs-servers

It worked - kind of. Issue is the leading zeros. Whatever I put in vars.nfs-server1_min, leading zeros get stripped off. But I need it in a format of 4 digits. Thats why I’m testing the format.