If you have been following the iRules Tech Tips here on DevCentral lately, you’ll see more and more use of the Session table for data storage and retrieval. Colin recently put up a few articles around building Heatmaps with iRules. In those examples, he uses the session table to store all his geolocation data for later reporting.
The Session table is a great and wonderful thing, but it does have one weakness – it resides in memory and is not persistent across server restarts. If you have a HA-pair of servers, the session data will be replicated across them, but there will be those edge cases where you need to take them both down and will lose anything you have stored in the session table. You may also want to analyze that data in an external program.
There needs to be some way of archiving that data off of the BIG-IP!
Well, now there is a way – thanks to iRules. I’m going to limit this article to exporting “subtable” data as most of the examples we are seeing now-a-days are focused on using subtables to segment the data within the session table. Also, it doesn’t hurt that there is a nifty “table keys” command to return all the entries in a specified subtable.
The solution is actually quite simple and will qualify for Colin’s 20-Lines-Or-Less series.
The iRule looks for a request on the virtual server to the URL “http://hostname/exporttable/tablename” The sections of the URI is split apart and the tablename portion is removed with the getfield command.
At this point, I call the “table keys ” sub-command to get a list of all the keys in that sub-table. A variable is created to store the resulting output. In this example, I opted to go with a simple Comma Separated Values (CSV) format but it would be trivial to convert this into XML or any other format you care to use. The list of keys is then iterated through with the “table lookup” sub-command and the resulting record is appended to the output. A unique file name is created with the TCL “clock” command to include the date and time along with the requested table name.
Finally, the output is returned to the client with the correct Mime type of “text/csv” as well as a Content-Disposition header to tell the browser the file name as well as indicating that it should attempt to save it to disk.
Fancying It Up A Bit
I could have stopped there with the archiving, but I’m going to go a step further and add a user interface to this export iRule.
Be on the lookout for an upcoming article where I’ll illustrate how to reverse this article and Import an archived file back into the session table.