Forum Discussion

4 Replies

  • Hello,

    I think there is a bit of interpretation missing from this iRule content, and you should be going through a method overload problem.

    See this line:

    call thisiRule::sendEmail $errorSubject $errorBody

    "thisiRule" means the iRule name what you are creating.

    and in this other line:

    catch {call procLibrary::sendEmail $subject $body "email1@myorg.org email2@myorg.org"}

    Indicates that the overload method sendEmail, has to be in another irule e.g. "procLibrary"

    So, try creating the methods in different libraries (iRules), e.g.:

    "thisiRule" content:

    
     this Irule sends an email to the F5 admin via the mailserver.myorg.org mailserver. This iRule calls the procedure in the procLibrary iRule.
    
    when HTTP_RESPONSE {
        set error 0
        if {$error} {
            set errorSubject "F5 Email Notification."
            set errorBody "There was an event that happened. Check your F5 local log!"
            call thisiRule::sendEmail $errorSubject $errorBody
        }
        else {
            Do something cool
        }
    }   
    proc sendEmail {sourceSubject sourceBody} {
            set currentTime [clock format [clock seconds] -format {%H:%M:%S}]
            set todaysDate [clock format [clock seconds] -format {%m-%d:%Y}]
            set subject ${sourceSubject}
            set body "${sourceBody}\r\n\r\n ${currentTime}hrs on ${todaysDate}.\r\n\r\n This is an automated email sent via the this iRule iRule.\r\n\r\nSincerely,\r\nYour F5 Support Team"
            log local0. $body
            catch {call procLibrary::sendEmail $subject $body "email1@myorg.org email2@myorg.org"}
    }
    

    "procLibrary" irule content:

    
     This procedure library procLibrary sends an email using a sideband connection to your organizations email server.
     It has 3 input parameters; subject, body and additional email addresses. The calling procedure needs to seperate the additional 
       emails with a " ". Example "email1@myorg.org email2@myorg.org".
    
    proc sendEmail {sourceSubject sourceEmailBody sourceAddEmailAddress} {
        upvar 1 $sourceSubject n1
        upvar 1 $sourceEmailBody n2
        upvar 1 $sourceAddEmailAddress n3
        set email_debug 0
        set hostName [info hostname]
        set mailFrom "root@$hostname"
        set mailTo "admin@myorg.org"
        set mailServer "xxx.xxx.xxx.xxx:25"
        set subject $sourceSubject
        set emailBody $sourceEmailBody
        set addEmailAddress ""
        if {$sourceAddEmailAddress ne ""} {
            append sourceAddEmailAddress "${mailTo}"
            foreach emailAddress $sourceAddEmailAddress {
                set emailConn [connect -timeout 2000 -idle 10 -status conn_status $mailServer]
                if {$email_debug} {log local0. "connect_status: $conn_status"}
                set emailData "HELO mailserver.myorg.org\r\nMAIL FROM: $mailFrom\r\nRCPT To: ${emailAddress}\r\nDATA\r\nSUBJECT: \r\n\r\n\r\n$emailBody\r\n.\r\n"
                set send_info [send -timeout 2000 -status send_status $emailConn $emailData]
                if {$email_debug} {log local0. "emailData: $emailData"}
                if {$email_debug} {log local0. "send_status: $send_status"}
                set recv_data [recv -timeout 2000 -status recv_status $emailConn]
                if {$email_debug} {log local0. "recv_Data: $recv_data"}
                close $emailConn
                if {$email_debug} {log local0. "EmailTo: $emailAddress"}
            }
        }
        else {
            if {$email_debug} {log local0. "connect_status: $conn_status"}
            set emailData "HELO mailserver.myorg.org\r\nMAIL FROM: $mailFrom\r\nRCPT To: ${emailAddress}\r\nDATA\r\nSUBJECT: \r\n\r\n\r\n$emailBody\r\n.\r\n"
            set send_info [send -timeout 2000 -status send_status $emailConn $emailData]
            if {$email_debug} {log local0. "emailData: $emailData"}
            if {$email_debug} {log local0. "send_status: $send_status"}
            set recv_data [recv -timeout 2000 -status recv_status $emailConn]
            if {$email_debug} {log local0. "recv_Data: $recv_data"}
            close $emailConn
        }   
    }
    

    Particulary, I prefer to rewrite all this content to one iRule, if use it to simple thing.

    I hope it helps you.
    • jomedusa's avatar
      jomedusa
      Icon for Altostratus rankAltostratus

      I am trying to do something similar and have created 2 irules, I can see log entries that the "event" is occuring, but I never see that the second irule is called. I am going to attempt to combine into a single one. Has anyone had success using the above?

      • cjunior's avatar
        cjunior
        Icon for Nacreous rankNacreous

        Hello,

        I'm not sure issues you are facing, so did you write the rule library name in front of procedure on the call statement?

        e.g.

        call procLibrary::sendEmail

        Please, check if this short example makes more sense to you:

        # Some virtual server
        ltm virtual vs_devcentral {
            destination 172.30.30.101:http
            ip-protocol tcp
            mask 255.255.255.255
            profiles {
                http { }
                tcp { }
            }
            rules {
                irule_devcentral
            }
            source 0.0.0.0/0
        }
        # iRule with events
        ltm rule irule_devcentral {
            when HTTP_REQUEST {
                   log local0. "This is from irule_devcentral"
                   call irule_lib_dc::externalProcedure()
            }
        }
         
        # Library with procedures
        ltm rule irule_lib_dc {
            proc externalProcedure() {
                log local0. "This is from externalProcedure() into irule_lib_dc"
                HTTP::respond 200 content "<html><head></head><body>Voila!</body></html>" Connection close
            }
        }

        The test:

        [root@bigipdelta:Active:Standalone] tmp # curl -v http://172.30.30.101
        * Rebuilt URL to: http://172.30.30.101/
        *   Trying 172.30.30.101...
        * Connected to 172.30.30.101 (172.30.30.101) port 80 (#0)
        > GET / HTTP/1.1
        > Host: 172.30.30.101
        > User-Agent: curl/7.47.1
        > Accept: */*
        >
        * HTTP 1.0, assume close after body
        < HTTP/1.0 200 OK
        < Server: BigIP
        < Connection: close
        < Content-Length: 45
        <
        * Closing connection 0
        <html><head></head><body>Voila!</body></html>
         

        Log:

        [root@bigipdelta:Active:Standalone] tmp # tail /var/log/ltm
        Jan 15 14:38:13 bigipdelta info tmm[11153]: Rule /Common/irule_devcentral <HTTP_REQUEST>: This is from irule_devcentral
        Jan 15 14:38:13 bigipdelta info tmm[11153]: Rule /Common/irule_devcentral <HTTP_REQUEST>: This is from externalProcedure() into irule_lib_dc

        Regards.

    • IbbyVK's avatar
      IbbyVK
      Icon for Nimbostratus rankNimbostratus

      I am trying to make the simplest imaginable iRule that sends an email when a VIP gets hit. I do not want to use multiple iRules. How do I write one like that?