├── spec ├── spec.opts ├── rcov.opts ├── ohai_spec.rb ├── ohai │ ├── plugins │ │ ├── linux │ │ │ ├── kernel_spec.rb │ │ │ ├── hostname_spec.rb │ │ │ ├── lsb_spec.rb │ │ │ ├── uptime_spec.rb │ │ │ ├── platform_spec.rb │ │ │ ├── cpu_spec.rb │ │ │ └── virtualization_spec.rb │ │ ├── darwin │ │ │ ├── hostname_spec.rb │ │ │ ├── kernel_spec.rb │ │ │ └── platform_spec.rb │ │ ├── freebsd │ │ │ ├── hostname_spec.rb │ │ │ ├── kernel_spec.rb │ │ │ └── platform_spec.rb │ │ ├── hostname_spec.rb │ │ ├── ohai_time_spec.rb │ │ ├── ruby_spec.rb │ │ ├── kernel_spec.rb │ │ ├── os_spec.rb │ │ ├── platform_spec.rb │ │ ├── python_spec.rb │ │ ├── ec2_spec.rb │ │ ├── erlang_spec.rb │ │ ├── perl_spec.rb │ │ └── java_spec.rb │ ├── log │ │ └── log_formatter_spec.rb │ ├── mixin │ │ └── from_file_spec.rb │ ├── log_spec.rb │ └── system_spec.rb └── spec_helper.rb ├── .gitignore ├── features ├── steps │ ├── env.rb │ └── common.rb └── ohai.feature.pending ├── lib ├── ohai │ ├── plugins │ │ ├── keys.rb │ │ ├── command.rb │ │ ├── ohai_time.rb │ │ ├── languages.rb │ │ ├── solaris2 │ │ │ ├── kernel.rb │ │ │ ├── ps.rb │ │ │ ├── hostname.rb │ │ │ ├── ssh_host_key.rb │ │ │ ├── platform.rb │ │ │ ├── cpu.rb │ │ │ └── network.rb │ │ ├── darwin │ │ │ ├── ps.rb │ │ │ ├── hostname.rb │ │ │ ├── ssh_host_key.rb │ │ │ ├── system_profiler.rb │ │ │ ├── platform.rb │ │ │ ├── kernel.rb │ │ │ ├── filesystem.rb │ │ │ └── network.rb │ │ ├── linux │ │ │ ├── ps.rb │ │ │ ├── hostname.rb │ │ │ ├── ssh_host_key.rb │ │ │ ├── uptime.rb │ │ │ ├── kernel.rb │ │ │ ├── lsb.rb │ │ │ ├── block_device.rb │ │ │ ├── platform.rb │ │ │ ├── filesystem.rb │ │ │ ├── cpu.rb │ │ │ ├── virtualization.rb │ │ │ ├── memory.rb │ │ │ └── network.rb │ │ ├── freebsd │ │ │ ├── hostname.rb │ │ │ ├── ps.rb │ │ │ ├── platform.rb │ │ │ ├── ssh_host_key.rb │ │ │ ├── uptime.rb │ │ │ ├── kernel.rb │ │ │ ├── filesystem.rb │ │ │ ├── cpu.rb │ │ │ ├── memory.rb │ │ │ ├── virtualization.rb │ │ │ └── network.rb │ │ ├── hostname.rb │ │ ├── platform.rb │ │ ├── kernel.rb │ │ ├── os.rb │ │ ├── perl.rb │ │ ├── erlang.rb │ │ ├── java.rb │ │ ├── python.rb │ │ ├── ruby.rb │ │ ├── uptime.rb │ │ ├── network.rb │ │ ├── ec2.rb │ │ └── virtualization.rb │ ├── exception.rb │ ├── mixin │ │ ├── from_file.rb │ │ └── command.rb │ ├── log │ │ └── formatter.rb │ ├── log.rb │ ├── config.rb │ └── system.rb └── ohai.rb ├── NOTICE ├── Rakefile ├── README.rdoc ├── bin └── ohai └── CHANGELOG /spec/spec.opts: -------------------------------------------------------------------------------- 1 | --format specdoc 2 | --colour 3 | -------------------------------------------------------------------------------- /spec/rcov.opts: -------------------------------------------------------------------------------- 1 | --exclude 2 | spec,bin,/Library/Ruby 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | pkg/ 2 | tmp/ 3 | coverage/ 4 | ohai.gemspec 5 | -------------------------------------------------------------------------------- /features/steps/env.rb: -------------------------------------------------------------------------------- 1 | require File.dirname(__FILE__) + "/../../lib/ohai" 2 | 3 | gem 'cucumber' 4 | require 'cucumber' 5 | gem 'rspec' 6 | require 'spec' 7 | -------------------------------------------------------------------------------- /lib/ohai/plugins/keys.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook Name:: apache2 3 | # Recipe:: default 4 | # 5 | # Copyright 2008, OpsCode, Inc. 6 | # 7 | # Licensed under the Apache License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | # 19 | 20 | provides "keys" 21 | 22 | keys Mash.new -------------------------------------------------------------------------------- /lib/ohai/plugins/command.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | provides "command" 20 | 21 | command Mash.new -------------------------------------------------------------------------------- /lib/ohai/plugins/ohai_time.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | provides "ohai_time" 20 | 21 | ohai_time Time.now.to_f -------------------------------------------------------------------------------- /features/ohai.feature.pending: -------------------------------------------------------------------------------- 1 | Feature: Collects data about a system 2 | In order to understand the state of my system 3 | As a Systems Administrator 4 | I want to have a program that detects information for me 5 | 6 | Scenario: Collect data about the system via a single plugin 7 | Given a plugin called 'platform' 8 | When I run ohai 9 | Then I should have a 'platform' of 'kitties' 10 | 11 | Scenario: Collect data about the system via a directory of plugins 12 | Given a plugin directory at './plugins' 13 | When I run ohai 14 | Then I should have a 'platform' of 'kitties' 15 | And I should have a 'foghorn' of 'leghorn' 16 | 17 | Scenario: Collect data about the system via an external script 18 | Given a plugin called 'perl' 19 | When I run ohai 20 | Then I should have a 'perl_major_version' of '5' 21 | -------------------------------------------------------------------------------- /lib/ohai/plugins/languages.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | provides "languages" 20 | 21 | languages Mash.new 22 | -------------------------------------------------------------------------------- /lib/ohai/plugins/solaris2/kernel.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Benjamin Black () 3 | # Copyright:: Copyright (c) 2009 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 | provides "kernel/os" 20 | 21 | kernel[:os] = from("uname -s") 22 | -------------------------------------------------------------------------------- /lib/ohai/exception.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 24 | -------------------------------------------------------------------------------- /lib/ohai/plugins/darwin/ps.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | provides "command/ps" 20 | 21 | require_plugin 'command' 22 | 23 | command[:ps] = 'ps -ef' -------------------------------------------------------------------------------- /lib/ohai/plugins/solaris2/ps.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | provides "command/ps" 20 | 21 | require_plugin 'command' 22 | 23 | command[:ps] = 'ps -ef' -------------------------------------------------------------------------------- /lib/ohai/plugins/darwin/hostname.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | provides "fqdn", "hostname" 20 | 21 | hostname from("hostname -s") 22 | fqdn from("hostname") -------------------------------------------------------------------------------- /lib/ohai/plugins/linux/ps.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | provides "command/ps" 20 | 21 | require_plugin 'command' 22 | 23 | command[:ps] = 'ps -ef' 24 | -------------------------------------------------------------------------------- /lib/ohai/plugins/freebsd/hostname.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Bryan McLellan (btm@loftninjas.org) 3 | # Copyright:: Copyright (c) 2009 Bryan McLellan 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 | provides "hostname", "fqdn" 20 | 21 | hostname from("hostname -s") 22 | fqdn from("hostname -f") 23 | -------------------------------------------------------------------------------- /lib/ohai/plugins/solaris2/hostname.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Benjamin Black () 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 | provides "hostname", "fqdn" 20 | 21 | hostname from("hostname").split('.')[0] 22 | fqdn from("hostname") 23 | -------------------------------------------------------------------------------- /lib/ohai/plugins/freebsd/ps.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Bryan McLellan (btm@loftninjas.org) 3 | # Copyright:: Copyright (c) 2009 Bryan McLellan 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 | provides "command/ps" 20 | 21 | require_plugin 'command' 22 | 23 | # ps -e requires procfs 24 | command[:ps] = 'ps -ax' 25 | -------------------------------------------------------------------------------- /lib/ohai/plugins/freebsd/platform.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Bryan McLellan (btm@loftninjas.org) 3 | # Copyright:: Copyright (c) 2009 Bryan McLellan 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 | provides "platform", "platform_version" 20 | 21 | platform from("uname -s").downcase 22 | platform_version from("uname -r") 23 | 24 | -------------------------------------------------------------------------------- /lib/ohai/plugins/hostname.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | provides "fqdn", "domain" 20 | 21 | require_plugin "#{os}::hostname" 22 | 23 | # Domain is everything after the first dot 24 | if fqdn 25 | fqdn =~ /.+?\.(.*)/ 26 | domain $1 27 | end -------------------------------------------------------------------------------- /spec/ohai_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 File.dirname(__FILE__) + '/spec_helper.rb' 20 | 21 | describe Ohai do 22 | 23 | it "should have a version constant defined" do 24 | Ohai::VERSION.should be_a_kind_of(String) 25 | end 26 | 27 | end 28 | -------------------------------------------------------------------------------- /lib/ohai/plugins/platform.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | provides "platform", "platform_version" 20 | 21 | require_plugin "#{os}::platform" 22 | 23 | platform os unless attribute?("platform") 24 | 25 | platform_version os_version unless attribute?("platform_version") 26 | -------------------------------------------------------------------------------- /lib/ohai/plugins/linux/hostname.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | provides "hostname", "fqdn" 20 | 21 | hostname from("hostname") 22 | begin 23 | fqdn from("hostname --fqdn") 24 | rescue 25 | Ohai::Log.debug("hostname -f returned an error, probably no domain is set") 26 | end 27 | -------------------------------------------------------------------------------- /lib/ohai.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | $:.unshift(File.dirname(__FILE__)) unless 20 | $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__))) 21 | 22 | require 'ohai/config' 23 | require 'ohai/system' 24 | 25 | module Ohai 26 | VERSION = '0.2.1' 27 | end 28 | -------------------------------------------------------------------------------- /lib/ohai/plugins/darwin/ssh_host_key.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | provides "keys/ssh" 20 | 21 | require_plugin "keys" 22 | 23 | keys[:ssh] = Mash.new 24 | keys[:ssh][:host_dsa_public] = IO.read("/etc/ssh_host_dsa_key.pub").split[1] 25 | keys[:ssh][:host_rsa_public] = IO.read("/etc/ssh_host_rsa_key.pub").split[1] -------------------------------------------------------------------------------- /lib/ohai/plugins/kernel.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | provides "kernel" 20 | 21 | kernel Mash.new 22 | 23 | kernel[:name] = from("uname -s") 24 | kernel[:release] = from("uname -r") 25 | kernel[:version] = from("uname -v") 26 | kernel[:machine] = from("uname -m") 27 | kernel[:modules] = Mash.new 28 | 29 | 30 | -------------------------------------------------------------------------------- /lib/ohai/plugins/freebsd/ssh_host_key.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | provides "keys/ssh" 20 | 21 | require_plugin "keys" 22 | 23 | keys[:ssh] = Mash.new 24 | 25 | keys[:ssh][:host_dsa_public] = IO.read("/etc/ssh/ssh_host_dsa_key.pub").split[1] 26 | keys[:ssh][:host_rsa_public] = IO.read("/etc/ssh/ssh_host_rsa_key.pub").split[1] 27 | -------------------------------------------------------------------------------- /lib/ohai/plugins/linux/ssh_host_key.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | provides "keys/ssh" 20 | 21 | require_plugin "keys" 22 | 23 | keys[:ssh] = Mash.new 24 | 25 | keys[:ssh][:host_dsa_public] = IO.read("/etc/ssh/ssh_host_dsa_key.pub").split[1] 26 | keys[:ssh][:host_rsa_public] = IO.read("/etc/ssh/ssh_host_rsa_key.pub").split[1] 27 | -------------------------------------------------------------------------------- /lib/ohai/plugins/solaris2/ssh_host_key.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | provides "keys/ssh" 20 | 21 | require_plugin "keys" 22 | 23 | keys[:ssh] = Mash.new 24 | 25 | keys[:ssh][:host_dsa_public] = IO.read("/etc/ssh/ssh_host_dsa_key.pub").split[1] 26 | keys[:ssh][:host_rsa_public] = IO.read("/etc/ssh/ssh_host_rsa_key.pub").split[1] 27 | -------------------------------------------------------------------------------- /lib/ohai/plugins/linux/uptime.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | provides "uptime", "idletime", "uptime_seconds", "idletime_seconds" 20 | 21 | uptime, idletime = File.open("/proc/uptime").gets.split(" ") 22 | uptime_seconds uptime.to_i 23 | uptime self._seconds_to_human(uptime.to_i) 24 | idletime_seconds idletime.to_i 25 | idletime self._seconds_to_human(idletime.to_i) 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | ============ 2 | Ohai Notices 3 | ============ 4 | 5 | Developed at Opscode (http://www.opscode.com). 6 | 7 | Contributors and Copyright holders: 8 | 9 | * Copyright 2008-2009, Opscode 10 | * Copyright 2008-2009, Adam Jacob 11 | * Copyright 2008-2009, Ben Black 12 | * Copyright 2008-2009, Ezra Zygmuntowicz 13 | * Copyright 2009, Joe Williams 14 | * Copyright 2009, Paul Nasrat 15 | 16 | Parts of Ohai were taken from Chef, a configuration management system. 17 | 18 | Ohai incorporates functionality from Open4 (http://www.codeforpeople.com/lib/ruby/open4/). 19 | 20 | ============ 21 | Chef Notices 22 | ============ 23 | 24 | Developed at Opscode (http://www.opscode.com). 25 | 26 | Contributors and Copyright holders: 27 | 28 | * Copyright 2008, Adam Jacob 29 | * Copyright 2008, Arjuna Christensen 30 | * Copyright 2008, Bryan McLellan 31 | * Copyright 2008, Ezra Zygmuntowicz 32 | 33 | -------------------------------------------------------------------------------- /lib/ohai/plugins/os.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | provides "os", "os_version" 20 | 21 | require_plugin 'ruby' 22 | require_plugin 'kernel' 23 | 24 | case languages[:ruby][:host_os] 25 | when /darwin(.+)$/ 26 | os "darwin" 27 | when /linux/ 28 | os "linux" 29 | when /freebsd(.+)$/ 30 | os "freebsd" 31 | else 32 | os languages[:ruby][:host_os] 33 | end 34 | 35 | os_version kernel[:release] 36 | 37 | -------------------------------------------------------------------------------- /lib/ohai/plugins/linux/kernel.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | provides "kernel" 20 | 21 | kernel[:os] = from("uname -o") 22 | 23 | kext = Mash.new 24 | popen4("/sbin/lsmod") do |pid, stdin, stdout, stderr| 25 | stdin.close 26 | stdout.each do |line| 27 | if line =~ /([a-zA-Z0-9\_]+)\s+(\d+)\s+(\d+)/ 28 | kext[$1] = { :size => $2, :refcount => $3 } 29 | end 30 | end 31 | end 32 | 33 | kernel[:modules] = kext 34 | -------------------------------------------------------------------------------- /lib/ohai/plugins/darwin/system_profiler.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Benjamin Black () 3 | # Copyright:: Copyright (c) 2009 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 | provides "system_profile" 20 | 21 | begin 22 | require 'plist' 23 | 24 | system_profile Array.new 25 | popen4("system_profiler -xml -detailLevel mini") do |pid, stdin, stdout, stderr| 26 | stdin.close 27 | Plist::parse_xml(stdout.read).each do |e| 28 | system_profile << e 29 | end 30 | end 31 | rescue LoadError => e 32 | Ohai::Log.info("Can't load gem: #{e})") 33 | end -------------------------------------------------------------------------------- /lib/ohai/plugins/freebsd/uptime.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Bryan McLellan (btm@loftninjas.org) 3 | # Copyright:: Copyright (c) 2009 Bryan McLellan 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 | provides "uptime", "uptime_seconds" 20 | 21 | # kern.boottime: { sec = 1232765114, usec = 823118 } Fri Jan 23 18:45:14 2009 22 | 23 | popen4("/sbin/sysctl kern.boottime") do |pid, stdin, stdout, stderr| 24 | stdin.close 25 | stdout.each do |line| 26 | if line =~ /kern.boottime:\D+(\d+)/ 27 | uptime_seconds Time.new.to_i - $1.to_i 28 | uptime self._seconds_to_human(uptime_seconds) 29 | end 30 | end 31 | end 32 | 33 | -------------------------------------------------------------------------------- /lib/ohai/plugins/solaris2/platform.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Benjamin Black () 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 | provides "platform", "platform_version", "platform_build" 20 | 21 | popen4("uname -X") do |pid, stdin, stdout, stderr| 22 | stdin.close 23 | stdout.each do |line| 24 | case line 25 | when /^System =\s+(.+)$/ 26 | platform = ($1.eql?("SunOS") ? "solaris2" : $1.downcase) 27 | when /^Release =\s+(.+)$/ 28 | platform_version $1 29 | when /^KernelID =\s+(.+)$/ 30 | platform_build $1 31 | end 32 | end 33 | end 34 | -------------------------------------------------------------------------------- /lib/ohai/plugins/perl.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Joshua Timberman () 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 | provides "languages/perl" 20 | 21 | require_plugin "languages" 22 | output = nil 23 | 24 | perl = Mash.new 25 | status = popen4("perl -V:version -V:archname") do |pid, stdin, stdout, stderr| 26 | stdin.close 27 | stdout.each do |line| 28 | case line 29 | when /^version=\'(.+)\';$/: perl[:version] = $1 30 | when /^archname=\'(.+)\';$/: perl[:archname] = $1 31 | end 32 | end 33 | end 34 | 35 | if status == 0 36 | languages[:perl] = perl 37 | end 38 | -------------------------------------------------------------------------------- /spec/ohai/plugins/linux/kernel_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | 20 | require File.join(File.dirname(__FILE__), '..', '..', '..', '/spec_helper.rb') 21 | 22 | describe Ohai::System, "Linux kernel plugin" do 23 | before(:each) do 24 | @ohai = Ohai::System.new 25 | @ohai._require_plugin("kernel") 26 | @ohai.stub!(:require_plugin).and_return(true) 27 | @ohai.stub!(:from).with("uname -o").and_return("Linux") 28 | end 29 | 30 | it_should_check_from_deep_mash("linux::kernel", "kernel", "os", "uname -o", "Linux") 31 | end -------------------------------------------------------------------------------- /lib/ohai/plugins/linux/lsb.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | provides "lsb" 20 | 21 | lsb Mash.new 22 | 23 | begin 24 | File.open("/etc/lsb-release").each do |line| 25 | case line 26 | when /^DISTRIB_ID=(.+)$/ 27 | lsb[:id] = $1 28 | when /^DISTRIB_RELEASE=(.+)$/ 29 | lsb[:release] = $1 30 | when /^DISTRIB_CODENAME=(.+)$/ 31 | lsb[:codename] = $1 32 | when /^DISTRIB_DESCRIPTION=(.+)$/ 33 | lsb[:description] = $1 34 | end 35 | end 36 | rescue 37 | Ohai::Log.debug("Skipping LSB, cannot find /etc/lsb-release") 38 | end 39 | -------------------------------------------------------------------------------- /lib/ohai/plugins/darwin/platform.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | provides "platform", "platform_version", "platform_build" 20 | 21 | popen4("/usr/bin/sw_vers") do |pid, stdin, stdout, stderr| 22 | stdin.close 23 | stdout.each do |line| 24 | case line 25 | when /^ProductName:\s+(.+)$/ 26 | macname = $1 27 | macname.downcase! 28 | macname.gsub!(" ", "_") 29 | platform macname 30 | when /^ProductVersion:\s+(.+)$/ 31 | platform_version $1 32 | when /^BuildVersion:\s+(.+)$/ 33 | platform_build $1 34 | end 35 | end 36 | end -------------------------------------------------------------------------------- /lib/ohai/plugins/darwin/kernel.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | provides "kernel" 20 | 21 | kernel[:os] = kernel[:name] 22 | 23 | if from("sysctl -n hw.optional.x86_64").to_i == 1 24 | kernel[:machine] = 'x86_64' 25 | end 26 | 27 | kext = Mash.new 28 | popen4("kextstat -k -l") do |pid, stdin, stdout, stderr| 29 | stdin.close 30 | stdout.each do |line| 31 | if line =~ /(\d+)\s+(\d+)\s+0x[0-9a-f]+\s+0x([0-9a-f]+)\s+0x[0-9a-f]+\s+([a-zA-Z0-9\.]+) \(([0-9\.]+)\)/ 32 | kext[$4] = { :version => $5, :size => $3.hex, :index => $1, :refcount => $2 } 33 | end 34 | end 35 | end 36 | 37 | kernel[:modules] = kext 38 | -------------------------------------------------------------------------------- /lib/ohai/mixin/from_file.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 37 | -------------------------------------------------------------------------------- /lib/ohai/plugins/freebsd/kernel.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Bryan McLellan (btm@loftninjas.org) 3 | # Copyright:: Copyright (c) 2009 Bryan McLellan 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 | provides "kernel" 20 | 21 | kernel[:os] = kernel[:name] 22 | kernel[:ident] = from("uname -i") 23 | kernel[:securelevel] = from_with_regex("sysctl kern.securelevel", /kern.securelevel: (.+)$/) 24 | 25 | kld = Mash.new 26 | popen4("/sbin/kldstat") do |pid, stdin, stdout, stderr| 27 | stdin.close 28 | stdout.each do |line| 29 | # 1 7 0xc0400000 97f830 kernel 30 | if line =~ /(\d+)\s+(\d+)\s+([0-9a-fx]+)\s+([0-9a-fx]+)\s+([a-zA-Z0-9\_]+)/ 31 | kld[$5] = { :size => $4, :refcount => $2 } 32 | end 33 | end 34 | end 35 | 36 | kernel[:modules] = kld 37 | 38 | -------------------------------------------------------------------------------- /lib/ohai/plugins/erlang.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Joe Williams () 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 | provides "languages/erlang" 20 | 21 | require_plugin "languages" 22 | 23 | output = nil 24 | 25 | erlang = Mash.new 26 | status = popen4("erl +V") do |pid, stdin, stdout, stderr| 27 | stdin.close 28 | output = stderr.gets.split if stderr 29 | options = output[1] 30 | options.gsub!(/(\(|\))/, '') 31 | erlang[:version] = output[5] 32 | erlang[:options] = options.split(',') 33 | erlang[:emulator] = output[2].gsub!(/(\(|\))/, '') 34 | end 35 | 36 | if status == 0 37 | if erlang[:version] and erlang[:options] and erlang[:emulator] 38 | languages[:erlang] = erlang 39 | end 40 | end 41 | -------------------------------------------------------------------------------- /lib/ohai/plugins/java.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Benjamin Black () 3 | # Copyright:: Copyright (c) 2009 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 | provides "languages/java" 19 | 20 | require_plugin "languages" 21 | 22 | java = Mash.new 23 | status = popen4("java -version") do |pid, stdin, stdout, stderr| 24 | stdin.close 25 | stderr.each do |line| 26 | case line 27 | when /java version \"([0-9\.\_]+)\"/: java[:version] = $1 28 | when /^(.+Runtime Environment.*) \(build (.+)\)$/: java[:runtime] = { "name" => $1, "build" => $2 } 29 | when /^(.+ Client VM) \(build (.+)\)$/: java[:hotspot] = { "name" => $1, "build" => $2 } 30 | end 31 | end 32 | end 33 | 34 | if status == 0 35 | languages[:java] = java if java[:version] 36 | end 37 | -------------------------------------------------------------------------------- /lib/ohai/plugins/python.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Thom May () 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 | provides "languages/python" 20 | 21 | require_plugin "languages" 22 | 23 | output = nil 24 | 25 | python = Mash.new 26 | status = popen4("python -c \"import sys; print sys.version\"") do |pid, stdin, stdout, stderr| 27 | stdin.close 28 | output_string = stdout.gets 29 | if output_string 30 | output = output_string.split 31 | python[:version] = output[0] 32 | python[:builddate] = "%s %s %s %s" % [output[2],output[3],output[4],output[5].gsub!(/\)/,'')] 33 | end 34 | end 35 | 36 | if status == 0 37 | languages[:python] = python if python[:version] and python[:builddate] 38 | end 39 | -------------------------------------------------------------------------------- /spec/ohai/plugins/darwin/hostname_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | 20 | require File.join(File.dirname(__FILE__), '..', '..', '..', '/spec_helper.rb') 21 | 22 | describe Ohai::System, "Darwin hostname plugin" do 23 | before(:each) do 24 | @ohai = Ohai::System.new 25 | @ohai.stub!(:require_plugin).and_return(true) 26 | @ohai[:os] = "darwin" 27 | @ohai.stub!(:from).with("hostname -s").and_return("katie") 28 | @ohai.stub!(:from).with("hostname").and_return("katie.bethell") 29 | end 30 | 31 | it_should_check_from("darwin::hostname", "hostname", "hostname -s", "katie") 32 | 33 | it_should_check_from("darwin::hostname", "fqdn", "hostname", "katie.bethell") 34 | end -------------------------------------------------------------------------------- /spec/ohai/plugins/freebsd/hostname_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | 20 | require File.join(File.dirname(__FILE__), '..', '..', '..', '/spec_helper.rb') 21 | 22 | describe Ohai::System, "FreeBSD hostname plugin" do 23 | before(:each) do 24 | @ohai = Ohai::System.new 25 | @ohai.stub!(:require_plugin).and_return(true) 26 | @ohai[:os] = "freebsd" 27 | @ohai.stub!(:from).with("hostname -s").and_return("katie") 28 | @ohai.stub!(:from).with("hostname -f").and_return("katie.bethell") 29 | end 30 | 31 | it_should_check_from("freebsd::hostname", "hostname", "hostname -s", "katie") 32 | 33 | it_should_check_from("freebsd::hostname", "fqdn", "hostname -f", "katie.bethell") 34 | end 35 | -------------------------------------------------------------------------------- /spec/ohai/plugins/hostname_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | 20 | require File.join(File.dirname(__FILE__), '..', '..', '/spec_helper.rb') 21 | 22 | describe Ohai::System, "hostname plugin" do 23 | before(:each) do 24 | @ohai = Ohai::System.new 25 | @ohai.stub!(:require_plugin).and_return(true) 26 | end 27 | 28 | it "should set the domain to everything after the first dot of the fqdn" do 29 | @ohai[:fqdn] = "katie.bethell" 30 | @ohai._require_plugin("hostname") 31 | @ohai.domain.should == "bethell" 32 | end 33 | 34 | it "should not set a domain if fqdn is not set" do 35 | @ohai._require_plugin("hostname") 36 | @ohai.domain.should == nil 37 | end 38 | 39 | end 40 | -------------------------------------------------------------------------------- /lib/ohai/plugins/linux/block_device.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | provides "block_device" 20 | 21 | if File.exists?("/sys/block") 22 | block = Mash.new 23 | Dir["/sys/block/*"].each do |block_device_dir| 24 | dir = File.basename(block_device_dir) 25 | block[dir] = Mash.new 26 | %w{size removable}.each do |check| 27 | if File.exists?("/sys/block/#{dir}/#{check}") 28 | block[dir][check] = File.read("/sys/block/#{dir}/#{check}").chomp 29 | end 30 | end 31 | %w{model rev state timeout vendor}.each do |check| 32 | if File.exists?("/sys/block/#{dir}/device/#{check}") 33 | block[dir][check] = File.read("/sys/block/#{dir}/device/#{check}").chomp 34 | end 35 | end 36 | end 37 | block_device block 38 | end 39 | -------------------------------------------------------------------------------- /spec/ohai/plugins/freebsd/kernel_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | 20 | require File.join(File.dirname(__FILE__), '..', '..', '..', '/spec_helper.rb') 21 | 22 | describe Ohai::System, "FreeBSD kernel plugin" do 23 | before(:each) do 24 | @ohai = Ohai::System.new 25 | @ohai.stub!(:require_plugin).and_return(true) 26 | @ohai.stub!(:from).with("uname -i").and_return("foo") 27 | @ohai.stub!(:from_with_regex).with("sysctl kern.securlevel").and_return("kern.securelevel: 1") 28 | @ohai[:kernel] = Mash.new 29 | @ohai[:kernel][:name] = "freebsd" 30 | end 31 | 32 | it "should set the kernel_os to the kernel_name value" do 33 | @ohai._require_plugin("freebsd::kernel") 34 | @ohai[:kernel][:os].should == @ohai[:kernel][:name] 35 | end 36 | 37 | end 38 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | require 'rake/gempackagetask' 3 | require 'rubygems/specification' 4 | require 'date' 5 | require 'spec/rake/spectask' 6 | 7 | GEM = "ohai" 8 | GEM_VERSION = "0.2.1" 9 | AUTHOR = "Adam Jacob" 10 | EMAIL = "adam@opscode.com" 11 | HOMEPAGE = "http://wiki.opscode.com/display/ohai" 12 | SUMMARY = "Ohai profiles your system and emits JSON" 13 | 14 | spec = Gem::Specification.new do |s| 15 | s.name = GEM 16 | s.version = GEM_VERSION 17 | s.platform = Gem::Platform::RUBY 18 | s.has_rdoc = true 19 | s.summary = SUMMARY 20 | s.description = s.summary 21 | s.author = AUTHOR 22 | s.email = EMAIL 23 | s.homepage = HOMEPAGE 24 | 25 | s.add_dependency "json" 26 | s.add_dependency "extlib" 27 | s.bindir = "bin" 28 | s.executables = %w(ohai) 29 | 30 | s.require_path = 'lib' 31 | s.autorequire = GEM 32 | s.files = %w(LICENSE README.rdoc Rakefile) + Dir.glob("{extras,lib,spec}/**/*") 33 | end 34 | 35 | task :default => :spec 36 | 37 | desc "Run specs" 38 | Spec::Rake::SpecTask.new do |t| 39 | t.spec_files = FileList['spec/**/*_spec.rb'] 40 | t.spec_opts = %w(-fs --color) 41 | end 42 | 43 | 44 | Rake::GemPackageTask.new(spec) do |pkg| 45 | pkg.gem_spec = spec 46 | end 47 | 48 | desc "install the gem locally" 49 | task :install => [:package] do 50 | sh %{sudo gem install pkg/#{GEM}-#{GEM_VERSION}} 51 | end 52 | 53 | desc "create a gemspec file" 54 | task :make_spec do 55 | File.open("#{GEM}.gemspec", "w") do |file| 56 | file.puts spec.to_ruby 57 | end 58 | end 59 | -------------------------------------------------------------------------------- /lib/ohai/plugins/ruby.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | provides "languages/ruby" 20 | 21 | require_plugin "languages" 22 | 23 | languages[:ruby] = Mash.new 24 | 25 | languages[:ruby][:platform] = RUBY_PLATFORM 26 | languages[:ruby][:version] = RUBY_VERSION 27 | languages[:ruby][:release_date] = RUBY_RELEASE_DATE 28 | languages[:ruby][:target] = ::Config::CONFIG['target'] 29 | languages[:ruby][:target_cpu] = ::Config::CONFIG['target_cpu'] 30 | languages[:ruby][:target_vendor] = ::Config::CONFIG['target_vendor'] 31 | languages[:ruby][:target_os] = ::Config::CONFIG['target_os'] 32 | languages[:ruby][:host] = ::Config::CONFIG['host'] 33 | languages[:ruby][:host_cpu] = ::Config::CONFIG['host_cpu'] 34 | languages[:ruby][:host_os] = ::Config::CONFIG['host_os'] 35 | languages[:ruby][:host_vendor] = ::Config::CONFIG['host_vendor'] 36 | -------------------------------------------------------------------------------- /lib/ohai/plugins/uptime.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | 43 | -------------------------------------------------------------------------------- /lib/ohai/plugins/linux/platform.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | provides "platform", "platform_version" 20 | 21 | require_plugin 'linux::lsb' 22 | 23 | if lsb[:id] 24 | platform lsb[:id].downcase 25 | platform_version lsb[:release] 26 | elsif File.exists?("/etc/debian_version") 27 | platform "debian" 28 | platform_version File.read("/etc/debian_version").chomp 29 | elsif File.exists?("/etc/redhat-release") 30 | platform "redhat" 31 | File.open("/etc/redhat-release").each do |line| 32 | platform "centos" if line =~ /centos/i 33 | case line 34 | when /\(Rawhide\)/ 35 | platform_version "rawhide" 36 | when /release (\d+)/ 37 | platform_version $1 38 | end 39 | end 40 | elsif File.exists?('/etc/gentoo-release') 41 | platform "gentoo" 42 | platform_version IO.read('/etc/gentoo-release').scan(/(\d+|\.+)/).join 43 | end 44 | -------------------------------------------------------------------------------- /README.rdoc: -------------------------------------------------------------------------------- 1 | = ohai 2 | 3 | * http://wiki.opscode.com/display/ohai 4 | 5 | == DESCRIPTION: 6 | 7 | Ohai detects data about your operating system. 8 | 9 | I'm in yur server, findin yer dater 10 | 11 | == SYNOPSIS: 12 | 13 | ./bin/ohai 14 | 15 | Will print out a JSON structure for all the known data about 16 | your system. 17 | 18 | == REQUIREMENTS: 19 | 20 | * rake 21 | * json 22 | * rspec 23 | * extlib 24 | 25 | == INSTALL: 26 | 27 | * git clone git@github.com:hjkp/ohai.git 28 | * cd ohai 29 | * rake install 30 | 31 | == SPECIAL THANKS: 32 | 33 | Ohai was significantly easier to build because of the pre-existing 34 | work of everyone who contributed to Facter, especially Luke Kanies 35 | of Reductive Labs. Thanks, guys. (We didn't use your code, but we 36 | were strongly inspired by it.) 37 | 38 | == LICENSE: 39 | 40 | Ohai - system information application 41 | 42 | Author:: Adam Jacob () 43 | Copyright:: Copyright (c) 2008 Opscode, Inc. 44 | License:: Apache License, Version 2.0 45 | 46 | Licensed under the Apache License, Version 2.0 (the "License"); 47 | you may not use this file except in compliance with the License. 48 | You may obtain a copy of the License at 49 | 50 | http://www.apache.org/licenses/LICENSE-2.0 51 | 52 | Unless required by applicable law or agreed to in writing, software 53 | distributed under the License is distributed on an "AS IS" BASIS, 54 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 55 | See the License for the specific language governing permissions and 56 | limitations under the License. 57 | -------------------------------------------------------------------------------- /spec/ohai/plugins/freebsd/platform_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | 20 | require File.join(File.dirname(__FILE__), '..', '..', '..', '/spec_helper.rb') 21 | 22 | describe Ohai::System, "FreeBSD plugin platform" do 23 | before(:each) do 24 | @ohai = Ohai::System.new 25 | @ohai.stub!(:require_plugin).and_return(true) 26 | @ohai.stub!(:from).with("uname -s").and_return("FreeBSD") 27 | @ohai.stub!(:from).with("uname -r").and_return("7.1") 28 | @ohai[:os] = "freebsd" 29 | end 30 | 31 | it "should set platform to lowercased lsb[:id]" do 32 | @ohai._require_plugin("freebsd::platform") 33 | @ohai[:platform].should == "freebsd" 34 | end 35 | 36 | it "should set platform_version to lsb[:release]" do 37 | @ohai._require_plugin("freebsd::platform") 38 | @ohai[:platform_version].should == "7.1" 39 | end 40 | end 41 | -------------------------------------------------------------------------------- /spec/ohai/plugins/ohai_time_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | 20 | require File.join(File.dirname(__FILE__), '..', '..', '/spec_helper.rb') 21 | 22 | describe Ohai::System, "plugin ohai_time" do 23 | before(:each) do 24 | @ohai = Ohai::System.new 25 | @ohai.stub!(:require_plugin).and_return(true) 26 | end 27 | 28 | it "should get the current time" do 29 | Time.should_receive(:now) 30 | @ohai._require_plugin("ohai_time") 31 | end 32 | 33 | it "should turn the time into a floating point number" do 34 | time = Time.now 35 | time.should_receive(:to_f) 36 | Time.stub!(:now).and_return(time) 37 | @ohai._require_plugin("ohai_time") 38 | end 39 | 40 | it "should set ohai_time to the current time" do 41 | time = Time.now 42 | Time.stub!(:now).and_return(time) 43 | @ohai._require_plugin("ohai_time") 44 | @ohai[:ohai_time].should == time.to_f 45 | end 46 | end -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | begin 2 | require 'spec' 3 | rescue LoadError 4 | require 'rubygems' 5 | gem 'rspec' 6 | require 'spec' 7 | end 8 | 9 | $:.unshift(File.dirname(__FILE__) + '/../lib') 10 | require 'ohai' 11 | Ohai::Config[:log_level] = :error 12 | 13 | def it_should_check_from(plugin, attribute, from, value) 14 | it "should get the #{attribute} value from '#{from}'" do 15 | @ohai.should_receive(:from).with(from).and_return(value) 16 | @ohai._require_plugin(plugin) 17 | end 18 | 19 | it "should set the #{attribute} to the value from '#{from}'" do 20 | @ohai._require_plugin(plugin) 21 | @ohai[attribute].should == value 22 | end 23 | end 24 | 25 | def it_should_check_from_mash(plugin, attribute, from, value) 26 | it "should get the #{plugin}[:#{attribute}] value from '#{from}'" do 27 | @ohai.should_receive(:from).with(from).and_return(value) 28 | @ohai._require_plugin(plugin) 29 | end 30 | 31 | it "should set the #{plugin}[:#{attribute}] to the value from '#{from}'" do 32 | @ohai._require_plugin(plugin) 33 | @ohai[plugin][attribute].should == value 34 | end 35 | end 36 | 37 | def it_should_check_from_deep_mash(plugin, mash, attribute, from, value) 38 | it "should get the #{mash}[:#{attribute}] value from '#{from}'" do 39 | @ohai.should_receive(:from).with(from).and_return(value) 40 | @ohai._require_plugin(plugin) 41 | end 42 | 43 | it "should set the #{mash}[:#{attribute}] to the value from '#{from}'" do 44 | @ohai._require_plugin(plugin) 45 | @ohai[mash][attribute].should == value 46 | end 47 | end 48 | 49 | module SimpleFromFile 50 | def from_file(filename) 51 | self.instance_eval(IO.read(filename), filename, 1) 52 | end 53 | end 54 | -------------------------------------------------------------------------------- /lib/ohai/plugins/solaris2/cpu.rb: -------------------------------------------------------------------------------- 1 | #$ psrinfo -v 2 | #Status of virtual processor 0 as of: 01/11/2009 23:31:55 3 | # on-line since 05/29/2008 15:05:28. 4 | # The i386 processor operates at 2660 MHz, 5 | # and has an i387 compatible floating point processor. 6 | #Status of virtual processor 1 as of: 01/11/2009 23:31:55 7 | # on-line since 05/29/2008 15:05:30. 8 | # The i386 processor operates at 2660 MHz, 9 | # and has an i387 compatible floating point processor. 10 | #Status of virtual processor 2 as of: 01/11/2009 23:31:55 11 | # on-line since 05/29/2008 15:05:30. 12 | # The i386 processor operates at 2660 MHz, 13 | # and has an i387 compatible floating point processor. 14 | #Status of virtual processor 3 as of: 01/11/2009 23:31:55 15 | # on-line since 05/29/2008 15:05:30. 16 | # The i386 processor operates at 2660 MHz, 17 | # and has an i387 compatible floating point processor. 18 | #Status of virtual processor 4 as of: 01/11/2009 23:31:55 19 | # on-line since 05/29/2008 15:05:30. 20 | # The i386 processor operates at 2660 MHz, 21 | # and has an i387 compatible floating point processor. 22 | #Status of virtual processor 5 as of: 01/11/2009 23:31:55 23 | # on-line since 05/29/2008 15:05:30. 24 | # The i386 processor operates at 2660 MHz, 25 | # and has an i387 compatible floating point processor. 26 | #Status of virtual processor 6 as of: 01/11/2009 23:31:55 27 | # on-line since 05/29/2008 15:05:30. 28 | # The i386 processor operates at 2660 MHz, 29 | # and has an i387 compatible floating point processor. 30 | #Status of virtual processor 7 as of: 01/11/2009 23:31:55 31 | # on-line since 05/29/2008 15:05:30. 32 | # The i386 processor operates at 2660 MHz, 33 | # and has an i387 compatible floating point processor. 34 | -------------------------------------------------------------------------------- /spec/ohai/plugins/darwin/kernel_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | 20 | require File.join(File.dirname(__FILE__), '..', '..', '..', '/spec_helper.rb') 21 | 22 | describe Ohai::System, "Darwin kernel plugin" do 23 | before(:each) do 24 | @ohai = Ohai::System.new 25 | @ohai.stub!(:require_plugin).and_return(true) 26 | @ohai[:kernel] = Mash.new 27 | @ohai[:kernel][:name] = "darwin" 28 | end 29 | 30 | it "should not set kernel_machine to x86_64" do 31 | @ohai.stub!(:from).with("sysctl -n hw.optional.x86_64").and_return("0") 32 | @ohai._require_plugin("darwin::kernel") 33 | @ohai[:kernel][:machine].should_not == 'x86_64' 34 | end 35 | 36 | it "should set kernel_machine to x86_64" do 37 | @ohai.stub!(:from).with("sysctl -n hw.optional.x86_64").and_return("1") 38 | @ohai._require_plugin("darwin::kernel") 39 | @ohai[:kernel][:machine].should == 'x86_64' 40 | end 41 | 42 | it "should set the kernel_os to the kernel_name value" do 43 | @ohai._require_plugin("darwin::kernel") 44 | @ohai[:kernel][:os].should == @ohai[:kernel][:name] 45 | end 46 | end -------------------------------------------------------------------------------- /spec/ohai/plugins/ruby_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | 20 | require File.join(File.dirname(__FILE__), '..', '..', '/spec_helper.rb') 21 | 22 | describe Ohai::System, "plugin ruby" do 23 | 24 | before(:each) do 25 | @ohai = Ohai::System.new 26 | @ohai[:languages] = Mash.new 27 | @ohai.stub!(:require_plugin).and_return(true) 28 | end 29 | 30 | { 31 | :platform => RUBY_PLATFORM, 32 | :version => RUBY_VERSION, 33 | :release_date => RUBY_RELEASE_DATE, 34 | :target => ::Config::CONFIG['target'], 35 | :target_cpu => ::Config::CONFIG['target_cpu'], 36 | :target_vendor => ::Config::CONFIG['target_vendor'], 37 | :target_os => ::Config::CONFIG['target_os'], 38 | :host => ::Config::CONFIG['host'], 39 | :host_cpu => ::Config::CONFIG['host_cpu'], 40 | :host_os => ::Config::CONFIG['host_os'], 41 | :host_vendor => ::Config::CONFIG['host_vendor'] 42 | }.each do |attribute, value| 43 | it "should have #{attribute} set" do 44 | @ohai._require_plugin("ruby") 45 | @ohai[:languages][:ruby][attribute].should eql(value) 46 | end 47 | end 48 | 49 | end -------------------------------------------------------------------------------- /spec/ohai/plugins/kernel_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | 20 | require File.join(File.dirname(__FILE__), '..', '..', '/spec_helper.rb') 21 | 22 | describe Ohai::System, "plugin kernel" do 23 | before(:each) do 24 | @ohai = Ohai::System.new 25 | @ohai.stub!(:require_plugin).and_return(true) 26 | @ohai.stub!(:from).with("uname -s").and_return("Darwin") 27 | @ohai.stub!(:from).with("uname -r").and_return("9.5.0") 28 | @ohai.stub!(:from).with("uname -v").and_return("Darwin Kernel Version 9.5.0: Wed Sep 3 11:29:43 PDT 2008; root:xnu-1228.7.58~1\/RELEASE_I386") 29 | @ohai.stub!(:from).with("uname -m").and_return("i386") 30 | @ohai.stub!(:from).with("uname -o").and_return("Linux") 31 | end 32 | 33 | it_should_check_from_mash("kernel", "name", "uname -s", "Darwin") 34 | 35 | it_should_check_from_mash("kernel", "release", "uname -r", "9.5.0") 36 | 37 | it_should_check_from_mash("kernel", "version", "uname -v", "Darwin Kernel Version 9.5.0: Wed Sep 3 11:29:43 PDT 2008; root:xnu-1228.7.58~1\/RELEASE_I386") 38 | 39 | it_should_check_from_mash("kernel", "machine", "uname -m", "i386") 40 | 41 | end -------------------------------------------------------------------------------- /lib/ohai/plugins/freebsd/filesystem.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | provides "filesystem" 20 | 21 | fs = Mash.new 22 | 23 | # Grab filesystem data from df 24 | popen4("df") do |pid, stdin, stdout, stderr| 25 | stdin.close 26 | stdout.each do |line| 27 | case line 28 | when /^Filesystem/ 29 | next 30 | when /^(.+?)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+\%)\s+(.+)$/ 31 | filesystem = $1 32 | fs[filesystem] = Mash.new 33 | fs[filesystem][:kb_size] = $2 34 | fs[filesystem][:kb_used] = $3 35 | fs[filesystem][:kb_available] = $4 36 | fs[filesystem][:percent_used] = $5 37 | fs[filesystem][:mount] = $6 38 | end 39 | end 40 | end 41 | 42 | # Grab mount information from mount 43 | popen4("mount -l") do |pid, stdin, stdout, stderr| 44 | stdin.close 45 | stdout.each do |line| 46 | if line =~ /^(.+?) on (.+?) \((.+?), (.+?)\)$/ 47 | filesystem = $1 48 | fs[filesystem] = Mash.new unless fs.has_key?(filesystem) 49 | fs[filesystem][:mount] = $2 50 | fs[filesystem][:fs_type] = $3 51 | fs[filesystem][:mount_options] = $4.split(/,\s*/) 52 | end 53 | end 54 | end 55 | 56 | # Set the filesystem data 57 | filesystem fs 58 | -------------------------------------------------------------------------------- /spec/ohai/plugins/linux/hostname_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | 20 | require File.join(File.dirname(__FILE__), '..', '..', '..', '/spec_helper.rb') 21 | 22 | describe Ohai::System, "Linux hostname plugin" do 23 | before(:each) do 24 | @ohai = Ohai::System.new 25 | @ohai.stub!(:require_plugin).and_return(true) 26 | @ohai[:os] = "linux" 27 | @ohai.stub!(:from).with("hostname").and_return("katie") 28 | @ohai.stub!(:from).with("hostname --fqdn").and_return("katie.bethell") 29 | end 30 | 31 | it_should_check_from("linux::hostname", "hostname", "hostname", "katie") 32 | 33 | it_should_check_from("linux::hostname", "fqdn", "hostname --fqdn", "katie.bethell") 34 | 35 | describe "when domain name is unset" do 36 | before(:each) do 37 | @ohai.should_receive(:from).with("hostname --fqdn").and_raise("Ohai::Exception::Exec") 38 | end 39 | 40 | it "should not raise an error" do 41 | lambda { @ohai._require_plugin("linux::hostname") }.should_not raise_error 42 | end 43 | 44 | it "should not set fqdn" do 45 | @ohai._require_plugin("linux::hostname") 46 | @ohai.fqdn.should == nil 47 | end 48 | 49 | end 50 | 51 | end 52 | 53 | -------------------------------------------------------------------------------- /lib/ohai/plugins/linux/filesystem.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | provides "filesystem" 20 | 21 | fs = Mash.new 22 | 23 | # Grab filesystem data from df 24 | popen4("df -P") do |pid, stdin, stdout, stderr| 25 | stdin.close 26 | stdout.each do |line| 27 | case line 28 | when /^Filesystem\s+1024-blocks/ 29 | next 30 | when /^(.+?)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+\%)\s+(.+)$/ 31 | filesystem = $1 32 | fs[filesystem] = Mash.new 33 | fs[filesystem][:kb_size] = $2 34 | fs[filesystem][:kb_used] = $3 35 | fs[filesystem][:kb_available] = $4 36 | fs[filesystem][:percent_used] = $5 37 | fs[filesystem][:mount] = $6 38 | end 39 | end 40 | end 41 | 42 | # Grab mount information from /bin/mount 43 | popen4("mount -l") do |pid, stdin, stdout, stderr| 44 | stdin.close 45 | stdout.each do |line| 46 | if line =~ /^(.+?) on (.+?) type (.+?) \((.+?)\)$/ 47 | filesystem = $1 48 | fs[filesystem] = Mash.new unless fs.has_key?(filesystem) 49 | fs[filesystem][:mount] = $2 50 | fs[filesystem][:fs_type] = $3 51 | fs[filesystem][:mount_options] = $4.split(",") 52 | end 53 | end 54 | end 55 | 56 | # Set the filesystem data 57 | filesystem fs 58 | -------------------------------------------------------------------------------- /spec/ohai/plugins/os_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | 20 | require File.join(File.dirname(__FILE__), '..', '..', '/spec_helper.rb') 21 | 22 | describe Ohai::System, "plugin os" do 23 | before(:each) do 24 | @ohai = Ohai::System.new 25 | @ohai.stub!(:require_plugin).and_return(true) 26 | @ohai[:languages] = Mash.new 27 | @ohai[:languages][:ruby] = Mash.new 28 | @ohai[:kernel] = Mash.new 29 | @ohai[:kernel][:release] = "kings of leon" 30 | end 31 | 32 | it "should set os_version to kernel_release" do 33 | @ohai._require_plugin("os") 34 | @ohai[:os_version].should == @ohai[:kernel][:release] 35 | end 36 | 37 | describe "on linux" do 38 | before(:each) do 39 | @ohai[:languages][:ruby][:host_os] = "linux" 40 | end 41 | 42 | it "should set the os to linux" do 43 | @ohai._require_plugin("os") 44 | @ohai[:os].should == "linux" 45 | end 46 | end 47 | 48 | describe "on darwin" do 49 | before(:each) do 50 | @ohai[:languages][:ruby][:host_os] = "darwin" 51 | end 52 | 53 | it "should set the os to darwin" do 54 | @ohai._require_plugin("os") 55 | @ohai[:os].should == "darwin" 56 | end 57 | end 58 | end -------------------------------------------------------------------------------- /lib/ohai/plugins/darwin/filesystem.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Benjamin Black () 3 | # Copyright:: Copyright (c) 2009 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 | provides "filesystem" 20 | 21 | fs = Mash.new 22 | 23 | block_size = 0 24 | popen4("df") do |pid, stdin, stdout, stderr| 25 | stdin.close 26 | stdout.each do |line| 27 | case line 28 | when /^Filesystem\s+(\d+)-/ 29 | block_size = $1.to_i 30 | next 31 | when /^(.+?)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+\%)\s+(.+)$/ 32 | filesystem = $1 33 | fs[filesystem] = Mash.new 34 | fs[filesystem][:block_size] = block_size 35 | fs[filesystem][:kb_size] = $2.to_i / (1024 / block_size) 36 | fs[filesystem][:kb_used] = $3.to_i / (1024 / block_size) 37 | fs[filesystem][:kb_available] = $4.to_i / (1024 / block_size) 38 | fs[filesystem][:percent_used] = $5 39 | fs[filesystem][:mount] = $6 40 | end 41 | end 42 | end 43 | 44 | popen4("mount") do |pid, stdin, stdout, stderr| 45 | stdin.close 46 | stdout.each do |line| 47 | if line =~ /^(.+?) on (.+?) \((.+?), (.+?)\)$/ 48 | filesystem = $1 49 | fs[filesystem] = Mash.new unless fs.has_key?(filesystem) 50 | fs[filesystem][:mount] = $2 51 | fs[filesystem][:fs_type] = $3 52 | fs[filesystem][:mount_options] = $4.split(/,\s*/) 53 | end 54 | end 55 | end 56 | 57 | filesystem fs -------------------------------------------------------------------------------- /spec/ohai/log/log_formatter_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 -------------------------------------------------------------------------------- /lib/ohai/log/formatter.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 -------------------------------------------------------------------------------- /spec/ohai/mixin/from_file_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 File.join(File.dirname(__FILE__), '..', '..', '/spec_helper.rb') 20 | 21 | describe Ohai::System, "from_file" do 22 | before(:each) do 23 | @ohai = Ohai::System.new 24 | File.stub!(:exists?).and_return(true) 25 | File.stub!(:readable?).and_return(true) 26 | IO.stub!(:read).and_return("king 'herod'") 27 | end 28 | 29 | it "should check to see that the file exists" do 30 | File.should_receive(:exists?).and_return(true) 31 | @ohai.from_file("/tmp/foo") 32 | end 33 | 34 | it "should check to see that the file is readable" do 35 | File.should_receive(:readable?).and_return(true) 36 | @ohai.from_file("/tmp/foo") 37 | end 38 | 39 | it "should actually read the file" do 40 | IO.should_receive(:read).and_return("king 'herod'") 41 | @ohai.from_file("/tmp/foo") 42 | end 43 | 44 | it "should call instance_eval with the contents of the file, file name, and line 1" do 45 | @ohai.should_receive(:instance_eval).with("king 'herod'", "/tmp/foo", 1) 46 | @ohai.from_file("/tmp/foo") 47 | end 48 | 49 | it "should raise an IOError if it cannot read the file" do 50 | File.stub!(:exists?).and_return(false) 51 | lambda { @ohai.from_file("/tmp/foo") }.should raise_error(IOError) 52 | end 53 | end 54 | -------------------------------------------------------------------------------- /lib/ohai/plugins/linux/cpu.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | provides "cpu" 20 | 21 | cpuinfo = Mash.new 22 | real_cpu = Mash.new 23 | cpu_number = 0 24 | current_cpu = nil 25 | 26 | File.open("/proc/cpuinfo").each do |line| 27 | case line 28 | when /processor\s+:\s(.+)/ 29 | cpuinfo[$1] = Mash.new 30 | current_cpu = $1 31 | cpu_number += 1 32 | when /vendor_id\s+:\s(.+)/ 33 | cpuinfo[current_cpu]["vendor_id"] = $1 34 | when /cpu family\s+:\s(.+)/ 35 | cpuinfo[current_cpu]["family"] = $1 36 | when /model\s+:\s(.+)/ 37 | cpuinfo[current_cpu]["model"] = $1 38 | when /stepping\s+:\s(.+)/ 39 | cpuinfo[current_cpu]["stepping"] = $1 40 | when /physical id\s+:\s(.+)/ 41 | cpuinfo[current_cpu]["physical_id"] = $1 42 | real_cpu[$1] = true 43 | when /core id\s+:\s(.+)/ 44 | cpuinfo[current_cpu]["core_id"] = $1 45 | when /cpu cores\s+:\s(.+)/ 46 | cpuinfo[current_cpu]["cores"] = $1 47 | when /model name\s+:\s(.+)/ 48 | cpuinfo[current_cpu]["model_name"] = $1 49 | when /cpu MHz\s+:\s(.+)/ 50 | cpuinfo[current_cpu]["mhz"] = $1 51 | when /cache size\s+:\s(.+)/ 52 | cpuinfo[current_cpu]["cache_size"] = $1 53 | when /flags\s+:\s(.+)/ 54 | cpuinfo[current_cpu]["flags"] = $1.split(' ') 55 | end 56 | end 57 | 58 | cpu cpuinfo 59 | cpu[:total] = cpu_number 60 | cpu[:real] = real_cpu.keys.length -------------------------------------------------------------------------------- /bin/ohai: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # 3 | # ./ohai - I'm in ur serverz, showin you the daters 4 | # 5 | # Author:: Adam Jacob () 6 | # Copyright:: Copyright (c) 2008 Opscode, Inc. 7 | # License:: Apache License, Version 2.0 8 | # 9 | # Licensed under the Apache License, Version 2.0 (the "License"); 10 | # you may not use this file except in compliance with the License. 11 | # You may obtain a copy of the License at 12 | # 13 | # http://www.apache.org/licenses/LICENSE-2.0 14 | # 15 | # Unless required by applicable law or agreed to in writing, software 16 | # distributed under the License is distributed on an "AS IS" BASIS, 17 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | # See the License for the specific language governing permissions and 19 | # limitations under the License. 20 | # 21 | 22 | $: << File.join(File.dirname(__FILE__), "..", "lib") 23 | require 'optparse' 24 | require 'rubygems' 25 | require 'ohai' 26 | require 'json' 27 | 28 | config = { 29 | :directory => nil, 30 | :file => nil, 31 | :log_level => :info 32 | } 33 | 34 | opts = OptionParser.new do |opts| 35 | opts.banner = "Usage: #{$0} (options)" 36 | opts.on("-d", "--directory NAME", "A directory to add to the Ohai search path") do |d| 37 | config[:directory] = d 38 | end 39 | opts.on("-f", "--file NAME", "A file to run Ohai against") do |f| 40 | config[:file] = f 41 | end 42 | opts.on("-l", "--loglevel NAME", "Set log level for Ohai") do |l| 43 | config[:log_level] = l.to_sym 44 | end 45 | opts.on_tail("-h", "--help", "Show this message") do 46 | puts opts 47 | exit 48 | end 49 | end 50 | opts.parse!(ARGV) 51 | 52 | ohai = Ohai::System.new 53 | 54 | if [ :debug, :info, :warn, :error, :fatal ].include?(config[:log_level]) 55 | Ohai::Config[:log_level] = config[:log_level] 56 | end 57 | if config[:file] 58 | ohai.from_file(config[:file]) 59 | else 60 | if config[:directory] 61 | Ohai::Config[:plugin_path] << config[:directory] 62 | end 63 | ohai.all_plugins 64 | end 65 | 66 | puts ohai.json_pretty_print 67 | -------------------------------------------------------------------------------- /spec/ohai/plugins/platform_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | 20 | require File.join(File.dirname(__FILE__), '..', '..', '/spec_helper.rb') 21 | 22 | describe Ohai::System, "plugin platform" do 23 | before(:each) do 24 | @ohai = Ohai::System.new 25 | @ohai.stub!(:require_plugin).and_return(true) 26 | @ohai[:os] = 'monkey' 27 | @ohai[:os_version] = 'poop' 28 | end 29 | 30 | it "should require the os platform plugin" do 31 | @ohai.should_receive(:require_plugin).with("monkey::platform") 32 | @ohai._require_plugin("platform") 33 | end 34 | 35 | it "should set the platform to the os if it was not set earlier" do 36 | @ohai._require_plugin("platform") 37 | @ohai[:platform].should eql("monkey") 38 | end 39 | 40 | it "should not set the platform to the os if it was set earlier" do 41 | @ohai[:platform] = 'lars' 42 | @ohai._require_plugin("platform") 43 | @ohai[:platform].should eql("lars") 44 | end 45 | 46 | it "should set the platform_version to the os_version if it was not set earlier" do 47 | @ohai._require_plugin("platform") 48 | @ohai[:os_version].should eql("poop") 49 | end 50 | 51 | it "should not set the platform to the os if it was set earlier" do 52 | @ohai[:platform_version] = 'ulrich' 53 | @ohai._require_plugin("platform") 54 | @ohai[:platform_version].should eql("ulrich") 55 | end 56 | end -------------------------------------------------------------------------------- /lib/ohai/plugins/network.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | provides "network", "counters/network" 20 | 21 | network Mash.new unless network 22 | network[:interfaces] = Mash.new unless network[:interfaces] 23 | counters Mash.new unless counters 24 | counters[:network] = Mash.new unless counters[:network] 25 | 26 | require_plugin "hostname" 27 | require_plugin "#{os}::network" 28 | 29 | def find_ip_and_mac(addresses) 30 | ip = nil; mac = nil 31 | addresses.keys.each do |addr| 32 | ip = addr if addresses[addr]["family"].eql?("inet") 33 | mac = addr if addresses[addr]["family"].eql?("lladdr") 34 | break if (ip and mac) 35 | end 36 | [ip, mac] 37 | end 38 | 39 | if attribute?("default_interface") 40 | im = find_ip_and_mac(network["interfaces"][iface]["addresses"]) 41 | ipaddress im.shift 42 | macaddress im.shift 43 | else 44 | unless network[:default_interface].nil? 45 | im = find_ip_and_mac(network["interfaces"][network[:default_interface]]["addresses"]) 46 | ipaddress im.shift 47 | macaddress im.shift 48 | else 49 | network["interfaces"].keys.sort.each do |iface| 50 | if network["interfaces"][iface]["encapsulation"].eql?("Ethernet") 51 | im = find_ip_and_mac(network["interfaces"][iface]["addresses"]) 52 | ipaddress im.shift 53 | macaddress im.shift 54 | return if (ipaddress and macaddress) 55 | end 56 | end 57 | end 58 | end 59 | -------------------------------------------------------------------------------- /lib/ohai/plugins/freebsd/cpu.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Bryan McLellan (btm@loftninjas.org) 3 | # Copyright:: Copyright (c) 2008 Bryan McLellan 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 | provides "cpu" 20 | 21 | # all dmesg output for smp I can find only provides info about a single processor 22 | # identical processors is probably a hardware requirement so we'll duplicate data for each cpu 23 | # old examples: http://www.bnv-bamberg.de/home/ba3294/smp/rbuild/index.htm 24 | cpuinfo = Mash.new 25 | 26 | # /var/run/dmesg.boot 27 | #CPU: QEMU Virtual CPU version 0.9.1 (1862.02-MHz 686-class CPU) 28 | # Origin = "GenuineIntel" Id = 0x623 Stepping = 3 29 | # Features=0x78bfbfd 30 | # Features2=0x80000001> 31 | 32 | File.open("/var/run/dmesg.boot").each do |line| 33 | case line 34 | when /CPU:\s+(.+) \(([\d.]+).+\)/ 35 | cpuinfo["model_name"] = $1 36 | cpuinfo["mhz"] = $2 37 | when /Origin = "(.+)"\s+Id = (.+)\s+Stepping = (.+)/ 38 | cpuinfo["vendor_id"] = $1 39 | cpuinfo["stepping"] = $3 40 | # These _should_ match /AMD Features2?/ lines as well 41 | when /Features=.+<(.+)>/ 42 | cpuinfo["flags"] = $1.downcase.split(',') 43 | # Features2=0x80000001> 44 | when /Features2=[a-f\dx]+<(.+)>/ 45 | cpuinfo["flags"].concat($1.downcase.split(',')) 46 | when /Logical CPUs per core: (\d+)/ 47 | cpuinfo["cores"] = $1 48 | end 49 | end 50 | 51 | cpu cpuinfo 52 | cpu[:total] = from("sysctl -n hw.ncpu") 53 | -------------------------------------------------------------------------------- /lib/ohai/plugins/freebsd/memory.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Bryan McLellan (btm@loftninjas.org) 3 | # Copyright:: Copyright (c) 2009 Bryan McLellan 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 | provides "memory" 20 | 21 | memory Mash.new 22 | memory[:swap] = Mash.new 23 | 24 | # /usr/src/sys/sys/vmmeter.h 25 | memory[:page_size] = from("sysctl -n vm.stats.vm.v_page_size") 26 | memory[:page_count] = from("sysctl -n vm.stats.vm.v_page_count") 27 | memory[:total] = memory[:page_size].to_i * memory[:page_count].to_i 28 | memory[:free] = memory[:page_size].to_i * from("sysctl -n vm.stats.vm.v_free_count").to_i 29 | memory[:active] = memory[:page_size].to_i * from("sysctl -n vm.stats.vm.v_active_count").to_i 30 | memory[:inactive] = memory[:page_size].to_i * from("sysctl -n vm.stats.vm.v_inactive_count").to_i 31 | memory[:cache] = memory[:page_size].to_i * from("sysctl -n vm.stats.vm.v_cache_count").to_i 32 | memory[:wired] = memory[:page_size].to_i * from("sysctl -n vm.stats.vm.v_wire_count").to_i 33 | memory[:buffers] = from("sysctl -n vfs.bufspace") 34 | 35 | popen4("swapinfo") do |pid, stdin, stdout, stderr| 36 | stdin.close 37 | stdout.each do |line| 38 | # Device 1K-blocks Used Avail Capacity 39 | # /dev/ad0s1b 253648 0 253648 0% 40 | if line =~ /^([\d\w\/]+)\s+(\d+)\s+(\d+)\s+(\d+)\s+([\d\%]+)/ 41 | mdev = $1 42 | memory[:swap][mdev] = Mash.new 43 | memory[:swap][mdev][:total] = $2 44 | memory[:swap][mdev][:used] = $3 45 | memory[:swap][mdev][:free] = $4 46 | memory[:swap][mdev][:percent_free] = $5 47 | end 48 | end 49 | end 50 | 51 | -------------------------------------------------------------------------------- /spec/ohai/plugins/linux/lsb_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | 20 | require File.join(File.dirname(__FILE__), '..', '..', '..', '/spec_helper.rb') 21 | 22 | describe Ohai::System, "Linux lsb plugin" do 23 | before(:each) do 24 | @ohai = Ohai::System.new 25 | @ohai[:os] = "linux" 26 | @ohai.stub!(:require_plugin).and_return(true) 27 | @mock_file = mock("/etc/lsb-release") 28 | @mock_file.stub!(:each). 29 | and_yield("DISTRIB_ID=Ubuntu"). 30 | and_yield("DISTRIB_RELEASE=8.04"). 31 | and_yield("DISTRIB_CODENAME=hardy"). 32 | and_yield('DISTRIB_DESCRIPTION="Ubuntu 8.04"') 33 | File.stub!(:open).with("/etc/lsb-release").and_return(@mock_file) 34 | end 35 | 36 | it "should set lsb[:id]" do 37 | @ohai._require_plugin("linux::lsb") 38 | @ohai[:lsb][:id].should == "Ubuntu" 39 | end 40 | 41 | it "should set lsb[:release]" do 42 | @ohai._require_plugin("linux::lsb") 43 | @ohai[:lsb][:release].should == "8.04" 44 | end 45 | 46 | it "should set lsb[:codename]" do 47 | @ohai._require_plugin("linux::lsb") 48 | @ohai[:lsb][:codename].should == "hardy" 49 | end 50 | 51 | it "should set lsb[:description]" do 52 | @ohai._require_plugin("linux::lsb") 53 | @ohai[:lsb][:description].should == "\"Ubuntu 8.04\"" 54 | end 55 | 56 | it "should not set any lsb values if /etc/lsb-release cannot be read" do 57 | File.stub!(:open).with("/etc/lsb-release").and_raise(IOError) 58 | @ohai.attribute?(:lsb).should be(false) 59 | end 60 | end -------------------------------------------------------------------------------- /spec/ohai/log_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 -------------------------------------------------------------------------------- /spec/ohai/plugins/linux/uptime_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | 20 | require File.join(File.dirname(__FILE__), '..', '..', '..', '/spec_helper.rb') 21 | 22 | describe Ohai::System, "Linux plugin uptime" do 23 | before(:each) do 24 | @ohai = Ohai::System.new 25 | @ohai.stub!(:require_plugin).and_return(true) 26 | @ohai[:os] = "linux" 27 | @ohai._require_plugin("uptime") 28 | @mock_file = mock("/proc/uptime", { :gets => "18423 989" }) 29 | File.stub!(:open).with("/proc/uptime").and_return(@mock_file) 30 | end 31 | 32 | it "should check /proc/uptime for the uptime and idletime" do 33 | File.should_receive(:open).with("/proc/uptime").and_return(@mock_file) 34 | @ohai._require_plugin("linux::uptime") 35 | end 36 | 37 | it "should split the value of /proc uptime" do 38 | @mock_file.gets.should_receive(:split).with(" ").and_return(["18423", "989"]) 39 | @ohai._require_plugin("linux::uptime") 40 | end 41 | 42 | it "should set uptime_seconds to uptime" do 43 | @ohai._require_plugin("linux::uptime") 44 | @ohai[:uptime_seconds].should == 18423 45 | end 46 | 47 | it "should set uptime to a human readable date" do 48 | @ohai._require_plugin("linux::uptime") 49 | @ohai[:uptime].should == "5 hours 07 minutes 03 seconds" 50 | end 51 | 52 | it "should set idletime_seconds to uptime" do 53 | @ohai._require_plugin("linux::uptime") 54 | @ohai[:idletime_seconds].should == 989 55 | end 56 | 57 | it "should set idletime to a human readable date" do 58 | @ohai._require_plugin("linux::uptime") 59 | @ohai[:idletime].should == "16 minutes 29 seconds" 60 | end 61 | end -------------------------------------------------------------------------------- /lib/ohai/plugins/freebsd/virtualization.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Bryan McLellan (btm@loftninjas.org) 3 | # Copyright:: Copyright (c) 2009 Bryan McLellan 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 | provides "virtualization" 20 | 21 | virtualization Mash.new 22 | 23 | # KVM Host support for FreeBSD is in development 24 | # http://feanor.sssup.it/~fabio/freebsd/lkvm/ 25 | 26 | # Detect KVM/QEMU from cpu, report as KVM 27 | # hw.model: QEMU Virtual CPU version 0.9.1 28 | if from("sysctl -n hw.model") =~ /QEMU Virtual CPU/ 29 | virtualization[:system] = "kvm" 30 | virtualization[:role] = "guest" 31 | end 32 | 33 | # http://www.dmo.ca/blog/detecting-virtualization-on-linux 34 | if File.exists?("/usr/local/sbin/dmidecode") 35 | popen4("dmidecode") do |pid, stdin, stdout, stderr| 36 | stdin.close 37 | found_virt_manufacturer = nil 38 | found_virt_product = nil 39 | stdout.each do |line| 40 | case line 41 | when /Manufacturer: Microsoft/ 42 | found_virt_manufacturer = "microsoft" 43 | when /Product Name: Virtual Machine/ 44 | found_virt_product = "microsoft" 45 | when /Version: 5.0/ 46 | if found_virt_manufacturer == "microsoft" && found_virt_product == "microsoft" 47 | virtualization[:system] = "virtualpc" 48 | virtualization[:role] = "guest" 49 | end 50 | when /Version: VS2005R2/ 51 | if found_virt_manufacturer == "microsoft" && found_virt_product == "microsoft" 52 | virtualization[:system] = "virtualserver" 53 | virtualization[:role] = "guest" 54 | end 55 | when /Manufacturer: VMware/ 56 | found_virt_manufacturer = "vmware" 57 | when /Product Name: VMware Virtual Platform/ 58 | if found_virt_manufacturer == "vmware" 59 | virtualization[:system] = "vmware" 60 | virtualization[:role] = "guest" 61 | end 62 | end 63 | end 64 | end 65 | end 66 | 67 | -------------------------------------------------------------------------------- /spec/ohai/plugins/darwin/platform_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | 20 | require File.join(File.dirname(__FILE__), '..', '..', '..', '/spec_helper.rb') 21 | 22 | describe Ohai::System, "Darwin plugin platform" do 23 | before(:each) do 24 | @ohai = Ohai::System.new 25 | @ohai.stub!(:require_plugin).and_return(true) 26 | @ohai[:os] = "darwin" 27 | @pid = 10 28 | @stdin = mock("STDIN", { :close => true }) 29 | @stdout = mock("STDOUT") 30 | @stdout.stub!(:each). 31 | and_yield("ProductName: Mac OS X"). 32 | and_yield("ProductVersion: 10.5.5"). 33 | and_yield("BuildVersion: 9F33") 34 | @stderr = mock("STDERR") 35 | @ohai.stub!(:popen4).with("/usr/bin/sw_vers").and_yield(@pid, @stdin, @stdout, @stderr) 36 | end 37 | 38 | it "should run sw_vers" do 39 | @ohai.should_receive(:popen4).with("/usr/bin/sw_vers").and_return(true) 40 | @ohai._require_plugin("darwin::platform") 41 | end 42 | 43 | it "should close sw_vers stdin" do 44 | @stdin.should_receive(:close) 45 | @ohai._require_plugin("darwin::platform") 46 | end 47 | 48 | it "should iterate over each line of sw_vers stdout" do 49 | @stdout.should_receive(:each).and_return(true) 50 | @ohai._require_plugin("darwin::platform") 51 | end 52 | 53 | it "should set platform to ProductName, downcased with _ for \\s" do 54 | @ohai._require_plugin("darwin::platform") 55 | @ohai[:platform].should == "mac_os_x" 56 | end 57 | 58 | it "should set platform_version to ProductVersion" do 59 | @ohai._require_plugin("darwin::platform") 60 | @ohai[:platform_version].should == "10.5.5" 61 | end 62 | 63 | it "should set platform_build to BuildVersion" do 64 | @ohai._require_plugin("darwin::platform") 65 | @ohai[:platform_build].should == "9F33" 66 | end 67 | end -------------------------------------------------------------------------------- /spec/ohai/plugins/python_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | 20 | require File.join(File.dirname(__FILE__), '..', '..', '/spec_helper.rb') 21 | 22 | describe Ohai::System, "plugin python" do 23 | 24 | before(:each) do 25 | @ohai = Ohai::System.new 26 | @ohai[:languages] = Mash.new 27 | @ohai.stub!(:require_plugin).and_return(true) 28 | @pid = mock("PID", :null_object => true) 29 | @stderr = mock("STDERR", :null_object => true) 30 | @stdout = mock( 31 | "STDOUT", 32 | :null_object => true, 33 | :gets => "2.5.2 (r252:60911, Jan 4 2009, 17:40:26)\n[GCC 4.3.2]\n" 34 | ) 35 | @stdin = mock("STDIN", :null_object => true) 36 | @status = 0 37 | @ohai.stub!(:popen4).with("python -c \"import sys; print sys.version\"").and_yield( 38 | @pid, 39 | @stdin, 40 | @stdout, 41 | @stderr 42 | ).and_return(@status) 43 | end 44 | 45 | it "should get the python version from printing sys.version and sys.platform" do 46 | @ohai.should_receive(:popen4).with("python -c \"import sys; print sys.version\"").and_return(true) 47 | @ohai._require_plugin("python") 48 | end 49 | 50 | it "should close stdin" do 51 | @stdin.should_receive(:close) 52 | @ohai._require_plugin("python") 53 | end 54 | 55 | it "should read the version data from stdout" do 56 | @stdout.should_receive(:gets).and_return("2.5.2 (r252:60911, Jan 4 2009, 17:40:26)\n[GCC 4.3.2]\n") 57 | @ohai._require_plugin("python") 58 | end 59 | 60 | it "should set languages[:python][:version]" do 61 | @ohai._require_plugin("python") 62 | @ohai.languages[:python][:version].should eql("2.5.2") 63 | end 64 | 65 | it "should not set the languages[:python] tree up if python command fails" do 66 | @status = 1 67 | @ohai.stub!(:popen4).with("python -c \"import sys; print sys.version\"").and_yield( 68 | @pid, 69 | @stdin, 70 | @stdout, 71 | @stderr 72 | ).and_return(@status) 73 | @ohai._require_plugin("python") 74 | @ohai.languages.should_not have_key(:python) 75 | end 76 | 77 | end 78 | -------------------------------------------------------------------------------- /spec/ohai/plugins/ec2_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Tim Dysinger () 3 | # Author:: Christopher Brown (cb@opscode.com) 4 | # Copyright:: Copyright (c) 2008 Opscode, Inc. 5 | # License:: Apache License, Version 2.0 6 | # 7 | # Licensed under the Apache License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | # 19 | 20 | require File.join(File.dirname(__FILE__), '..', '..', '/spec_helper.rb') 21 | require 'open-uri' 22 | 23 | describe Ohai::System, "plugin ec2" do 24 | before(:each) do 25 | @ohai = Ohai::System.new 26 | @ohai.stub!(:require_plugin).and_return(true) 27 | @ohai[:network] = {:interfaces => {:eth0 => {} } } 28 | end 29 | 30 | describe "!ec2", :shared => true do 31 | it "should NOT attempt to fetch the ec2 metadata" do 32 | OpenURI.should_not_receive(:open) 33 | @ohai._require_plugin("ec2") 34 | end 35 | end 36 | 37 | describe "ec2", :shared => true do 38 | before(:each) do 39 | OpenURI.stub!(:open_uri). 40 | with("http://169.254.169.254/2008-02-01/meta-data/"). 41 | and_return(mock(IO, :read => "instance_type\nami_id\n")) 42 | OpenURI.stub!(:open_uri). 43 | with("http://169.254.169.254/2008-02-01/meta-data/instance_type"). 44 | and_return(mock(IO, :gets => "c1.medium")) 45 | OpenURI.stub!(:open_uri). 46 | with("http://169.254.169.254/2008-02-01/meta-data/ami_id"). 47 | and_return(mock(IO, :gets => "ami-5d2dc934")) 48 | OpenURI.stub!(:open_uri). 49 | with("http://169.254.169.254/2008-02-01/user-data/"). 50 | and_return(mock(IO, :gets => "By the pricking of my thumb...")) 51 | end 52 | 53 | it "should recursively fetch all the ec2 metadata" do 54 | @ohai._require_plugin("ec2") 55 | @ohai[:ec2].should_not be_nil 56 | @ohai[:ec2]['instance_type'].should == "c1.medium" 57 | @ohai[:ec2]['ami_id'].should == "ami-5d2dc934" 58 | end 59 | end 60 | 61 | describe "with ec2 mac and metadata address connected" do 62 | it_should_behave_like "ec2" 63 | 64 | before(:each) do 65 | IO.stub!(:select).and_return([[],[1],[]]) 66 | @ohai[:network][:interfaces][:eth0][:arp] = {"169.254.1.0"=>"fe:ff:ff:ff:ff:ff"} 67 | end 68 | end 69 | 70 | describe "without ec2 mac and metadata address connected" do 71 | it_should_behave_like "!ec2" 72 | 73 | before(:each) do 74 | @ohai[:network][:interfaces][:eth0][:arp] = {"169.254.1.0"=>"00:50:56:c0:00:08"} 75 | end 76 | end 77 | end 78 | -------------------------------------------------------------------------------- /lib/ohai/plugins/linux/virtualization.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Thom May () 3 | # Copyright:: Copyright (c) 2009 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 | provides "virtualization" 20 | 21 | virtualization Mash.new 22 | 23 | # if it is possible to detect paravirt vs hardware virt, it should be put in 24 | # virtualization[:mechanism] 25 | if File.exists?("/proc/xen/capabilities") && File.read("/proc/xen/capabilities") =~ /control_d/i 26 | virtualization[:emulator] = "xen" 27 | virtualization[:role] = "host" 28 | elsif File.exists?("/proc/sys/xen/independent_wallclock") 29 | virtualization[:emulator] = "xen" 30 | virtualization[:role] = "guest" 31 | end 32 | 33 | # Detect KVM hosts by kernel module 34 | if File.exists?("/proc/modules") 35 | if File.read("/proc/modules") =~ /^kvm/ 36 | virtualization[:emulator] = "kvm" 37 | virtualization[:role] = "host" 38 | end 39 | end 40 | 41 | # Detect KVM/QEMU from cpuinfo, report as KVM 42 | # We could pick KVM from 'Booting paravirtualized kernel on KVM' in dmesg 43 | # 2.6.27-9-server (intrepid) has this / 2.6.18-6-amd64 (etch) does not 44 | # It would be great if we could read pv_info in the kernel 45 | # Wait for reply to: http://article.gmane.org/gmane.comp.emulators.kvm.devel/27885 46 | if File.exists?("/proc/cpuinfo") 47 | if File.read("/proc/cpuinfo") =~ /QEMU Virtual CPU/ 48 | virtualization[:emulator] = "kvm" 49 | virtualization[:role] = "guest" 50 | end 51 | end 52 | 53 | # http://www.dmo.ca/blog/detecting-virtualization-on-linux 54 | if File.exists?("/usr/sbin/dmidecode") 55 | popen4("dmidecode") do |pid, stdin, stdout, stderr| 56 | stdin.close 57 | found_virt_manufacturer = nil 58 | stdout.each do |line| 59 | case line 60 | when /Manufacturer: Microsoft/ 61 | found_virt_manufacturer = "virtualpc" 62 | when / Product Name: Virtual Machine/ 63 | if found_virt_manufacturer == "virtualpc" 64 | virtualization[:emulator] = "virtualpc" 65 | virtualization[:role] = "guest" 66 | end 67 | when /Manufacturer: VMware/ 68 | found_virt_manufacturer = "vmware" 69 | when /Product Name: VMware Virtual Platform/ 70 | if found_virt_manufacturer == "vmware" 71 | virtualization[:emulator] = "vmware" 72 | virtualization[:role] = "guest" 73 | end 74 | end 75 | end 76 | end 77 | end 78 | -------------------------------------------------------------------------------- /spec/ohai/plugins/linux/platform_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | 20 | require File.join(File.dirname(__FILE__), '..', '..', '..', '/spec_helper.rb') 21 | 22 | describe Ohai::System, "Linux plugin platform" do 23 | before(:each) do 24 | @ohai = Ohai::System.new 25 | @ohai.stub!(:require_plugin).and_return(true) 26 | @ohai.extend(SimpleFromFile) 27 | @ohai[:os] = "linux" 28 | @ohai[:lsb] = Mash.new 29 | File.stub!(:exists?).with("/etc/debian_version").and_return(false) 30 | File.stub!(:exists?).with("/etc/redhat-release").and_return(false) 31 | File.stub!(:exists?).with("/etc/gentoo-release").and_return(false) 32 | end 33 | 34 | it "should require the lsb plugin" do 35 | @ohai.should_receive(:require_plugin).with("linux::lsb").and_return(true) 36 | @ohai._require_plugin("linux::platform") 37 | end 38 | 39 | describe "on lsb compliant distributions" do 40 | before(:each) do 41 | @ohai[:lsb][:id] = "Ubuntu" 42 | @ohai[:lsb][:release] = "8.04" 43 | end 44 | 45 | it "should set platform to lowercased lsb[:id]" do 46 | @ohai._require_plugin("linux::platform") 47 | @ohai[:platform].should == "ubuntu" 48 | end 49 | 50 | it "should set platform_version to lsb[:release]" do 51 | @ohai._require_plugin("linux::platform") 52 | @ohai[:platform_version].should == "8.04" 53 | end 54 | end 55 | 56 | describe "on debian" do 57 | before(:each) do 58 | @ohai.lsb = nil 59 | File.should_receive(:exists?).with("/etc/debian_version").and_return(true) 60 | end 61 | 62 | it "should check for the existance of debian_version" do 63 | @ohai._require_plugin("linux::platform") 64 | end 65 | 66 | it "should read the version from /etc/debian_version" do 67 | File.should_receive(:read).with("/etc/debian_version").and_return("5.0") 68 | @ohai._require_plugin("linux::platform") 69 | @ohai[:platform_version].should == "5.0" 70 | end 71 | 72 | it "should correctly strip any newlines" do 73 | File.should_receive(:read).with("/etc/debian_version").and_return("5.0\n") 74 | @ohai._require_plugin("linux::platform") 75 | @ohai[:platform_version].should == "5.0" 76 | end 77 | 78 | end 79 | 80 | 81 | end 82 | -------------------------------------------------------------------------------- /spec/ohai/plugins/erlang_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | 20 | require File.join(File.dirname(__FILE__), '..', '..', '/spec_helper.rb') 21 | 22 | describe Ohai::System, "plugin erlang" do 23 | 24 | before(:each) do 25 | @ohai = Ohai::System.new 26 | @ohai[:languages] = Mash.new 27 | @ohai.stub!(:require_plugin).and_return(true) 28 | @pid = mock("PID", :null_object => true) 29 | @stdout = mock("STDOUT", :null_object => true) 30 | @stderr = mock( 31 | "STDERR", 32 | :null_object => true, 33 | :gets => "Erlang (ASYNC_THREADS,SMP,HIPE) (BEAM) emulator version 5.6.2\n" 34 | ) 35 | @stdin = mock("STDIN", :null_object => true) 36 | @status = 0 37 | @ohai.stub!(:popen4).with("erl +V").and_yield( 38 | @pid, 39 | @stdin, 40 | @stdout, 41 | @stderr 42 | ).and_return(@status) 43 | end 44 | 45 | it "should get the erlang version from erl +V" do 46 | @ohai.should_receive(:popen4).with("erl +V").and_return(true) 47 | @ohai._require_plugin("erlang") 48 | end 49 | 50 | it "should close stdin" do 51 | @stdin.should_receive(:close) 52 | @ohai._require_plugin("erlang") 53 | end 54 | 55 | it "should read the version data from stderr" do 56 | @stderr.should_receive(:gets).and_return("Erlang (ASYNC_THREADS,SMP,HIPE) (BEAM) emulator version 5.6.2\n") 57 | @ohai._require_plugin("erlang") 58 | end 59 | 60 | it "should set languages[:erlang][:version]" do 61 | @ohai._require_plugin("erlang") 62 | @ohai.languages[:erlang][:version].should eql("5.6.2") 63 | end 64 | 65 | it "should set languages[:erlang][:options]" do 66 | @ohai._require_plugin("erlang") 67 | @ohai.languages[:erlang][:options].should eql(["ASYNC_THREADS", "SMP", "HIPE"]) 68 | end 69 | 70 | it "should set languages[:erlang][:emulator]" do 71 | @ohai._require_plugin("erlang") 72 | @ohai.languages[:erlang][:emulator].should eql("BEAM") 73 | end 74 | 75 | it "should not set the languages[:erlang] tree up if erlang command fails" do 76 | @status = 1 77 | @ohai.stub!(:popen4).with("erl +V").and_yield( 78 | @pid, 79 | @stdin, 80 | @stdout, 81 | @stderr 82 | ).and_return(@status) 83 | @ohai._require_plugin("erlang") 84 | @ohai.languages.should_not have_key(:erlang) 85 | end 86 | 87 | end -------------------------------------------------------------------------------- /CHANGELOG: -------------------------------------------------------------------------------- 1 | Thu Mar 5 23:15:03 PST 2009 2 | Release Notes - Ohai - Version 0.2.0 3 | http://tickets.opscode.com 4 | 5 | ** Bug 6 | * [OHAI-40] - virt and lsb spec's lack use of should/should_not 7 | * [OHAI-44] - darwin network.rb should always use -n on relevant commands 8 | * [OHAI-45] - vmnet interfaces confuse counter collection on darwin 9 | * [OHAI-46] - lo0 counters not collected on darwin 10 | * [OHAI-47] - virtualization plugin doesn't correctly detect VMware or VirtualPC platforms 11 | * [OHAI-51] - If hostname -f exits with 1, ohai linux hostname plugin vomits 12 | * [OHAI-54] - Use IO.read rather than cat 13 | * [OHAI-61] - several items in lib/ohai/plugins/freebsd/network.rb have not been converted to Mash 14 | * [OHAI-62] - ohai throws stack when python is not in path 15 | * [OHAI-63] - ohai does not properly detect 64-bit on darwin 16 | * [OHAI-64] - ohai/plugins/freebsd/kernel.rb does not set kernel[:os] 17 | 18 | ** Improvement 19 | * [OHAI-49] - network interface addresses should be stored in a hash not an array 20 | * [OHAI-50] - python language plugin 21 | * [OHAI-58] - Plugin specifications for FreeBSD 22 | 23 | ** New Feature 24 | * [OHAI-36] - Add libvirt-sourced data to ohai 25 | * [OHAI-57] - ohai needs a --debug option to show errors during collection 26 | 27 | ** Task 28 | * [OHAI-55] - Need to grab 'user-data' as well as 'meta-data' for EC2 29 | 30 | Sat Jan 31 19:05:49 PST 2009 31 | Release Notes - Ohai - Version 0.1.4 32 | http://tickets.opscode.com 33 | 34 | ** Bug 35 | * [OHAI-16] - 9 failing specs - investigate / patch 36 | * [OHAI-17] - The kitten must die 37 | * [OHAI-27] - linux network counters should have an extra array dimension for rx/tx 38 | * [OHAI-35] - Clean up files left around from non-simple newgem, add some of the ones that are generated to gitignore 39 | * [OHAI-42] - Properly detect Java env on RH/Fedora 40 | 41 | ** New Feature 42 | * [OHAI-4] - OpenSolaris plugins 43 | * [OHAI-18] - Erlang language plugin 44 | * [OHAI-20] - per-interface ARP table collection on linux and darwin 45 | * [OHAI-22] - sysctl network setting reporting for darwin 46 | * [OHAI-24] - not enough freebsd7 attributes for chef-client 47 | * [OHAI-26] - darwin network interface counters 48 | * [OHAI-33] - ec2 metadata should show up in ohai 49 | * [OHAI-37] - add [:command][:ps] to freebsd 50 | * [OHAI-38] - report langauges::java 51 | 52 | Thu Jan 15 11:13:45 PST 2009 53 | Release Notes - Ohai - Version 0.1.2 54 | http://tickets.opscode.com 55 | 56 | ** Bug 57 | * [OHAI-2] - Unit tests and Integration tests must all pass 58 | * [OHAI-13] - need command[:ps] attribute for node 59 | * [OHAI-15] - Ohai network plugin is broken on osx 60 | 61 | ** Improvement 62 | * [OHAI-10] - Refactor node data structure for bb's sanity 63 | * [OHAI-12] - Refactor network interface section(s) 64 | 65 | ** New Feature 66 | * [OHAI-1] - Ensure Ohai gem builds completely 67 | * [OHAI-8] - Add ssh host key attributes for Linux and Darwin 68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /lib/ohai/log.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 -------------------------------------------------------------------------------- /lib/ohai/plugins/linux/memory.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | provides "memory" 20 | 21 | memory Mash.new 22 | memory[:swap] = Mash.new 23 | 24 | File.open("/proc/meminfo").each do |line| 25 | case line 26 | when /^MemTotal:\s+(\d+) (.+)$/ 27 | memory[:total] = "#{$1}#{$2}" 28 | when /^MemFree:\s+(\d+) (.+)$/ 29 | memory[:free] = "#{$1}#{$2}" 30 | when /^Buffers:\s+(\d+) (.+)$/ 31 | memory[:buffers] = "#{$1}#{$2}" 32 | when /^Cached:\s+(\d+) (.+)$/ 33 | memory[:cached] = "#{$1}#{$2}" 34 | when /^Active:\s+(\d+) (.+)$/ 35 | memory[:active] = "#{$1}#{$2}" 36 | when /^Inactive:\s+(\d+) (.+)$/ 37 | memory[:inactive] = "#{$1}#{$2}" 38 | when /^HighTotal:\s+(\d+) (.+)$/ 39 | memory[:high_total] = "#{$1}#{$2}" 40 | when /^HighFree:\s+(\d+) (.+)$/ 41 | memory[:high_free] = "#{$1}#{$2}" 42 | when /^LowTotal:\s+(\d+) (.+)$/ 43 | memory[:low_total] = "#{$1}#{$2}" 44 | when /^LowFree:\s+(\d+) (.+)$/ 45 | memory[:low_free] = "#{$1}#{$2}" 46 | when /^Dirty:\s+(\d+) (.+)$/ 47 | memory[:dirty] = "#{$1}#{$2}" 48 | when /^Writeback:\s+(\d+) (.+)$/ 49 | memory[:writeback] = "#{$1}#{$2}" 50 | when /^AnonPages:\s+(\d+) (.+)$/ 51 | memory[:anon_pages] = "#{$1}#{$2}" 52 | when /^Mapped:\s+(\d+) (.+)$/ 53 | memory[:mapped] = "#{$1}#{$2}" 54 | when /^Slab:\s+(\d+) (.+)$/ 55 | memory[:slab] = "#{$1}#{$2}" 56 | when /^SReclaimable:\s+(\d+) (.+)$/ 57 | memory[:slab_reclaimable] = "#{$1}#{$2}" 58 | when /^SUnreclaim:\s+(\d+) (.+)$/ 59 | memory[:slab_unreclaim] = "#{$1}#{$2}" 60 | when /^PageTables:\s+(\d+) (.+)$/ 61 | memory[:page_tables] = "#{$1}#{$2}" 62 | when /^NFS_Unstable:\s+(\d+) (.+)$/ 63 | memory[:nfs_unstable] = "#{$1}#{$2}" 64 | when /^Bounce:\s+(\d+) (.+)$/ 65 | memory[:bounce] = "#{$1}#{$2}" 66 | when /^CommitLimit:\s+(\d+) (.+)$/ 67 | memory[:commit_limit] = "#{$1}#{$2}" 68 | when /^Committed_AS:\s+(\d+) (.+)$/ 69 | memory[:committed_as] = "#{$1}#{$2}" 70 | when /^VmallocTotal:\s+(\d+) (.+)$/ 71 | memory[:vmalloc_total] = "#{$1}#{$2}" 72 | when /^VmallocUsed:\s+(\d+) (.+)$/ 73 | memory[:vmalloc_used] = "#{$1}#{$2}" 74 | when /^VmallocChunk:\s+(\d+) (.+)$/ 75 | memory[:vmalloc_chunk] = "#{$1}#{$2}" 76 | when /^SwapCached:\s+(\d+) (.+)$/ 77 | memory[:swap][:cached] = "#{$1}#{$2}" 78 | when /^SwapTotal:\s+(\d+) (.+)$/ 79 | memory[:swap][:total] = "#{$1}#{$2}" 80 | when /^SwapFree:\s+(\d+) (.+)$/ 81 | memory[:swap][:free] = "#{$1}#{$2}" 82 | end 83 | end 84 | -------------------------------------------------------------------------------- /spec/ohai/plugins/perl_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Joshua Timberman() 3 | # Copyright:: Copyright (c) 2009 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 File.join(File.dirname(__FILE__), '..', '..', '/spec_helper.rb') 20 | 21 | describe Ohai::System, "plugin perl" do 22 | before(:each) do 23 | @ohai = Ohai::System.new 24 | @ohai[:languages] = Mash.new 25 | @ohai.stub!(:require_plugin).and_return(true) 26 | @pid = mock("PID", :null_object => true) 27 | @stderr = mock("STDERR", :null_object => true) 28 | @stdout = mock("STDOUT", :null_object => true) 29 | @stdout.stub!(:each).and_yield("version='5.8.8';"). 30 | and_yield("archname='darwin-thread-multi-2level';") 31 | @stdin = mock("STDIN", :null_object => true) 32 | @status = 0 33 | @ohai.stub!(:popen4).with("perl -V:version -V:archname").and_yield( 34 | @pid, 35 | @stdin, 36 | @stdout, 37 | @stderr 38 | ).and_return(@status) 39 | end 40 | 41 | it "should run perl -V:version -V:archname" do 42 | @ohai.should_receive(:popen4).with("perl -V:version -V:archname").and_return(true) 43 | @ohai._require_plugin("perl") 44 | end 45 | 46 | it "should close perl command's stdin" do 47 | @stdin.should_receive(:close) 48 | @ohai._require_plugin("perl") 49 | end 50 | 51 | it "should iterate over each line of perl command's stdout" do 52 | @stdout.should_receive(:each).and_return(true) 53 | @ohai._require_plugin("perl") 54 | end 55 | 56 | it "should set languages[:perl][:version]" do 57 | @ohai._require_plugin("perl") 58 | @ohai.languages[:perl][:version].should eql("5.8.8") 59 | end 60 | 61 | it "should set languages[:perl][:archname]" do 62 | @ohai._require_plugin("perl") 63 | @ohai.languages[:perl][:archname].should eql("darwin-thread-multi-2level") 64 | end 65 | 66 | it "should set languages[:perl] if perl command succeeds" do 67 | @status = 0 68 | @ohai.stub!(:popen4).with("perl -V:version -V:archname").and_yield( 69 | @pid, 70 | @stdin, 71 | @stdout, 72 | @stderr 73 | ).and_return(@status) 74 | @ohai._require_plugin("perl") 75 | @ohai.languages.should have_key(:perl) 76 | end 77 | 78 | it "should not set languages[:perl] if perl command fails" do 79 | @status = 1 80 | @ohai.stub!(:popen4).with("perl -V:version -V:archname").and_yield( 81 | @pid, 82 | @stdin, 83 | @stdout, 84 | @stderr 85 | ).and_return(@status) 86 | @ohai._require_plugin("perl") 87 | @ohai.languages.should_not have_key(:perl) 88 | end 89 | end 90 | -------------------------------------------------------------------------------- /lib/ohai/plugins/ec2.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Tim Dysinger () 3 | # Author:: Benjamin Black () 4 | # Author:: Christopher Brown () 5 | # Copyright:: Copyright (c) 2009 Opscode, Inc. 6 | # License:: Apache License, Version 2.0 7 | # 8 | # Licensed under the Apache License, Version 2.0 (the "License"); 9 | # you may not use this file except in compliance with the License. 10 | # You may obtain a copy of the License at 11 | # 12 | # http://www.apache.org/licenses/LICENSE-2.0 13 | # 14 | # Unless required by applicable law or agreed to in writing, software 15 | # distributed under the License is distributed on an "AS IS" BASIS, 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | # See the License for the specific language governing permissions and 18 | # limitations under the License. 19 | 20 | provides "ec2" 21 | 22 | require 'open-uri' 23 | require 'socket' 24 | 25 | require_plugin "hostname" 26 | require_plugin "kernel" 27 | require_plugin "network" 28 | 29 | EC2_METADATA_ADDR = "169.254.169.254" unless defined?(EC2_METADATA_ADDR) 30 | EC2_METADATA_URL = "http://#{EC2_METADATA_ADDR}/2008-02-01/meta-data" unless defined?(EC2_METADATA_URL) 31 | EC2_USERDATA_URL = "http://#{EC2_METADATA_ADDR}/2008-02-01/user-data" unless defined?(EC2_USERDATA_URL) 32 | 33 | def can_metadata_connect?(addr, port, timeout=2) 34 | t = Socket.new(Socket::Constants::AF_INET, Socket::Constants::SOCK_STREAM, 0) 35 | saddr = Socket.pack_sockaddr_in(port, addr) 36 | connected = false 37 | 38 | begin 39 | t.connect_nonblock(saddr) 40 | rescue Errno::EINPROGRESS 41 | r,w,e = IO::select(nil,[t],nil,timeout) 42 | if !w.nil? 43 | connected = true 44 | else 45 | begin 46 | t.connect_nonblock(saddr) 47 | rescue Errno::EISCONN 48 | t.close 49 | connected = true 50 | rescue SystemCallError 51 | end 52 | end 53 | rescue SystemCallError 54 | end 55 | 56 | connected 57 | end 58 | 59 | def has_ec2_mac? 60 | network[:interfaces].values.each do |iface| 61 | unless iface[:arp].nil? 62 | return true if iface[:arp].value?("fe:ff:ff:ff:ff:ff") 63 | end 64 | end 65 | false 66 | end 67 | 68 | def metadata(id='') 69 | OpenURI.open_uri("#{EC2_METADATA_URL}/#{id}").read.split("\n").each do |o| 70 | key = "#{id}#{o.gsub(/\=.*$/, '/')}" 71 | if key[-1..-1] != '/' 72 | ec2[key.gsub(/\-|\//, '_').to_sym] = 73 | OpenURI.open_uri("#{EC2_METADATA_URL}/#{key}").gets 74 | else 75 | metadata(key) 76 | end 77 | end 78 | end 79 | 80 | def userdata() 81 | ec2[:userdata] = nil 82 | # assumes the only expected error is the 404 if there's no user-data 83 | begin 84 | ec2[:userdata] = OpenURI.open_uri("#{EC2_USERDATA_URL}/").gets 85 | rescue OpenURI::HTTPError 86 | end 87 | end 88 | 89 | def looks_like_ec2? 90 | # Try non-blocking connect so we don't "block" if 91 | # the Xen environment is *not* EC2 92 | has_ec2_mac? && can_metadata_connect?(EC2_METADATA_ADDR,80) 93 | end 94 | 95 | if looks_like_ec2? 96 | ec2 Mash.new 97 | self.metadata 98 | self.userdata 99 | end 100 | 101 | -------------------------------------------------------------------------------- /spec/ohai/plugins/java_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Benjamin Black () 3 | # Copyright:: Copyright (c) 2009 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 File.join(File.dirname(__FILE__), '..', '..', '/spec_helper.rb') 20 | 21 | describe Ohai::System, "plugin java" do 22 | before(:each) do 23 | @ohai = Ohai::System.new 24 | @ohai.stub!(:require_plugin).and_return(true) 25 | @ohai[:languages] = Mash.new 26 | @pid = 10 27 | @status = 0 28 | @stdin = mock("STDIN", { :close => true }) 29 | @stdout = mock("STDOUT") 30 | @stderr = mock("STDERR") 31 | @stderr.stub!(:each). 32 | and_yield("java version \"1.5.0_16\""). 33 | and_yield("Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_16-b06-284)"). 34 | and_yield("Java HotSpot(TM) Client VM (build 1.5.0_16-133, mixed mode, sharing)") 35 | @ohai.stub!(:popen4).with("java -version").and_yield( 36 | @pid, 37 | @stdin, 38 | @stdout, 39 | @stderr 40 | ).and_return(@status) 41 | end 42 | 43 | it "should run java -version" do 44 | @ohai.should_receive(:popen4).with("java -version").and_return(true) 45 | @ohai._require_plugin("java") 46 | end 47 | 48 | it "should close java -version stdin" do 49 | @stdin.should_receive(:close) 50 | @ohai._require_plugin("java") 51 | end 52 | 53 | it "should iterate over each line of java -version stderr" do 54 | @stderr.should_receive(:each).and_return(true) 55 | @ohai._require_plugin("java") 56 | end 57 | 58 | it "should set java[:version]" do 59 | @ohai._require_plugin("java") 60 | @ohai.languages[:java][:version].should eql("1.5.0_16") 61 | end 62 | 63 | it "should set java[:runtime][:name] to runtime name" do 64 | @ohai._require_plugin("java") 65 | @ohai.languages[:java][:runtime][:name].should eql("Java(TM) 2 Runtime Environment, Standard Edition") 66 | end 67 | 68 | it "should set java[:runtime][:build] to runtime build" do 69 | @ohai._require_plugin("java") 70 | @ohai.languages[:java][:runtime][:build].should eql("1.5.0_16-b06-284") 71 | end 72 | 73 | it "should set java[:hotspot][:name] to hotspot name" do 74 | @ohai._require_plugin("java") 75 | @ohai.languages[:java][:hotspot][:name].should eql("Java HotSpot(TM) Client VM") 76 | end 77 | 78 | it "should set java[:hotspot][:build] to hotspot build" do 79 | @ohai._require_plugin("java") 80 | @ohai.languages[:java][:hotspot][:build].should eql("1.5.0_16-133, mixed mode, sharing") 81 | end 82 | 83 | it "should not set the languages[:java] tree up if java command fails" do 84 | @status = 1 85 | @ohai.stub!(:popen4).with("java -version").and_yield( 86 | @pid, 87 | @stdin, 88 | @stdout, 89 | @stderr 90 | ).and_return(@status) 91 | @ohai._require_plugin("java") 92 | @ohai.languages.should_not have_key(:java) 93 | end 94 | end 95 | -------------------------------------------------------------------------------- /lib/ohai/plugins/virtualization.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Benjamin Black () 3 | # Copyright:: Copyright (c) 2009 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 | provides "virtualization" 20 | 21 | require_plugin "#{os}::virtualization" 22 | 23 | unless virtualization.nil? || !(virtualization[:role].eql?("host")) 24 | begin 25 | require 'libvirt' 26 | require 'hpricot' 27 | 28 | emu = (virtualization[:emulator].eql?('kvm') ? 'qemu' : virtualization[:emulator]) 29 | virtualization[:libvirt_version] = Libvirt::version(emu)[0].to_s 30 | 31 | virtconn = Libvirt::openReadOnly("#{emu}:///system") 32 | 33 | virtualization[:uri] = virtconn.uri 34 | virtualization[:capabilities] = Mash.new 35 | virtualization[:capabilities][:xml_desc] = (virtconn.capabilities.split("\n").collect {|line| line.strip}).join 36 | #xdoc = Hpricot virtualization[:capabilities][:xml_desc] 37 | 38 | virtualization[:nodeinfo] = Mash.new 39 | ni = virtconn.node_get_info 40 | ['cores','cpus','memory','mhz','model','nodes','sockets','threads'].each {|a| virtualization[:nodeinfo][a] = ni.send(a)} 41 | 42 | virtualization[:domains] = Mash.new 43 | virtconn.list_domains.each do |d| 44 | dv = virtconn.lookup_domain_by_id d 45 | virtualization[:domains][dv.name] = Mash.new 46 | virtualization[:domains][dv.name][:id] = d 47 | virtualization[:domains][dv.name][:xml_desc] = (dv.xml_desc.split("\n").collect {|line| line.strip}).join 48 | ['os_type','uuid'].each {|a| virtualization[:domains][dv.name][a] = dv.send(a)} 49 | ['cpu_time','max_mem','memory','nr_virt_cpu','state'].each {|a| virtualization[:domains][dv.name][a] = dv.info.send(a)} 50 | #xdoc = Hpricot virtualization[:domains][dv.name][:xml_desc] 51 | 52 | end 53 | 54 | virtualization[:networks] = Mash.new 55 | virtconn.list_networks.each do |n| 56 | nv = virtconn.lookup_network_by_name n 57 | virtualization[:networks][n] = Mash.new 58 | virtualization[:networks][n][:xml_desc] = (nv.xml_desc.split("\n").collect {|line| line.strip}).join 59 | ['bridge_name','uuid'].each {|a| virtualization[:networks][n][a] = nv.send(a)} 60 | #xdoc = Hpricot virtualization[:networks][n][:xml_desc] 61 | 62 | end 63 | 64 | virtualization[:storage] = Mash.new 65 | virtconn.list_storage_pools.each do |pool| 66 | sp = virtconn.lookup_storage_pool_by_name pool 67 | virtualization[:storage][pool] = Mash.new 68 | virtualization[:storage][pool][:xml_desc] = (sp.xml_desc.split("\n").collect {|line| line.strip}).join 69 | ['autostart','uuid'].each {|a| virtualization[:storage][pool][a] = sp.send(a)} 70 | ['allocation','available','capacity','state'].each {|a| virtualization[:storage][pool][a] = sp.info.send(a)} 71 | #xdoc = Hpricot virtualization[:storage][pool][:xml_desc] 72 | 73 | virtualization[:storage][pool][:volumes] = Mash.new 74 | sp.list_volumes.each do |v| 75 | virtualization[:storage][pool][:volumes][v] = Mash.new 76 | sv = sp.lookup_volume_by_name pool 77 | ['key','name','path'].each {|a| virtualization[:storage][pool][:volumes][v][a] = sv.send(a)} 78 | ['allocation','capacity','type'].each {|a| virtualization[:storage][pool][:volumes][v][a] = sv.info.send(a)} 79 | end 80 | end 81 | 82 | virtconn.close 83 | rescue LoadError => e 84 | Ohai::Log.info("Can't load gem: #{e})") 85 | end 86 | end 87 | -------------------------------------------------------------------------------- /lib/ohai/config.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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/mixin/from_file' 20 | 21 | # Ohai::Config[:variable] 22 | # @config = Ohai::Config.new() 23 | # Ohai::Config.from_file(foo) 24 | # Ohai::Config[:cookbook_path] 25 | # Ohai::Config.cookbook_path 26 | # Ohai::Config.cookbook_path "one", "two" 27 | 28 | module Ohai 29 | class Config 30 | 31 | @configuration = { 32 | :log_level => :info, 33 | :log_location => STDOUT, 34 | :plugin_path => [ File.expand_path(File.join(File.dirname(__FILE__), 'plugins')) ] 35 | } 36 | 37 | class << self 38 | include Ohai::Mixin::FromFile 39 | 40 | # Pass Ohai::Config.configure() a block, and it will yield @configuration. 41 | # 42 | # === Parameters 43 | # :: A block that takes @configure as it's argument 44 | def configure(&block) 45 | yield @configuration 46 | end 47 | 48 | # Get the value of a configuration option 49 | # 50 | # === Parameters 51 | # config_option:: The configuration option to return 52 | # 53 | # === Returns 54 | # value:: The value of the configuration option 55 | # 56 | # === Raises 57 | # :: If the configuration option does not exist 58 | def [](config_option) 59 | if @configuration.has_key?(config_option.to_sym) 60 | @configuration[config_option.to_sym] 61 | else 62 | raise ArgumentError, "Cannot find configuration option #{config_option.to_s}" 63 | end 64 | end 65 | 66 | # Set the value of a configuration option 67 | # 68 | # === Parameters 69 | # config_option:: The configuration option to set (within the []) 70 | # value:: The value for the configuration option 71 | # 72 | # === Returns 73 | # value:: The new value of the configuration option 74 | def []=(config_option, value) 75 | @configuration[config_option.to_sym] = value 76 | end 77 | 78 | # Check if Ohai::Config has a configuration option. 79 | # 80 | # === Parameters 81 | # key:: The configuration option to check for 82 | # 83 | # === Returns 84 | # :: If the configuration option exists 85 | # :: If the configuration option does not exist 86 | def has_key?(key) 87 | @configuration.has_key?(key.to_sym) 88 | end 89 | 90 | # Allows for simple lookups and setting of configuration options via method calls 91 | # on Ohai::Config. If there any arguments to the method, they are used to set 92 | # the value of the configuration option. Otherwise, it's a simple get operation. 93 | # 94 | # === Parameters 95 | # method_symbol:: The method called. Must match a configuration option. 96 | # *args:: Any arguments passed to the method 97 | # 98 | # === Returns 99 | # value:: The value of the configuration option. 100 | # 101 | # === Raises 102 | # :: If the method_symbol does not match a configuration option. 103 | def method_missing(method_symbol, *args) 104 | if @configuration.has_key?(method_symbol) 105 | if args.length == 1 106 | @configuration[method_symbol] = args[0] 107 | elsif args.length > 1 108 | @configuration[method_symbol] = args 109 | end 110 | return @configuration[method_symbol] 111 | else 112 | raise ArgumentError, "Cannot find configuration option #{method_symbol.to_s}" 113 | end 114 | end 115 | 116 | end # class << self 117 | end 118 | end 119 | -------------------------------------------------------------------------------- /spec/ohai/system_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 File.join(File.dirname(__FILE__), '..', '/spec_helper.rb') 20 | 21 | describe Ohai::System, "initialize" do 22 | it "should return an Ohai::System object" do 23 | Ohai::System.new.should be_a_kind_of(Ohai::System) 24 | end 25 | 26 | it "should set @data to a Mash" do 27 | Ohai::System.new.data.should be_a_kind_of(Mash) 28 | end 29 | 30 | it "should set @seen_plugins to a Hash" do 31 | Ohai::System.new.seen_plugins.should be_a_kind_of(Hash) 32 | end 33 | end 34 | 35 | describe Ohai::System, "method_missing" do 36 | before(:each) do 37 | @ohai = Ohai::System.new 38 | end 39 | 40 | it "should take a missing method and store the method name as a key, with it's arguments as values" do 41 | @ohai.guns_n_roses("chinese democracy") 42 | @ohai.data["guns_n_roses"].should eql("chinese democracy") 43 | end 44 | 45 | it "should return the current value of the method name" do 46 | @ohai.guns_n_roses("chinese democracy").should eql("chinese democracy") 47 | end 48 | 49 | it "should allow you to get the value of a key by calling method_missing with no arguments" do 50 | @ohai.guns_n_roses("chinese democracy") 51 | @ohai.guns_n_roses.should eql("chinese democracy") 52 | end 53 | end 54 | 55 | describe Ohai::System, "attribute?" do 56 | before(:each) do 57 | @ohai = Ohai::System.new 58 | @ohai.metallica("death magnetic") 59 | end 60 | 61 | it "should return true if an attribute exists with the given name" do 62 | @ohai.attribute?("metallica").should eql(true) 63 | end 64 | 65 | it "should return false if an attribute does not exist with the given name" do 66 | @ohai.attribute?("alice in chains").should eql(false) 67 | end 68 | end 69 | 70 | describe Ohai::System, "set_attribute" do 71 | before(:each) do 72 | @ohai = Ohai::System.new 73 | end 74 | 75 | it "should let you set an attribute" do 76 | @ohai.set_attribute(:tea, "is soothing") 77 | @ohai.data["tea"].should eql("is soothing") 78 | end 79 | end 80 | 81 | describe Ohai::System, "get_attribute" do 82 | before(:each) do 83 | @ohai = Ohai::System.new 84 | @ohai.set_attribute(:tea, "is soothing") 85 | end 86 | 87 | it "should let you get an attribute" do 88 | @ohai.get_attribute("tea").should eql("is soothing") 89 | end 90 | end 91 | 92 | describe Ohai::System, "require_plugin" do 93 | before(:each) do 94 | @plugin_path = Ohai::Config[:plugin_path] 95 | Ohai::Config[:plugin_path] = ["/tmp/plugins"] 96 | File.stub!(:exists?).and_return(true) 97 | @ohai = Ohai::System.new 98 | @ohai.stub!(:from_file).and_return(true) 99 | end 100 | 101 | after(:each) do 102 | Ohai::Config[:plugin_path] = @plugin_path 103 | end 104 | 105 | it "should convert the name of the plugin to a file path" do 106 | plugin_name = "foo::bar" 107 | plugin_name.should_receive(:gsub).with("::", File::SEPARATOR) 108 | @ohai.require_plugin(plugin_name) 109 | end 110 | 111 | it "should check each part of the Ohai::Config[:plugin_path] for the plugin_filename.rb" do 112 | @ohai.should_receive(:from_file).with("/tmp/plugins/foo.rb").and_return(true) 113 | @ohai.require_plugin("foo") 114 | end 115 | 116 | it "should add a found plugin to the list of seen plugins" do 117 | @ohai.require_plugin("foo") 118 | @ohai.seen_plugins["foo"].should eql(true) 119 | end 120 | 121 | it "should return true if the plugin has been seen" do 122 | @ohai.seen_plugins["foo"] = true 123 | @ohai.require_plugin("foo") 124 | end 125 | 126 | it "should return true if the plugin has been loaded" do 127 | @ohai.require_plugin("foo").should eql(true) 128 | end 129 | end 130 | 131 | -------------------------------------------------------------------------------- /lib/ohai/plugins/linux/network.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | provides "network", "counters/network" 20 | 21 | network[:default_interface] = from("route -n \| grep ^0.0.0.0 \| awk \'{print \$8\}\'") 22 | 23 | def encaps_lookup(encap) 24 | return "Loopback" if encap.eql?("Local Loopback") 25 | return "PPP" if encap.eql?("Point-to-Point Protocol") 26 | return "SLIP" if encap.eql?("Serial Line IP") 27 | return "VJSLIP" if encap.eql?("VJ Serial Line IP") 28 | return "IPIP" if encap.eql?("IPIP Tunnel") 29 | return "6to4" if encap.eql?("IPv6-in-IPv4") 30 | encap 31 | end 32 | 33 | iface = Mash.new 34 | net_counters = Mash.new 35 | popen4("ifconfig -a") do |pid, stdin, stdout, stderr| 36 | stdin.close 37 | cint = nil 38 | stdout.each do |line| 39 | tmp_addr = nil 40 | if line =~ /^([0-9a-zA-Z\.\:\-]+)\s+/ 41 | cint = $1 42 | iface[cint] = Mash.new 43 | if cint =~ /^(\w+)(\d+.*)/ 44 | iface[cint][:type] = $1 45 | iface[cint][:number] = $2 46 | end 47 | end 48 | if line =~ /Link encap:(Local Loopback)/ || line =~ /Link encap:(.+?)\s/ 49 | iface[cint][:encapsulation] = encaps_lookup($1) 50 | end 51 | if line =~ /HWaddr (.+?)\s/ 52 | iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] 53 | iface[cint][:addresses][$1] = { "family" => "lladdr" } 54 | end 55 | if line =~ /inet addr:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/ 56 | iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] 57 | iface[cint][:addresses][$1] = { "family" => "inet" } 58 | tmp_addr = $1 59 | end 60 | if line =~ /inet6 addr: ([a-f0-9\:]+)\/(\d+) Scope:(\w+)/ 61 | iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] 62 | iface[cint][:addresses][$1] = { "family" => "inet6", "prefixlen" => $2, "scope" => ($3.eql?("Host") ? "Node" : $3) } 63 | end 64 | if line =~ /Bcast:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/ 65 | iface[cint][:addresses][tmp_addr]["broadcast"] = $1 66 | end 67 | if line =~ /Mask:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/ 68 | iface[cint][:addresses][tmp_addr]["netmask"] = $1 69 | end 70 | flags = line.scan(/(UP|BROADCAST|DEBUG|LOOPBACK|POINTTOPOINT|NOTRAILERS|RUNNING|NOARP|PROMISC|ALLMULTI|SLAVE|MASTER|MULTICAST|DYNAMIC)\s/) 71 | if flags.length > 1 72 | iface[cint][:flags] = flags.flatten 73 | end 74 | if line =~ /MTU:(\d+)/ 75 | iface[cint][:mtu] = $1 76 | end 77 | if line =~ /RX packets:(\d+) errors:(\d+) dropped:(\d+) overruns:(\d+) frame:(\d+)/ 78 | net_counters[cint] = Mash.new unless net_counters[cint] 79 | net_counters[cint][:rx] = { "packets" => $1, "errors" => $2, "drop" => $3, "overrun" => $4, "frame" => $5 } 80 | end 81 | if line =~ /TX packets:(\d+) errors:(\d+) dropped:(\d+) overruns:(\d+) carrier:(\d+)/ 82 | net_counters[cint][:tx] = { "packets" => $1, "errors" => $2, "drop" => $3, "overrun" => $4, "carrier" => $5 } 83 | end 84 | if line =~ /collisions:(\d+)/ 85 | net_counters[cint][:tx]["collisions"] = $1 86 | end 87 | if line =~ /txqueuelen:(\d+)/ 88 | net_counters[cint][:tx]["queuelen"] = $1 89 | end 90 | if line =~ /RX bytes:(\d+) \((\d+?\.\d+ .+?)\)/ 91 | net_counters[cint][:rx]["bytes"] = $1 92 | end 93 | if line =~ /TX bytes:(\d+) \((\d+?\.\d+ .+?)\)/ 94 | net_counters[cint][:tx]["bytes"] = $1 95 | end 96 | end 97 | end 98 | 99 | popen4("arp -an") do |pid, stdin, stdout, stderr| 100 | stdin.close 101 | stdout.each do |line| 102 | if line =~ /^\S+ \((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\) at ([a-fA-F0-9\:]+) \[(\w+)\] on ([0-9a-zA-Z\.\:\-]+)/ 103 | next unless iface[$4] # this should never happen 104 | iface[$4][:arp] = Mash.new unless iface[$4][:arp] 105 | iface[$4][:arp][$1] = $2.downcase 106 | end 107 | end 108 | end 109 | 110 | counters[:network][:interfaces] = net_counters 111 | 112 | network["interfaces"] = iface 113 | -------------------------------------------------------------------------------- /spec/ohai/plugins/linux/cpu_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | 20 | require File.join(File.dirname(__FILE__), '..', '..', '..', '/spec_helper.rb') 21 | 22 | describe Ohai::System, "Linux cpu plugin" do 23 | before(:each) do 24 | @ohai = Ohai::System.new 25 | @ohai.stub!(:require_plugin).and_return(true) 26 | @ohai[:os] = "linux" 27 | @mock_file = mock("/proc/cpuinfo") 28 | @mock_file.stub!(:each). 29 | and_yield("processor : 0"). 30 | and_yield("vendor_id : GenuineIntel"). 31 | and_yield("cpu family : 6"). 32 | and_yield("model : 23"). 33 | and_yield("model name : Intel(R) Core(TM)2 Duo CPU T8300 @ 2.40GHz"). 34 | and_yield("stepping : 6"). 35 | and_yield("cpu MHz : 1968.770"). 36 | and_yield("cache size : 64 KB"). 37 | and_yield("fdiv_bug : no"). 38 | and_yield("hlt_bug : no"). 39 | and_yield("f00f_bug : no"). 40 | and_yield("coma_bug : no"). 41 | and_yield("fpu : yes"). 42 | and_yield("fpu_exception : yes"). 43 | and_yield("cpuid level : 10"). 44 | and_yield("wp : yes"). 45 | and_yield("flags : fpu pse tsc msr mce cx8 sep mtrr pge cmov"). 46 | and_yield("bogomips : 2575.86"). 47 | and_yield("clflush size : 32") 48 | File.stub!(:open).with("/proc/cpuinfo").and_return(@mock_file) 49 | end 50 | 51 | it "should set cpu[:total] to 1" do 52 | @ohai._require_plugin("linux::cpu") 53 | @ohai[:cpu][:total].should == 1 54 | end 55 | 56 | it "should set cpu[:real] to 0" do 57 | @ohai._require_plugin("linux::cpu") 58 | @ohai[:cpu][:real].should == 0 59 | end 60 | 61 | it "should have a cpu 0" do 62 | @ohai._require_plugin("linux::cpu") 63 | @ohai[:cpu].should have_key("0") 64 | end 65 | 66 | it "should have a vendor_id for cpu 0" do 67 | @ohai._require_plugin("linux::cpu") 68 | @ohai[:cpu]["0"].should have_key("vendor_id") 69 | @ohai[:cpu]["0"]["vendor_id"].should eql("GenuineIntel") 70 | end 71 | 72 | it "should have a family for cpu 0" do 73 | @ohai._require_plugin("linux::cpu") 74 | @ohai[:cpu]["0"].should have_key("family") 75 | @ohai[:cpu]["0"]["family"].should eql("6") 76 | end 77 | 78 | it "should have a model for cpu 0" do 79 | @ohai._require_plugin("linux::cpu") 80 | @ohai[:cpu]["0"].should have_key("model") 81 | @ohai[:cpu]["0"]["model"].should eql("23") 82 | end 83 | 84 | it "should have a stepping for cpu 0" do 85 | @ohai._require_plugin("linux::cpu") 86 | @ohai[:cpu]["0"].should have_key("stepping") 87 | @ohai[:cpu]["0"]["stepping"].should eql("6") 88 | end 89 | 90 | it "should not have a phyiscal_id for cpu 0" do 91 | @ohai._require_plugin("linux::cpu") 92 | @ohai[:cpu]["0"].should_not have_key("physical_id") 93 | end 94 | 95 | it "should not have a core_id for cpu 0" do 96 | @ohai._require_plugin("linux::cpu") 97 | @ohai[:cpu]["0"].should_not have_key("core_id") 98 | end 99 | 100 | it "should not have a cores for cpu 0" do 101 | @ohai._require_plugin("linux::cpu") 102 | @ohai[:cpu]["0"].should_not have_key("cores") 103 | end 104 | 105 | it "should have a model name for cpu 0" do 106 | @ohai._require_plugin("linux::cpu") 107 | @ohai[:cpu]["0"].should have_key("model_name") 108 | @ohai[:cpu]["0"]["model_name"].should eql("Intel(R) Core(TM)2 Duo CPU T8300 @ 2.40GHz") 109 | end 110 | 111 | it "should have a mhz for cpu 0" do 112 | @ohai._require_plugin("linux::cpu") 113 | @ohai[:cpu]["0"].should have_key("mhz") 114 | @ohai[:cpu]["0"]["mhz"].should eql("1968.770") 115 | end 116 | 117 | it "should have a cache_size for cpu 0" do 118 | @ohai._require_plugin("linux::cpu") 119 | @ohai[:cpu]["0"].should have_key("cache_size") 120 | @ohai[:cpu]["0"]["cache_size"].should eql("64 KB") 121 | end 122 | 123 | it "should have flags for cpu 0" do 124 | @ohai._require_plugin("linux::cpu") 125 | @ohai[:cpu]["0"].should have_key("flags") 126 | @ohai[:cpu]["0"]["flags"].should == %w{fpu pse tsc msr mce cx8 sep mtrr pge cmov} 127 | end 128 | end -------------------------------------------------------------------------------- /lib/ohai/plugins/freebsd/network.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Bryan McLellan (btm@loftninjas.org) 3 | # Copyright:: Copyright (c) 2009 Bryan McLellan 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 | provides "network", "counters/network" 20 | 21 | network[:default_interface] = from("route -n get default \| grep interface: \| awk \'/: / \{print \$2\}\'") 22 | 23 | iface = Mash.new 24 | popen4("/sbin/ifconfig -a") do |pid, stdin, stdout, stderr| 25 | stdin.close 26 | cint = nil 27 | stdout.each do |line| 28 | if line =~ /^([0-9a-zA-Z\.]+):\s+/ 29 | cint = $1 30 | iface[cint] = Mash.new 31 | if cint =~ /^(\w+)(\d+.*)/ 32 | iface[cint][:type] = $1 33 | iface[cint][:number] = $2 34 | end 35 | end 36 | # call the family lladdr to match linux for consistency 37 | if line =~ /\s+ether (.+?)\s/ 38 | iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] 39 | iface[cint][:addresses][$1] = { "family" => "lladdr" } 40 | end 41 | if line =~ /\s+inet ([\d.]+) netmask ([\da-fx]+)\s*\w*\s*([\d.]*)/ 42 | iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] 43 | # convert the netmask to decimal for consistency 44 | netmask = "#{$2[2,2].hex}.#{$2[4,2].hex}.#{$2[6,2].hex}.#{$2[8,2].hex}" 45 | if $3.empty? 46 | iface[cint][:addresses][$1] = { "family" => "inet", "netmask" => netmask } 47 | else 48 | # found a broadcast address 49 | iface[cint][:addresses][$1] = { "family" => "inet", "netmask" => netmask, "broadcast" => $3 } 50 | end 51 | end 52 | if line =~ /\s+inet6 ([a-f0-9\:]+)%?(\w*)\s+prefixlen\s+(\d+)\s*\w*\s*([\da-fx]*)/ 53 | iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] 54 | if $4.empty? 55 | iface[cint][:addresses][$1] = { "family" => "inet6", "prefixlen" => $3 } 56 | else 57 | # found a zone_id / scope 58 | iface[cint][:addresses][$1] = { "family" => "inet6", "zoneid" => $2, "prefixlen" => $3, "scopeid" => $4 } 59 | end 60 | end 61 | if line =~ /flags=\d+<(.+)>/ 62 | flags = $1.split(',') 63 | iface[cint][:flags] = flags if flags.length > 0 64 | end 65 | if line =~ /metric: (\d+) mtu: (\d+)/ 66 | iface[cint][:metric] = $1 67 | iface[cint][:mtu] = $2 68 | end 69 | end 70 | end 71 | 72 | popen4("arp -an") do |pid, stdin, stdout, stderr| 73 | stdin.close 74 | stdout.each do |line| 75 | if line =~ /\((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\) at ([a-fA-F0-9\:]+) on ([0-9a-zA-Z\.\:\-]+)/ 76 | next unless iface[$3] # this should never happen 77 | iface[$3][:arp] = Mash.new unless iface[$3][:arp] 78 | iface[$3][:arp][$1] = $2.downcase 79 | end 80 | end 81 | end 82 | 83 | network["interfaces"] = iface 84 | 85 | net_counters = Mash.new 86 | # From netstat(1), not sure of the implications: 87 | # Show the state of all network interfaces or a single interface 88 | # which have been auto-configured (interfaces statically configured 89 | # into a system, but not located at boot time are not shown). 90 | popen4("netstat -ibdn") do |pid, stdin, stdout, stderr| 91 | stdin.close 92 | stdout.each do |line| 93 | # Name Mtu Network Address Ipkts Ierrs Ibytes Opkts Oerrs Obytes Coll Drop 94 | # ed0 1500 54:52:00:68:92:85 333604 26 151905886 175472 0 24897542 0 905 95 | # $1 $2 $3 $4 $5 $6 $7 $8 $9 $10 96 | if line =~ /^([\w\.\*]+)\s+\d+\s+\s+([\w:]*)\s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/ 97 | net_counters[$1] = Mash.new unless net_counters[$1] 98 | net_counters[$1]["rx"] = Mash.new unless net_counters[$1]["rx"] 99 | net_counters[$1]["tx"] = Mash.new unless net_counters[$1]["tx"] 100 | net_counters[$1]["rx"]["packets"] = $3 101 | net_counters[$1]["rx"]["errors"] = $4 102 | net_counters[$1]["rx"]["bytes"] = $5 103 | net_counters[$1]["tx"]["packets"] = $6 104 | net_counters[$1]["tx"]["errors"] = $7 105 | net_counters[$1]["tx"]["bytes"] = $8 106 | net_counters[$1]["tx"]["collisions"] = $9 107 | net_counters[$1]["tx"]["dropped"] = $10 108 | end 109 | end 110 | end 111 | 112 | counters[:network][:interfaces] = net_counters -------------------------------------------------------------------------------- /spec/ohai/plugins/linux/virtualization_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Thom May () 3 | # Copyright:: Copyright (c) 2009 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 File.join(File.dirname(__FILE__), '..', '..', '..', '/spec_helper.rb') 20 | 21 | describe Ohai::System, "Linux virtualization platform" do 22 | before(:each) do 23 | @ohai = Ohai::System.new 24 | @ohai[:os] = "linux" 25 | @ohai.stub!(:require_plugin).and_return(true) 26 | @ohai.extend(SimpleFromFile) 27 | 28 | # default to all requested Files not existing 29 | File.stub!(:exists?).with("/proc/xen/capabilities").and_return(false) 30 | File.stub!(:exists?).with("/proc/sys/xen/independent_wallclock").and_return(false) 31 | File.stub!(:exists?).with("/proc/modules").and_return(false) 32 | File.stub!(:exists?).with("/proc/cpuinfo").and_return(false) 33 | File.stub!(:exists?).with("/usr/sbin/dmidecode").and_return(false) 34 | end 35 | 36 | describe "when we are checking for xen" do 37 | it "should set xen host if /proc/xen/capabilities contains control_d" do 38 | File.should_receive(:exists?).with("/proc/xen/capabilities").and_return(true) 39 | File.stub!(:read).with("/proc/xen/capabilities").and_return("control_d") 40 | @ohai._require_plugin("linux::virtualization") 41 | @ohai[:virtualization][:emulator].should == "xen" 42 | @ohai[:virtualization][:role].should == "host" 43 | end 44 | 45 | it "should set xen guest if /proc/sys/xen/independent_wallclock exists" do 46 | File.should_receive(:exists?).with("/proc/sys/xen/independent_wallclock").and_return(true) 47 | @ohai._require_plugin("linux::virtualization") 48 | @ohai[:virtualization][:emulator].should == "xen" 49 | @ohai[:virtualization][:role].should == "guest" 50 | end 51 | 52 | it "should not set virtualization if xen isn't there" do 53 | File.should_receive(:exists?).at_least(:once).and_return(false) 54 | @ohai._require_plugin("linux::virtualization") 55 | @ohai[:virtualization].should == {} 56 | end 57 | end 58 | 59 | describe "when we are checking for kvm" do 60 | it "should set kvm host if /proc/modules contains kvm" do 61 | File.should_receive(:exists?).with("/proc/modules").and_return(true) 62 | File.stub!(:read).with("/proc/modules").and_return("kvm 165872 1 kvm_intel") 63 | @ohai._require_plugin("linux::virtualization") 64 | @ohai[:virtualization][:emulator].should == "kvm" 65 | @ohai[:virtualization][:role].should == "host" 66 | end 67 | 68 | it "should set kvm guest if /proc/cpuinfo contains QEMU Virtual CPU" do 69 | File.should_receive(:exists?).with("/proc/cpuinfo").and_return(true) 70 | File.stub!(:read).with("/proc/cpuinfo").and_return("QEMU Virtual CPU") 71 | @ohai._require_plugin("linux::virtualization") 72 | @ohai[:virtualization][:emulator].should == "kvm" 73 | @ohai[:virtualization][:role].should == "guest" 74 | end 75 | 76 | it "should not set virtualization if kvm isn't there" do 77 | File.should_receive(:exists?).at_least(:once).and_return(false) 78 | @ohai._require_plugin("linux::virtualization") 79 | @ohai[:virtualization].should == {} 80 | end 81 | end 82 | 83 | describe "when we are parsing dmidecode" do 84 | before(:each) do 85 | File.should_receive(:exists?).with("/usr/sbin/dmidecode").and_return(true) 86 | @stdin = mock("STDIN", { :close => true }) 87 | @pid = 10 88 | @stderr = mock("STDERR") 89 | @stdout = mock("STDOUT") 90 | @status = 0 91 | end 92 | 93 | it "should run dmidecode" do 94 | @ohai.should_receive(:popen4).with("dmidecode").and_return(true) 95 | @ohai._require_plugin("linux::virtualization") 96 | end 97 | 98 | it "should set virtualpc guest if dmidecode detects Microsoft Virtual Machine" do 99 | @stdout.stub!(:each). 100 | and_yield("Manufacturer: Microsoft"). 101 | and_yield(" Product Name: Virtual Machine") 102 | @ohai.stub!(:popen4).with("dmidecode").and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status) 103 | @ohai._require_plugin("linux::virtualization") 104 | @ohai[:virtualization][:emulator].should == "virtualpc" 105 | @ohai[:virtualization][:role].should == "guest" 106 | end 107 | 108 | it "should set vmware guest if dmidecode detects VMware Virtual Platform" do 109 | @stdout.stub!(:each). 110 | and_yield("Manufacturer: VMware"). 111 | and_yield("Product Name: VMware Virtual Platform") 112 | @ohai.stub!(:popen4).with("dmidecode").and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status) 113 | @ohai._require_plugin("linux::virtualization") 114 | @ohai[:virtualization][:emulator].should == "vmware" 115 | @ohai[:virtualization][:role].should == "guest" 116 | end 117 | 118 | it "should run dmidecode and not set virtualization if nothing is detected" do 119 | @ohai.should_receive(:popen4).with("dmidecode").and_return(true) 120 | @ohai._require_plugin("linux::virtualization") 121 | @ohai[:virtualization].should == {} 122 | end 123 | end 124 | 125 | it "should not set virtualization if no tests match" do 126 | @ohai._require_plugin("linux::virtualization") 127 | @ohai[:virtualization].should == {} 128 | end 129 | end 130 | 131 | 132 | -------------------------------------------------------------------------------- /lib/ohai/plugins/solaris2/network.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Benjamin Black () 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 | # EXAMPLE SOLARIS IFCONFIG OUTPUT; CURRENTLY, ONLY SIMPLE STUFF IS SUPPORTED (E.G., NO TUNNELS) 20 | # DEAR SUN: YOU GET AN F FOR YOUR IFCONFIG 21 | #lo0:3: flags=2001000849 mtu 8232 index 1 22 | # inet 127.0.0.1 netmask ff000000 23 | #e1000g0:3: flags=201000843 mtu 1500 index 3 24 | # inet 72.2.115.28 netmask ffffff80 broadcast 72.2.115.127 25 | #e1000g2:1: flags=201000843 mtu 1500 index 4 26 | # inet 10.2.115.28 netmask ffffff80 broadcast 10.2.115.127 27 | # inet6 2001:0db8:3c4d:55:a00:20ff:fe8e:f3ad/64 28 | #ip.tun0: flags=2200851 mtu 1480 index 3 29 | # inet tunnel src 109.146.85.57 tunnel dst 109.146.85.212 30 | # tunnel security settings --> use 'ipsecconf -ln -i ip.tun1' 31 | # tunnel hop limit 60 32 | # inet6 fe80::6d92:5539/10 --> fe80::6d92:55d4 33 | #ip.tun0:1: flags=2200851 mtu 1480 index 3 34 | # inet6 2::45/128 --> 2::46 35 | #lo0: flags=1000849 mtu 8232 index 1 36 | # inet 127.0.0.1 netmask ff000000 37 | #eri0: flags=1004843 mtu 1500 \ 38 | #index 2 39 | # inet 172.17.128.208 netmask ffffff00 broadcast 172.17.128.255 40 | #ip6.tun0: flags=10008d1 \ 41 | #mtu 1460 42 | # index 3 43 | # inet6 tunnel src fe80::1 tunnel dst fe80::2 44 | # tunnel security settings --> use 'ipsecconf -ln -i ip.tun1' 45 | # tunnel hop limit 60 tunnel encapsulation limit 4 46 | # inet 10.0.0.208 --> 10.0.0.210 netmask ff000000 47 | #qfe1: flags=2000841 mtu 1500 index 3 48 | # usesrc vni0 49 | # inet6 fe80::203:baff:fe17:4be0/10 50 | # ether 0:3:ba:17:4b:e0 51 | #vni0: flags=2002210041 mtu 0 52 | # index 5 53 | # srcof qfe1 54 | # inet6 fe80::203:baff:fe17:4444/128 55 | 56 | provides "network" 57 | 58 | require 'scanf' 59 | 60 | def encaps_lookup(ifname) 61 | return "Ethernet" if ifname.eql?("e1000g") 62 | return "Ethernet" if ifname.eql?("eri") 63 | return "Loopback" if ifname.eql?("lo") 64 | "Unknown" 65 | end 66 | 67 | def arpname_to_ifname(iface, arpname) 68 | iface.keys.each do |ifn| 69 | return ifn if ifn.split(':')[0].eql?(arpname) 70 | end 71 | 72 | nil 73 | end 74 | 75 | iface = Mash.new 76 | popen4("ifconfig -a") do |pid, stdin, stdout, stderr| 77 | stdin.close 78 | cint = nil 79 | stdout.each do |line| 80 | if line =~ /^([0-9a-zA-Z\.\:\-]+): \S+ mtu (\d+) index (\d+)/ 81 | cint = $1 82 | iface[cint] = Mash.new 83 | iface[cint][:mtu] = $2 84 | iface[cint][:index] = $3 85 | if line =~ / flags\=\d+\<((ADDRCONF|ANYCAST|BROADCAST|CoS|DEPRECATED|DHCP|DUPLICATE|FAILED|FIXEDMTU|INACTIVE|LOOPBACK|MIP|MULTI_BCAST|MULTICAST|NOARP|NOFAILOVER|NOLOCAL|NONUD|NORTEXCH|NOXMIT|OFFLINE|POINTOPOINT|PREFERRED|PRIVATE|ROUTER|RUNNING|STANDBY|TEMPORARY|UNNUMBERED|UP|VIRTUAL|XRESOLV|IPv4|IPv6|,)+)\>\s/ 86 | flags = $1.split(',') 87 | else 88 | flags = Array.new 89 | end 90 | iface[cint][:flags] = flags.flatten 91 | if cint =~ /^(\w+)(\d+.*)/ 92 | iface[cint][:type] = $1 93 | iface[cint][:number] = $2 94 | iface[cint][:encapsulation] = encaps_lookup($1) 95 | end 96 | end 97 | if line =~ /\s+inet (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) netmask (([0-9a-f]){1,8})\s*$/ 98 | iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] 99 | iface[cint][:addresses][$1] = { "family" => "inet", "netmask" => $2.scanf('%2x'*4)*"."} 100 | end 101 | if line =~ /\s+inet (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) netmask (([0-9a-f]){1,8}) broadcast (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/ 102 | iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] 103 | iface[cint][:addresses][$1] = { "family" => "inet", "netmask" => $2.scanf('%2x'*4)*".", "broadcast" => $4 } 104 | end 105 | if line =~ /\s+inet6 ([a-f0-9\:]+)(\s*|(\%[a-z0-9]+)\s*)\/(\d+)\s*$/ 106 | iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] 107 | iface[cint][:addresses][$1] = { "family" => "inet6", "prefixlen" => $4 } 108 | end 109 | end 110 | end 111 | 112 | popen4("arp -an") do |pid, stdin, stdout, stderr| 113 | stdin.close 114 | stdout.each do |line| 115 | if line =~ /([0-9a-zA-Z]+)\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+(\w+)\s+([a-zA-Z0-9\.\:\-]+)/ 116 | next unless iface[arpname_to_ifname(iface, $1)] # this should never happen, except on solaris because sun hates you. 117 | iface[arpname_to_ifname(iface, $1)][:arp] = Mash.new unless iface[arpname_to_ifname(iface, $1)][:arp] 118 | iface[arpname_to_ifname(iface, $1)][:arp][$2] = $5 119 | end 120 | end 121 | end 122 | 123 | iface.keys.each do |ifn| 124 | iaddr = nil 125 | if iface[ifn][:encapsulation].eql?("Ethernet") 126 | iface[ifn][:addresses].keys.each do |addr| 127 | if [ifn][:addresses][addr]["family"].eql?("inet") 128 | iaddr = addr 129 | break 130 | end 131 | end 132 | iface[ifn][:arp].keys.each do |addr| 133 | if addr.eql?(iaddr) 134 | iface[ifn][:addresses][iface[ifn][:arp][iaddr]] = { "family" => "lladdr" } 135 | break 136 | end 137 | end 138 | end 139 | end 140 | 141 | network[:interfaces] = iface 142 | -------------------------------------------------------------------------------- /features/steps/common.rb: -------------------------------------------------------------------------------- 1 | def in_project_folder(&block) 2 | project_folder = @active_project_folder || @tmp_root 3 | FileUtils.chdir(project_folder, &block) 4 | end 5 | 6 | def in_home_folder(&block) 7 | FileUtils.chdir(@home_path, &block) 8 | end 9 | 10 | Given %r{^a safe folder} do 11 | FileUtils.rm_rf @tmp_root = File.dirname(__FILE__) + "/../../tmp" 12 | FileUtils.mkdir_p @tmp_root 13 | FileUtils.mkdir_p @home_path = File.expand_path(File.join(@tmp_root, "home")) 14 | @lib_path = File.expand_path(File.dirname(__FILE__) + '/../../lib') 15 | Given "env variable $HOME set to '#{@home_path}'" 16 | end 17 | 18 | Given %r{^this project is active project folder} do 19 | Given "a safe folder" 20 | @active_project_folder = File.expand_path(File.dirname(__FILE__) + "/../..") 21 | end 22 | 23 | Given %r{^env variable \$([\w_]+) set to '(.*)'} do |env_var, value| 24 | ENV[env_var] = value 25 | end 26 | 27 | def force_local_lib_override(project_name = @project_name) 28 | rakefile = File.read(File.join(project_name, 'Rakefile')) 29 | File.open(File.join(project_name, 'Rakefile'), "w+") do |f| 30 | f << "$:.unshift('#{@lib_path}')\n" 31 | f << rakefile 32 | end 33 | end 34 | 35 | def setup_active_project_folder project_name 36 | @active_project_folder = File.join(@tmp_root, project_name) 37 | @project_name = project_name 38 | end 39 | 40 | Given %r{'(.*)' folder is deleted} do |folder| 41 | in_project_folder do 42 | FileUtils.rm_rf folder 43 | end 44 | end 45 | 46 | When %r{^'(.*)' generator is invoked with arguments '(.*)'$} do |generator, arguments| 47 | FileUtils.chdir(@active_project_folder) do 48 | if Object.const_defined?("APP_ROOT") 49 | APP_ROOT.replace(FileUtils.pwd) 50 | else 51 | APP_ROOT = FileUtils.pwd 52 | end 53 | run_generator(generator, arguments.split(' '), SOURCES) 54 | end 55 | end 56 | 57 | When %r{run executable '(.*)' with arguments '(.*)'} do |executable, arguments| 58 | @stdout = File.expand_path(File.join(@tmp_root, "executable.out")) 59 | FileUtils.chdir(@active_project_folder) do 60 | system "ruby #{executable} #{arguments} > #{@stdout}" 61 | end 62 | end 63 | 64 | When %r{^task 'rake (.*)' is invoked$} do |task| 65 | @stdout = File.expand_path(File.join(@tmp_root, "tests.out")) 66 | FileUtils.chdir(@active_project_folder) do 67 | system "rake #{task} --trace > #{@stdout} 2> #{@stdout}" 68 | end 69 | end 70 | 71 | Then %r{^folder '(.*)' is created} do |folder| 72 | in_project_folder do 73 | File.exists?(folder).should be_true 74 | end 75 | end 76 | 77 | Then %r{^file '(.*)' (is|is not) created} do |file, is| 78 | in_project_folder do 79 | File.exists?(file).should(is == 'is' ? be_true : be_false) 80 | end 81 | end 82 | 83 | Then %r{^file with name matching '(.*)' is created} do |pattern| 84 | in_project_folder do 85 | Dir[pattern].should_not be_empty 86 | end 87 | end 88 | 89 | Then %r{gem file '(.*)' and generated file '(.*)' should be the same} do |gem_file, project_file| 90 | File.exists?(gem_file).should be_true 91 | File.exists?(project_file).should be_true 92 | gem_file_contents = File.read(File.dirname(__FILE__) + "/../../#{gem_file}") 93 | project_file_contents = File.read(File.join(@active_project_folder, project_file)) 94 | project_file_contents.should == gem_file_contents 95 | end 96 | 97 | Then %r{^output same as contents of '(.*)'$} do |file| 98 | expected_output = File.read(File.join(File.dirname(__FILE__) + "/../expected_outputs", file)) 99 | actual_output = File.read(File.dirname(__FILE__) + "/../../tmp/#{@stdout}") 100 | actual_output.should == expected_output 101 | end 102 | 103 | Then %r{^(does|does not) invoke generator '(.*)'$} do |does_invoke, generator| 104 | actual_output = File.read(File.dirname(__FILE__) + "/../../tmp/#{@stdout}") 105 | does_invoke == "does" ? 106 | actual_output.should(match(/dependency\s+#{generator}/)) : 107 | actual_output.should_not(match(/dependency\s+#{generator}/)) 108 | end 109 | 110 | Then %r{help options '(.*)' and '(.*)' are displayed} do |opt1, opt2| 111 | actual_output = File.read(@stdout) 112 | actual_output.should match(/#{opt1}/) 113 | actual_output.should match(/#{opt2}/) 114 | end 115 | 116 | Then %r{^output (does|does not) match \/(.*)\/} do |does, regex| 117 | actual_output = File.read(@stdout) 118 | (does == 'does') ? 119 | actual_output.should(match(/#{regex}/)) : 120 | actual_output.should_not(match(/#{regex}/)) 121 | end 122 | 123 | Then %r{^contents of file '(.*)' (does|does not) match \/(.*)\/} do |file, does, regex| 124 | in_project_folder do 125 | actual_output = File.read(file) 126 | (does == 'does') ? 127 | actual_output.should(match(/#{regex}/)) : 128 | actual_output.should_not(match(/#{regex}/)) 129 | end 130 | end 131 | 132 | Then %r{^all (\d+) tests pass} do |expected_test_count| 133 | expected = %r{^#{expected_test_count} tests, \d+ assertions, 0 failures, 0 errors} 134 | actual_output = File.read(@stdout) 135 | actual_output.should match(expected) 136 | end 137 | 138 | Then %r{^all (\d+) examples pass} do |expected_test_count| 139 | expected = %r{^#{expected_test_count} examples?, 0 failures} 140 | actual_output = File.read(@stdout) 141 | actual_output.should match(expected) 142 | end 143 | 144 | Then %r{^yaml file '(.*)' contains (\{.*\})} do |file, yaml| 145 | in_project_folder do 146 | yaml = eval yaml 147 | YAML.load(File.read(file)).should == yaml 148 | end 149 | end 150 | 151 | Then %r{^Rakefile can display tasks successfully} do 152 | @stdout = File.expand_path(File.join(@tmp_root, "rakefile.out")) 153 | FileUtils.chdir(@active_project_folder) do 154 | system "rake -T > #{@stdout} 2> #{@stdout}" 155 | end 156 | actual_output = File.read(@stdout) 157 | actual_output.should match(/^rake\s+\w+\s+#\s.*/) 158 | end 159 | 160 | Then %r{^task 'rake (.*)' is executed successfully} do |task| 161 | @stdout.should_not be_nil 162 | actual_output = File.read(@stdout) 163 | actual_output.should_not match(/^Don't know how to build task '#{task}'/) 164 | actual_output.should_not match(/Error/i) 165 | end 166 | 167 | Then %r{^gem spec key '(.*)' contains \/(.*)\/} do |key, regex| 168 | in_project_folder do 169 | gem_file = Dir["pkg/*.gem"].first 170 | gem_spec = Gem::Specification.from_yaml(`gem spec #{gem_file}`) 171 | spec_value = gem_spec.send(key.to_sym) 172 | spec_value.to_s.should match(/#{regex}/) 173 | end 174 | end 175 | -------------------------------------------------------------------------------- /lib/ohai/system.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 | @providers = Mash.new 37 | @plugin_path = "" 38 | end 39 | 40 | def [](key) 41 | @data[key] 42 | end 43 | 44 | def []=(key, value) 45 | @data[key] = value 46 | end 47 | 48 | def each(&block) 49 | @data.each do |key, value| 50 | block.call(key, value) 51 | end 52 | end 53 | 54 | def attribute?(name) 55 | @data.has_key?(name) 56 | end 57 | 58 | def set(name, *value) 59 | set_attribute(name, *value) 60 | end 61 | 62 | def from(cmd) 63 | status, stdout, stderr = run_command(:command => cmd) 64 | stdout.chomp! 65 | end 66 | 67 | def provides(*paths) 68 | paths.each do |path| 69 | parts = path.split('/') 70 | h = @providers 71 | unless parts.length == 0 72 | parts.shift if parts[0].length == 0 73 | parts.each do |part| 74 | h[part] ||= Mash.new 75 | h = h[part] 76 | end 77 | end 78 | h[:_providers] ||= [] 79 | h[:_providers] << @plugin_path 80 | end 81 | end 82 | 83 | # Set the value equal to the stdout of the command, plus run through a regex - the first piece of match data is the value. 84 | def from_with_regex(cmd, *regex_list) 85 | regex_list.flatten.each do |regex| 86 | status, stdout, stderr = run_command(:command => cmd) 87 | stdout.chomp! 88 | md = stdout.match(regex) 89 | return md[1] 90 | end 91 | end 92 | 93 | def set_attribute(name, *value) 94 | @data[name] = *value 95 | @data[name] 96 | end 97 | 98 | def get_attribute(name) 99 | @data[name] 100 | end 101 | 102 | def all_plugins 103 | require_plugin('os') 104 | 105 | Ohai::Config[:plugin_path].each do |path| 106 | [ 107 | Dir[File.join(path, '*')], 108 | Dir[File.join(path, @data[:os], '**', '*')] 109 | ].flatten.each do |file| 110 | file_regex = Regexp.new("#{path}#{File::SEPARATOR}(.+).rb$") 111 | md = file_regex.match(file) 112 | if md 113 | plugin_name = md[1].gsub(File::SEPARATOR, "::") 114 | require_plugin(plugin_name) unless @seen_plugins.has_key?(plugin_name) 115 | end 116 | end 117 | end 118 | end 119 | 120 | def collect_providers(providers) 121 | refreshments = [] 122 | if providers.is_a?(Mash) 123 | providers.keys.each do |provider| 124 | if provider.eql?("_providers") 125 | refreshments << providers[provider] 126 | else 127 | refreshments << collect_providers(providers[provider]) 128 | end 129 | end 130 | else 131 | refreshments << providers 132 | end 133 | refreshments.flatten.uniq 134 | end 135 | 136 | def refresh_plugins(path = '/') 137 | parts = path.split('/') 138 | if parts.length == 0 139 | h = @providers 140 | else 141 | parts.shift if parts[0].length == 0 142 | h = @providers 143 | parts.each do |part| 144 | break unless h.has_key?(part) 145 | h = h[part] 146 | end 147 | end 148 | 149 | refreshments = collect_providers(h) 150 | Ohai::Log.debug("Refreshing plugins: #{refreshments.join(", ")}") 151 | 152 | refreshments.each do |r| 153 | @seen_plugins.delete(r) if @seen_plugins.has_key?(r) 154 | end 155 | refreshments.each do |r| 156 | require_plugin(r) unless @seen_plugins.has_key?(r) 157 | end 158 | end 159 | 160 | def require_plugin(plugin_name, force=false) 161 | unless force 162 | return true if @seen_plugins[plugin_name] 163 | end 164 | 165 | @plugin_path = plugin_name 166 | 167 | filename = "#{plugin_name.gsub("::", File::SEPARATOR)}.rb" 168 | 169 | Ohai::Config[:plugin_path].each do |path| 170 | check_path = File.expand_path(File.join(path, filename)) 171 | begin 172 | @seen_plugins[plugin_name] = true 173 | Ohai::Log.debug("Loading plugin #{plugin_name}") 174 | from_file(check_path) 175 | return true 176 | rescue IOError => e 177 | Ohai::Log.debug("No #{plugin_name} at #{check_path}") 178 | rescue Exception,Errno::ENOENT => e 179 | Ohai::Log.debug("Plugin #{plugin_name} threw exception #{e.inspect}") 180 | end 181 | end 182 | end 183 | 184 | # Sneaky! Lets us stub out require_plugin when testing plugins, but still 185 | # call the real require_plugin to kick the whole thing off. 186 | alias :_require_plugin :require_plugin 187 | 188 | # Serialize this object as a hash 189 | def to_json(*a) 190 | output = @data.clone 191 | output["json_class"] = self.class.name 192 | output.to_json(*a) 193 | end 194 | 195 | # Pretty Print this object as JSON 196 | def json_pretty_print 197 | JSON.pretty_generate(@data) 198 | end 199 | 200 | # Create an Ohai::System from JSON 201 | def self.json_create(o) 202 | ohai = new 203 | o.each do |key, value| 204 | ohai.data[key] = value unless key == "json_class" 205 | end 206 | ohai 207 | end 208 | 209 | def method_missing(name, *args) 210 | return get_attribute(name) if args.length == 0 211 | 212 | set_attribute(name, *args) 213 | end 214 | 215 | private 216 | def load_plugin_file 217 | 218 | end 219 | end 220 | end 221 | -------------------------------------------------------------------------------- /lib/ohai/mixin/command.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 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 . 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 -------------------------------------------------------------------------------- /lib/ohai/plugins/darwin/network.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Benjamin Black () 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 | provides "network", "counters/network" 20 | 21 | require 'scanf' 22 | 23 | network[:default_interface] = from("route -n get default \| grep interface: \| awk \'/: / \{print \$2\}\'") 24 | 25 | def parse_media(media_string) 26 | media = Array.new 27 | line_array = media_string.split(' ') 28 | 29 | 0.upto(line_array.length - 1) do |i| 30 | unless line_array[i].eql?("none") 31 | if line_array[i + 1] =~ /^\<([a-zA-Z\-\,]+)\>$/ 32 | media << { line_array[i] => { "options" => $1.split(',') }} 33 | else 34 | media << { "autoselect" => { "options" => [] } } if line_array[i].eql?("autoselect") 35 | end 36 | else 37 | media << { "none" => { "options" => [] } } 38 | end 39 | end 40 | 41 | media 42 | end 43 | 44 | def encaps_lookup(ifname) 45 | return "Loopback" if ifname.eql?("lo") 46 | return "1394" if ifname.eql?("fw") 47 | return "IPIP" if ifname.eql?("gif") 48 | return "6to4" if ifname.eql?("stf") 49 | return "dot1q" if ifname.eql?("vlan") 50 | "Unknown" 51 | end 52 | 53 | def scope_lookup(scope) 54 | return "Link" if scope.match(/^fe80\:/) 55 | return "Site" if scope.match(/^fec0\:/) 56 | "Global" 57 | end 58 | 59 | def excluded_setting?(setting) 60 | setting.match('_sw_cksum') 61 | end 62 | 63 | def locate_interface(ifaces, ifname, mac) 64 | return ifname unless ifaces[ifname].nil? 65 | # oh well, time to go hunting! 66 | return ifname.chop if ifname.match /\*$/ 67 | ifaces.keys.each do |ifc| 68 | ifaces[ifc][:addresses].keys.each do |addr| 69 | return ifc if addr.eql? mac 70 | end 71 | end 72 | 73 | nil 74 | end 75 | 76 | iface = Mash.new 77 | popen4("ifconfig -a") do |pid, stdin, stdout, stderr| 78 | stdin.close 79 | cint = nil 80 | stdout.each do |line| 81 | if line =~ /^([0-9a-zA-Z\.\:\-]+): \S+ mtu (\d+)$/ 82 | cint = $1 83 | iface[cint] = Mash.new unless iface[cint]; iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] 84 | iface[cint][:mtu] = $2 85 | if line =~ /\sflags\=\d+\<((UP|BROADCAST|DEBUG|SMART|SIMPLEX|LOOPBACK|POINTOPOINT|NOTRAILERS|RUNNING|NOARP|PROMISC|ALLMULTI|SLAVE|MASTER|MULTICAST|DYNAMIC|,)+)\>\s/ 86 | flags = $1.split(',') 87 | else 88 | flags = Array.new 89 | end 90 | iface[cint][:flags] = flags.flatten 91 | if cint =~ /^(\w+)(\d+.*)/ 92 | iface[cint][:type] = $1 93 | iface[cint][:number] = $2 94 | iface[cint][:encapsulation] = encaps_lookup($1) 95 | end 96 | end 97 | if line =~ /^\s+ether ([0-9a-f\:]+)\s/ 98 | iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] 99 | iface[cint][:addresses][$1] = { "family" => "lladdr" } 100 | iface[cint][:encapsulation] = "Ethernet" 101 | end 102 | if line =~ /^\s+lladdr ([0-9a-f\:]+)\s/ 103 | iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] 104 | iface[cint][:addresses][$1] = { "family" => "lladdr" } 105 | end 106 | if line =~ /\s+inet (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) netmask 0x(([0-9a-f]){1,8})\s*$/ 107 | iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] 108 | iface[cint][:addresses][$1] = { "family" => "inet", "netmask" => $2.scanf('%2x'*4)*"."} 109 | end 110 | if line =~ /\s+inet (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) netmask 0x(([0-9a-f]){1,8}) broadcast (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/ 111 | iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] 112 | iface[cint][:addresses][$1] = { "family" => "inet", "netmask" => $2.scanf('%2x'*4)*".", "broadcast" => $4 } 113 | end 114 | if line =~ /\s+inet6 ([a-f0-9\:]+)(\s*|(\%[a-z0-9]+)\s*) prefixlen (\d+)\s*$/ 115 | iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] 116 | iface[cint][:addresses][$1] = { "family" => "inet6", "prefixlen" => $4 , "scope" => "Node" } 117 | end 118 | if line =~ /\s+inet6 ([a-f0-9\:]+)(\s*|(\%[a-z0-9]+)\s*) prefixlen (\d+) scopeid 0x([a-f0-9]+)/ 119 | iface[cint][:addresses] = Mash.new unless iface[cint][:addresses] 120 | iface[cint][:addresses][$1] = { "family" => "inet6", "prefixlen" => $4 , "scope" => scope_lookup($1) } 121 | end 122 | if line =~ /^\s+media: ((\w+)|(\w+ [a-zA-Z0-9\-\<\>]+)) status: (\w+)/ 123 | iface[cint][:media] = Mash.new unless iface[cint][:media] 124 | iface[cint][:media][:selected] = parse_media($1) 125 | iface[cint][:status] = $4 126 | end 127 | if line =~ /^\s+supported media: (.*)/ 128 | iface[cint][:media] = Mash.new unless iface[cint][:media] 129 | iface[cint][:media][:supported] = parse_media($1) 130 | end 131 | end 132 | end 133 | 134 | popen4("arp -an") do |pid, stdin, stdout, stderr| 135 | stdin.close 136 | stdout.each do |line| 137 | if line =~ /^\S+ \((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\) at ([a-fA-F0-9\:]+) on ([a-zA-Z0-9\.\:\-]+) \[(\w+)\]/ 138 | # MAC addr really should be normalized to include all the zeroes. 139 | next if iface[$3].nil? # this should never happen 140 | iface[$3][:arp] = Mash.new unless iface[$3][:arp] 141 | iface[$3][:arp][$1] = $2 142 | end 143 | end 144 | end 145 | 146 | settings = Mash.new 147 | popen4("sysctl net") do |pid, stdin, stdout, stderr| 148 | stdin.close 149 | stdout.each do |line| 150 | if line =~ /^([a-zA-Z0-9\.\_]+)\: (.*)/ 151 | # should normalize names between platforms for the same settings. 152 | settings[$1] = $2 unless excluded_setting?($1) 153 | end 154 | end 155 | end 156 | 157 | network[:settings] = settings 158 | network[:interfaces] = iface 159 | 160 | net_counters = Mash.new 161 | popen4("netstat -i -d -l -b -n") do |pid, stdin, stdout, stderr| 162 | stdin.close 163 | stdout.each do |line| 164 | if line =~ /^([a-zA-Z0-9\.\:\-\*]+)\s+\d+\s+\<[a-zA-Z0-9\#]+\>\s+([a-f0-9\:]+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/ || 165 | line =~ /^([a-zA-Z0-9\.\:\-\*]+)\s+\d+\s+\<[a-zA-Z0-9\#]+\>(\s+)(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/ 166 | ifname = locate_interface(iface, $1, $2) 167 | next if iface[ifname].nil? # this shouldn't happen, but just in case 168 | net_counters[ifname] = Mash.new unless net_counters[ifname] 169 | net_counters[ifname] = { :rx => { :bytes => $5, :packets => $3, :errors => $4, :drop => 0, :overrun => 0, :frame => 0, :compressed => 0, :multicast => 0 }, 170 | :tx => { :bytes => $8, :packets => $6, :errors => $7, :drop => 0, :overrun => 0, :collisions => $9, :carrier => 0, :compressed => 0 } 171 | } 172 | end 173 | end 174 | end 175 | 176 | counters[:network][:interfaces] = net_counters --------------------------------------------------------------------------------