Skip to content

Instantly share code, notes, and snippets.

@a2ikm
Created September 29, 2017 09:16
Show Gist options
  • Select an option

  • Save a2ikm/a6c695e15f49ad3e175a213d64e74ea4 to your computer and use it in GitHub Desktop.

Select an option

Save a2ikm/a6c695e15f49ad3e175a213d64e74ea4 to your computer and use it in GitHub Desktop.
mpstat -P ALLから%usrだけを抜き出すやつ
#!/usr/bin/env ruby
@current = []
@header = false
def init(time, all)
@current << time
@current << all
end
def append(usr)
@current << usr
end
def flush
@current.compact!
if @current.length > 0
unless @header
puts (%w(time all) + (@current.length - 2).times.to_a).join("\t")
@header = true
end
puts @current.join("\t")
end
@current.clear
end
STDIN.each do |line|
if line.empty? || line.start_with?("Linux")
next
else
time, core, usr, _ = line.split(/ +/, 4)
if core == "CPU"
flush
elsif core == "all"
init(time, usr)
else
append(usr)
end
end
end
flush
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment