opscode-ohai 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. data/LICENSE +201 -0
  2. data/README.rdoc +56 -0
  3. data/Rakefile +58 -0
  4. data/bin/ohai +58 -0
  5. data/lib/ohai.rb +27 -0
  6. data/lib/ohai/config.rb +118 -0
  7. data/lib/ohai/exception.rb +23 -0
  8. data/lib/ohai/log.rb +89 -0
  9. data/lib/ohai/log/formatter.rb +57 -0
  10. data/lib/ohai/mixin/command.rb +198 -0
  11. data/lib/ohai/mixin/from_file.rb +36 -0
  12. data/lib/ohai/plugins/command.rb +19 -0
  13. data/lib/ohai/plugins/darwin/hostname.rb +20 -0
  14. data/lib/ohai/plugins/darwin/kernel.rb +31 -0
  15. data/lib/ohai/plugins/darwin/network.rb +154 -0
  16. data/lib/ohai/plugins/darwin/platform.rb +34 -0
  17. data/lib/ohai/plugins/darwin/ps.rb +21 -0
  18. data/lib/ohai/plugins/darwin/ssh_host_key.rb +24 -0
  19. data/lib/ohai/plugins/ec2.rb +41 -0
  20. data/lib/ohai/plugins/hostname.rb +23 -0
  21. data/lib/ohai/plugins/kernel.rb +24 -0
  22. data/lib/ohai/plugins/keys.rb +20 -0
  23. data/lib/ohai/plugins/languages.rb +19 -0
  24. data/lib/ohai/plugins/linux/block_device.rb +36 -0
  25. data/lib/ohai/plugins/linux/cpu.rb +58 -0
  26. data/lib/ohai/plugins/linux/filesystem.rb +55 -0
  27. data/lib/ohai/plugins/linux/hostname.rb +20 -0
  28. data/lib/ohai/plugins/linux/kernel.rb +31 -0
  29. data/lib/ohai/plugins/linux/lsb.rb +36 -0
  30. data/lib/ohai/plugins/linux/memory.rb +81 -0
  31. data/lib/ohai/plugins/linux/network.rb +103 -0
  32. data/lib/ohai/plugins/linux/platform.rb +41 -0
  33. data/lib/ohai/plugins/linux/ps.rb +21 -0
  34. data/lib/ohai/plugins/linux/ssh_host_key.rb +24 -0
  35. data/lib/ohai/plugins/linux/uptime.rb +26 -0
  36. data/lib/ohai/plugins/network.rb +48 -0
  37. data/lib/ohai/plugins/ohai_time.rb +19 -0
  38. data/lib/ohai/plugins/os.rb +34 -0
  39. data/lib/ohai/plugins/platform.rb +23 -0
  40. data/lib/ohai/plugins/ruby.rb +33 -0
  41. data/lib/ohai/plugins/uptime.rb +42 -0
  42. data/lib/ohai/system.rb +163 -0
  43. data/spec/ohai/log/log_formatter_spec.rb +50 -0
  44. data/spec/ohai/log_spec.rb +63 -0
  45. data/spec/ohai/mixin/from_file_spec.rb +53 -0
  46. data/spec/ohai/plugins/darwin/hostname_spec.rb +34 -0
  47. data/spec/ohai/plugins/darwin/kernel_spec.rb +35 -0
  48. data/spec/ohai/plugins/darwin/platform_spec.rb +67 -0
  49. data/spec/ohai/plugins/hostname_spec.rb +33 -0
  50. data/spec/ohai/plugins/kernel_spec.rb +41 -0
  51. data/spec/ohai/plugins/linux/cpu_spec.rb +128 -0
  52. data/spec/ohai/plugins/linux/hostname_spec.rb +34 -0
  53. data/spec/ohai/plugins/linux/kernel_spec.rb +31 -0
  54. data/spec/ohai/plugins/linux/lsb_spec.rb +59 -0
  55. data/spec/ohai/plugins/linux/platform_spec.rb +51 -0
  56. data/spec/ohai/plugins/linux/uptime_spec.rb +61 -0
  57. data/spec/ohai/plugins/ohai_time_spec.rb +46 -0
  58. data/spec/ohai/plugins/os_spec.rb +58 -0
  59. data/spec/ohai/plugins/platform_spec.rb +56 -0
  60. data/spec/ohai/plugins/ruby_spec.rb +49 -0
  61. data/spec/ohai/system_spec.rb +130 -0
  62. data/spec/ohai_spec.rb +27 -0
  63. data/spec/rcov.opts +2 -0
  64. data/spec/spec.opts +2 -0
  65. data/spec/spec_helper.rb +47 -0
  66. metadata +138 -0
