opscode-ohai 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,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
+ module Ohai
20
+ module Exception
21
+ class Exec < RuntimeError; end
22
+ end
23
+ end
@@ -0,0 +1,89 @@
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 'ohai/config'
20
+ require 'ohai/log/formatter'
21
+ require 'logger'
22
+
23
+ module Ohai
24
+ class Log
25
+
26
+ @logger = nil
27
+
28
+ class << self
29
+ attr_accessor :logger #:nodoc
30
+
31
+ # Use Ohai::Logger.init when you want to set up the logger manually. Arguments to this method
32
+ # get passed directly to Logger.new, so check out the documentation for the standard Logger class
33
+ # to understand what to do here.
34
+ #
35
+ # If this method is called with no arguments, it will log to STDOUT at the :info level.
36
+ #
37
+ # It also configures the Logger instance it creates to use the custom Ohai::Log::Formatter class.
38
+ def init(*opts)
39
+ if opts.length == 0
40
+ @logger = Logger.new(STDOUT)
41
+ else
42
+ @logger = Logger.new(*opts)
43
+ end
44
+ @logger.formatter = Ohai::Log::Formatter.new()
45
+ level(Ohai::Config.log_level)
46
+ end
47
+
48
+ # Sets the level for the Logger object by symbol. Valid arguments are:
49
+ #
50
+ # :debug
51
+ # :info
52
+ # :warn
53
+ # :error
54
+ # :fatal
55
+ #
56
+ # Throws an ArgumentError if you feed it a bogus log level.
57
+ def level(loglevel)
58
+ init() unless @logger
59
+ case loglevel
60
+ when :debug
61
+ @logger.level = Logger::DEBUG
62
+ when :info
63
+ @logger.level = Logger::INFO
64
+ when :warn
65
+ @logger.level = Logger::WARN
66
+ when :error
67
+ @logger.level = Logger::ERROR
68
+ when :fatal
69
+ @logger.level = Logger::FATAL
70
+ else
71
+ raise ArgumentError, "Log level must be one of :debug, :info, :warn, :error, or :fatal"
72
+ end
73
+ end
74
+
75
+ # Passes any other method calls on directly to the underlying Logger object created with init. If
76
+ # this method gets hit before a call to Ohai::Logger.init has been made, it will call
77
+ # Ohai::Logger.init() with no arguments.
78
+ def method_missing(method_symbol, *args)
79
+ init() unless @logger
80
+ if args.length > 0
81
+ @logger.send(method_symbol, *args)
82
+ else
83
+ @logger.send(method_symbol)
84
+ end
85
+ end
86
+
87
+ end # class << self
88
+ end
89
+ end
@@ -0,0 +1,57 @@
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 'logger'
20
+ require 'time'
21
+
22
+ module Ohai
23
+ class Log
24
+ class Formatter < Logger::Formatter
25
+ @@show_time = true
26
+
27
+ def self.show_time=(show=false)
28
+ @@show_time = show
29
+ end
30
+
31
+ # Prints a log message as '[time] severity: message' if Ohai::Log::Formatter.show_time == true.
32
+ # Otherwise, doesn't print the time.
33
+ def call(severity, time, progname, msg)
34
+ if @@show_time
35
+ sprintf("[%s] %s: %s\n", time.rfc2822(), severity, msg2str(msg))
36
+ else
37
+ sprintf("%s: %s\n", severity, msg2str(msg))
38
+ end
39
+ end
40
+
41
+ # Converts some argument to a Logger.severity() call to a string. Regular strings pass through like
42
+ # normal, Exceptions get formatted as "message (class)\nbacktrace", and other random stuff gets
43
+ # put through "object.inspect"
44
+ def msg2str(msg)
45
+ case msg
46
+ when ::String
47
+ msg
48
+ when ::Exception
49
+ "#{ msg.message } (#{ msg.class })\n" <<
50
+ (msg.backtrace || []).join("\n")
51
+ else
52
+ msg.inspect
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,198 @@
1
+ #
2
+ # Author:: Adam Jacob (<adam@opscode.com>)
3
+ # Copyright:: Copyright (c) 2008 Opscode, Inc.
4
+ # License:: GNU GPL, Version 3
5
+ #
6
+ # Copyright (C) 2008, Opscode Inc.
7
+ #
8
+ # This program is free software: you can redistribute it and/or modify
9
+ # it under the terms of the GNU General Public License as published by
10
+ # the Free Software Foundation, either version 3 of the License, or
11
+ # (at your option) any later version.
12
+ #
13
+ # This program is distributed in the hope that it will be useful,
14
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ # GNU General Public License for more details.
17
+ #
18
+ # You should have received a copy of the GNU General Public License
19
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
+ #
21
+
22
+ require 'ohai/exception'
23
+ require 'ohai/config'
24
+ require 'ohai/log'
25
+ require 'tmpdir'
26
+ require 'fcntl'
27
+ require 'etc'
28
+
29
+ module Ohai
30
+ module Mixin
31
+ module Command
32
+
33
+ def run_command(args={})
34
+ if args.has_key?(:creates)
35
+ if File.exists?(args[:creates])
36
+ Ohai::Log.debug("Skipping #{args[:command]} - creates #{args[:creates]} exists.")
37
+ return false
38
+ end
39
+ end
40
+
41
+ stdout_string = nil
42
+ stderr_string = nil
43
+
44
+ exec_processing_block = lambda do |pid, stdin, stdout, stderr|
45
+ stdin.close
46
+
47
+ stdout_string = stdout.gets(nil)
48
+ if stdout_string
49
+ Ohai::Log.debug("---- Begin #{args[:command]} STDOUT ----")
50
+ Ohai::Log.debug(stdout_string.strip)
51
+ Ohai::Log.debug("---- End #{args[:command]} STDOUT ----")
52
+ end
53
+ stderr_string = stderr.gets(nil)
54
+ if stderr_string
55
+ Ohai::Log.debug("---- Begin #{args[:command]} STDERR ----")
56
+ Ohai::Log.debug(stderr_string.strip)
57
+ Ohai::Log.debug("---- End #{args[:command]} STDERR ----")
58
+ end
59
+ end
60
+
61
+ args[:cwd] ||= Dir.tmpdir
62
+ unless File.directory?(args[:cwd])
63
+ raise Ohai::Exception::Exec, "#{args[:cwd]} does not exist or is not a directory"
64
+ end
65
+
66
+ status = nil
67
+ Dir.chdir(args[:cwd]) do
68
+ if args[:timeout]
69
+ begin
70
+ Timeout.timeout(args[:timeout]) do
71
+ status = popen4(args[:command], args, &exec_processing_block)
72
+ end
73
+ rescue Exception => e
74
+ Ohai::Log.error("#{args[:command_string]} exceeded timeout #{args[:timeout]}")
75
+ raise(e)
76
+ end
77
+ else
78
+ status = popen4(args[:command], args, &exec_processing_block)
79
+ end
80
+
81
+ args[:returns] ||= 0
82
+ if status.exitstatus != args[:returns]
83
+ raise Ohai::Exception::Exec, "#{args[:command_string]} returned #{status.exitstatus}, expected #{args[:returns]}"
84
+ else
85
+ Ohai::Log.debug("Ran #{args[:command_string]} (#{args[:command]}) returned #{status.exitstatus}")
86
+ end
87
+ end
88
+ return status, stdout_string, stderr_string
89
+ end
90
+
91
+ module_function :run_command
92
+
93
+ # This is taken directly from Ara T Howard's Open4 library, and then
94
+ # modified to suit the needs of Ohai. Any bugs here are most likely
95
+ # my own, and not Ara's.
96
+ #
97
+ # The original appears in external/open4.rb in it's unmodified form.
98
+ #
99
+ # Thanks, Ara.
100
+ def popen4(cmd, args={}, &b)
101
+
102
+ args[:user] ||= nil
103
+ unless args[:user].kind_of?(Integer)
104
+ args[:user] = Etc.getpwnam(args[:user]).uid if args[:user]
105
+ end
106
+ args[:group] ||= nil
107
+ unless args[:group].kind_of?(Integer)
108
+ args[:group] = Etc.getgrnam(args[:group]).gid if args[:group]
109
+ end
110
+ args[:environment] ||= nil
111
+
112
+ pw, pr, pe, ps = IO.pipe, IO.pipe, IO.pipe, IO.pipe
113
+
114
+ verbose = $VERBOSE
115
+ begin
116
+ $VERBOSE = nil
117
+ ps.last.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
118
+
119
+ cid = fork {
120
+ pw.last.close
121
+ STDIN.reopen pw.first
122
+ pw.first.close
123
+
124
+ pr.first.close
125
+ STDOUT.reopen pr.last
126
+ pr.last.close
127
+
128
+ pe.first.close
129
+ STDERR.reopen pe.last
130
+ pe.last.close
131
+
132
+ STDOUT.sync = STDERR.sync = true
133
+
134
+ if args[:user]
135
+ Process.euid = args[:user]
136
+ Process.uid = args[:user]
137
+ end
138
+
139
+ if args[:group]
140
+ Process.egid = args[:group]
141
+ Process.gid = args[:group]
142
+ end
143
+
144
+ if args[:environment]
145
+ args[:environment].each do |key,value|
146
+ ENV[key] = value
147
+ end
148
+ end
149
+
150
+ begin
151
+ if cmd.kind_of?(Array)
152
+ exec(*cmd)
153
+ else
154
+ exec(cmd)
155
+ end
156
+ raise 'forty-two'
157
+ rescue Exception => e
158
+ Marshal.dump(e, ps.last)
159
+ ps.last.flush
160
+ end
161
+ ps.last.close unless (ps.last.closed?)
162
+ exit!
163
+ }
164
+ ensure
165
+ $VERBOSE = verbose
166
+ end
167
+
168
+ [pw.first, pr.last, pe.last, ps.last].each{|fd| fd.close}
169
+
170
+ begin
171
+ e = Marshal.load ps.first
172
+ raise(Exception === e ? e : "unknown failure!")
173
+ rescue EOFError # If we get an EOF error, then the exec was successful
174
+ 42
175
+ ensure
176
+ ps.first.close
177
+ end
178
+
179
+ pw.last.sync = true
180
+
181
+ pi = [pw.last, pr.first, pe.first]
182
+
183
+ if b
184
+ begin
185
+ b[cid, *pi]
186
+ Process.waitpid2(cid).last
187
+ ensure
188
+ pi.each{|fd| fd.close unless fd.closed?}
189
+ end
190
+ else
191
+ [cid, pw.last, pr.first, pe.first]
192
+ end
193
+ end
194
+
195
+ module_function :popen4
196
+ end
197
+ end
198
+ end
@@ -0,0 +1,36 @@
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
+ module Ohai
20
+ module Mixin
21
+ module FromFile
22
+
23
+ # Loads a given ruby file, and runs instance_eval against it in the context of the current
24
+ # object.
25
+ #
26
+ # Raises an IOError if the file cannot be found, or is not readable.
27
+ def from_file(filename)
28
+ if File.exists?(filename) && File.readable?(filename)
29
+ self.instance_eval(IO.read(filename), filename, 1)
30
+ else
31
+ raise IOError, "Cannot open or read #{filename}!"
32
+ end
33
+ end
34
+ end
35
+ end
36
+ 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
+ command Mash.new
@@ -0,0 +1,20 @@
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
+ hostname from("hostname -s")
20
+ fqdn from("hostname")