DX Unified Infrastructure Management

 View Only
  • 1.  DB query in lua

    Posted Jun 13, 2019 07:35 AM
    Hi,

    I am trying to get the history of a particular alert before triggering an action in nas. The script works perfectly in test UIM but throws error in prod.

    NAS version in test - 9.06HF7
    NAS version in prod - 9.06

    a = alarm.get('AH65869489-88882')
    database.open("Provider=nis;database=nis;driver=none")

    --Query to get the process down alert count from a robot in last 1 hour

    rs = database.query("select count(nimid) as alertcount from nas_transaction_summary where (robot = '..a.robot..' and supp_key = '..a.supp_key..' and created > dateadd(minute, -60, getdate()))")
    database.close()

    Error : attempt to index field '?' (a nil value)

    If i replace look for nimid instead of robot it works not sure why it is not working when i use robot in prod UIM .

    rs = database.query("select count(nimid) as alertcount from nas_transaction_summary where (nimid= '..a.nimid..' and created > dateadd(minute, -60, getdate()))")


  • 2.  RE: DB query in lua

    Posted Jun 13, 2019 12:01 PM
    Had a nice reply for you but this silly forum tool lost it.

    So, to shorten it up

    Dump table a with:

    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


  • 3.  RE: DB query in lua

    Posted Jun 14, 2019 03:11 AM
    Hi Garin,

    Thanks for the reply, i am sorry , i am not able to get your logic.


  • 4.  RE: DB query in lua

    Posted Jun 13, 2019 01:15 PM
    Hi,
    I'm not quite sure what you are trying to accomplish (objective) with this. If you are just needing to get a count of the occurrences of the one particular alarm ID, then the suppcount alarm attribute contains that count (Note: it starts at 0). 
    a = alarm.get("XXXXXX-XXXXX")
    print(a.suppcount)

    If you need to get a count of all individual alarms for a particular robot and alarm type (suppression ID), then I will have to investigate further. One thing you will want to add is a bit of error/condition checking to make sure you have valid data. Always check to make sure a table (variable) is not nil.

    Example:
    a = alarm.get("XXXXXX-XXXXX")
    if a ~= nil then
                   <do something with a>
    end

    I will post more when I can on this.


  • 5.  RE: DB query in lua

    Posted Jun 14, 2019 03:12 AM
    Hi James,

    I have just posted the code snippet which is causing the error. The idea behind this script is to get the repeated alerts from the nas_transaction_summary table using robot name and suppression key.We are trying to start a process and  If the same alert is repeated for 3 times in an hour, the script will not start the process and ticket will be created . Recently we found the script is keep starting the process whenever we receive alert but the process was going down for every 10 minutes, to avoid this situation ,i developed this script to check the historic alert and skip the process start if the alert is repeating.


  • 6.  RE: DB query in lua

    Posted Jun 24, 2019 03:27 AM
    Any help on this


  • 7.  RE: DB query in lua
    Best Answer

    Posted Jun 26, 2019 12:00 PM
    Hi,

    I query the DB regularly to check to see if something has happened (or hasn't happened) at some point in the past.  Great for assurance monitoring, here's the script:

    rc = database.open("provider=nis;database=nis;driver=none")
    query = "SELECT message from nas_transaction_summary where created > DATE_SUB(CURRENT_TIMESTAMP(),INTERVAL 6 HOUR) and message like 'EXPECTED_ALARM_TEST%' union select 'Dummy Record' from dual limit 1;"

    alarms, rc = database.query(query)

       for _, al in pairs (alarms) do

    require ("library/html-alarms-lib")

    recipient = "SOMEONE@SOME.COM"
    title = "SOMETHING HAS NOT HAPPENED within the last 6 hours"
    message = "SOMETHING HAS NOT HAPPENED within the last 6 hours"
    message1 = "Severity = Critical"
    SUPPKEY = "SUPP_KEY"
    SUBSYS = "SUBSYS"
    SOURCE = "SOURCE"

    buf = ""
    buf = buf .. html_start (title)
    buf = buf .. html_alarm_message(message)
    buf = buf .. html_separator(hr)
    buf = buf .. html_alarm_message(message1)
    buf = buf .. html_separator(hr)
    buf = buf .. html_end ()

    if regexp (al.message,"*EXPECTED_ALARM_TEXT*")

    --then print (al.message)

    then print ("Everything is fine")

    --else print (al.message)

    else nimbus.alarm (5, message , SUPPKEY , SUBSYS , SOURCE)

    action.email (recipient,title,buf)

    end

    database.close()

    end
     
     
    So, if something has happened, great! Take no action.

    If not, create a critical alert and email someone.

    The hashed out lines are good for offline troubleshooting, so you're not creating alerts all over the place!

    Hope it helps :)


  • 8.  RE: DB query in lua

    Posted Jun 26, 2019 12:01 PM
    Edited by Sam Green Jun 26, 2019 12:01 PM
    Oh, I forgot to mention that the script does rely on a "dummy record" been inserted into your DB so there is at least one result rather than nothing.  As the regex check will fail to work it "nothing" is returned.  My SQL DBA did this for me.