Learn F5 Technologies, Get Answers & Share Community Solutions Join DevCentral

Filter by:
  • Solution
  • Technology
Answers

Timestamp in email

Hi f5 team,

Can you help us with proposal how can we add timestamp in the body of the mail when configure /config/user_alert.conf?

0
Rate this Question

Answers to this Question

placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

Hello Preslav,

You can use the shell command "date" to get the local time before sending the mail.

example :

timestamp=`date`

Then you can use the $timestamp variable

It will display something like that by default :

Wed Aug 16 10:50:50 CEST 2017

You can also customize the date as you want using arguments with command "date"

Regards

0
Comments on this Answer
Comment made 16-Aug-2017 by preslav.ilevski 283

Thank you JTI,

Do you mean to add:

 timestamp=`date` and $timestamp 

in the body in /config/user_alert.conf?

If I understood correctly that must be the configuration:

alert  BIGIP_MCPD_MCPDERR_NODE_ADDRESS_MON_STATUS_DOWN { 
    snmptrap OID=".1.3.6.1.4.1.3375.2.4.0.12"; 
    email toaddress="example@domain.com" 
    fromaddress="root" 
    timestamp=`date`
    body="On $timestamp. Warning the node is down. Recommended action - Check the node and the cable connection."

}

alert  BIGIP_MCPD_MCPDERR_NODE_ADDRESS_MON_STATUS_UP { 
    snmptrap OID=".1.3.6.1.4.1.3375.2.4.0.13"; 
    email toaddress="example@domain.com" 
    fromaddress="root" 
    timestamp=`date`
    body="On $timestamp. The node is up. Recommended action - Information, no action required"

}

Regards

0
Comment made 16-Aug-2017 by Jad Tabbara (JTI) 2361

No you will need to run this in an external script.

https://support.f5.com/csp/article/K14397#tmsh

In your user_alert.conf you will only call this script.

I don't think it is possible to use variables within the user_alert.conf.

Regards

0
Comment made 16-Aug-2017 by preslav.ilevski 283

Thanks JTI,

Sorry if my questions seem to be elementary.

I understand that I can execute date command and to put the output in variable like you wrote before. The problem is how to put the output from date command(or variable's value) in the mail body.

In the link you post I see that I can add a row to the user_alert.conf like that:

exec command="/path/to/command_or_customscript"

And as I understood must create /path/to/command_or_customscript in which to put something like that:

timestamp=`date`
print timestamp

And if I understood correctly where to put "exec command" row in the user_alert.conf file?
Please see the this example:

alert  BIGIP_MCPD_MCPDERR_NODE_ADDRESS_MON_STATUS_DOWN { 
    snmptrap OID=".1.3.6.1.4.1.3375.2.4.0.12"; 
    email toaddress="user@example.com" 
    fromaddress="root" 
    body="Warning the node is down. Recommended action - Check the node and the cable       connection."
    exec command="/path/to/command_or_customscript"
    }

Is this correct? Do I need to do something else?

Regards

0
Comment made 16-Aug-2017 by Jad Tabbara (JTI) 2361

Sorry I thought that it is clear.

First you will only need to do that in the user_alert.conf file

alert  BIGIP_MCPD_MCPDERR_NODE_ADDRESS_MON_STATUS_DOWN { 
    exec command="/path/to/command_or_customscript"
    }

Then you will need to create a custom script that will send email notification. This custom script will look like :

#!/bin/bash
timestamp=`date`
echo "On $timestamp Warning the node is down. Recommended action - Check the node and the cable connection." | mail -s "a subject" user@example.com
0
Comment made 17-Aug-2017 by preslav.ilevski 283

Hello JTI,

Thank you for the explanation.

I completed the following steps:

  1. Change the user_alert.conf file to look like that:

    alert  BIGIP_MCPD_MCPDERR_NODE_ADDRESS_MON_STATUS_DOWN { 
        exec command="~/bin/email_notif.sh"
    }
    
  2. Create a file ~/bin/email_notif.sh ->

    #!/bin/bash
    #This is used for sending mail about changes of node's status,
    #and is called from file /config/user_alert.conf
    
    timestamp=`date`
    echo "Date -  $timestamp. Warning the node is down. Recommended action - Check the node and                             the cable connection." | mail -s "Node is down" user@example.com
    

And when i try to execute the file email_notif.sh ->

    # ~/bin/email_notif.sh 

I receive the mail - all is ok. But when I put a node in DOWN state - nothing happens. I think that the problem is related to permissions and changed them to

    user_alert.conf -> -rw-r--r--.
   and 
    email_notif.sh -> -rwxrwxrwx.

but this didn't resolve the issue. After that I tried to add row in the user_alert.conf file with OID for node down ->

    snmptrap OID=".1.3.6.1.4.1.3375.2.4.0.12";

but failed again. May be I missed something. Do you have an idea how to proceed?

Regards,

0
Comment made 17-Aug-2017 by preslav.ilevski 283

... I just want to share some changes for the config:

The user_alert.conf file is below:

    alert BIGIP_MCPD_MCPDERR_NODE_ADDRESS_MON_STATUS "Node /Common/Node_name address x.x.x.x monitor status down" {
    exec command="~/bin/email_notif.sh";
    }

And after that when I peruse /var/log/ltm I saw this error:

err alertd[23217]: 01100014:3: Action ~/bin/email_notif.sh is failed.

But when I tried to execute the

    ~/bin/email_notif.sh

I receive the mail notif without any problems. I can not see the reason for this error.

Any suggestions?

Regards

0
Comment made 17-Aug-2017 by Jad Tabbara (JTI) 2361

I've done it on my lab and it works fine.

Try to modify the "~" by the full path.

Go to "email_notify.sh" folder using "cd" then type the command "pwd" then copy that path to your user_alertd.sh

regards

0
Comment made 18-Aug-2017 by preslav.ilevski 283

Thanks JTI,

Obviously the problem is in my config. I have tried several times but it failed. I will continue today with a chance to succeed, cause everything from your explanation is clear.

Thank you again for your time and effort...

Regards

0
Comment made 31-Aug-2017 by preslav.ilevski 283

Hello JTI/all,

I have opened Service Request in f5 about the case, and I have reply.

May be it will be interesting for somebody of you.

The problem is with version 12.1.2, and it is related to changes about SELinux for "mail" command. So the custom script must changed to use "sendmail" instead of "mail", and to look like that:

#!/bin/bash
#This is used for sending mail about changes of node's status,
#and is called from file /config/user_alert.conf

timestamp=`date`
echo "Date -  $timestamp. Warning the node is down. Recommended action - Check the node and the cable connection." | /usr/sbin/sendmail -s "Node is down"  user@example.com

Regards,

Preslav

1
Comment made 31-Aug-2017 by Jad Tabbara (JTI) 2361

Thanks for the update !

0