knife-stalenodes 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/chef/knife/stalenodes.rb +107 -0
  2. metadata +63 -0
@@ -0,0 +1,107 @@
1
+ # Author:: Paul Mooring <paul@opscode.com>
2
+ # Copyright:: Copyright (c) 2012 Opscode, Inc.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ module KnifeSuppport
19
+ class Stalenodes < Chef::Knife
20
+ banner "knife stalenodes [options]"
21
+
22
+ # Don't lazy load or you'll get an error
23
+ require 'chef/environment'
24
+ require 'chef/node'
25
+ require 'chef/role'
26
+ require 'chef/api_client'
27
+ # Do lazy load this stuff
28
+ deps do
29
+ require 'highline'
30
+ require 'chef/search/query'
31
+ require 'chef/mixin/command'
32
+ end
33
+
34
+ option :reverse,
35
+ :short => "-r",
36
+ :long => "--reverse",
37
+ :description => "Reverses the search to return only nodes that have checked in recently",
38
+ :boolean => true
39
+
40
+ option :days,
41
+ :short => "-D DAYS",
42
+ :long => "--days DAYS",
43
+ :description => "The days since last check in",
44
+ :default => 0
45
+
46
+ option :hours,
47
+ :short => "-H HOURS",
48
+ :long => "--hours HOURS",
49
+ :description => "Adds hours to days since last check in",
50
+ :default => 1
51
+
52
+ option :minutes,
53
+ :short => "-m MINUTES",
54
+ :long => "--minutes MINUTES",
55
+ :description => "Adds minutes to hours since last check in",
56
+ :default => 0
57
+
58
+
59
+ def calculate_time
60
+ seconds = config[:days].to_i * 86400 + config[:hours].to_i * 3600 + config[:minutes].to_i * 60
61
+
62
+ return Time.now.to_i - seconds
63
+ end
64
+
65
+ def get_query
66
+ if config[:reverse]
67
+ "ohai_time:[#{calculate_time} TO *]"
68
+ else
69
+ "ohai_time:[* TO #{calculate_time}]"
70
+ end
71
+ end
72
+
73
+ def check_last_run_time(time)
74
+ diff = Time.now.to_i - time
75
+ minutes = (diff / 60)
76
+ days = (minutes / 60 / 24)
77
+
78
+ case
79
+ when diff < 3600
80
+ {
81
+ :color => :green,
82
+ :text => "#{minutes} minute#{minutes == 1 ? ' ' : 's'}"
83
+ }
84
+ when diff > 86400
85
+ {
86
+ :color => :red,
87
+ :text => "#{days} day#{days == 1 ? ' ' : 's'}"
88
+ }
89
+ else
90
+ {
91
+ :color => :yellow,
92
+ :text => "#{minutes / 60} hours"
93
+ }
94
+ end
95
+ end
96
+
97
+ def run
98
+ query = Chef::Search::Query.new
99
+
100
+ query.search(:node, get_query).first.each do |node|
101
+ # nodes.each do |node|
102
+ msg = check_last_run_time(node['ohai_time'].to_i)
103
+ HighLine.new.say "#{@ui.color(msg[:text], msg[:color])} ago: #{node.name}"
104
+ end
105
+ end
106
+ end
107
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: knife-stalenodes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Paul Mooring
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-31 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: chef
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: Knife plugin for listing nodes that have not checked in
31
+ email:
32
+ - paul@opscode.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - lib/chef/knife/stalenodes.rb
38
+ homepage: https://github.com/paulmooring/knife-stalenodes
39
+ licenses: []
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 1.8.23
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: Knife plugin for listing nodes that have not checked in
62
+ test_files: []
63
+ has_rdoc: