NagiosEnterprises / nagioscore

Nagios Core
GNU General Public License v2.0
1.56k stars 448 forks source link

a custom variable macro should be expanded to an empty string if the reference variable does not exist #983

Closed klaernie closed 3 weeks ago

klaernie commented 3 months ago

We have quite a few custom properties in our checks, which makes it really useful to write declarative checks like this:

define server {
    use fs-check
    host_name foo
    description fs /mnt/test
    _fs /mnt/test
    _crit 10
    _warn 15
}

But I've noticed a small problem for us: when I now include $_SERVICEFS$ in my perfdata template (which is global for all checks) the expanded value becomes $_SERVICEFS$ as well, which means in the following infrastructure processing the performance data I need to actively ignore this value, while an empty value would be ignored automatically. However the expanded string now also contains a literal $_SERVICEFS which is a valid shell variable.

For many of our checks I've worked around this by setting the custom variables to an empty in one of the templates early in the chain, but I do not want to add a custom variable to service definitions which have no point in having that variable (think a URL variable in a filesystem check and vice versa). This is further complicated by Thruk displaying a custom variable if it is defined, no matter if they have an empty string as the value or an actual value like /mnt/test above.

I'd much rather prefer, if nagios would expand custom variables to an empty string, instead of keeping the unexpanded text. This should be fairly easy, since nagios already tries to match the text following _HOST and _SERVICE to the custom variables, so it is aware that the macro currently being expanded is for a custom variable (grab_custom_object_macro_r() in macros.c), so if a variable of that name would be present it would be replaced, but without the value present we fall back to the behaviour that all macros share - not replacing the macro at all.

We btw currently run this in production:


diff -U 5 -r nagios-4.5.3-orig/common/macros.c nagios-4.5.3/common/macros.c
--- nagios-4.5.3-orig/common/macros.c   2024-06-11 16:50:20.000000000 +0200
+++ nagios-4.5.3/common/macros.c        2024-08-05 16:09:18.290398388 +0200
@@ -2482,10 +2482,16 @@
                        result = OK;
                        break;
                        }
                }

+       /* expand nonexistant custom variables as an empty string */
+       if( result == ERROR ){
+               *output = "";
+               result = OK;
+               }
+
        return result;
        }

 int grab_custom_object_macro(char *macro_name, customvariablesmember *vars, char **output) {
        return grab_custom_object_macro_r(&global_macros, macro_name, vars, output);```
ericloyd commented 3 months ago

This is such a wonderful idea! Yes, yes yes! Please make this happen!

sawolf commented 2 months ago

@dylan-at-nagios @nagiosgwesterman @aaronagios I also think this is reasonable, please feel free to add it