Class: Rubix::ChefMonitor

Inherits:
Monitor
  • Object
show all
Defined in:
lib/rubix/monitors/chef_monitor.rb

Overview

A generic monitor class for constructing Zabbix monitors that need to 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::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 do |data|
          data << ['webserver.available', 1]
        end
        return
      end
    rescue => e
    end
    write do |data|
      data << ([['webserver.available', 0]])
    end
  end
end

WebserverMonitor.run if $0 == __FILE__

See documentation for Rubix::Monitor to understand how to run this script.

Direct Known Subclasses

ClusterMonitor

Instance Attribute Summary

Attributes inherited from Monitor

#settings

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Monitor

#close, #fifo?, #file?, #loop?, #loop_period, #measure, #output, #output_path, #run, run, #stdout?, #write

Constructor Details

#initialize(settings) ⇒ ChefMonitor

Returns a new instance of ChefMonitor.



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

def initialize settings
  super(settings)
  set_chef_credentials
end

Class Method Details

.default_settingsObject



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

def self.default_settings
  super().tap do |s|
    s.define :chef_server_url, :description => "Chef server URL" ,                     :required => true
    s.define :chef_node_name,  :description => "Node name to identify to Chef server", :required => true
    s.define :chef_client_key, :description => "Path to Chef client private key",      :required => true
  end
end

Instance Method Details

#chef_node_from_node_name(node_name) ⇒ Object



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

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



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

def chef_node_name_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['node_name']
end

#search_nodes(*args) ⇒ Object



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

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

#set_chef_credentialsObject



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

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