Custom Macro

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
puspharaj
Posts: 14
Joined: Tue May 15, 2012 2:17 am

Custom Macro

Post by puspharaj »

Hi There,

I've been using Nagios core for the past 4months and i have integrated sms alert for my servers. I need help regarding setting custom macro.

Here is the example of an common line on the command.cfg :
define command{
command_name alert-service-by-sms
command_line /usr/local/nagios/libexec/alert-by-sms 01234567890 "Nagios Service $NOTIFICATIONTYPE$ Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$"
}
define command{
command_name alert-host-by-sms
command_line /usr/local/nagios/libexec/alert-by-sms 01234567890 "Nagios Host $NOTIFICATIONTYPE$ Alert: $HOSTALIAS$ is $HOSTSTATE$"
}
I need to use the macro as $ HOSTNAME $ instead of $HOSTNAME$ is there any possible way to do that ?

Please note that i have space inbetween the macro

Please help me out since i'm in the middle of production server issue

thanks
Puspharaj
User avatar
jsmurphy
Posts: 989
Joined: Wed Aug 18, 2010 9:46 pm

Re: Custom Macro

Post by jsmurphy »

Ok I'll bite... why? Why do you need spaces in the hostname macro?

If you enter it as $ HOSTNAME $ it's obviously not going to translate the macro into a hostname because whitespace is a string delimiter, if you put the spaces in and single quotes it's only going to pass the literal string "$ HOSTNAME $" otherwise it's just going to pass "$" which basically means invalid macro.

Explain what you are trying to accomplish and we can probably come up with a better solution :D
puspharaj
Posts: 14
Joined: Tue May 15, 2012 2:17 am

Re: Custom Macro

Post by puspharaj »

jsmurphy,

Thanks a ton for your reply!!

Yes my sms vendor did a idiotic job over the macro , Its bit complicated to explain everything from the beginning,

Let me short the story

So here is my command line on the commands.cfg to send a sms to my mobile in india

command_line wget 'https://api.mVaayoo.com/mvaayooapi/Mess ... =0&msgtxt='$ NOTIFICATIONTYPE $ Server is $ HOSTNAME $ : $ SERVICEDESC $ is $ SERVICESTATE $ @ $ LONGDATETIME $&state=4'

The sms vendor have to follow some certain rules .They pushed me to make a space in between the macro, before i was getting sms without any issues( they were no spaces in the macro) now nagios cant read the macro at all its just simply throwing the NOTIFICATIONTYPE blah blah....

I knew that changing the macro nagios will get confused, Is there any way i can send the message by giving spaces in between the macro.

Please guide me how to do that at the same time i will try also

thanks
Puspharaj
agriffin
Posts: 876
Joined: Mon May 09, 2011 9:36 am

Re: Custom Macro

Post by agriffin »

I'm having a hard time thinking of any rule that would dictate the syntax of Nagios config files. They literally should not be able to tell the difference between there being spaces or not. You said it was working fine before, so unless I'm missing something, just get rid of the spaces again.
puspharaj
Posts: 14
Joined: Tue May 15, 2012 2:17 am

Re: Custom Macro

Post by puspharaj »

agriffin,

No i my sms vendor system not sending the message at all

If i give space in between the macro its deliver the $ NOTIFICATIONTYPE $ $ HOSTNAME $ blah blah.... to mobile phone

if dont give the space inbetween the macro its never delivers the sms to my phone

any type concatenation on variable would help in this or any script would help me out ?

let just say $HOSTNAME=" $ HOSTNAME $ " on a script and nagios can read that ?

thanks
Puspharaj
User avatar
jsmurphy
Posts: 989
Joined: Wed Aug 18, 2010 9:46 pm

Re: Custom Macro

Post by jsmurphy »

I think you've got a slight misunderstanding as to how the macros function and why it's failing.

The macro text ($HOSTNAME$) will never ever leave the internal nagios processing engine... it will never reach your SMS gateway provider, however if you enter $ HOSTNAME $ then nagios does not translate it and passes that text string to your provider. So suppose you were to get $ HOSTNAME $ to translate, it would work exactly the same as it does now without putting the spaces, that is to say: it won't work at all.

