Created
May 2, 2015 04:27
-
-
Save hiroara/5e28799a5199986fef1c to your computer and use it in GitHub Desktop.
Watching Process and send YO
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
target_pid = ARGV[0].to_i | |
target_yo_user = ARGV[1].upcase | |
threshold_cpu = (ARGV[2] || 50).to_i | |
sampling_times = (ARGV[3] || 3).to_i | |
sleep_sec = (ARGV[4] || 1).to_i | |
puts "watching #{target_pid}..." | |
puts " - Target YO User: #{target_yo_user}" | |
puts " - Threshold of CPU Usage: #{threshold_cpu}%" | |
puts " - Sampling times: #{sampling_times}" | |
puts " - Sleep Seconds: #{sleep_sec}s" | |
loop do | |
sampling_times.times.map do | |
`ps o pid,%cpu,command`.split("\n").drop(1).map do |line| | |
pid, cpu, command = line.split(' ', 3) | |
{ pid: pid.to_i, cpu: cpu.to_f, command: command } | |
end.find{ |process| process[:pid] == target_pid }.tap{ sleep sleep_sec } | |
end.compact.tap do |processes| | |
if !processes.empty? && processes.map{ |process| process[:cpu] }.inject(0, :+) / sampling_times > threshold_cpu | |
puts "Sleeping... CPU (average of #{sampling_times} times): #{processes.first[:cpu]}%" | |
else | |
puts "Now sending YO!" | |
`curl -s -X POST "http://api.justyo.co/yo/?api_token=#{ENV["YO_API_TOKEN"]}&username=#{target_yo_user}"` | |
exit 0 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment