Skip to content

Commit

Permalink
v0.4.10 - process-portability
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Wilhelm committed Nov 24, 2013
2 parents feb27ec + 39aa3dd commit 7bffa8f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 0.4.10

* collectors/process - portability, rename ni -> nice, skip thcount
except on linux

# 0.4.9

* collectors/cpu - portable vmstat, skip wait if missing
Expand Down
20 changes: 15 additions & 5 deletions collectors/process/process
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env ruby

require 'panoptimon/util'

require 'json'
class Array; def to_h; Hash[self]; end; end

Expand Down Expand Up @@ -31,7 +33,11 @@ checks.each {|n,o|
}

ps = ['ps']
fields = %w(pid time etime thcount pcpu ni pri vsz rss command)
fields = %w(pid time etime pcpu nice pri vsz rss args)

# TODO discover whether our ps supports it -- and do that efficiently?
fields.push('thcount') if Panoptimon::Util.os == :linux

fieldcount = fields.length

convert = ->(){
Expand All @@ -42,27 +48,31 @@ convert = ->(){
d.to_i * 24 * 60**2 + h.to_i * 60**2 + m.to_i * 60 + s.to_i}
{
pcpu: to_f, time: to_sec, etime: to_sec, pid: to_i,
thcount: to_i, pri: to_i, rss: to_i, ni: to_i, vsz: to_i
thcount: to_i, pri: to_i, rss: to_i, nice: to_i, vsz: to_i
}
}[]

# ps gives us one row per pid => collect into `found` hash
IO.popen(ps + ['-ww', '-o', fields.join(','), '-p', names.keys.join(',')]).
readlines.drop(1).each {|l|
IO.popen(ps + ['-o', fields.join(','), '-p', names.keys.join(',')]) {
|io| io.readlines
}.drop(1).each {|l|
row = ->(){
f = l.chomp.sub(/^\s+/, '').split(/\s+/, fieldcount)
(0..fieldcount-1).map {|i| [fields[i].to_sym, f[i]]}.to_h
}[]
pid = row[:pid]
row[:command] = row.delete(:args)
info = %w(command).map {|k| [k, row.delete(k.to_sym)]}.to_h
convert.each {|k,func| row[k] = func.call(row[k])}
convert.each {|k,func|
row[k] = func.call(row[k]) if row.include?(k)}
names[pid].each {|n|
i = found[n][:i] ||= 0; found[n][:i] += 1
found[n][i] = row
_info = found[n][:_info] ||= Hash.new {|h,k| h[k] = []}
info.each {|k,v| _info[k].push(v)}
}
} if names.keys.length > 0
exit $?.exitstatus unless $?.success?

found.each {|k,v| v.delete(:i)} # cleanup

Expand Down
2 changes: 1 addition & 1 deletion lib/panoptimon/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright (C) 2012 Sourcefire, Inc.

module Panoptimon
VERSION = "0.4.9"
VERSION = "0.4.10"
end

0 comments on commit 7bffa8f

Please sign in to comment.