LUA: Defining messages in the config file with variable expansion

Discussion created by RobPol on Dec 18, 2012

I have been writing a probe and wanted to allow the users to customise the event message, severity, sid etc in the .cfg file <messages> section as in a normal nimsoft probe. 


For example here is a bit of a .cfg file:




      text = Something smells rotten $robotname value of widget $cur is exceedingly exceeding $max

      level = critical 
      subsystem =
      suppkey = BadStuff




Here is the lua function and an invocation example. I assume you have already read your config into the cfg variable. Variable substitution should also be added to the supp key but I leave that as an excercise for the reader. You can pass the variables in the invocation as below or you can give it a table such at the one returned by controller get_info then you could use any of the values returned by that callback as your substitution variables. Depends on your needs.


-- function send_event
-- Create an event based on the settings in the config file
-- performing variable substitution where appropriate
function send_event(name, var_table, cfg)
   local map = {
                clear = 0,
                information = 1,
                warning = 2,
                minor = 3, 
                major = 4,
                critical = 5,
   etable = cfg["/messages/"] -- try to grab the table for this event
   if etable == nil then
      probe.log(0, "no definition for event "" in config file")
     exit (3)
  probe.log (3, "Sending "" event")
   -- perform variable substitution on message text
   etable.text = string.gsub(etable.text, "%$([%w_]+)", var_table)
   probe.log (5, "substituted message is: "..etable.text)
   nimbus.alarm (map[etable.level], etable.text, etable.suppkey, etable.subsystem)


send_event("BadStuff", {robot = "Marvin", cur = 42, max = 3.1415927), cfg)