Module: Rubix::ChefMonitor

Included in:
HttpAvailabilityMonitor
Defined in:
lib/rubix/monitors/chef_monitor.rb

Overview

A module that lets monitors talk to Chef servers.

This class handles the low-level logic of connecting to Chef and parsing results from searches.

It’s still up to a subclass to determine how to make a measurement.

Here’s an example of a script which checks the availibility of a web server at the EC2 public hostname of the Chef node ‘webserver’.

#!/usr/bin/env ruby
# in webserver_monitor

require 'net/http'

class WebserverMonitor < Rubix::Monitor

  include Rubix::ChefMonitor

  def measure
    webserver = chef_node_from_node_name('webserver')
    begin
      if Net::HTTP.get_response(URI.parse("http://#{webserver['ec2']['public_hostname']}")).code.to_i == 200
        write ['webserver.available', 1]
        return
      end
    rescue => e
    end
    write ['webserver.available', 0]
  end
end

WebserverMonitor.run if $0 == __FILE__

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/rubix/monitors/chef_monitor.rb', line 38

def self.included klass
  klass.default_settings.tap do |s|
    s.define :chef_server_url, :description => "Chef server URL" ,                     :required => true, :default => 'http://localhost'
    s.define :chef_node_name,  :description => "Node name to identify to Chef server", :required => true, :default => ENV["HOSTNAME"]
    s.define :chef_client_key, :description => "Path to Chef client private key",      :required => true, :default => '/etc/chef/client.pem'
  end
end

Instance Method Details

#chef_node_from_ip(ip) ⇒ Object



69
70
71
72
73
74
# File 'lib/rubix/monitors/chef_monitor.rb', line 69

def chef_node_from_ip ip
  return if ip.nil? || ip.empty?
  results = search_nodes("ipaddress:#{ip} OR fqdn:#{ip}")
  return unless results.first.size > 0
  results.first.first
end

#chef_node_from_node_name(node_name) ⇒ Object



62
63
64
65
66
67
# File 'lib/rubix/monitors/chef_monitor.rb', line 62

def chef_node_from_node_name node_name
  return if node_name.nil? || node_name.empty?
  results = search_nodes("name:#{node_name}")
  return unless results.first.size > 0
  results.first.first
end

#chef_node_name_from_ip(ip) ⇒ Object



76
77
78
79
# File 'lib/rubix/monitors/chef_monitor.rb', line 76

def chef_node_name_from_ip ip
  node = chef_node_from_ip(ip)
  return node['node_name'] if node
end

#initialize(settings) ⇒ Object



46
47
48
49
# File 'lib/rubix/monitors/chef_monitor.rb', line 46

def initialize settings
  super(settings)
  set_chef_credentials
end

#search_nodes(*args) ⇒ Object



58
59
60
# File 'lib/rubix/monitors/chef_monitor.rb', line 58

def search_nodes *args
  Chef::Search::Query.new.search('node', *args)
end

#set_chef_credentialsObject



51
52
53
54
55
56
# File 'lib/rubix/monitors/chef_monitor.rb', line 51

def set_chef_credentials
  require 'chef'
  Chef::Config[:chef_server_url] = settings[:chef_server_url]
  Chef::Config[:node_name]       = settings[:chef_node_name]
  Chef::Config[:client_key]      = settings[:chef_client_key]
end