rubyohai-gem

How to use ohai gem


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.


Solution

  • 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