iRule stats formatter

Problem this snippet solves:

When you have a load of iRule stats in text format from your F5 device and need to get them into a nicer format.

The following Python 3 script takes in a text file in the following format:

------------------------------------------------------------------------------------------------------
Ltm::Rule Event: /web_testing/test_environment_rule:HTTP_RESPONSE
------------------------------------------------------------------------------------------------------
Priority                       12
Executions             
  Total                  31686860
  Failures                      0
  Aborts                        0
CPU Cycles on Executing
  Average                  404058
  Maximum                10703959
  Minimum                  264201

 (raw)                 
------------------------------------------------------------------------------------------------------
Ltm::Rule Event: /web_testing/test_environment_rule:HTTP_REQUEST
------------------------------------------------------------------------------------------------------
Priority                      899
Executions             
  Total                  31686860
  Failures                      0
  Aborts                        0
CPU Cycles on Executing
  Average                  404058
  Maximum                10703959
  Minimum                  264201

Put through the following python script to output a CSV file for further data manipulation.

How to use this snippet:

Python3 script, to use run the following (can also add in '--o' to define an output file, if not will replace the file extension '.txt' with '.csv' by default):

python statformating.py --i my_irule_stats.txt

output will be something like

Openning 'my_irule_stats.txt'
Saving output csv to 'my_irule_stats.csv'

Usage/help output:

usage: statformating.py [-h] [--i INPUT] [--o OUTPUT]

optional arguments:
  -h, --help  show this help message and exit
  --i INPUT   iRule Stats File input file name
  --o OUTPUT  iRule Stats File output csv file name

Code :

import re
import os
import argparse

def iruleStatsFormat(inputFile, outputFile):
    print('Openning \'{}\''.format(inputFile))
    iruleStats = open(inputFile, 'rt').read()

    iruleStats = re.sub(r'[ ]{2,}', ' ', iruleStats)
    iruleStats = re.sub(r'\n\s\(raw\)\s{1,}', '', iruleStats)
    iruleStats = re.sub(r'[-]{2,}\n', '', iruleStats)
    iruleStats = re.sub(r'\n ', r'\n', iruleStats)
    iruleStats = re.sub(r'CPU Cycles on Executing\n', '', iruleStats)
    iruleStats = re.sub(r'Executions \n', '', iruleStats)
    iruleStats = re.sub(r'\nPriority (\d{1,})\nTotal (\d{1,})\nFailures (\d{1,})\nAborts (\d{1,})\nAverage (\d{1,})\nMaximum (\d{1,})\nMinimum (\d{1,})', r'\t\1\t\2\t\3\t\4\t\5\t\6\t\7', iruleStats)
    iruleStats = re.sub(r'Ltm::Rule Event: /(.*?)/(.*?):(.*?\t)', r'\1\t\2\t\3', iruleStats)
    iruleStats = re.sub(r'Ltm::Rule Event: (.*?):(.*?\t)', r'Common\t\1\t\2', iruleStats)
    iruleStats = re.sub(r'\n{2,}', r'\n', iruleStats)
    iruleStats = re.sub(r'\t', r',', iruleStats)
    print('Saving output csv to \'{}\''.format(outputFile))
    with open(outputFile, 'wt') as f:
        print(iruleStats, file=f)

if __name__=='__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("--i", dest='input', help="iRule Stats File input file name", type=str)
    parser.add_argument("--o", dest='output', help="iRule Stats File output csv file name", type=str, default="")
    args = parser.parse_args()

    if args.input and os.path.isfile(args.input):
        if not args.output:
            args.output = args.input[:-3] + 'csv'
        iruleStatsFormat(args.input, args.output)
    else:
        parser.print_help()
Updated Jun 06, 2023
Version 2.0

Was this article helpful?

No CommentsBe the first to comment