Created
March 8, 2012 16:43
-
-
Save dmerrick/2001999 to your computer and use it in GitHub Desktop.
tool for adding required json attributes to add hosts to nagios
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| require 'rubygems' | |
| require 'json' | |
| require 'pp' if $DEBUG | |
| class UpdateJsonForNagios | |
| KNIFE_CONFIG = "~/work/chef/.chef/knife.rb" | |
| def initialize(nodename) | |
| @nodename = nodename | |
| end | |
| def get_current_json | |
| @short_json = `/usr/bin/knife node show -c #{KNIFE_CONFIG} -F json #{@nodename}` | |
| @long_json = `/usr/bin/knife node show -c #{KNIFE_CONFIG} -F json -l #{@nodename}` | |
| end | |
| def parse_current_json | |
| @parsed_json = JSON.parse(@short_json) | |
| end | |
| def add_required_attributes | |
| # adding "roles" attribute | |
| run_list = @parsed_json["run_list"] | |
| run_list = run_list.select {|r| r =~ /role/} | |
| run_list.map! {|r| r.sub(/role\[(.*)\]/,'\1')} | |
| @parsed_json["roles"] = %w{base} + run_list | |
| # adding "fqdn", "hostname", and "ipaddress" attributes | |
| parsed_long_json = JSON.parse(@long_json) | |
| @parsed_json["fqdn"] = parsed_long_json["automatic"]["fqdn"] | |
| @parsed_json["hostname"] = parsed_long_json["automatic"]["hostname"] | |
| @parsed_json["ipaddress"] = parsed_long_json["automatic"]["ipaddress"] | |
| end | |
| def return_final_json | |
| if $DEBUG | |
| pp @parsed_json | |
| else | |
| puts @parsed_json.to_json | |
| end | |
| end | |
| def run | |
| get_current_json | |
| parse_current_json | |
| add_required_attributes | |
| return_final_json | |
| end | |
| end | |
| if $0 == __FILE__ | |
| UpdateJsonForNagios.new(ARGV.shift).run | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment