LUA script to add sqlserver connection based on ntservices scan

Discussion created by jonhcw on Apr 17, 2015

I wrote a script to scan all robots with ntservices on a hub and add connection entries to sqlserver probe based on the scan.


Edit your parameters to the script and run it like:


nsa.exe -a <password> filename.lua


-- Configuration nimbus.login ("username", SCRIPT_ARGUMENT) local error_table = {} local connections = pds.create() local pdsargument = pds.create()   --required local sqlserver_robot = "/Domain/Hub/Robot" --robot where sqlserver profiles are created local hub_addr = "/Domain/Hub/Robot/hub" --hub to search sql instances local cfg_username = "domain\\username" --login to sql instance local cfg_password = "encrypted_password" --login to sql instance. get this by generating one profile in sqlserver.   --optional settings for sqlserver connection local cfg_auto_domain local cfg_description local cfg_retry local cfg_retry_delay local cfg_winauth local cfg_encryption local cfg_timeout   -- Functions   function error_exit(reason)   printf("----------------------------------------------------------------\n\tERROR: %s\n----------------------------------------------------------------", reason)   exit(1) end   function error_skip(reason)   table.insert(error_table, reason)   printf("----------------------------------------------------------------\n\tERROR: %s\n----------------------------------------------------------------", reason) end   function getrobots (hub_addr)   robots, rc =  nimbus.request(hub_addr, "getrobots")   if rc == 0 then   return robots.robotlist   else   error_exit(rc)   end end   function getntservices (robot_addr)   services, rc = nimbus.request(robot_addr.."/ntservices", "list_profilestatus")    if rc ~= 0 then   error_skip(robot_addr.."/ntservices: "..rc)   return nil   end     if services.profiles ~= nil then   return services.profiles   else   error_skip(robot_addr.." no service profiles")   return nil   end end   function getconnstring (robot_name, inst)   local conn_string   if string.find(inst, "^MSSQLSERVER$") ~= nil then   conn_string = robot_name   else   conn_string = robot_name.."\\"..inst   end    return conn_string end   function dumpconfig(config)   for a,b in pairs (pds.convert(connections)) do   printf("%s => %s", a,b)   end end   function dumperrors(error_table)   for a,b in pairs(error_table) do   printf("%d: %s", a, b)   end end   -- Main entry robotlist = getrobots(hub_addr)   for _, robot in pairs(robotlist) do   printf("processing %s..", robot.addr);   services = getntservices(robot.addr)    if services ~= nil then   for _, service in pairs (services) do   _,_,inst = string.find(service["description"], "SQL Server %((.+)%)")   if inst ~= nil then   local connection_section = "/connections/""-"..inst.."/"   local conn_string = getconnstring(, inst)    pds.putString(connections, connection_section.."user", cfg_username)   pds.putString(connections, connection_section.."password", cfg_password)   pds.putString(connections, connection_section.."auto_domain", cfg_auto_domain or "no")   pds.putString(connections, connection_section.."description", cfg_description or "")   pds.putString(connections, connection_section.."conn_string", conn_string or "")   pds.putString(connections, connection_section.."retry", cfg_retry or "0")   pds.putString(connections, connection_section.."retry_delay", cfg_retry_delay or "1 sec")   pds.putString(connections, connection_section.."winauth", cfg_winauth or "yes")   pds.putString(connections, connection_section.."encryption ", cfg_encryption or "no")   pds.putString(connections, connection_section.."timeout  ", cfg_timeout or "5 sec")   end   end   end end   pds.putString(pdsargument, "name", "sqlserver") pds.putPDS(pdsargument, "as_pds", connections) _, rc = nimbus.request(sqlserver_robot.."/controller", "probe_config_set", pdsargument)   if rc ~= 0 then   error_exit("write config to sqlserver: ".. rc) end   _, rc = nimbus.request(sqlserver_robot.."/sqlserver", "_restart")   if rc ~= 0 then   error_exit("restart sqlserver: ".. rc) end   --dumpconfig(connections)   print("Error list:") dumperrors(error_table)   printf("%s", "Profiles added to SQL server")