# encoding: utf-8 =begin TODO: All process initialization and management through this plugin? TODO: Server/Game/Generic app process manager Find open processes of all registered apps able to kill them or restart them, so record the various details somewhere..able to start them. Rpt/log tailer... also handy for updater log etc ;-) Will require user authorization at some point :P (Or no such thing, but ssh-recommended?) =end case RUBY_PLATFORM when /-mingw32$/, /-mswin32$/ require 'win32/process' # required already in six-updater-web.rb #require 'win32/registry' #require 'win32ole' end module Six module Appmanager module_function def logger ActionController::Base.logger end def wmi_init if @wmi.nil? WIN32OLE.codepage = WIN32OLE::CP_UTF8 #swbemloc = WIN32OLE.new("WbemScripting.SWbemLocator") #@wmi = swbemloc.ConnectServer#(@address.to_s, namespace, @user, @password) @wmi = WIN32OLE.connect('winmgmts://') end end def kill(process) Process.kill(9, process).each do |kill| #.ProcessId logger.debug "Killed: #{kill}" # if kill == process.ProcessId # logger.debug "Succesfully killed!" # else # logger.debug "Not succesfully killed, forcing!" # sleep 3 # Process.kill(9, process.ProcessId) # end end end # TODO: Kill by ... multiple properties. gamefolder, etc. def kill_by_name(name, path = nil) procs = find_process(name, path) logger.debug "Procs: #{procs}" procs.each { |process| kill(process) } procs end def find_process(name, path) pids = [] logger.info "Checking for #{name} - #{path}" begin case RUBY_PLATFORM when /-mingw32$/, /-mswin32$/ %x[WMIC PROCESS get Caption,ExecutablePath,ProcessId].each_line do |l| #Commandline line = l.clone line.chomp! line.strip! reg = /([0-9]*)$/ line[reg] pid = $1 next if pid.nil? line.sub!(reg, "") line.strip! line[/^([\w ]*\.\w*)[ ]*(.*)/] image, full = $1, $2 next if image.nil? case image.downcase when name.downcase pid = pid.to_i next unless pid > 0 unless path && !full.nil? pids << pid next end logger.debug "Full: #{full.downcase}" if File.join(path, name).gsub("/", "\\").downcase == full.downcase pids << pid end end end =begin out = %x[tasklist] out.split("\n").each do |line| if line =~ /\A#{name}[\t| ]*([0-9]*)/ pid = $1 if pid.size > 0 pid = pid.to_i pids << pid end end end =end else # TODO: Include exe/cmdnline check; # /proc/PID/exe # /proc/PID/cmdline %x[ps -A | grep #{name}].split("\n").each do |line| unless line[/grep/] line[/^\s*([0-9]*)/] pids << $1 end end end rescue => e logger.warn "Failed checking tasklist for #{name}" logger.debug "#{e.class}: #{e.message} #{e.backtrace.join("\n")}" end pids =begin wmi_init ar = [] processes = @wmi.ExecQuery("select * from win32_process") processes.each do |process| if process.Name == name logger.debug "Found m! #{process.ProcessId}" ar << process end end ar =end end def tail(file, interval=1) raise "Illegal interval #{interval}" if interval < 0 # Read to a table and display with ajax??? # RE: http://onlamp.com/pub/a/onlamp/2005/06/09/rails_ajax.html File.open(file) do |io| loop do while ( line = io.gets ) puts line end # uncomment next to watch what is happening # puts "-" sleep interval end end end def read_logfile(file) begin File.open('C:/Users/SB/Test.txt') do |file| file.read end rescue 'Error while reading logfile!' end end end end =begin processes = @wmi.ExecQuery("select * from win32_process") for process in @processes do for property in process.Properties_ do logger.debug property.Name end break end for process in @processes do logger.debug "Name: #{process.Name}" logger.debug "CommandLine: #{process.CommandLine}" logger.debug "CreationDate: #{process.CreationDate}" logger.debug "WorkingSetSize: #{process.WorkingSetSize}" logger.debug end =end