chris.naude

LUA - Windows Handles Monitor

Discussion created by chris.naude on Mar 18, 2011

I wrote this a while ago in an effort to learn LUA. It uses handle.exe (downloadable from microsoft.com) to monitor process handles in Windows. Someone might find it useful.

 

The main probe.

 

 

----------------------------------------------------------------- -- Name: winHandles.lua -- Purpose: Alarm when processes use up too many handles. -----------------------------------------------------------------  local loglevel   = 1 local prgname    = "winHandles" critical = 0 major = 0 minor = 0  -- -- Open configuration file (probename + ".cfg") or specify it explicitly with probe.config("other.cfg") -- function load_configuration()     local cfg = probe.config(prgname..".cfg")          if cfg ~= nil and cfg["/setup"] then         -- Look for the setup/your_key value         local value = cfg["/setup"].loglevel         if cfg["/setup"].loglevel ~= nil then loglevel = cfg["/setup"].loglevel end         probe.setloglevel (loglevel)     end     if cfg ~= nil and cfg["/setup"] then         -- Look for the setup/your_key value         local value = cfg["/setup"].critical         if cfg["/setup"].critical ~= nil then critical = tonumber(cfg["/setup"].critical) else critical = 0 end             end     if cfg ~= nil and cfg["/setup"] then         -- Look for the setup/your_key value         local value = cfg["/setup"].major         if cfg["/setup"].major ~= nil then major = tonumber(cfg["/setup"].major) else major = 0 end             end     if cfg ~= nil and cfg["/setup"] then         -- Look for the setup/your_key value         local value = cfg["/setup"].minor         if cfg["/setup"].minor ~= nil then minor = tonumber(cfg["/setup"].minor) else minor = 0 end             end end  function check_handles()      reply,rc = action.command("handle.exe")      pid=""      process=""      count=0      line=""      thresh=1      bad_procs = {}      sev=NIML_MINOR      if reply then           for k,v in pairs(reply) do                --printf ("%s => %s",k,v)                if regexp (v,"*pid:*") then                     line=v                                               list = split (line," ")                        pid=list[3]                        process=list[1]                        --printf ("PID: %s",pid)                        count=0                     --for i=1,#list do                     --   print (i,"=",list[i])                                            --end                   end                   if regexp (v,"^------------*") then                        --printf("%s",v)                          if count >= critical then                          sev=NIML_CRITICAL                                              elseif count >= major and sev < NIML_CRITICAL then                          sev=NIML_MAJOR                                                                       elseif count >= minor and sev < NIML_MAJOR then                          sev=NIML_MINOR                                                                       elseif sev < NIML_MINOR then                          sev=NIML_CLEAR                                              end                     msg = sprintf("Process %s (PID: %s) has %d handles. (%d >= %d)",process,pid,count,count,thresh)                     if sev ~= NIML_CLEAR then                                                   table.insert(bad_procs,msg)                      end                        count=0                   elseif pid then                         count=count+1                   end           end      end      if sev ~= NIML_CLEAR then           nimbus.alarm (sev,"There are processes with too many handles. See log for details.",SCRIPT_FILE,"1.1.1")           for k,v in pairs (bad_procs) do                probe.log(0,v)           end      else           nimbus.alarm (sev,"All process handles are okay.",SCRIPT_FILE,"1.1.1")      end end  ----------------------------------------------------------------- -- MAIN ENTRY ----------------------------------------------------------------- probe.log(0,"----------------- Starting  ------------------")  load_configuration()  check_handles()  probe.log(0,"Exiting program");

 

 Sample config file.

 

<setup>       loglevel = 0      critical = 50      major = 40      minor = 30 </setup>

Outcomes