DX Unified Infrastructure Management

  • 1.  Call a Perl script from Lua

    Posted Dec 21, 2011 12:25 AM

    Special thanks to Matt Ritter of YJT Solutions for his significant contributions to the Lua code.

     

    I'm more comfortable coding in Perl and was looking for a way to call a Perl script from Lua and then do most of the heavy lifting with Perl.  The Lua script below is called by an AO rule that runs on alarm arrival, gets the event message and passes it to a Perl script as a parameter.  The Perl script consumes the parameter and writes it to a file simply to prove-out the concept.

     

    I now need to update both scripts so that the a result can be returned to the same Lua script for processing.  Any help in this area would be most appreciated.

     

    Lua script

    -- --           Name: LuaPasses2Perl --         Author: Rob Kelley --   Date Created: December 19 2011 --        Purpose: Passes a parameter to a Perl script for execution --Important Notes: 1> This script can On message arrival (on_arrival) action mode. --                 2> Perl must be sourced in order to run this script unless --                    the Perl script is called from the Perl home directory. --  -- Get the AO filtered alarm al=alarm.get()  -- Write the Alarm message to a variable alarmMSG = al[1].message  -- Pass the Alarm message to a Perl script output,i = action.command('d:\\ESM\\bin\\LuaPasser.pl ' .. alarmMSG)

     

     Perl script

    #!d:\\perl64\\bin\\perl  ###################################################################### #           Name: LuaPasser.pl                                                    #         Author: Rob Kelley                                                      #   Date Created: December 19 2011                                                #        Purpose: Receives a parameter from a Lua script and writes it to a file  #                 in order to prove-out that a perl script can be called from Lua #Important Notes: This script is called by LuaPasses2Perl script                  ##################################################################################  $out = ">d:\\ESM\\logs\\LuaParser.out"; $param1 = @ARGV[0]; open OUT, $out or die "Unable to open log file.  Exiting script. [$!]"; #print OUT "@ARGV[0]\n"; print OUT "$param1\n"; close OUT;


  • 2.  Re: Call a Perl script from Lua

    Posted Dec 21, 2011 05:23 PM

    I like you can do more things with python but lua is a need and below is a function and example how to call it.  It will run whatever command and then return the output.

     

    --This funtion runs a system command and returns the output.
    function cmdout (cmd)
       local f = io.popen(cmd)
       local lout = f:read("*a")
       f:close()
       return lout
    end

     

     

    cmd = "/home/ntimm/MSN-SCRIPTS/create-ticket.py  "..newmsg.."  "..tag2..""
    print (cmd)
    lout = cmdout(cmd)
    print (lout)




  • 3.  Re: Call a Perl script from Lua

    Posted Dec 21, 2011 07:07 PM

    Actually the action.command() function will return the output as well. Using the io.popen() function would certainly work, but it is probably easier to use the action.command() function. It was nice of Carstein to include that function for convenience.

     

    After you get the output from the command into the output variable, which is done in your current script, you should be able to process the output line by line like this:

     

    for _,line in ipairs(output) do
       print(line)
    end

     

    Just out of curiosity, what do you need to do with the alarm details? I completely understand the preference for a familiar scripting language, but it may be relatively simple to accomplish what you need in Lua. We can certainly help in this forum if you know what you want to accomplish but are not sure how to code it.



  • 4.  Re: Call a Perl script from Lua

    Posted Dec 28, 2011 10:40 PM

     

    Thanks keithk a ntimm.

     

    My use of alarm.message was totally arbitrary.  I was just trying to prove-out that I could capture an alarm attribute and send it to a Perl script for processing.

     

    Thanks for your offer for further help in terms of Lua scripting.  I am planning to enrich alarms with knowledgebase ID and support group by performing a lookup from a flat file using Perl.  I'm a little stronger in Perl than Lua which is why I wanted to build the Lua wrapper in case I'm not able to achive what I set out to do in Lua.

     

    The updated Perl script below updates the value it received from the Lua script and sends it back to Lua.

     

    Thanks again!

     

    Lua script

    -- Get the AO filtered alarm
    al=alarm.get()
    
    -- Write the Alarm message to a variable
    alarmMSG = al[1].message
    --print(alarmMSG)
    
    -- Pass the Alarm message to a Perl script
    output,i = action.command('d:\\ESM\\bin\\PassBack2Lua.pl ' .. alarmMSG)
    print(output[1])

     

     

    Perl script

    $out = ">d:\\ESM\\logs\\PassBack2Lua.out";
    $param1 = @ARGV[0];
    $param2 = $param1 . "returned";
    
    open OUT, $out or die "Unable to open log file.  Exiting script. [$!]";
    print OUT "$param1\n";
    close OUT;
    
    print STDOUT $param2;

     



  • 5.  Re: Call a Perl script from Lua

    Posted Jan 04, 2012 04:49 PM

    I did something very similiar for our knowledge base getting the ID and such into the alarms.



  • 6.  Re: Call a Perl script from Lua

    Posted Jan 05, 2012 12:28 AM

    I use the below instead of action.command which i had issues with. this io.popen seemed to work better. But i am passing variables back and forth between vbscripts and lua.

     

    f = assert(io.popen("cscript F:\\RemedyScripts\\RemedyCreate.vbs ".."\""..a.message.."\"".." ".."\""..a.user_tag1.."\"".." ".."\""..a.user_tag2.."\"".." ".."\""..a.nimid.."\"".." ".."\""..a.hub.."\"".." ".."\""..a.robot.."\"",'r'))
    s = assert(f:read('*a'))