Injecting JavaScript by using F5 iRule


If you have the F5 server, you can use F5 iRules to automatically inject the TrueSight App Visibility Manager End User Monitoring JavaScript to the application pages. The iRule injects the JavaScript stub to html content-type pages that are requested from the F5 virtual server.

This section provides information about injecting the TrueSight App Visibility ManagerEnd User Monitoring JavaScript by using the  F5 irule and configuring the iRule.

Prerequisites

  • A stream profile must be enabled on your virtual server.
  • All your pages must have the <head> or <HEAD> tag. The JavaSript is not injected to pages that do not have the <head> or <HEAD> tag.
  • The request and response content must not be encrypted when the iRule accesses the content. The JavaScript is not injected to encrypted pages.
  • The content must be uncompressed, and the pages must not be chunked for the stream searches to work. If the content is compressed, do the following:
    1. Uncomment the commented code block at the end of the HTTP_REQUEST to remove the header from the request and to ensure that the server does not send a compressed response.
    2. In your HTTP profile, set the value of the Response Chunking parameter to Rechunk 

Inject JavaScript by using the F5 iRule

Do the following:

  1. If you have enabled JavaScript injection from the App Visibility Agent, disable it to prevent duplicate injection.

    Do the following to disable it.
    1. In the TrueSight console, go to Administration > App Visibility Agent Policies.
    2. Click the Policy menu of the Agent for which you want to disable the JavaScript injection.
    3. Click Save As to create a copy of the Medium policy, or the default policy in your system.

      app_visiblity_agent_policies.png

    4. On the Create App Visibility Agent Policy page, enter a name and description for the policy.
    5. In the text area, set the value for user.monitoring.enabled as false.

      app_visiblity_create_policies.png

    6. Click Save.
    7. On the App Visibility Agent Policies page, click the menu for the new policy, and click Set as Default.

      app_visiblity_set_default.png

    8. Click the menu for the new policy, and click Apply to Agents.

      app_visiblity_apply_agent.png

    9. On the Apply Policy to App Visibility Agents page, select all agents, and click Apply.

      app_visiblity_apply_default.png
  2. Create the iRule.

    Do the following.
    1. In the F5 Configuration Utility, go to Local Traffic > iRules > iRules List.
    2. Click Create.

      app_visiblity_create_irule.png

    3. Add a name to the iRule. For example, App_Visibility_RUM_JavaScript_Injection.

      app_visiblity_new_irule.png

    4. In the rule definition area, copy and paste the following content:

      iRule code

      when RULE_INIT {
          #Injection rules. Rule may be of type 'contains''ends_with''starts_with' or 'eq'.
          #Default is "starts_with /", to match all
          set PAGES_TO_INJECT_LIST [list \
              "starts_with /" \
          ];
      }
      when HTTP_REQUEST {

          if { [info exists should_inject] } {
              unset should_inject;
          }

          set page_uri [string tolower [HTTP::uri]]

          foreach {mapping} $::PAGES_TO_INJECT_LIST {
              set tokens [split $mapping " "]
              set op [lindex $tokens 0];
              set match [lindex $tokens 1];
              set found_match 0;

              set error [catch { set found_match [expr \$page_uri $op \$match]; }];

              if { $found_match } {
                  set should_inject 1;
                  break;
              }
          }

          #Uncomment this code block to prevent the server from sending a compressed respons by removing the compression header from the request
          #block starts
          #if {[info exists should_inject] && ([HTTP::header Content-Type] starts_with "text/html") } {
              #if { [HTTP::header exists "Accept-Encoding"] } {
                  #HTTP::header remove "Accept-Encoding";
              #}
          #}
          #block ends
      }

      when SERVER_CONNECTED {
         set SERVER_IP [IP::server_addr]
      }

      #Capture the request start time, will be used to calculate backend response time
      when HTTP_REQUEST_SEND {
          set REQUEST_SEND [clock clicks -milliseconds]
      }

      when HTTP_RESPONSE {
          set RESPONSE_TIME [expr {[clock clicks -milliseconds] - $REQUEST_SEND}]
       set SERVER_STATUS_CODE [HTTP::status]


          STREAM::disable;

          if {[info exists should_inject] && ([HTTP::header Content-Type] starts_with "text/html") } {

                  set bmc_eum_stub [subst {
                          <script type="text/javascript">
                              var BMCRum = {
                                      startTime: new Date().getTime(),
                                      ssd: $RESPONSE_TIME,
                                      sip: '$SERVER_IP', st: '$SERVER_STATUS_CODE',
                                      pxyInf: {
                                          "httpPort""<proxy-http-port>",
                                          "host""<proxy-host>",
                                          "httpsPort""<proxy-https-port>",
                                          "beaconsRes""/apm-proxy/rest/beacons",
                                      },
                                      isManual: true
                              };
                              </script>
                          <script type="text/javascript" src="https://<proxy-host>:<proxy-https-port>/static-resources/aeuem.min.js"></script>
                      }];

              set stream_expression "@<head>@<head>$bmc_eum_stub@ @<HEAD>@<HEAD>$bmc_eum_stub@";
              STREAM::expression $stream_expression

              STREAM::enable;
      }}

      when STREAM_MATCHED {
          STREAM::disable;
      }

  3. Apply the iRule to your virtual server.

    Do the following.
    1. In the F5 Configuration Utility, go to Local Traffic > Virtual Servers > Virtual Server List.
    2. Select the virtual server that you want to apply the iRule to.

      app_visiblity_created_irule.png

    3. Click the Resources tab.

      app_visiblity_irule_resources_tab.png

    4. In the iRules section, click Manage.

      app_visiblity_manage_irule.png
    5. Move the iRule from the Available list to the Enabled list.

      app_visiblity_irule_add_resource.png

    6. Click Finished.

Configure the iRule

Do the following to configure the iRule that you created:

  1. Configure the App Visibility host and ports as part of the the bmc_eum_stub variable. Set the following fields:
    <proxy-http-port>, <proxy-https-port>, <proxy-host>.
    If the App Visibility Proxy is hosted behind a load balancer, use the host and port of the load balancer entry for the proxy.
  2. (Optional) Configure the pages to which the iRule injects the JavaScript stub. You can use PAGES_TO_INJECT_LIST that is defined in the RULE_INIT section. By default, the iRule injects the JavaScript to all pages. Use the following tokens to define your rules:
    contains
    ends_withstarts_with, or eq

 

Tip: For faster searching, add an asterisk to the end of your partial query. Example: cert*