DX Infrastructure Manager

Expand all | Collapse all

LUA Maths - How to Average a number

Jump to Best Answer
  • 1.  LUA Maths - How to Average a number

    Posted 09-02-2014 05:49 PM

    Hi Forum!

     

    Does anyone have any example scripts for extracting numbers from an alarm, and ideally adding them together creating a total?

     

    There's surprisingly little resource for help on this kinda thing :smileysad:

     

    Thanks in advance!



  • 2.  Re: LUA Maths - How to Average a number

    Posted 09-02-2014 05:55 PM

    We could probably be more helpful if you provide a specific example of the alarm from which you want to extract the numbers.



  • 3.  Re: LUA Maths - How to Average a number

    Posted 09-04-2014 02:44 PM

    Very good point Keith,

     

    Here's an example of the alarm:

     

    2014-09-04 11:39:57,447 [WebContainer : 5] DEBUG org.apache.axis.TIME  - axisServlet.doPost: /WebServices pre=1 invoke=35 post=0 send=5 ProductCataloguePort.getProduct

     

    I'd like to add up the values for pre=, invoke=, post= and send=

     

    Then ideally I'd store them in the MySQL DB, then I could query them in the UMP..

     

    Thanks in advance!



  • 4.  Re: LUA Maths - How to Average a number
    Best Answer

    Posted 09-04-2014 05:49 PM

    Here is a starting example of how you can extract the variable values from the message:

     

    local msg = "2014-09-04 11:39:57,447 [WebContainer : 5] DEBUG org.apache.axis.TIME  - axisServlet.doPost: /WebServices pre=1 invoke=35 post=0 send=5 ProductCataloguePort.getProduct"
    
    local pattern = " pre=(%d+) invoke=(%d+) post=(%d+) send=(%d+) "
    
    local vpre,vinvoke,vpost,vsend = string.match(msg, pattern)
    
    print("pre= "..vpre)
    print("invoke= "..vinvoke)
    print("post= "..vpost)
    print("send= "..vsend)

    You can run this in the script window in the NAS GUI or in the NSA. That made it easy for me to test, so I wanted to provide it as a definite working example. The script assumes the values will always be positive integers, but handing decimal values or negative numbers would be simple with some changes to the pattern.

     

    Converting the script to extract the values from an alarm message is fairly easy. Getting it to submit QoS is also easy but requries a QoS definition. The script to extract and send QoS should look something like this:

     

    local qos_name   = "QOS_SOME_STUFF"
    local qos_source = "source"
    local qos_target = "target"
    
    local pattern = " pre=(%d+) invoke=(%d+) post=(%d+) send=(%d+) "
    
    local a = alarm.get()
    
    local vpre,vinvoke,vpost,vsend = string.match(a.message, pattern)
    
    local sum = vpre + vinvoke + vpost + vsend
    
    nimbus.qos(qos_name, qos_source, qos_target, sum, QOS_ASYNC)

    The qos_* variables should be set the values that make sense. The script assumes the QoS is asynchronous, which seems most likely when extracting from alarms. Before you send the first QoS, make sure you have a matching QoS definition. Assuming you need a custom QoS definition, you can create it manually or have the script do it (which might be unnecessary overhead depending on how often the script runs). If you try to do it in Lua, here is the relevant function call:

     

    nimbus.qos_definition ( QosName, QosGroup, Description, Unit, UnitAbbreviation, HasMax [, IsAsynch] )

    Creates a QoS definition named QosName. Unless the flag IsAsynch is true, an interval based QoS is created. Please note that subsequent definitions on the same name will not recreate or alter an existing QoS definition. The HasMax flag set requires that all qos data (issued by nimbus.qos) referring to this QoSName is issued with a MaxValue.



  • 5.  Re: LUA Maths - How to Average a number

    Posted 09-05-2014 12:28 AM

    Wow, amazing response Keithk!

     

    Thank you so much for your help, I'll have a play with the script tomorrow.

     

    Hopefully, I'll be able to get a matching alarm, extract the numbers, add them all together and then send the results as QOS data!

     

    Coupled with an AO profile this would be amazing!

     

    Thanks again, I'll post back and let you know how I get on :smileyhappy:



  • 6.  Re: LUA Maths - How to Average a number

    Posted 09-05-2014 01:47 PM
      |   view attached

    Hi Keith

     

    I'm struggling with the creating QOS part..

     

    I've tried adding to an existing QOS entry, as below:

     

    local qos_name   = "QOS_LOGMON_VARIABLE"
    local qos_source = "Nagiosac"
    local qos_target = "ACWASL03 - Core Web Services - GetProduct.axisServlet.doPost"

    local pattern = " pre=(%d+) invoke=(%d+) post=(%d+) send=(%d+) "

    local a = alarm.get("UG07670144-92223")

    local vpre,vinvoke,vpost,vsend = string.match(a.message, pattern)

    local sum = vpre + vinvoke + vpost + vsend

    nimbus.qos(qos_name, qos_source, qos_target, sum, QOS_ASYNC)

     

    But this bring back the error:

     

    Error in line 13: bad argument #5 to 'qos' (number expected, got nil)

    I'll happily create another QOS entry, but I'm also unsure on how to do that.  I've tried the one you mentioned in the previous post but it also errors, I'm not getting the syntax right:

     

    nimbus.qos_definition ( QosName, QosGroup, Description, Unit, UnitAbbreviation, HasMax [, IsAsynch] )

     

    Would the above command run everytime the script runs? Or does it only have to run once to create the definition?

     

    I took these details from the UMP to try and get an already exisiting QOS entry:

     

    local qos_name   = "QOS_LOGMON_VARIABLE"
    local qos_source = "Nagiosac"
    local qos_target = "ACWASL03 - Core Web Services - GetProduct.axisServlet.doPost"

     

     

    See screenshot



  • 7.  Re: LUA Maths - How to Average a number

    Posted 09-05-2014 04:53 PM

    Sorry, that should have been QOS_ASYNCH rather than QOS_ASYNC. My bad. Hopefully that will work better.

     

    nimbus.qos ( QosName, Source, Target, Value, Interval | QOS_ASYNCH [,MaxValue] )

    Will send an interval based QoS message when Interval is greater than zero, and a asynchronous QoS message when called with QOS_ASYNCH. Please note that no QoS data will be recorded unless a valid QoS definition has been sent prior to this request. Remember to set the MaxValue if definition was created using HasMax=true.



  • 8.  Re: LUA Maths - How to Average a number

    Posted 09-05-2014 04:55 PM

    And, yes, you only have to run the nimbus.qos_definition() function once. You can look at the QoS definition in SLM to confirm it was created correctly. Make sure you set IsAsync to true on the QoS definition. Otherwise your call to nimbus.qos() will work, but the data will probably not be saved once it gets to the data_engine.



  • 9.  Re: LUA Maths - How to Average a number

    Posted 09-10-2014 03:15 PM

    This works beautifully.

     

    Thank you so much Keith!



  • 10.  Re: LUA Maths - How to Average a number

    Posted 09-03-2014 03:48 PM

    http://lua-users.org/wiki/PatternsTutorial and http://www.lua.org/pil/3.1.html ?

    Perhaps you should mention what you plan to do with the total after you have created it as well.