Forum: Ruby how to get Windows top 10 cpu and memory process using ruby

46b11fb3f6fa11a70484f1348af4a2b4?d=identicon&s=25 Jaga Ray (jagadeesh)
on 2017-04-20 12:44
how to get  Windows top 10 cpu and memory process using ruby
B078cb4f4fb473c7a54d1fc36d10c70e?d=identicon&s=25 Regis d'Aubarede (raubarede)
on 2017-04-21 18:27
Jaga Ray wrote in post #1185823:
> how to get  Windows top 10 cpu and memory process using ruby

Here some (old, but working w10) code for acquiring process list.
it use OLE for doing wmi request,
wmi is a kernel access (as realtimedatabase) which contain dynamic data
of windows kernel.

==
require 'win32ole'

def make_list_process()
  wmi = WIN32OLE.connect("winmgmts://")
  lp = wmi.ExecQuery("select * from win32_process").enum_for(:each)
  l=lp.map { |pr|
[pr.Caption.strip,pr.ProcessId,pr.PageFileUsage.to_s,pr.CommandLine.strip[0..90].ljust(90)]
rescue nil}
  wmi.ole_free
  text=l.sort_by { |a| a && a[2].to_i || 100000}[0..160].select {|l|  l
&& true}
  max=[0]*30
  text.each { |line| next unless line; line.map! {|c| (c=~/[0-9]/) ?
c.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1 "): c}}
  text.map { |line| line.each_with_index.map {|c,i|
c.to_s.rjust(max[i])}}
end


def detail_process(pid)
  wmi = WIN32OLE.connect("winmgmts://")
  lp = wmi.ExecQuery("select * from win32_process where
ProcessId=#{pid}").enum_for(:each)
  descr=lp.first
  data={}
  descr.Properties_.each { |x|  data[x.name]=descr.send(x.name) }
  wmi.ole_free
  data
end
==

for wmi database, you can use WMIC.exe on  cmd.com :

> wmic process

> wmic  process where name="ruby.exe" get commandline
CommandLine
D:\usr\Ruby\Ruby200\bin\ruby.exe
d:/usr/Ruby/local/winmonitor/monitoring.rb waitboot
ruby D:/usr/Ruby/local/PicoWebServer/webserver.rb
ruby ../img/reddit.rb
ruby  notes.rb
B078cb4f4fb473c7a54d1fc36d10c70e?d=identicon&s=25 Regis d'Aubarede (raubarede)
on 2017-04-21 19:00
Jaga Ray wrote in post #1185823:
> how to get  Windows top 10 cpu and memory process using ruby

you can also use 'tasklist' command.
option
    /fo for specifi output format,
    /v for verbose output : get many data on proces
==
titles=[]
pl=`tasklist /fo csv /v`.each_line.to_a.each_with_index.map {|line,i|
 if i==0
   titles=line.chomp.split(/,/).map {|t| t[1..-2]}
   p titles
 end
 next if i<2
 data=line.chomp.split(/,/).map {|t| t[1..-2]}
 titles.zip(data).each_with_object({}) {|(t,d),h| h[t]=d}
}
==

Result (in french! : field name are country-dependent) :

{"Nom de l'image"=>"cmd.exe", "PID"=>"6052", "Nom de la
session"=>"Console", "Num\x82ro de session"=>"1", "Utilisation
m\x82moire"=>"3\xFF144 Ko", "\x90tat"=>"Unknown", "Nom
d'utilisateur"=>"D-CZH01404CV\\e", "Temps processeur"=>"0:00:00", "Titre
de la fen\x88tre"=>"N/A"}

{"Nom de l'image"=>"conhost.exe", "PID"=>"16464", "Nom de la
session"=>"Console", "Num\x82ro de session"=>"1", "Utilisation
m\x82moire"=>"4\xFF904 Ko", "\x90tat"=>"Unknown", "Nom
d'utilisateur"=>"D-CZH01404CV\\e", "Temps processeur"=>"0:00:00", "Titre
de la fen\x88tre"=>"N/A"}

....
46b11fb3f6fa11a70484f1348af4a2b4?d=identicon&s=25 Jaga Ray (jagadeesh)
on 2017-04-24 14:27
@Regis d'Aubarede : can you let me know execution steps for first
script?

my requirement is display top 10 memory and cpu consuming process with
process id  and file name
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.