DX Unified Infrastructure Management

Expand all | Collapse all

Need help with LUA script to run command remotely on monitored machine

  • 1.  Need help with LUA script to run command remotely on monitored machine

    Posted Aug 16, 2012 12:25 PM

    Hi all

     

    My first ever post so apologies if this has already been asked.

    The issue I have is

    • I am monitoring a Postcode service via urlresponse
    • Every now and then that service fails
    • the actual service keeps running but is does not work correctly so the URLrespons alerts(so ntservice probe no use)
    • I would like a way of restarting the service on the remote machine running the Postcode service if I get an alert from URL reponse probe
    • I think LUA may be the way to go here but I have zero knowledge of this(will try and learn)

    Hope this is enough for you to understand my issue

     

    Thanks in advance for any help you can give me

     

    Keith



  • 2.  Re: Need help with LUA script to run command remotely on monitored machine

    Posted Aug 16, 2012 01:58 PM

    Hi Keith,

     

    well, should be the same way, as the posting you find propably, as far as I understand that.

     

    The nexec-probe should be your friend, with it you define the script or command you would like to run.

    From the nas you do a script out of the alarm which runs the nexec profile.

     

    I just wrote something like that, where I check a windows-service via a rsp-probe and if the service is not running I run a script which fires a nexec profile, which (lotsa which) runs the command that starts the remote service again.

     

    I can put a bunch of hardcopies together if I have the time to show the steps, only disadvantage is that some things could be in german :-/

     

    cheers

    Matthias

     



  • 3.  Re: Need help with LUA script to run command remotely on monitored machine

    Posted Aug 16, 2012 02:05 PM

    Matthias

     

    Thanks for the reply

    It sounds as if this may help if you could provide examples of your scripts or hard copies etc it would help(I have someone who can probably translate the German).

     

    Thanks

     

    Keith



  • 4.  Re: Need help with LUA script to run command remotely on monitored machine

    Posted Aug 16, 2012 03:56 PM

    Can you still deploy the ntservices probe? If so you could execute a simple ntservices stop_service and start_service via the probe call backs.



  • 5.  Re: Need help with LUA script to run command remotely on monitored machine

    Posted Aug 16, 2012 04:03 PM

     BryanMorrow

     

    Thanks for your input

    The ntservice probe is deployed on the machine that the service is running on

     

    So the scenario would be

    1. URLResponse gernerates alert that the service is not working correctly as the urlresponse does not see expected text
    2. The service however is still running on the machine.
    3. I then need to trigger ntservices to restart the service on the failing machine(how would I go about doing this?).

    Regards

     

    KEith

     



  • 6.  Re: Need help with LUA script to run command remotely on monitored machine

    Posted Aug 16, 2012 04:33 PM

    There are different ways to handle this via scripting here is a example I use to restart services on a remote machine.I then update the alarm in one of the custom fields so our techs know we have attempted to restart.

     

    if a ~= nil then
    hub,host,assto,sid,robot = a.hub,a.hostname,a.assigned_to,a.sid,a.robot
    print (hub)
    if hub == "customer1" or hub == "customer2" then
    stopcmd = "/opt/nimsoft/bin/pu -u administrator -p mypass /Domain/".. hub .."/".. robot .."/ntservices stop_service PariWingService"
    startcmd = "/opt/nimsoft/bin/pu -u administrator -p mypass /Domain/".. hub .."/".. robot .."/ntservices start_service PariWingService"
    print ("stopping pariwing for "..robot.."")
    cmdout(stopcmd)
    --print (lout)
    sleep(5000)
    print ("starting pariwing for "..robot.."")
    --print (startcmd)
    cmdout(startcmd)
    a.custom_2 = "Auto restart complete"
    alarm.set(a)

     


  • 7.  Re: Need help with LUA script to run command remotely on monitored machine

    Posted Aug 16, 2012 04:44 PM

    Disclaimer on this, its early and I'm a LUA hacker at best.

     

    I used variables and functions incase you wanted to add other checks or features. It can pull the information from the triggered alarm. Also, you may need a pause of some sort between the starting and stopping of the service.

     

    ----Get information from alarm---
    a = alarm.get()
    
    ----Variables--------------------
    service = "CertPropSvc"
    probe = "ntservices"
    addr = "/"..a.domain.."/"..a.hub.."/"..a.robot.."/"..probe
    
    function CheckServiceState(addr,service)
       list_services = nimbus.request(addr, "list_services") 
       for k,v in pairs(list_services) do
          if k == service then
             for key,value in pairs(v) do
                if key == "state" then
                   if value == "running" then
                      StopService(addr,service)
                      StartService(addr,service)
                   else
                      StartService(addr,service)
                   end
                end
             end
          end
       end
    end
    
    function StopService(addr,service)
       args = pds.create()
       pds.putString(args, "name", service) 
       req = nimbus.request(addr,"stop_service", args)
       if req ~= nil then
          print (service .. " successfully stopped")
       else
          print (service .. " failed to stop")
       end
    end
    
    function StartService(addr,service)
       args = pds.create()
       pds.putString(args, "name", service) 
       req = nimbus.request(addr,"start_service", args)
       if req ~= nil then
          print (service .. " successfully started")
       else
          print (service .. " failed to start")
       end
    end
    
    
    CheckServiceState(addr,service)

     



  • 8.  Re: Need help with LUA script to run command remotely on monitored machine

    Posted Aug 16, 2012 05:10 PM

    Thanks for all the replies eveyone, I will take a look at everything and try to muddle through it. Not sure my LUA skill are up to it (as they are nonexistent)

     

    Thanks all I will let you know if I have any joy.

     

    Keith



  • 9.  Re: Need help with LUA script to run command remotely on monitored machine

    Posted Aug 17, 2012 10:28 AM
      |   view attached

    Hi!

     

    and now my version ;-)

     

    cheers

    Matthias

     

    Attachment(s)

    docx
    remotenexec.docx   85 KB 1 version


  • 10.  Re: Need help with LUA script to run command remotely on monitored machine

    Posted Aug 17, 2012 10:37 AM
    Mgruber

    Thanks for the details, I will take a look at it and hopefully be able to achieve a successful outcome.

    Keith


  • 11.  Re: Need help with LUA script to run command remotely on monitored machine

    Posted Aug 16, 2012 04:44 PM

    Here is an example of a Lua script that stops and starts a service:

     

    http://forum.nimsoft.com/t5/General/ntservices-question/m-p/9103

     

    In that case, the script first needed to check the status of the service, which you could probably skip and eliminate a bit of the script. You would have to make sure to fill in the probe_addr and service_name variables from the fields in the alarm. It works mostly like Neal's script except that it issues the callback requests natively in the script rather than by calling an external command.

     

    Here is another script with a similar purpose that does not have as much error checking (and apparently had a problem that the original poster never explained):

     

    http://forum.nimsoft.com/t5/General/Problem-with-restating-a-Service-from-Lua-Script/m-p/14139