Skip to content

Instantly share code, notes, and snippets.

@fishnix
Created February 6, 2016 00:52
Show Gist options
  • Save fishnix/087853dbd558cef05761 to your computer and use it in GitHub Desktop.
Save fishnix/087853dbd558cef05761 to your computer and use it in GitHub Desktop.
Chef Node Info
####
#
# Script to pull and display useful node information. OHAI!
# Author: E Camden Fisher <[email protected]>
####
# require 'formatador'
require 'colorize'
require 'pp'
args = ARGV[2..-1].join(' ')
ignore = []
c = clients.all.map { |c| c.name unless c.name =~ /-validator$/ }.compact.sort
n = nodes.all.map { |n| n.name unless n.name =~ /-validator$/ }.compact.sort
unless c - n
puts "Dangling node or client: #{c - n}"
end
dem_nodes = Hash.new
nodes.all do |n|
nom = n.name
dem_nodes[nom] = {}
dem_nodes[nom][:checked_in] = n[:ohai_time].nil? ? nil : n[:ohai_time]
dem_nodes[nom][:age] = n[:ohai_time].nil? ? nil : ((Time.now - Time.at(n[:ohai_time])) / 60).to_i
dem_nodes[nom][:attributes] = n.attributes.nil? ? [] : n.attributes
dem_nodes[nom][:roles] = n[:roles].nil? ? [] : n[:roles]
dem_nodes[nom][:environment] = n.chef_environment.nil? ? '' : n.chef_environment
dem_nodes[nom][:chef_version] = n[:chef_packages].nil? ? 'unknown' : n[:chef_packages][:chef][:version].to_s
end
# Formatador.display_table(dem_nodes.map { |n,v|
# { name: n.yellow,
# age: v[:age],
# roles: v[:roles].join(', ').blue,
# environment: v[:environment].light_red
# }
# }.sort { |a,b| b[:age] <=> a[:age] })
checked_in = dem_nodes.select { |_n,v| !v[:checked_in].nil? }.keys
never_checked_in = dem_nodes.select { |n,v| (v[:checked_in].nil? || v[:age] > 480) && !ignore.include?(n) }.keys
puts "\n--------- Nodes by last Check-in ---------".white unless checked_in.length == 0
checked_in.sort! { |a,b| dem_nodes[b][:age] <=> dem_nodes[a][:age] }
checked_in.each do |n|
age = dem_nodes[n][:age]
version = dem_nodes[n][:chef_version]
puts "#{n.yellow}(#{version.to_s.light_blue})\t #{age.to_s.light_blue} minutes ago #{dem_nodes[n][:environment].light_red}\t#{dem_nodes[n][:roles].join(',').blue}"
end
puts "\n--------- Nodes by Role ---------".white unless checked_in.length == 0
dem_nodes.map { |n,v| v[:roles] }.flatten.uniq.each do |r|
nodes = dem_nodes.select { |_n,v| v[:roles].include?(r) }.keys.join(' ').to_s.yellow
puts "#{r.blue}:\t\t#{nodes}"
end
puts "\n--------- Nodes by Environment ---------".white unless checked_in.length == 0
dem_nodes.map { |_n,v| v[:environment] }.uniq.each do |e|
nodes = dem_nodes.select { |_n,v| v[:environment].include?(e) }.keys.join(' ').to_s.yellow
puts "#{e.light_red}:\t#{nodes}"
end
puts "\n--------- Nodes by Instance Type ----------".white unless dem_nodes.length == 0
instance_types = dem_nodes.map do |_n,v|
ec2 = v[:attributes]['ec2']
ec2.nil? || ec2['instance_type'].nil? ? 'unknown' : ec2['instance_type']
end
instance_types.uniq.each do |i|
nodes = dem_nodes.select { |_n,v|
ec2 = v[:attributes]['ec2']
if (ec2.nil? || ec2['instance_type'].nil?) && i == 'unknown'
true
elsif !(ec2.nil? || ec2['instance_type'].nil?)
ec2['instance_type'].include?(i)
end
}.keys
puts "#{i.light_red} (#{nodes.length}):\t#{nodes.join(' ').to_s.yellow}"
end
puts "\n---- Nodes by Environment by Role ----------".white unless dem_nodes.length == 0
dem_nodes.map { |_n,v| v[:environment] }.uniq.each do |e|
nodes = dem_nodes.select { |_n,v| v[:environment].include?(e) }
puts "#{e.red}:"
nodes.map { |_n,v| v[:roles] }.flatten.uniq.each do |r|
no = nodes.select { |_n,v| v[:roles].include?(r) }.keys.join(' ').to_s.yellow
puts "\t#{r.blue}:\t\t#{no}"
end
puts "\n"
end
puts "\n---- Checked In more that 8 hours ago ----".white unless never_checked_in.length == 0
never_checked_in.each do |n|
e = dem_nodes[n][:environment]
r = dem_nodes[n][:roles].join(', ')
puts "knife node delete #{n} -y #{args} && knife client delete #{n} -y #{args} \# (#{e}:#{r})".red
end
puts "\n---- Total Nodes: #{dem_nodes.length} ----".white
# dem_nodes.each do |k,v|
# puts "Node: #{k}"
# pp v[:attributes]
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment