-1

It looks like some macros have been dropped between Icinga1 and Icinga2?

For example, I can only add "HOSTDURATIONSEC" = "$host.duration_sec$", to my template to then use it in the mail-host-notification script like: State: $HOSTSTATE for $HOSTDURATIONSEC which tells me how long the host has been in that state. But it only gives me seconds and that's not very helpful at a glance when receiving alerts.

Because HOSTDURATION is no longer available or not yet implemented, I've tried converting the seconds to something more readable using functions and other bash attempts within that mail-host-notification but it doesn't seem to work.

I'd like to be able to create a new variable in that script maybe using a command from a linux package timetrans like this:

#!/bin/sh
SECS=$HOSTDURATIONSEC
CONVERTSECONDS=`timetrans -c $SECS`
eval $CONVERTSECONDS
$HRTIME=`eval $CONVERTSECONDS`

template=`cat <<TEMPLATE
Date/Time: $SHORTDATETIME (EST)

Notification Type: $NOTIFICATIONTYPE

Host: $HOSTALIAS
Address: $HOSTADDRESS
State: $HOSTSTATE for $HRTIME

------------------------------------------
New Jersey DC Icinga Monitoring

Additional Info: $HOSTOUTPUT

Notes on this device:
$HOSTNOTES

Comments: $NOTIFICATIONAUTHORNAME $NOTIFICATIONCOMMENT
TEMPLATE
`

...but like the attempts I've tried using functions to convert the seconds variable $HOSTDURATIONSEC, it's not working. I'm tried in many ways, but it seems to me that Icinga isn't passing those env's set in the template.

  env = {
    "NOTIFICATIONTYPE" = "$notification.type$"
    "HOSTALIAS" = "$host.display_name$",
    "HOSTADDRESS" = "$address$",
    "HOSTSTATE" = "$host.state$",
    "HOSTNOTES" = "$host.notes$",
    "HOSTDURATIONSEC" = "$host.duration_sec$",
    "LONGDATETIME" = "$icinga.long_date_time$",
    "SHORTDATETIME" = "$icinga.short_date_time$",
    "HOSTOUTPUT" = "$host.output$",
    "NOTIFICATIONAUTHORNAME" = "$notification.author$",
    "NOTIFICATIONCOMMENT" = "$notification.comment$",
    "HOSTDISPLAYNAME" = "$host.display_name$",
    "USEREMAIL" = "$user.email$"
  }

Of course, anything I added to this list like "SHORTDATETIME" = "$icinga.short_date_time$",, "HOSTNOTES" = "$host.notes$",, etc. are working, but I'm having trouble trying to run linux commands against those variables in the script. Am I missing something? Anyone know where I'm going wrong? Any/All help is appreciated.

Keith
  • 4,627
  • 14
  • 25
TryTryAgain
  • 1,112
  • 4
  • 22
  • 40
  • Care to explain the downvote? If my logic is flawed. I'd appreciate fair reasons and consideration. That doesn't help anyone. – TryTryAgain Oct 17 '15 at 05:10
  • By philosophy and design voting happens unattributed and **neither voting up nor voting down requires any mandatory additional explanation**. Without a comment to the contrary one or more *possible* reasons for a down vote can be found in the tooltip that appears when your mouse pointer hoovers over the down button: *"this question does not show any research effort; it is unclear or not useful"*. That may or may not reflect the actual & private reason of whoever did cast a vote. – HBruijn Oct 20 '15 at 08:46

1 Answers1

1

I'd leave such calculation tricks inside the notification scripts. Since you are not working with Icinga 2 internals, it doesn't makes sense to fiddle with a function.

What I noticed in your script is:

$HRTIME=`eval $CONVERTSECONDS`

Corrent would be:

HRTIME=`eval $CONVERTSECONDS`

Btw. if you are using timetrans by the package dnssec-tools, it is in /usr/sbin, so not in path of the icinga/nagios user.

Modified working example:

#!/bin/sh
SECS=$HOSTDURATIONSEC
TIMEREADABLE=`/usr/sbin/timetrans -c $SECS`

template=`cat <<TEMPLATE
Date/Time: $SHORTDATETIME (EST)

Notification Type: $NOTIFICATIONTYPE

Host: $HOSTALIAS
Address: $HOSTADDRESS
State: $HOSTSTATE for $TIMEREADABLE

------------------------------------------
New Jersey DC Icinga Monitoring

Additional Info: $HOSTOUTPUT

Notes on this device:
$HOSTNOTES

Comments: $NOTIFICATIONAUTHORNAME $NOTIFICATIONCOMMENT
TEMPLATE
`
/usr/bin/printf "%b" "$template"

To be called via:

HOSTDURATIONSEC=123 sh test.sh

P.S.(1) If you are missing a macro in Icinga 2, open an issue to suggest it

P.S.(2) Don't call other users a coward, that is not nice!

lazyfrosch
  • 790
  • 4
  • 10