Page 1 of 1

create own user macro depending on $HOSTADDRESS$

Posted: Wed Aug 29, 2018 1:32 am
by junkertf
Hello,


I thinking on that can i create an own user macro for MGMT address for hosts depending on $HOSTADDRESS$ with some regexp?

Actually i just need to change the address 5th and 6th character to another than the original, but want spare some manual handwork...
(regexp replace?)

HOSTADDRESS=xxx.10.yyy.aa
MGMTADDR=xxx.20.yyy.aa

Is it that possible?


Thanks, Best regards,

Ferenc

Re: create own user macro depending on $HOSTADDRESS$

Posted: Wed Aug 29, 2018 9:50 am
by bheden
Depends on what you want to do with the macro, really. Is this for something outside of Core or XI? Cause you can always just pass $HOSTADDRESS$ to a script and perform the regex there and do whatever you wanted with it.

But if you want to really make your own macro, here are the steps. I'm using the master branch of nagioscore (https://www.github.com/NagiosEnterprises/nagioscore/):

1. Think of a name for it. We'll call ours $HOSTADDRESSADJUSTED$
2. We need to add it to common/macros.c and include/macros.h

3. Open include/macros.h and add our definition to the end of the macro list (line 226):

Code: Select all

#define MACRO_HOSTADDRESSADJUSTED 163
4. Increment the definition for MACRO_X_COUNT:

Change this line:

Code: Select all

#define MACRO_X_COUNT				163	/* size of macro_x[] array */
To this:

Code: Select all

#define MACRO_X_COUNT				164	/* size of macro_x[] array */
5. Open common/macros.c and look for the function add_macrox_name() (line 2673). Add our macro to the bottom of the initialization list. (line 2845).

Code: Select all

add_macrox_name(HOSTADDRESSADJUSTED);
6. Find the grab_macrox_value_r() function (line 658). Add our macro to the switch statement, directly after MACRO_HOSTNOTIFICATIONPERIOD (line 757).

So change these lines:

Code: Select all

		case MACRO_HOSTIMPORTANCE:
		case MACRO_HOSTANDSERVICESIMPORTANCE:
		case MACRO_HOSTNOTIFICATIONENABLED:
		case MACRO_HOSTNOTIFICATIONPERIOD:

			/* a standard host macro */
			if(arg2 == NULL) {
To this:

Code: Select all

		case MACRO_HOSTIMPORTANCE:
		case MACRO_HOSTANDSERVICESIMPORTANCE:
		case MACRO_HOSTNOTIFICATIONENABLED:
		case MACRO_HOSTNOTIFICATIONPERIOD:
		case MACRO_HOSTADDRESSADJUSTED:

			/* a standard host macro */
			if(arg2 == NULL) {
7. Now find the function grab_standard_host_macro_r() (if you've followed the above steps, this should be line 1603 now). This is where our magic will happen. To keep it easy, instead of adding to the bottom of the switch statement, we're going to add it right after the current HOSTADDRESS (breaks on line 1643).

So change these lines:

Code: Select all

		case MACRO_HOSTALIAS:
			*output = temp_host->alias;
			break;
		case MACRO_HOSTADDRESS:
			*output = temp_host->address;
			break;
#ifdef NSCORE
To this (IT HAS TO BE INSIDE THE #ifdef NSCORE BLOCK!!!):

Code: Select all

		case MACRO_HOSTALIAS:
			*output = temp_host->alias;
			break;
		case MACRO_HOSTADDRESS:
			*output = temp_host->address;
			break;
#ifdef NSCORE
		case MACRO_HOSTADDRESSADJUSTED:
			buf1 = temp_host->address;

			/* your code goes here */

			*output = mkstr("%s", buf1);
			free(buf1);
			break;
8. Recompile nagioscore (./configure && make all && make install) (https://assets.nagios.com/downloads/nag ... Source.pdf)

9. Enjoy your new macro :)

Re: create own user macro depending on $HOSTADDRESS$

Posted: Thu Aug 30, 2018 12:46 am
by junkertf
Actually i want use the macroed (or regex modified) $HOSTADRESS$ as an $ARG1$ in a service on the $HOSTADDRESS$ host.
I want give status information about the host management interface (hostaddress is xxx.10.aaa.zzz and management hostarddress is xxx.20.aaa.zzz)

If possible with regex i would be happy with that, without any recompile, but dont know how to make it...
Is there any manual for that to read it?

Thank you, best regards,

Ferenc

Re: create own user macro depending on $HOSTADDRESS$

Posted: Thu Aug 30, 2018 8:29 am
by mcapra
If all you care about is the status output, you could rig up a wrapper script to provide that information with some simple regex substitution or string operations in most languages' standard libraries (Python, PHP, Ruby, Perl all come to mind). Or if you're working with a specific plugin, that plugin could be modified to do some operations with $HOSTADDRESS$ or $ARG1$.

I don't know of any simple "step by step" tasks to do exactly what you want (beyond those so lovingly crafted by @bheden), but Google searches for your preferred language and the term "string manipulation" should be a good starting point.

Re: create own user macro depending on $HOSTADDRESS$

Posted: Thu Aug 30, 2018 10:07 am
by lmiltchev
junkertf, let us know if you have any more questions.

Re: create own user macro depending on $HOSTADDRESS$

Posted: Thu Aug 30, 2018 12:03 pm
by junkertf
:oops:

:facepalm: Thank you :facepalm:


solved, can be closed :)

Best regards,

Ferenc