chef_knives 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,6 @@
1
+ === 1.0.0 / 2009-09-09
2
+
3
+ * 1 major enhancement
4
+
5
+ * Birthday!
6
+
data/Manifest.txt ADDED
@@ -0,0 +1,7 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ bin/chef_node_fs_usage
6
+ bin/chef_node_mem_usage
7
+ lib/chef/knives.rb
data/README.txt ADDED
@@ -0,0 +1,14 @@
1
+ REQUIREMENTS
2
+ ------------
3
+
4
+ rubygems and the following gems installed:
5
+
6
+ chef
7
+ json
8
+ couchrest
9
+ choice
10
+
11
+ All the scripts try to connect to http://localhost:5984/chef (Chef CouchDB database).
12
+ Either run them from the chef-server or use SSH to create a tunnel:
13
+
14
+ ssh -L5984:localhost:5984 -N chef-server.example.com
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require './lib/chef/knives.rb'
6
+
7
+ Hoe.new('chef_knives', Chef::Knives::VERSION) do |p|
8
+ p.developer('Sergio Rubio', 'sergio@rubio.name')
9
+ p.summary = 'Chef addons to become a cooking master!'
10
+ p.description = 'Unofficial Chef Server scripts and related stuff. http://wiki.opscode.com/display/chef'
11
+ p.url = 'http://github.com/rubiojr/chef-knives'
12
+ end
13
+
14
+ # vim: syntax=Ruby
@@ -0,0 +1,69 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'couchrest'
4
+ require 'chef'
5
+ require 'choice'
6
+ require 'pp'
7
+
8
+ CHEF_DB_URL = 'http://localhost:5984/chef'
9
+
10
+ def main
11
+ Choice.options do
12
+ header ''
13
+ header 'Available options:'
14
+
15
+ option :help do
16
+ long '--help'
17
+ short '-h'
18
+ desc 'Show this message'
19
+ action do
20
+ Choice.help
21
+ exit
22
+ end
23
+ end
24
+
25
+ option :usage_threshold do
26
+ long '--usage-threshold'
27
+ short '-t'
28
+ desc 'Print only the filesystems with usage percent greater than this value'
29
+ end
30
+
31
+ option :match do
32
+ default '.*'
33
+ long '--match'
34
+ short '-m'
35
+ desc 'Match only the nodes with an FQDN matching this value'
36
+ end
37
+ end
38
+
39
+ db = CouchRest.database(CHEF_DB_URL)
40
+
41
+ db.documents['rows'].each do |doc|
42
+ if doc['id'] =~ /node_/
43
+ node = db.get(doc['id'])
44
+ next if node.fqdn !~ /#{Choice.choices[:match]}/
45
+ matching_fs = []
46
+ node.filesystem.each do |fsname,fsattrs|
47
+ if not (%w[proc binfmt_misc sysfs tmpfs devpts rpc_pipefs].include? fsattrs['fs_type'])
48
+ if Choice.choices[:usage_threshold]
49
+ uthres = Choice.choices[:usage_threshold].to_i
50
+ usage = fsattrs['percent_used'].chomp.strip.gsub('%','').to_i
51
+ (matching_fs << " #{fsname}".ljust(50) + fsattrs['percent_used']) if usage > uthres
52
+ else
53
+ matching_fs << " #{fsname}".ljust(50) + fsattrs['percent_used']
54
+ end
55
+ end
56
+ end
57
+ if not matching_fs.empty?
58
+ puts node.fqdn
59
+ matching_fs.each do |fs|
60
+ puts fs
61
+ end
62
+ puts
63
+ end
64
+ end
65
+ end
66
+
67
+ end
68
+
69
+ main
@@ -0,0 +1,110 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # List Filesystem Usage for every chef node that matches the hostname
4
+ # provided. If no argument is provided, matches all the nodes.
5
+ #
6
+ # Example:
7
+ # chef_node_fs_usage '*.cdn.example.com'
8
+ # (matches every node whose fqdn ends with cdn.example.com)
9
+ #
10
+ require 'rubygems'
11
+ require 'couchrest'
12
+ require 'chef'
13
+ require 'choice'
14
+ require 'pp'
15
+
16
+ CHEF_DB_URL = 'http://localhost:5984/chef'
17
+
18
+ def humanize_bytes(bytes)
19
+ return "0 Bytes" if bytes == 0
20
+ m = bytes.to_i
21
+ units = %w[Bits Bytes MB GB TB PB]
22
+ while (m/1024.0) >= 1
23
+ m = m/1024.0
24
+ units.shift
25
+ end
26
+ return m.round.to_s + " #{units[0]}"
27
+ end
28
+
29
+ def main
30
+ Choice.options do
31
+ header ''
32
+ header 'Available options:'
33
+
34
+ option :help do
35
+ long '--help'
36
+ short '-h'
37
+ desc 'Show this message'
38
+ action do
39
+ Choice.help
40
+ exit
41
+ end
42
+ end
43
+
44
+ option :swap_usage_threshold do
45
+ long '--swap-usage-threshold'
46
+ short '-s'
47
+ desc 'Print only the hosts with swap usage percent greater than this value'
48
+ end
49
+
50
+ option :mem_usage_threshold do
51
+ long '--mem-usage-threshold'
52
+ short '-u'
53
+ desc 'Print only the hosts with memory usage percent greater than this value'
54
+ end
55
+
56
+ option :match do
57
+ default '.*'
58
+ long '--match'
59
+ short '-m'
60
+ desc 'Match only the nodes with an FQDN matching this value'
61
+ end
62
+ end
63
+
64
+ db = CouchRest.database(CHEF_DB_URL)
65
+
66
+ db.documents['rows'].each do |doc|
67
+ if doc['id'] =~ /node_/
68
+ node = db.get(doc['id'])
69
+ next if node.fqdn !~ /#{Choice.choices[:match]}/
70
+ free_mem = node.memory.free.strip.gsub("kB",'').to_i
71
+ total_mem = node.memory.total.strip.gsub("kB",'').to_i
72
+ used_mem = total_mem - free_mem
73
+ free_mem_per = (free_mem * 100 / total_mem)
74
+ used_mem_per = 100 - (free_mem * 100 / total_mem)
75
+ free_swap = node.memory.swap.free.strip.gsub("kB",'').to_i
76
+ total_swap = node.memory.swap.total.strip.gsub("kB",'').to_i
77
+ used_swap = total_swap - free_swap
78
+ # The host may not have swap!
79
+ if total_swap == 0
80
+ used_swap_per = 100
81
+ free_swap_per = 0
82
+ else
83
+ used_swap_per = 100 - (free_swap * 100 / total_swap)
84
+ free_swap_per = (free_swap * 100 / total_swap)
85
+ end
86
+
87
+ if Choice.choices[:mem_usage_threshold] and used_mem_per <= Choice.choices[:mem_usage_threshold].to_i
88
+ next
89
+ end
90
+
91
+ if Choice.choices[:swap_usage_threshold] and used_swap_per <= Choice.choices[:swap_usage_threshold].to_i
92
+ next
93
+ end
94
+
95
+ puts node.fqdn
96
+
97
+ puts " Mem Free:".ljust(20) + "#{humanize_bytes(free_mem * 1024)} (#{free_mem_per}%)"
98
+ puts " Mem Used:".ljust(20) + "#{humanize_bytes(used_mem * 1024)} (#{used_mem_per}%)"
99
+ puts " Mem Total:".ljust(20) + "#{humanize_bytes(total_mem * 1024)}"
100
+
101
+ puts " Swap Free:".ljust(20) + "#{humanize_bytes(free_swap * 1024)} (#{free_swap_per}%)"
102
+ puts " Swap Used:".ljust(20) + "#{humanize_bytes(used_swap * 1024)} (#{used_swap_per}%)"
103
+ puts " Swap Total:".ljust(20) + "#{humanize_bytes(total_swap * 1024)}"
104
+
105
+ end
106
+ end
107
+
108
+ end
109
+
110
+ main
@@ -0,0 +1,5 @@
1
+ module Chef
2
+ module Knives
3
+ VERSION = '0.1'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chef_knives
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - Sergio Rubio
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-09 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.3.3
24
+ version:
25
+ description: Unofficial Chef Server scripts and related stuff. http://wiki.opscode.com/display/chef
26
+ email:
27
+ - sergio@rubio.name
28
+ executables:
29
+ - chef_node_fs_usage
30
+ - chef_node_mem_usage
31
+ extensions: []
32
+
33
+ extra_rdoc_files:
34
+ - History.txt
35
+ - Manifest.txt
36
+ - README.txt
37
+ files:
38
+ - History.txt
39
+ - Manifest.txt
40
+ - README.txt
41
+ - Rakefile
42
+ - bin/chef_node_fs_usage
43
+ - bin/chef_node_mem_usage
44
+ - lib/chef/knives.rb
45
+ has_rdoc: true
46
+ homepage: http://github.com/rubiojr/chef-knives
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options:
51
+ - --main
52
+ - README.txt
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ requirements: []
68
+
69
+ rubyforge_project: chef_knives
70
+ rubygems_version: 1.3.3
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Chef addons to become a cooking master!
74
+ test_files: []
75
+