Skip to content

Instantly share code, notes, and snippets.

@jbz
Created July 18, 2011 17:28
Show Gist options
  • Save jbz/1090107 to your computer and use it in GitHub Desktop.
Save jbz/1090107 to your computer and use it in GitHub Desktop.
Script to parse chef nodes by last-checkin-time
#!/usr/bin/ruby
#
# Filter hosts wose time since last checkin >= X
#
# usage: (process that outputs lines) | filterHosts.rb <#hrs>
# to prune hosts in opscode: knife status *:* | filterHosts.rb <#hrs> | xargs -I % knife node delete % -y
STDIN.each_line {|line|
#split the line by commas, strip whitespace, map to fields
time_interval,host,os,ip = line.split(/,/).map {|s|s.strip}
#split the hours description by spaces, extract first field (the number), convert to integer
time_parts = time_interval.split(/\s/)
hours = time_parts[0]
hrs = hours.split(/m/)[1].to_i
# puts time_parts.inspect
# put the result on stdout if the hours > first argument to script coerced to integer
# puts "#{hrs} #{host}" if hrs > ARGV[0].to_i
puts host if hrs > ARGV[0].to_i
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment