How to retrive Macros Value of a host from nagios core?

169 views Asked by At

I have a Nagios-core server for monitoring my network and I want to configure an alert via sms but the problem is the sms gateway is on another host,
my idea is to retrieve the macro's value of the hosts and send it to the sms gateway.

Not: The sms gateway used is Gammu.

I create a command-line on the Nagios server but it doesn't work :

the command line contains a full path of the script notify_BY-SMS.sh

define command{
        command_name    notify-host-by-sms
        command_line    /usr/local/nagios/libexec/notify_BY-SMS.sh

        }

the script notify_BY-SMS.sh is described below:

#!/bin/bash

I try to retrieve the $HOSTNAME$ $HOSTSTATE$ Macros from Nagios and set it on a variable named $message :

message=$(/usr/bin/printf "%b" "Alert $HOSTNAME$ is $HOSTSTATE$")

retrieve the $CONTACTPAGER$ macros and set it on a variable named $contact :

contact=$(/usr/bin/printf "%b" $CONTACTPAGER$)

connect to MySQL users remotely centreon :

mysql -h 192.X.Y.Z -u centreon -D smsd --password=<PASSWORD>

and I try to insert the variable sets INTO the Gammu outbox table :

-e "INSERT INTO outbox (\`CreatorID\`, \`SenderID\`, \`DeliveryReport\`, \`MultiPart\`, \`InsertIntoDB\`, \`Text\`, \`DestinationNumber\`, \`RelativeValidity\`, \`Coding\`, \`UDH\`, \`Class\`, \`TextDecoded\`) VALUES ('Gammu 1.41.0', '2', 'default', 'FALSE', NOW(), '', '$contact', 255, 'Default_No_Compression', '', -1, '$message');"
1

There are 1 answers

0
Mark Clayton On

You are using nagios variables in your bash script. You need to use bash variables: $1, $2, $3, etc.

If you have something like: command_line /usr/local/nagios/libexec/notify_BY-SMS.sh $HOSTNAME$ $HOSTSTATE$

Then in your bash script do something like: message=$(/usr/bin/printf "%b" "Alert $1 is $2")

Refer to this question for a deep explanation:

Nagios not resolving macros in email alert