The issue is that whatever text your macros are translating into is causing your provider to be unable to deliver the message, are there any special characters in your hosts or services? Can you echo the command you are trying to execute to a file so you can see the exact message you are trying to send to them? Is it a managed service, are you able to work with them to work out why certain messages are not allowed to pass through their system?

The problem here is the message; we need to work out if it's a problem on your side or the provider side, it's not the macro translation that is the issue.
puspharaj
Posts: 14
Joined: Tue May 15, 2012 2:17 am

Re: Custom Macro

Post by puspharaj »

jsmurphy ,

I totally agree with you!! its nothing to do with the macro the problem is they system wont accept $HOSTNAME$ they are asking me to put space on the macro $ HOSTNAME $
but this will never work with nagios.

Is there any way i can translate the $ HOSTNAME $ to $HOSTNAME$ ? Or a any script will translate this

command_line wget 'https://api.mVaayoo.com/mvaayooapi/Mess ... =0&msgtxt='$ NOTIFICATIONTYPE $ Server is $ HOSTNAME $ : $ SERVICEDESC $ is $ SERVICESTATE $ @ $ LONGDATETIME $&state=4'

The above will just sending the sms to mobile phone as it is

example like this :
'$ NOTIFICATIONTYPE $ Server is $ HOSTNAME $ : $ SERVICEDESC $ is $ SERVICESTATE $ @ $ LONGDATETIME $

thanks
Puspharaj
agriffin
Posts: 876
Joined: Mon May 09, 2011 9:36 am

Re: Custom Macro

Post by agriffin »

The problem might be that the macros are not URL encoded, but you're inserting them into a URL. Try using curl instead of wget, which can perform the URL-encoding for you. For example:

Code: Select all

wget 'https://api.mVaayoo.com/mvaayooapi/[email protected]:password&senderID=PUSP&receipientno=7845175097&dcs=0&msgtxt='$ NOTIFICATIONTYPE $ Server is $ HOSTNAME $ : $ SERVICEDESC $ is $ SERVICESTATE $ @ $ LONGDATETIME $&state=4'
becomes

Code: Select all

curl -G https://api.mVaayoo.com/mvaayooapi/MessageCompose --data [email protected]:password --data senderID=PUSP --data receipientno=7845175097 --data dcs=0 --data-urlencode msgtxt='$NOTIFICATIONTYPE$ Server is $HOSTNAME$ : $SERVICEDESC$ is $SERVICESTATE$ @ $LONGDATETIME$' --data state=4
puspharaj
Posts: 14
Joined: Tue May 15, 2012 2:17 am

Re: Custom Macro

Post by puspharaj »

agriffin,

Thanks for your suggestion

Code: Select all

curl -G https://api.mVaayoo.com/mvaayooapi/MessageCompose --data [email protected]:password --data senderID=PUSP --data receipientno=7845175097 --data dcs=0 --data-urlencode msgtxt='$ NOTIFICATIONTYPE $ Server is $ HOSTNAME $ : $ SERVICEDESC $ is $ SERVICESTATE $ @ $ LONGDATETIME $' --data state=4
will it be possible above like this ?
agriffin
Posts: 876
Joined: Mon May 09, 2011 9:36 am

Re: Custom Macro

Post by agriffin »

No. Again, you have misunderstood the problem. Even if inserting spaces in the macro names worked like you expected (which would be extremely difficult to do, and would involve extensive modification of Nagios' code), SMS notifications would still not work for you. Please re-read jsmurphy's explanation above if you do not understand why. Rather than focusing on what you think the solution is, I am trying to focus on the actual problem of not being able to send notifications.

Please try using the command definition I posted above without modifications and let me know how it goes.

You've obviously been in contact with your SMS provider about this. If the above does not work, then it would help if you posted part of your exchange with them that included their advice and rationale for inserting spaces in the macro, as well as the "rules" you mentioned that must be followed. There was probably some miscommunication there that resulted in your insistence on putting spaces where they don't belong.
Locked