Class: ESMonitor

Inherits:
Rubix::ClusterMonitor show all
Defined in:
lib/rubix/examples/es_monitor.rb

Constant Summary collapse

CLUSTER_HOSTGROUPS =

Hostgroup for any hosts that needs to be created.

'Elasticsearch clusters'
CLUSTER_TEMPLATES =

Templates for any hosts that need to be created.

'Template_Elasticsearch_Cluster'
NODE_TEMPLATES =
'Template_Elasticsearch_Node'
CLUSTER_APPLICATIONS =

Applications for new items

'_cluster'
NODE_APPLICATIONS =
'Elasticsearch'

Instance Attribute Summary

Attributes inherited from Rubix::ClusterMonitor

#nodes_by_cluster, #private_ips_by_cluster

Attributes inherited from Rubix::Monitor

#settings

Instance Method Summary collapse

Methods inherited from Rubix::ClusterMonitor

#clusters, #group_nodes_by_cluster, #initialize, #matching_chef_nodes, #measure

Methods inherited from Rubix::ChefMonitor

#chef_node_from_node_name, #chef_node_name_from_ip, default_settings, #initialize, #search_nodes, #set_chef_credentials

Methods inherited from Rubix::Monitor

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

Constructor Details

This class inherits a constructor from Rubix::ClusterMonitor

Instance Method Details

#es_url(private_ip, *args) ⇒ Object



26
27
28
# File 'lib/rubix/examples/es_monitor.rb', line 26

def es_url private_ip, *args
  "http://" + File.join(private_ip + ":9200", *args)
end

#measure_cluster(cluster_name) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rubix/examples/es_monitor.rb', line 30

def measure_cluster cluster_name
  measured_cluster_health  = false
  measured_cluster_indices = false
  measured_cluster_nodes   = false
  private_ips_by_cluster[cluster_name].each do |private_ip|
    measured_cluster_health  = measure_cluster_health(cluster_name, private_ip)  unless measured_cluster_health
    measured_cluster_indices = measure_cluster_indices(cluster_name, private_ip) unless measured_cluster_indices
    measured_cluster_nodes   = measure_cluster_nodes(cluster_name, private_ip)   unless measured_cluster_nodes
    break if measured_cluster_health && measured_cluster_indices && measured_cluster_nodes
  end
end

#measure_cluster_health(cluster_name, private_ip) ⇒ Object

Measure the cluster health metrics – /_cluster/health



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rubix/examples/es_monitor.rb', line 43

def measure_cluster_health cluster_name, private_ip
  begin
    cluster_health = JSON.parse(open(es_url(private_ip, '_cluster', 'health')).read)
  rescue SocketError, OpenURI::HTTPError, JSON::ParserError, Errno::ECONNREFUSED => e
    # This node may not be running a webnode...
    return false
  end
  write({
          :hostname    => "#{cluster_name}-elasticsearch",
          :hostgroup   => self.class::CLUSTER_HOSTGROUPS,
          :templates   => self.class::CLUSTER_TEMPLATES,
          :application => self.class::CLUSTER_APPLICATIONS
        }) do |d|
    d << ['status',              cluster_health['status']               ]
    d << ['nodes.total',         cluster_health['number_of_nodes']      ]
    d << ['nodes.data',          cluster_health['number_of_data_nodes'] ]
    d << ['shards.active',       cluster_health['active_shards']        ]
    d << ['shards.relocating',   cluster_health['relocating_shards']    ]
    d << ['shards.unassigned',   cluster_health['unassigned_shards']    ]
    d << ['shards.initializing', cluster_health['initializing_shards']  ]
  end
  true
end

#measure_cluster_indices(cluster_name, private_ip) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rubix/examples/es_monitor.rb', line 67

def measure_cluster_indices cluster_name, private_ip
  begin
    index_data = JSON.parse(open(es_url(private_ip, '_status')).read)
  rescue SocketError, OpenURI::HTTPError, JSON::ParserError, Errno::ECONNREFUSED => e
    # This node may not be running a webnode...
    return false
  end
  index_data['indices'].each_pair do |index_name, index_data|
    write({
            :hostname   => "#{cluster_name}-elasticsearch",
            :hostgroup  => self.class::CLUSTER_HOSTGROUPS,
            :templates  => self.class::CLUSTER_TEMPLATES,
            :appliation => index_name
          }) do |d|
      d << ["#{index_name}.size",           index_data["index"]["size_in_bytes"] ]
      d << ["#{index_name}.docs.num",       index_data["docs"]["num_docs"]       ]
      d << ["#{index_name}.docs.max",       index_data["docs"]["max_doc"]        ]
      d << ["#{index_name}.docs.deleted",   index_data["docs"]["deleted_docs"]   ]
      d << ["#{index_name}.operations",     index_data["translog"]["operations"] ]
      d << ["#{index_name}.merges.total",   index_data["merges"]["total"]        ]
      d << ["#{index_name}.merges.current", index_data["merges"]["current"]      ]
    end
  end
  true
end

#measure_cluster_nodes(cluster_name, private_ip) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/rubix/examples/es_monitor.rb', line 93

def measure_cluster_nodes cluster_name, private_ip
  begin
    nodes_data       = JSON.parse(open(es_url(private_ip, '_cluster', 'nodes')).read)
    nodes_stats_data = JSON.parse(open(es_url(private_ip, '_cluster', 'nodes', 'stats')).read)
  rescue SocketError, OpenURI::HTTPError, JSON::ParserError, Errno::ECONNREFUSED => e
    # This node may not be running a webnode...
    return false
  end

  nodes_stats_data['nodes'].each_pair do |id, stats|

    ip        = nodes_data['nodes'][id]['network']['primary_interface']['address']
    node_name = chef_node_name_from_ip(ip)
    next unless node_name
    write({
            :hostname    => node_name,
            :templates   => self.class::NODE_TEMPLATES,
            :application => self.class::NODE_APPLICATIONS
          }) do |d|
      # concurrency
      d << ['es.jvm.threads.count',     stats['jvm']['threads']['count']                   ]

      # garbage collection
      d << ['es.jvm.gc.coll_time',      stats['jvm']['gc']['collection_time_in_millis']    ]
      d << ['es.jvm.gc.coll_count',     stats['jvm']['gc']['collection_count']             ]

      # memory
      d << ['es.jvm.mem.heap_used',     stats['jvm']['mem']['heap_used_in_bytes']          ]
      d << ['es.jvm.mem.non_heap_used', stats['jvm']['mem']['non_heap_used_in_bytes']      ]
      d << ['es.jvm.mem.heap_comm',     stats['jvm']['mem']['heap_committed_in_bytes']     ]
      d << ['es.jvm.mem.non_heap_comm', stats['jvm']['mem']['non_heap_committed_in_bytes'] ]

      # indices
      d << ['es.indices.size',          stats['indices']['size_in_bytes']                  ]
    end
  end
  true
end

#node_queryObject



22
23
24
# File 'lib/rubix/examples/es_monitor.rb', line 22

def node_query
  'provides_service:*-elasticsearch'
end