Module: Rubix::ClusterMonitor

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

Overview

A module for building monitors which measure items for several hosts in a cluster as well as items for the cluster itself.

This module assumes that an existing hosts method returns an Array of Zabbix hosts that can be grouped into clusters.

Here's an example:

#!/usr/bin/env ruby

class ClusterPingMonitor < Rubix::Monitor

  include Rubix::ClusterMonitor

  def measure_cluster cluster_name
    total_ping = 0.0
    num_hosts  = 0
    hosts_by_cluster[cluster_name].each do |host|
      total_ping += measure_host(host)
      num_hosts  += 1
    end
    write [cluster_name, 'average_ping', total_ping / num_hosts] unless num_hosts == 0
  end

  def measure_host host
    ping = measure_ping_to(host.ip)
    write [host.name, 'ping', ping]
    ping # return this so the measure_cluster method can use it
  end
end

ClusterPingMonitor.run if $0 == __FILE__

You may want to override the cluster_name_from_host method. By defaul it assumes that hosts in Zabbix are named 'cluster-facet-index', a la Ironfan.

Constant Summary collapse

DEFAULT_CLUSTER =

The name of the default cluster.

'All Hosts'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#hosts_by_clusterObject (readonly)

Returns the value of attribute hosts_by_cluster


44
45
46
# File 'lib/rubix/monitors/cluster_monitor.rb', line 44

def hosts_by_cluster
  @hosts_by_cluster
end

Instance Method Details

#cluster_name_from_host(host) ⇒ Object


70
71
72
73
74
75
76
77
78
# File 'lib/rubix/monitors/cluster_monitor.rb', line 70

def cluster_name_from_host host
  return default_cluster if host.name.nil? || host.name.empty?
  parts = host.name.split("-")
  if parts.size == 3
    parts.first
  else
    default_cluster
  end
end

#clustersObject


80
81
82
# File 'lib/rubix/monitors/cluster_monitor.rb', line 80

def clusters
  @hosts_by_cluster.keys
end

#default_clusterObject


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

def default_cluster
  ::Rubix::ClusterMonitor::DEFAULT_CLUSTER
end

#group_hosts_by_clusterObject


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

def group_hosts_by_cluster
  hosts.each do |host|
    cluster_name = cluster_name_from_host(host)
    @hosts_by_cluster[cluster_name] ||= []
    @hosts_by_cluster[cluster_name] << host
  end
end

#initialize(settings) ⇒ Object


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

def initialize settings
  super(settings)
  @hosts_by_cluster = {}
  group_hosts_by_cluster
end

#measureObject


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

def measure
  clusters.each do |cluster_name|
    measure_cluster(cluster_name)
  end
end