This installment of iC2I will show you how to dump an iRule - including statistic information and the actual rule - to the command line. Our purpose in building this class is the same as the last few articles in the iC2I series - to show you how to get your hands on this information to use however your application sees fit.

There's not a lot to an iRule, actually, and since we're going to show what iRules a Virtual uses in the Virtual we wrote up earlier in the i2CI series, we did not write a routine here to list what other objects rely upon this iRule. That excercise was covered in a previous installment.

With that said, here is the code:

 

package iC2i;

import iControl.LocalLBRuleBindingStub;
import iControl.LocalLBRuleLocator;
import iControl.LocalLBRuleRuleDefinition;
import iControl.LocalLBRuleRuleStatistics;
import iControl.LocalLBRuleRuleStatisticEntry;
import iControl.CommonStatistic;
import iControlIntermediary.UsefulU64;
import iControl.CommonStatisticType;

public class BigIpRule extends BigIpBase {

    private LocalLBRuleBindingStub stub = null;
   
    public BigIpRule(String username, String password, String addressOrName) {
        super(username, password, addressOrName);
        try {
            connect();
        } catch(Exception e) {
            System.out.println("Error connecting to Rule Interface: " + e.getLocalizedMessage());
        }
    }
   
    private void connect() throws Exception {
        stub = (LocalLBRuleBindingStub) new LocalLBRuleLocator().getLocalLBRulePort(new java.net.URL(fullUrl));
        stub.setTimeout(6000);       
    }


    public void dumpRule(String ruleName) throws Exception {
        // iControl expects an array of names, so give it one - with our rule name as the only element.
        String ruleNames[] = {ruleName};
       
        // Get the rule Definition from iControl.
        LocalLBRuleRuleDefinition defs[] = stub.query_rule(ruleNames);
       
        // Print the "core" information - the rule name as returne and the actual text of the rule.
        System.out.println("RULE: " + defs[0].getRule_name());
        System.out.println("Defined as: ");
        System.out.println(defs[0].getRule_definition());
       
        // Get the statistics for our rule name.
        LocalLBRuleRuleStatistics rs = stub.get_statistics(ruleNames);
        // Print the date that statistics were last updated.
        System.out.println("Statistics as of" );
        System.out.print(rs.getTime_stamp().getHour() + ":" + rs.getTime_stamp().getMinute() + ":" + rs.getTime_stamp().getSecond());
        System.out.println(" on " + rs.getTime_stamp().getMonth() + "/" + rs.getTime_stamp().getDay() + "/" + rs.getTime_stamp().getYear());
       
        // Traverse the statistics tree and print out relevant information.
        LocalLBRuleRuleStatisticEntry rse[] = rs.getStatistics();
        for(int i = 0; i < rse.length; i++) {
            // The rule name for a sanity check, the event this statistic is for, and the rule's priority
            System.out.println("For Rule: " + rse[i].getRule_name() + " Event: " + rse[i].getEvent_name() + " of priority " + rse[i].getPriority());
            CommonStatistic cStat[] = rse[i].getStatistics();
            for(int j = 0; j < cStat.length; j++ ) {
                // Now print out the Statistic name and the value it currently holds. Use UsefulU64 to generate
                // numbers that are in terms of Kilobytes and Megabytes because some statistics are rather large.
                CommonStatisticType type = cStat[j].getType();
                UsefulU64 value = new UsefulU64(cStat[j].getValue());
                System.out.println("\t" + type.toString() + "\t" + value.toString());
            }
        }
    }
}

 

 

There really isn't too much tricky about this - getRule_definition() returns the actual text of the rule, and it can be a bit confusing that you have to call the get_statistics() interface of iControl, then call the getStatistics() method of the return value to get to useful statistics for a given rule, but not too confusing if you check the wiki information on get_statistics()  Or you can "just trust me" that this is how it works. I'd check the wiki if I were you.

We use the Useful64 class we defined in previous i2CI articles to translate the statistics data into data we can print. Part of that class converts large numbers into kilobyte and megabyte values, making the output more readable.

Other than that, there's nothing to the class, and you can just call the dumprule() routine directly from the main of any of the previous projects.

But having statistic data about your rules can be useful. While testing this class we discovered that one rule was not being accessed for some events, and we needed to get it straightened out. Good stuff in my opinion. Sure it's only a box we use for testing, but that's real data going through it, so the rules needed to be accessed correctly.