LUA - Table Dumper and LUA includes

Document ID : KB000047892
Last Modified Date : 29/01/2019
Show Technical Document Details
Introduction:

When developing LUA scripts and probes one of the most frustrating set backs can be trying to navigate your table structures returned from nimbus.request.


The tdumper function provides an easy way to traverse a table and print its content. Within the article we also discuss (all be it in brief) "including" external .lua files so functions can be reused in more of an object oriented way.

The following article discusses creation and use of the a tdump function to print out nested tables when developing LUA scripts/probes.


 

 

 

 

Background:

Assume we wish to return the output from the hub callback "list_subcribers". The below image illustrated the return from this callback when using the probe utility GUI (CRTL+P).




Using LUA we can take this information into a table which enables us to process the information much easier. Without going into detail lets quickly examine the syntax to issue this call back (This topic is covered in greater details in other LUA articles):


local output = {}
output = nimbus.request("/nevil-nmsdom/nevil-nmshub/nevil-nms/hub", "list_subscribers")
print(output)


The above code creates an empty table ?output? and then fills it with data from the return of nimbus.request (the ?list_subscribers? callback). When the print the ?output? table:

----------- Executing script at 07/08/2012 13:02:55 ----------

 table:0x2390d50


Not exactly useful data! Not to worry tdumper to the rescue
Instructions:

The following code provides us with the function "tdumper"


function tdump(t)
 local function dmp(t, l, k)
 if type(t) == "table" then
    print(string.format("%s%s:", string.rep(" ", l*2), tostring(k)))
    for k, v in pairs(t) do
       dmp(v, l+1, k)
    end
 else
     print(string.format("%s%s:%s", string.rep(" ", l*2), tostring(k), tostring(t)))
 end
end

dmp(t, 1, "root")
end


Appending our previous nimbus.request code to this and calling the tdumper function to print the table provides us with a much more useful output. Open the NAS probe and the script editor under the pre-processing tab and copy the following into the editor.


function tdump(t)
local function dmp(t, l, k)
 if type(t) == "table" then
    print(string.format("%s%s:", string.rep(" ", l*2), tostring(k)))
    for k, v in pairs(t) do
      dmp(v, l+1, k)
    end
else

print(string.format("%s%s:%s", string.rep(" ", l*2), tostring(k), tostring(t)))
end
end
dmp(t, 1, "root")
end

local output = {}
output = nimbus.request("/nevil-nmsdom/nevil-nmshub/nevil-nms/hub", "list_subscribers")
tdump(output)


The following is a exert from output from the script:

----------- Executing script at 07/08/2012 14:11:52 ----------

 root:
 postroute:
 1:
 established:1344240799
 type:TEMP
 id:dashboard_engine
 fsize:0
 bulk_size:0
 npds:0
 fname:q/t_18.sds
 fd:26
 count:773
 wpos:0
 rpos:0
 connected:192.168.1.51/35605
name:t_18
subject:alarm_new,alarm_update,alarm_assign,alarm_close,alarm_stats


As you can see the whole table is dumped in a much more useful format. The tdump function allows you to see the full table structure so you can format your code to address specific elements of the table as required. The actual syntax for addressing nested tables is outside the scope of this article but watch out for further discussions on this topic.

While this is a useful function you might be right in thinking this is somewhat of an overkill for what was a three line script. When developing LUA scripts we tend to deal a lot with tables so I recommend saving this as a separate .lua file and calling it as part of your script ?as required?, lets face it once your script is coded and debug and ready for production you might not need this function anymore anyway.

Copy the function code to a file called tdumper.lua and save it in the $nimsoft_home/probes/service/nas/scripts/ folder and add the following to the top of your LUA script:

dofile "scripts/tdumper.lua"

This will "include" this code function every time this LUA script is run saving the need to clutter your scripts or copy paste this function into your development code.