forked from magmax/sensu-plugins-windows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
performance-counters.rb
executable file
·114 lines (100 loc) · 2.83 KB
/
performance-counters.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
#! /usr/bin/env ruby
#
# metrics.rb
#
# DESCRIPTION:
#
# OUTPUT:
# metric data
#
# PLATFORMS:
# Windows
#
# DEPENDENCIES:
# gem: sensu-plugin
#
# USAGE:
# A configuration file is required. It should be a YAML with this structure:
# ---
# PERFORMANCE COUNTER:
# scheme: RELATIVE.SCHEME
# min: VALUE
# max: VALUE
# ...
#
# Just the performance counter is mandatory. The rest is optional and his meaning is:
# - scheme: relative scheme to be used. It will use the performance counter name if empty.
# - min: checks the value to be higher than this or fails.
# - max: checks the value to be lower than this or fails.
#
# NOTES:
# Tested on Windows 2012RC2.
#
# LICENSE:
# Miguel Angel Garcia <miguelangel.garcia@gmail.com>
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.
#
require 'sensu-plugin/metric/cli'
require 'socket'
require 'yaml'
require 'csv'
#
# Generic Metrics
#
class GenericMetric < Sensu::Plugin::Metric::CLI::Graphite
option :scheme,
description: 'Global scheme, text to prepend to .$relative_scheme',
long: '--scheme SCHEME',
default: "#{Socket.gethostname}"
option :file,
short: '-f file',
default: 'metrics.yaml'
def check_min(data, key, value)
return true unless data.include? 'min'
min = data['min']
return true unless value.to_f < min.to_f
puts "CHECK ERROR: Value #{value} is less than #{min} for key #{key}"
false
end
def check_max(data, key, value)
return true unless data.include? 'max'
max = data['max']
return true unless value.to_f > max.to_f
puts "CHECK ERROR: Value #{value} is greater than #{max} for key #{key}"
false
end
def run
metrics = YAML.load_file(config[:file])
counters = metrics.keys
is_ok = true
flatten = counters.map { |s| "\"#{s}\"" }.join(' ')
timestamp = Time.now.utc.to_i
IO.popen("typeperf -sc 1 #{flatten} ") do |io|
CSV.parse(io.read, headers: true) do |row|
row.each do |k, v|
next unless v && k
break if row.to_s.start_with? 'Exiting'
key = k.split('\\', 4)[3]
data = metrics.fetch("\\#{key}", nil)
next unless data
relative_scheme = data.fetch('scheme', nil)
relative_scheme ||= key.tr('{}()\- .', '_').tr('\\', '.')
value = format('%.2f', v.to_f)
name = [config[:scheme], relative_scheme].join('.')
output name, value, timestamp
# if min and max keys not included then skip the rest
next unless data.include?('min') || data.include?('max')
# if values is less then min dont bother checking max
next unless (is_ok = check_min(data, key, value))
is_ok = check_max(data, key, value)
end
end
end
if is_ok
ok
else
critical
end
end
end