Skip to content

Commit

Permalink
Merge branch 'master' into kisoku/ohai-58
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhjk committed Mar 6, 2009
2 parents f2aed65 + 386ba20 commit a4522bb
Show file tree
Hide file tree
Showing 20 changed files with 201 additions and 53 deletions.
11 changes: 10 additions & 1 deletion bin/ohai
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ require 'json'

config = {
:directory => nil,
:file => nil
:file => nil,
:log_level => :info
}

with_debug = false
opts = OptionParser.new do |opts|
opts.banner = "Usage: #{$0} (options)"
opts.on("-d", "--directory NAME", "A directory to add to the Ohai search path") do |d|
Expand All @@ -38,6 +40,9 @@ opts = OptionParser.new do |opts|
opts.on("-f", "--file NAME", "A file to run Ohai against") do |f|
config[:file] = f
end
opts.on("-l", "--loglevel NAME", "Set log level for Ohai") do |l|
config[:log_level] = l.to_sym
end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
Expand All @@ -46,6 +51,10 @@ end
opts.parse!(ARGV)

ohai = Ohai::System.new

if [ :debug, :info, :warn, :error, :fatal ].include?(config[:log_level])
Ohai::Config[:log_level] = config[:log_level]
end
if config[:file]
ohai.from_file(config[:file])
else
Expand Down
2 changes: 1 addition & 1 deletion lib/ohai/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

module Ohai
class Config

@configuration = {
:log_level => :info,
:log_location => STDOUT,
Expand Down
55 changes: 55 additions & 0 deletions lib/ohai/plugins/darwin/filesystem.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#
# Author:: Benjamin Black (<[email protected]>)
# Copyright:: Copyright (c) 2009 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

fs = Mash.new

block_size = 0
popen4("df") do |pid, stdin, stdout, stderr|
stdin.close
stdout.each do |line|
case line
when /^Filesystem\s+(\d+)-/
block_size = $1.to_i
next
when /^(.+?)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+\%)\s+(.+)$/
filesystem = $1
fs[filesystem] = Mash.new
fs[filesystem][:block_size] = block_size
fs[filesystem][:kb_size] = $2.to_i / (1024 / block_size)
fs[filesystem][:kb_used] = $3.to_i / (1024 / block_size)
fs[filesystem][:kb_available] = $4.to_i / (1024 / block_size)
fs[filesystem][:percent_used] = $5
fs[filesystem][:mount] = $6
end
end
end

popen4("mount") do |pid, stdin, stdout, stderr|
stdin.close
stdout.each do |line|
if line =~ /^(.+?) on (.+?) \((.+?), (.+?)\)$/
filesystem = $1
fs[filesystem] = Mash.new unless fs.has_key?(filesystem)
fs[filesystem][:mount] = $2
fs[filesystem][:fs_type] = $3
fs[filesystem][:mount_options] = $4.split(/,\s*/)
end
end
end

filesystem fs
4 changes: 4 additions & 0 deletions lib/ohai/plugins/darwin/kernel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

kernel[:os] = kernel[:name]

if from("sysctl -n hw.optional.x86_64").to_i == 1
kernel[:machine] = 'x86_64'
end

kext = Mash.new
popen4("/usr/sbin/kextstat -k -l") do |pid, stdin, stdout, stderr|
stdin.close
Expand Down
20 changes: 10 additions & 10 deletions lib/ohai/plugins/erlang.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@
require_plugin "languages"
output = nil

erlang = Mash.new
status = popen4("erl +V") do |pid, stdin, stdout, stderr|
stdin.close
output = stderr.gets.split
end

if status == 0
languages[:erlang] = Mash.new

output = stderr.gets.split if stderr
options = output[1]
options.gsub!(/(\(|\))/, '')

languages[:erlang][:version] = output[5]
languages[:erlang][:options] = options.split(',')
languages[:erlang][:emulator] = output[2].gsub!(/(\(|\))/, '')
erlang[:version] = output[5]
erlang[:options] = options.split(',')
erlang[:emulator] = output[2].gsub!(/(\(|\))/, '')
end

if status == 0
if erlang[:version] and erlang[:options] and erlang[:emulator]
languages[:erlang] = erlang
end
end
16 changes: 8 additions & 8 deletions lib/ohai/plugins/freebsd/filesystem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
when /^(.+?)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+\%)\s+(.+)$/
filesystem = $1
fs[filesystem] = Mash.new
fs[filesystem]['kb_size'] = $2
fs[filesystem]['kb_used'] = $3
fs[filesystem]['kb_available'] = $4
fs[filesystem]['percent_used'] = $5
fs[filesystem]['mount'] = $6
fs[filesystem][:kb_size] = $2
fs[filesystem][:kb_used] = $3
fs[filesystem][:kb_available] = $4
fs[filesystem][:percent_used] = $5
fs[filesystem][:mount] = $6
end
end
end
Expand All @@ -44,9 +44,9 @@
if line =~ /^(.+?) on (.+?) \((.+?), (.+?)\)$/
filesystem = $1
fs[filesystem] = Mash.new unless fs.has_key?(filesystem)
fs[filesystem]['mount'] = $2
fs[filesystem]['fs_type'] = $3
fs[filesystem]['mount-options'] = $4.split(/,\s*/)
fs[filesystem][:mount] = $2
fs[filesystem][:fs_type] = $3
fs[filesystem][:mount_options] = $4.split(/,\s*/)
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/ohai/plugins/freebsd/kernel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# limitations under the License.
#

kernel[:os] = kernel[:name]
kernel[:ident] = from("uname -i")
kernel[:securelevel] = from_with_regex("sysctl kern.securelevel", /kern.securelevel: (.+)$/)

Expand Down
6 changes: 4 additions & 2 deletions lib/ohai/plugins/hostname.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@
require_plugin "#{os}::hostname"