@@ -0,0 +1,48 @@
1
+ #
2
+ # Author:: Adam Jacob (<adam@opscode.com>)
3
+ # Copyright:: Copyright (c) 2008 Opscode, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ network Mash.new unless network
20
+ network[:interfaces] = Mash.new unless network[:interfaces]
21
+
22
+ require_plugin "hostname"
23
+ require_plugin "#{os}::network"
24
+
25
+ def find_ip_and_mac(addresses)
26
+ ip = nil; mac = nil
27
+ addresses.each do |addr|
28
+ ip = addr["address"] if addr["family"].eql?("inet")
29
+ mac = addr["address"] if addr["family"].eql?("lladdr")
30
+ break if (ip and mac)
31
+ end
32
+ [ip, mac]
33
+ end
34
+
35
+ if attribute?("default_interface")
36
+ im = find_ip_and_mac(network["interfaces"][iface]["addresses"])
37
+ ipaddress im.shift
38
+ macaddress im.shift
39
+ else
40
+ network["interfaces"].keys.each do |iface|
41
+ if network["interfaces"][iface]["encapsulation"].eql?("Ethernet")
42
+ im = find_ip_and_mac(network["interfaces"][iface]["addresses"])
43
+ ipaddress im.shift
44
+ macaddress im.shift
45
+ return if (ipaddress and macaddress)
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,19 @@
1
+ #
2
+ # Author:: Adam Jacob (<adam@opscode.com>)
3
+ # Copyright:: Copyright (c) 2008 Opscode, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ ohai_time Time.now.to_f
@@ -0,0 +1,34 @@
1
+ #
2
+ # Author:: Adam Jacob (<adam@opscode.com>)
3
+ # Copyright:: Copyright (c) 2008 Opscode, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require_plugin 'ruby'
20
+ require_plugin 'kernel'
21
+
22
+ case languages[:ruby][:host_os]
23
+ when /darwin(.+)$/
24
+ os "darwin"
25
+ when /linux/
26
+ os "linux"
27
+ when /freebsd(.+)$/
28
+ os "freebsd"
29
+ else
30
+ os languages[:ruby][:host_os]
31
+ end
32
+
33
+ os_version kernel[:release]
34
+
@@ -0,0 +1,23 @@
1
+ #
2
+ # Author:: Adam Jacob (<adam@opscode.com>)
3
+ # Copyright:: Copyright (c) 2008 Opscode, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require_plugin "#{os}::platform"
20
+
21
+ platform os unless attribute?("platform")
22
+
23
+ platform_version os_version unless attribute?("platform_version")
@@ -0,0 +1,33 @@
1
+ #
2
+ # Author:: Adam Jacob (<adam@opscode.com>)
3
+ # Copyright:: Copyright (c) 2008 Opscode, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require_plugin "languages"
20
+
21
+ languages[:ruby] = Mash.new
22
+
23
+ languages[:ruby][:platform] = RUBY_PLATFORM
24
+ languages[:ruby][:version] = RUBY_VERSION
25
+ languages[:ruby][:release_date] = RUBY_RELEASE_DATE
26
+ languages[:ruby][:target] = ::Config::CONFIG['target']
27
+ languages[:ruby][:target_cpu] = ::Config::CONFIG['target_cpu']
28
+ languages[:ruby][:target_vendor] = ::Config::CONFIG['target_vendor']
29
+ languages[:ruby][:target_os] = ::Config::CONFIG['target_os']
30
+ languages[:ruby][:host] = ::Config::CONFIG['host']
31
+ languages[:ruby][:host_cpu] = ::Config::CONFIG['host_cpu']
32
+ languages[:ruby][:host_os] = ::Config::CONFIG['host_os']
33
+ languages[:ruby][:host_vendor] = ::Config::CONFIG['host_vendor']
@@ -0,0 +1,42 @@
1
+ #
2
+ # Author:: Adam Jacob (<adam@opscode.com>)
3
+ # Copyright:: Copyright (c) 2008 Opscode, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ def _seconds_to_human(seconds)
20
+ days = seconds.to_i / 86400
21
+ seconds -= 86400 * days
22
+
23
+ hours = seconds.to_i / 3600
24
+ seconds -= 3600 * hours
25
+
26
+ minutes = seconds.to_i / 60
27
+ seconds -= 60 * minutes
28
+
29
+ if days > 1
30
+ return sprintf("%d days %02d hours %02d minutes %02d seconds", days, hours, minutes, seconds)
31
+ elsif days == 1
32
+ return sprintf("%d day %02d hours %02d minutes %02d seconds", days, hours, minutes, seconds)
33
+ elsif hours > 0
34
+ return sprintf("%d hours %02d minutes %02d seconds", hours, minutes, seconds)
35
+ elsif minutes > 0
36
+ return sprintf("%d minutes %02d seconds", minutes, seconds)
37
+ else
38
+ return sprintf("%02d seconds", seconds)
39
+ end
40
+ end
41
+
42
+
@@ -0,0 +1,163 @@
1
+ #
2
+ # Author:: Adam Jacob (<adam@opscode.com>)
3
+ # Copyright:: Copyright (c) 2008 Opscode, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'rubygems'
20
+ require 'extlib'
21
+ require 'ohai/log'
22
+ require 'ohai/mixin/from_file'
23
+ require 'ohai/mixin/command'
24
+ require 'json'
25
+
26
+ module Ohai
27
+ class System
28
+ attr_accessor :data, :seen_plugins
29
+
30
+ include Ohai::Mixin::FromFile
31
+ include Ohai::Mixin::Command
32
+
33
+ def initialize
34
+ @data = Mash.new
35
+ @seen_plugins = Hash.new
36
+ end
37
+
38
+ def [](key)
39
+ @data[key]
40
+ end
41
+
42
+ def []=(key, value)
43
+ @data[key] = value
44
+ end
45
+
46
+ def each(&block)
47
+ @data.each do |key, value|
48
+ block.call(key, value)
49
+ end
50
+ end
51
+
52
+ def attribute?(name)
53
+ Ohai::Log.debug("Attribute #{name} is #{@data.has_key?(name)}")
54
+ @data.has_key?(name)
55
+ end
56
+
57
+ def set(name, *value)
58
+ set_attribute(name, *value)
59
+ end
60
+
61
+ def from(cmd)
62
+ status, stdout, stderr = run_command(:command => cmd)
63
+ stdout.chomp!
64
+ end
65
+
66
+ # Set the value equal to the stdout of the command, plus run through a regex - the first piece of match data is the value.
67
+ def from_with_regex(cmd, *regex_list)
68
+ regex_list.flatten.each do |regex|
69
+ status, stdout, stderr = run_command(:command => cmd)
70
+ stdout.chomp!
71
+ md = stdout.match(regex)
72
+ return md[1]
73
+ end
74
+ end
75
+
76
+ def set_attribute(name, *value)
77
+ Ohai::Log.debug("Setting attribute #{name} to #{value.inspect}")
78
+ @data[name] = *value
79
+ @data[name]
80
+ end
81
+
82
+ def get_attribute(name)
83
+ Ohai::Log.debug("Getting attribute #{name}, value #{@data[name].inspect}")
84
+ @data[name]
85
+ end
86
+
87
+ def all_plugins
88
+ require_plugin('os')
89
+
90
+ Ohai::Config[:plugin_path].each do |path|
91
+ [
92
+ Dir[File.join(path, '*')],
93
+ Dir[File.join(path, @data[:os], '**', '*')]
94
+ ].flatten.each do |file|
95
+ file_regex = Regexp.new("#{path}#{File::SEPARATOR}(.+).rb$")
96
+ md = file_regex.match(file)
97
+ if md
98
+ plugin_name = md[1].gsub(File::SEPARATOR, "::")
99
+ require_plugin(plugin_name) unless @seen_plugins.has_key?(plugin_name)
100
+ end
101
+ end
102
+ end
103
+ end
104
+
105
+ def require_plugin(plugin_name, force=false)
106
+ unless force
107
+ return true if @seen_plugins[plugin_name]
108
+ end
109
+
110
+ filename = "#{plugin_name.gsub("::", File::SEPARATOR)}.rb"
111
+
112
+ Ohai::Config[:plugin_path].each do |path|
113
+ check_path = File.expand_path(File.join(path, filename))
114
+ begin
115
+ @seen_plugins[plugin_name] = true
116
+ Ohai::Log.info("Loading plugin #{plugin_name}")
117
+ from_file(check_path)
118
+ return true
119
+ rescue IOError => e
120
+ Ohai::Log.debug("No #{plugin_name} at #{check_path}")
121
+ rescue Exception,Errno::ENOENT => e
122
+ Ohai::Log.debug("Plugin #{plugin_name} threw exception #{e.inspect}")
123
+ end
124
+ end
125
+ end
126
+
127
+ # Sneaky! Lets us stub out require_plugin when testing plugins, but still
128
+ # call the real require_plugin to kick the whole thing off.
129
+ alias :_require_plugin :require_plugin
130
+
131
+ # Serialize this object as a hash
132
+ def to_json(*a)
133
+ output = @data.clone
134
+ output["json_class"] = self.class.name
135
+ output.to_json(*a)
136
+ end
137
+
138
+ # Pretty Print this object as JSON
139
+ def json_pretty_print
140
+ JSON.pretty_generate(@data)
141
+ end
142
+
143
+ # Create an Ohai::System from JSON
144
+ def self.json_create(o)
145
+ ohai = new
146
+ o.each do |key, value|
147
+ ohai.data[key] = value unless key == "json_class"
148
+ end
149
+ ohai
150
+ end
151
+
152
+ def method_missing(name, *args)
153
+ return get_attribute(name) if args.length == 0
154
+
155
+ set_attribute(name, *args)
156
+ end
157
+
158
+ private
159
+ def load_plugin_file
160
+
161
+ end
162
+ end
163
+ end
@@ -0,0 +1,50 @@
1
+ #
2
+ # Author:: Adam Jacob (<adam@opscode.com>)
3
+ # Copyright:: Copyright (c) 2008 Opscode, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'time'
20
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
21
+
22
+ describe Ohai::Log::Formatter do
23
+ before(:each) do
24
+ @formatter = Ohai::Log::Formatter.new
25
+ end
26
+
27
+ it "should print raw strings with msg2str(string)" do
28
+ @formatter.msg2str("nuthin new").should == "nuthin new"
29
+ end
30
+
31
+ it "should format exceptions properly with msg2str(e)" do
32
+ e = IOError.new("legendary roots crew")
33
+ @formatter.msg2str(e).should == "legendary roots crew (IOError)\n"
34
+ end
35
+
36
+ it "should format random objects via inspect with msg2str(Object)" do
37
+ @formatter.msg2str([ "black thought", "?uestlove" ]).should == '["black thought", "?uestlove"]'
38
+ end
39
+
40
+ it "should return a formatted string with call" do
41
+ time = Time.new
42
+ Ohai::Log::Formatter.show_time = true
43
+ @formatter.call("monkey", time, "test", "mos def").should == "[#{time.rfc2822}] monkey: mos def\n"
44
+ end
45
+
46
+ it "should allow you to turn the time on and off in the output" do
47
+ Ohai::Log::Formatter.show_time = false
48
+ @formatter.call("monkey", Time.new, "test", "mos def").should == "monkey: mos def\n"
49
+ end
50
+ end
@@ -0,0 +1,63 @@
1
+ #
2
+ # Author:: Adam Jacob (<adam@opscode.com>)
3
+ # Copyright:: Copyright (c) 2008 Opscode, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'tempfile'
20
+ require 'logger'
21
+
22
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
23
+
24
+ describe Ohai::Log do
25
+ it "should accept regular options to Logger.new via init" do
26
+ tf = Tempfile.new("chef-test-log")
27
+ tf.open
28
+ lambda { Ohai::Log.init(STDOUT) }.should_not raise_error
29
+ lambda { Ohai::Log.init(tf) }.should_not raise_error
30
+ end
31
+
32
+ it "should set the log level with :debug, :info, :warn, :error, or :fatal" do
33
+ levels = {
34
+ :debug => Logger::DEBUG,
35
+ :info => Logger::INFO,
36
+ :warn => Logger::WARN,
37
+ :error => Logger::ERROR,
38
+ :fatal => Logger::FATAL
39
+ }
40
+ levels.each do |symbol, constant|
41
+ Ohai::Log.level(symbol)
42
+ Ohai::Log.logger.level.should == constant
43
+ end
44
+ end
45
+
46
+ it "should raise an ArgumentError if you try and set the level to something strange" do
47
+ lambda { Ohai::Log.level(:the_roots) }.should raise_error(ArgumentError)
48
+ end
49
+
50
+ it "should pass other method calls directly to logger" do
51
+ Ohai::Log.level(:debug)
52
+ Ohai::Log.should be_debug
53
+ lambda { Ohai::Log.debug("Gimme some sugar!") }.should_not raise_error
54
+ end
55
+
56
+ it "should default to STDOUT if init is called with no arguments" do
57
+ logger_mock = mock(Logger, :null_object => true)
58
+ Logger.stub!(:new).and_return(logger_mock)
59
+ Logger.should_receive(:new).with(STDOUT).and_return(logger_mock)
60
+ Ohai::Log.init
61
+ end
62
+
63
+ end