How to use ohai gem

3k Views Asked by At

In the OpsCode Wiki there is there following documentation:

require 'ohai'

# ...
# Profit! ;-)

How can I print the JSON data provided by the 'ohai' command but using IRB? I tried to see the code in application.rb but I get empty data.

require 'ohai/application'
ohai = Ohai::System.new
ohai.json_pretty_print
 => "{\n\n}" 

I am not trying to do this within Chef (or Shef), I just want to use the ohai gem itself, in my own app.

2

There are 2 best solutions below

0
On BEST ANSWER

Poking about in the Ohai::Application class (what you get when you run ohai), #run_application instantiates Ohai::System and, unless it was configured by a file, it calls all_plugins to populate it with data.

Presumably, Ohai::System#all_plugins delegates the data collection to the lib/ohai/plugins directory.

$ irb -rohai
> system = Ohai::System.new
 => #<Ohai::System:0x00000100988950 @data={}, @seen_plugins={}, @providers={}, @plugin_path="", @hints={}> 
> system.all_plugins
 => true 
> puts system.to_json
{"languages":{"ruby":{"platform":"x86_64-darwin10.8.0","version":"1.9.2", ...
> system.to_json.size
 => 42395 
0
On

I too searched for a simple and good library to do that...

I came across

Usagewatch

and after 5 minutes had my monitoring.rb done..

Install : gem install usagewatch

Install on mac : gem install usagewatch_ext

Refer to this article for more info