# Domain is everything after the first dot
fqdn =~ /.+?\.(.*)/
domain $1
if fqdn
fqdn =~ /.+?\.(.*)/
domain $1
end
4 changes: 3 additions & 1 deletion lib/ohai/plugins/java.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@
end
end

languages[:java] = java if java[:version]
if status == 0
languages[:java] = java if java[:version]
end
4 changes: 2 additions & 2 deletions lib/ohai/plugins/linux/block_device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
block[dir] = Mash.new
%w{size removable}.each do |check|
if File.exists?("/sys/block/#{dir}/#{check}")
block[dir][check] = from("cat /sys/block/#{dir}/#{check}")
block[dir][check] = File.read("/sys/block/#{dir}/#{check}").chomp
end
end
%w{model rev state timeout vendor}.each do |check|
if File.exists?("/sys/block/#{dir}/device/#{check}")
block[dir][check] = from("cat /sys/block/#{dir}/device/#{check}")
block[dir][check] = File.read("/sys/block/#{dir}/device/#{check}").chomp
end
end
end
Expand Down
20 changes: 10 additions & 10 deletions lib/ohai/plugins/linux/filesystem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
fs = Mash.new

# Grab filesystem data from df
popen4("/bin/df -P") do |pid, stdin, stdout, stderr|
popen4("df -P") do |pid, stdin, stdout, stderr|
stdin.close
stdout.each do |line|
case line
Expand All @@ -28,25 +28,25 @@
when /^(.+?)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+\%)\s+(.+)$/
filesystem = $1
fs[filesystem] = Mash.new
fs[filesystem]['kb_size'] = $2
fs[filesystem]['kb_used'] = $3
fs[filesystem]['kb_available'] = $4
fs[filesystem]['percent_used'] = $5
fs[filesystem]['mount'] = $6
fs[filesystem][:kb_size] = $2
fs[filesystem][:kb_used] = $3
fs[filesystem][:kb_available] = $4
fs[filesystem][:percent_used] = $5
fs[filesystem][:mount] = $6
end
end
end

# Grab mount information from /bin/mount
popen4("/bin/mount -l") do |pid, stdin, stdout, stderr|
popen4("mount -l") do |pid, stdin, stdout, stderr|
stdin.close
stdout.each do |line|
if line =~ /^(.+?) on (.+?) type (.+?) \((.+?)\)$/
filesystem = $1
fs[filesystem] = Mash.new unless fs.has_key?(filesystem)
fs[filesystem]['mount'] = $2
fs[filesystem]['fs_type'] = $3
fs[filesystem]['mount-options'] = $4.split(",")
fs[filesystem][:mount] = $2
fs[filesystem][:fs_type] = $3
fs[filesystem][:mount_options] = $4.split(",")
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/ohai/plugins/linux/hostname.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@
#

hostname from("hostname")
fqdn from("hostname --fqdn")
begin
fqdn from("hostname --fqdn")
rescue
Ohai::Log.debug("hostname -f returned an error, probably no domain is set")
end
2 changes: 1 addition & 1 deletion lib/ohai/plugins/linux/platform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
platform_version lsb[:release]
elsif File.exists?("/etc/debian_version")
platform "debian"
platform_version from("cat /etc/debian_version")
platform_version File.read("/etc/debian_version").chomp
elsif File.exists?("/etc/redhat-release")
platform "redhat"
File.open("/etc/redhat-release").each do |line|
Expand Down
2 changes: 1 addition & 1 deletion lib/ohai/plugins/os.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
when /linux/
os "linux"
when /freebsd(.+)$/
os "freebsd"
os "freebsd"
else
os languages[:ruby][:host_os]
end
Expand Down
4 changes: 3 additions & 1 deletion lib/ohai/plugins/python.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@
python[:builddate] = "%s %s %s %s" % [output[2],output[3],output[4],output[5].gsub!(/\)/,'')]
end

languages[:python] = python if python[:version] and python[:builddate]
if status == 0
languages[:python] = python if python[:version] and python[:builddate]
end
5 changes: 1 addition & 4 deletions lib/ohai/system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ def each(&block)
end

def attribute?(name)
Ohai::Log.debug("Attribute #{name} is #{@data.has_key?(name)}")
@data.has_key?(name)
end

Expand All @@ -74,13 +73,11 @@ def from_with_regex(cmd, *regex_list)
end

def set_attribute(name, *value)
Ohai::Log.debug("Setting attribute #{name} to #{value.inspect}")
@data[name] = *value
@data[name]
end

def get_attribute(name)
Ohai::Log.debug("Getting attribute #{name}, value #{@data[name].inspect}")
@data[name]
end

Expand Down Expand Up @@ -113,7 +110,7 @@ def require_plugin(plugin_name, force=false)
check_path = File.expand_path(File.join(path, filename))
begin
@seen_plugins[plugin_name] = true
Ohai::Log.info("Loading plugin #{plugin_name}")
Ohai::Log.debug("Loading plugin #{plugin_name}")
from_file(check_path)
return true
rescue IOError => e
Expand Down
10 changes: 8 additions & 2 deletions spec/ohai/plugins/hostname_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,17 @@
before(:each) do
@ohai = Ohai::System.new
@ohai.stub!(:require_plugin).and_return(true)
@ohai[:fqdn] = "katie.bethell"
end

it "should set the domain to everything after the first dot of the fqdn" do
@ohai[:fqdn] = "katie.bethell"
@ohai._require_plugin("hostname")
@ohai.domain.should == "bethell"
end
end

it "should not set a domain if fqdn is not set" do
@ohai._require_plugin("hostname")
@ohai.domain.should == nil
end

end
Loading

0 comments on commit a4522bb

Please sign in to comment.