forked from sensu-plugins/sensu-plugins-mesos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics-mesos.rb
executable file
·116 lines (106 loc) · 2.92 KB
/
metrics-mesos.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#! /usr/bin/env ruby
#
# mesos-metrics
#
# DESCRIPTION:
# This plugin extracts the stats from a mesos master or slave
#
# OUTPUT:
# metric data
#
# PLATFORMS:
# Linux
#
# DEPENDENCIES:
# gem: sensu-plugin
# gem: rest-client
# gem: socket
# gem: json
#
# USAGE:
# #YELLOW
#
# NOTES:
#
# LICENSE:
# Copyright 2015, Tom Stockton ([email protected])
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.
#
require 'sensu-plugin/metric/cli'
require 'rest-client'
require 'socket'
require 'json'
# Mesos default ports are defined here: http://mesos.apache.org/documentation/latest/configuration
MASTER_DEFAULT_PORT ||= '5050'.freeze
SLAVE_DEFAULT_PORT ||= '5051'.freeze
class MesosMetrics < Sensu::Plugin::Metric::CLI::Graphite
option :mode,
description: 'master or slave',
short: '-m MODE',
long: '--mode MODE',
required: true
option :scheme,
description: 'Metric naming scheme',
short: '-s SCHEME',
long: '--scheme SCHEME',
default: Socket.gethostname.to_s
option :server,
description: 'Mesos Host',
short: '-h SERVER',
long: '--host SERVER',
default: 'localhost'
option :port,
description: "port (default #{MASTER_DEFAULT_PORT} for master, #{SLAVE_DEFAULT_PORT} for slave)",
short: '-p PORT',
long: '--port PORT',
required: false
option :uri,
description: 'Endpoint URI',
short: '-u URI',
long: '--uri URI',
default: '/metrics/snapshot'
option :timeout,
description: 'timeout in seconds',
short: '-t TIMEOUT',
long: '--timeout TIMEOUT',
proc: proc(&:to_i),
default: 5
option :include_role,
description: 'Include master role in metrics',
short: '-r INCLUDE_ROLE',
long: '--host INCLUDE_ROLE',
default: 'false'
def run
uri = config[:uri]
case config[:mode]
when 'master'
port = config[:port] || MASTER_DEFAULT_PORT
when 'slave'
port = config[:port] || SLAVE_DEFAULT_PORT
end
scheme = "#{config[:scheme]}.mesos-#{config[:mode]}"
begin
r = RestClient::Resource.new("http://#{config[:server]}:#{port}#{uri}", timeout: config[:timeout]).get
results = JSON.parse(r)
if config[:include_role] == 'true' && config[:mode] == 'master'
add_metric = if results['master/elected'] != 0.0
'leader.'
else
'standby.'
end
else
add_metric = ''
end
results.each do |k, v|
k_copy = k.tr('/', '.')
output([scheme, add_metric + k_copy].join('.'), v)
end
rescue Errno::ECONNREFUSED
critical "Mesos #{config[:mode]} is not responding"
rescue RestClient::RequestTimeout
critical "Mesos #{config[:mode]} Connection timed out"
end
ok
end
end