Skip to content

Instantly share code, notes, and snippets.

@ChadyG
Last active December 31, 2015 16:39
Show Gist options
  • Save ChadyG/8014701 to your computer and use it in GitHub Desktop.
Save ChadyG/8014701 to your computer and use it in GitHub Desktop.
require 'sys/proctable'
include Sys
def parse_opts(options)
val = {}
options.each_slice(2) { |opt|
val[opt.first.downcase] = opt[1]
}
val
options.collect {|opt| opt.downcase}
end
class Integer
def to_mb
val = self.to_f
%w(B KB MB GB).each { |abbr|
return "#{val.round(4)}#{abbr}" if val < 1024
val /= 1024
}
end
end
if __FILE__ == $0
opts = parse_opts($*)
if opts.include?("-h")
puts %{
List Processes
updated 12-17-2013 ChadyG
=========================
Usage
=====
list_procs.rb [options]
Options
=======
-h This help text
-pid Append pid of processes to output
-ws Append Working Set size to output
-vs Append Virtual Size to output
-cd Append Creation Date to output
}
exit
end
out_table = []
header = []
header << "Name"
header << "PID" if opts.include?("-pid")
header << "Working Set" if opts.include?("-ws")
header << "Virtual Size" if opts.include?("-vs")
header << "Creation Date" if opts.include?("-cd")
out_table << header
#TODO: add options for what methods to print
ProcTable.ps { |proc_struct|
row = [proc_struct.name]
row << proc_struct.pid.to_s if opts.include?("-pid")
row << proc_struct.working_set_size.to_mb if opts.include?("-ws")
row << proc_struct.virtual_size.to_mb if opts.include?("-vs")
row << (proc_struct.creation_date.nil? ? "" : proc_struct.creation_date.strftime("%d-%b-%Y %H:%M:%S")) if opts.include?("-cd")
out_table << row
}
#compute column lengths
out_col_lengths = []
out_table.first.each_with_index { |obj, col|
length = 0
out_table.each { |row|
entry = row[col]
length = entry.length if entry.length > length
}
out_col_lengths << length
}
#print with computed padding
out_table.each_with_index { |row, index|
str = ""
row.each_with_index { |entry, col|
rem = out_col_lengths[col] - entry.length
str += entry + ' ' * rem + " | "
}
puts str
puts '-' * str.length if index == 0
}
end
=begin
#<struct Struct::ProcTableStruct
caption="ruby.exe",
cmdline="\"C:\\Ruby193\\bin\\ruby.exe\" \"C:\\Ruby193\\bin\\irb\"",
comm="ruby.exe",
creation_class_name="Win32_Process",
creation_date=#<DateTime: 2013-12-17T14:25:50-04:00 ((2456644j,66350s,275532000n),-14400s,2299161j)>,
cs_creation_class_name="Win32_ComputerSystem",
cs_name="CGODSEY-WIN7",
description="ruby.exe",
executable_path="C:\\Ruby193\\bin\\ruby.exe",
execution_state=nil,
handle="9200",
handle_count=152,
install_date=nil,
kernel_mode_time=624004,
maximum_working_set_size=1380,
minimum_working_set_size=200,
name="ruby.exe",
os_creation_class_name="Win32_OperatingSystem",
os_name="Microsoft Windows 7 Ultimate N |C:\\Windows|\\Device\\Harddisk0\\Partition2",
other_operation_count=3022,
other_transfer_count=381830,
page_faults=4508,
page_file_usage=10896,
ppid=10952,
peak_page_file_usage=10896,
peak_virtual_size=95563776,
peak_working_set_size=17048,
priority=8,
private_page_count=11157504,
pid=9200,
quota_non_paged_pool_usage=16,
quota_paged_pool_usage=133,
quota_peak_non_paged_pool_usage=16,
quota_peak_paged_pool_usage=133,
read_operation_count=255,
read_transfer_count=635466,
session_id=1,
status=nil,
termination_date=nil,
thread_count=7,
user_mode_time=3900025,
virtual_size=95547392,
windows_version="6.1.7601",
working_set_size=17457152,
write_operation_count=0,
write_transfer_count=0>
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment