/* steve jansen */

// another day in paradise hacking code and more

Finding Chef Nodes Bootstrapped in the Last X Hours

| Comments

I needed to write a script to garbage collect old nodes in Chef related to auto-scaling groups.

I decided to search for nodes bootstrapped in the last X hours.

I experimented with ways to find nodes that have been up for less than X hours. In this example, I search for nodes that have been up for 8 hours or less. Of course, this assumes you never restart your nodes:

1
knife exec -E 'search(:node, "uptime_seconds:[0 TO #{ 8 * 60 * 60 }]") { |n| puts n.name }'

I also tried finding nodes that converged in the last 8 hours (which would have to be combined with some other filter of course):

1
knife exec -E 'b = Time.now.to_i; a = (b - (8*60*60)).to_i; search(:node, "ohai_time:[#{a} TO #{b}]") { |n| puts n.name }'

Overall, I think the easiest option is to just set a node attribute like ‘bootstrap_date’ at bootstrap (or set it if it’s nil). This would be a clearcut way to find out how old a node truly is.

One of my colleagues pointed out that Chef Metal sets a very handy node['metal']['location']['allocated_at'] attribute that gets the job done if you are spinning up new nodes with metal.

Comments