sensu-plugins-influxdb-metrics-checker 0.3.2 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2958d13425df17c77a4a87981fd296d80671920d
4
- data.tar.gz: 32e02acbf484a02e223db6b56b124cb114cd1617
3
+ metadata.gz: 86fe26cfb281849f4b73356b47011b08bf1a7960
4
+ data.tar.gz: dca6df28cc658d7faff6274090ecb5b050b5654a
5
5
  SHA512:
6
- metadata.gz: 4754bb681f3e2679293466e1ce90a507ebad630ca8a756c539cfcf979d853a75df785f0125068e6d47b23bf56629c70a82da5b5c3ef7d99ed96c7f864a1a8319
7
- data.tar.gz: 75534ba3330f532126d1c8e8e06d37911d937b13c5a1f20f4ea60ebce2de35bd8a9a197ba792fdd2b307e8e70302d3b7e62cded889d0fa938e9f176c3598b5f7
6
+ metadata.gz: 861427b6a1b16058eb9bfc51c0186eed75d765942325762932933ec8d67efb81f0192c2804be9c4c443acd73545ce8a134ae6f5e96de219504a23ebda7b32524
7
+ data.tar.gz: 74d9af1f5b6de576c991bcece54f24041ca43d2b7128c60e7c5f7f825b4ad90eb6e1dfb49e83f711b3130c95d6c799328b35c694aab67cd7319593a2e82d1f3e
data/CHANGELOG.md CHANGED
@@ -3,7 +3,13 @@ This project adheres to [Semantic Versioning](http://semver.org/).
3
3
 
4
4
  This CHANGELOG follows the format listed at [Keep A Changelog](http://keepachangelog.com/)
5
5
 
6
- # [0.3.1] - 2016-12-14
6
+ # [0.3.4] - 2017-01-17
7
+ - sixth release
8
+ Allow the usage of regex expressions that we can identify as "/^[your_regex]$". I'll strongly recommend to use this only for exceptions, and always aim for zero-exceptions, or it wouldn't be accurate. At the moment it will fire when the number of exception today is bigger than the number of exceptions yesterday.
9
+ Also, if you have the same number of metrics found (let's say, exceptions) it will compare just the first one. I'll not rely on this tool (yet) for a deep analysis of differences in exceptions.
10
+ Improve feedback when returning null from InfluxDB (usually because the query is pointing to a metric that doesn't exists).
11
+
12
+ # [0.3.2] - 2016-12-14
7
13
  ### Added
8
14
  - fifth release
9
15
  Rounded float result to 5 decimal places
@@ -33,5 +39,6 @@ Leaving 5 minutes to the data to consolidate.
33
39
  [0.1.0]: https://github.com/pliyosenpai/sensu-plugins-influxdb-metrics-checker/0.1.0...0.2.0
34
40
  [0.2.0]: https://github.com/pliyosenpai/sensu-plugins-influxdb-metrics-checker/0.2.0...0.2.1
35
41
  [0.2.1]: https://github.com/pliyosenpai/sensu-plugins-influxdb-metrics-checker/0.2.1...0.3.0
36
- [0.3.0]: https://github.com/pliyosenpai/sensu-plugins-influxdb-metrics-checker/0.3.0...0.3.1
37
- [0.3.1]: https://github.com/pliyosenpai/sensu-plugins-influxdb-metrics-checker/0.3.1...HEAD
42
+ [0.3.0]: https://github.com/pliyosenpai/sensu-plugins-influxdb-metrics-checker/0.3.0...0.3.2
43
+ [0.3.2]: https://github.com/pliyosenpai/sensu-plugins-influxdb-metrics-checker/0.3.2...0.3.4
44
+ [0.3.3]: https://github.com/pliyosenpai/sensu-plugins-influxdb-metrics-checker/0.3.4...HEAD
data/README.md CHANGED
@@ -40,12 +40,28 @@ ruby check-influxdb-metrics.rb --host=metrics-influxdb.internal.com --port=8086
40
40
 
41
41
  ```
42
42
 
43
- Also, you can set the period that you want for your queries, for example:
43
+ You can set the period that you want for your queries, for example:
44
44
  ```
45
45
  ruby check-influxdb-metrics.rb --host=metrics-influxdb.internal.com --port=8086 --user=admin --password=password -c -20 -w -10 --db=statsd_metrics --metric=api.request.counter --tag=datacenter --filter=ci --period=1440
46
46
 
47
47
  ```
48
48
 
49
+ ## Advanced Queries
50
+
51
+ You can use Regex in your metrics. The spirit behind this feature is to gather information about exceptions only, always aiming for a zero exception policy. So I'll advise against using it for other purposes.
52
+
53
+ **How it works**
54
+ 1. It will understand that is a regex only when the metric name contains '/'. In the bash you'll need to include your metric inside double quotes.
55
+ 2. It will compare the number of metrics gathered today vs the number of metrics gathered yesterday.
56
+ 3. If today we read more than yesterday, it will blow up as **Critical**.
57
+ 4. If today we read the same number of metrics than yesterday, at the moment it will compare only the first one.
58
+ I'll not rely on this tool (yet) for a deep analysis of differences when comparing multiple metrics (such as exceptions) using Regex.
59
+ ```
60
+ ruby check-influxdb-metrics.rb --host=metrics-influxdb.internal.com --port=8086 --user=admin --password=password -c -20 -w -10 --db=statsd_metrics --metric="/^prefix.datacenter.([A-Za-z0-9-]+).([A-Za-z0-9-]+).exceptions$/"
61
+
62
+ ```
63
+
64
+
49
65
  ## Lessons learnt
50
66
  The InfluxDb query language that we used is not the latest, you can find it here:
51
67
 
@@ -90,7 +90,24 @@ class CheckInfluxDbMetrics < Sensu::Plugin::Check::CLI
90
90
  end
91
91
 
92
92
  def base_query
93
- "SELECT sum(\"value\") from \"#{config[:metric]}\" "
93
+ 'SELECT sum("value") from '
94
+ end
95
+
96
+ def base_query_with_metricname
97
+ base_query + clean_quotes_when_regex
98
+ end
99
+
100
+ def clean_quotes_when_regex
101
+ metric = " \"#{config[:metric]}\""
102
+ clean_metric = ''
103
+ if metric.include?('/')
104
+ clean_metric = metric.tr '\"', ''
105
+ @is_using_regex = true
106
+ else
107
+ clean_metric = metric
108
+ end
109
+
110
+ clean_metric
94
111
  end
95
112
 
96
113
  def today_query_for_a_period
@@ -108,13 +125,20 @@ class CheckInfluxDbMetrics < Sensu::Plugin::Check::CLI
108
125
  end
109
126
 
110
127
  def query_for_a_period(start_period, end_period)
111
- query = base_query + ' WHERE time > now() - ' + end_period.to_s + 'm AND time < now() - ' + start_period.to_s + 'm'
128
+ query = base_query_with_metricname + ' WHERE time > now() - ' + end_period.to_s + 'm AND time < now() - ' + start_period.to_s + 'm'
112
129
  query + filter_by_environment_when_needed
113
130
  end
114
131
 
115
132
  def encode_parameters(parameters)
116
133
  encodedparams = Addressable::URI.escape(parameters)
117
- "#{config[:db]}&q=" + encodedparams
134
+ # this is needed after encoding because Addressable will not encode +. So for ex: ([A-Za-z0-9-]+) will miss the + and with that it will not find the metrics
135
+ encode_for_regex = if @is_using_regex
136
+ encodedparams.gsub! '+', '%2B'
137
+ else
138
+ encodedparams
139
+ end
140
+
141
+ "#{config[:db]}&q=" + encode_for_regex
118
142
  end
119
143
 
120
144
  def yesterday_query_encoded
@@ -128,30 +152,108 @@ class CheckInfluxDbMetrics < Sensu::Plugin::Check::CLI
128
152
  end
129
153
 
130
154
  def today_value
131
- second_query = today_query_encoded
132
- response_to_compare = request(second_query)
133
- read_metrics(response_to_compare)
155
+ response = request(today_query_encoded)
156
+ metrics = parse_json(response)
157
+ @today_metric_count = validate_metrics_and_count(metrics)
158
+ value = if @today_metric_count > 0
159
+ series = read_series_from_metrics(metrics)
160
+ @today_metrics = store_metrics(series)
161
+ read_value_from_series(series)
162
+ end
163
+ value
134
164
  end
135
165
 
136
166
  def yesterday_value
137
- query = yesterday_query_encoded
138
- response = request(query)
139
- read_metrics(response)
167
+ response = request(yesterday_query_encoded)
168
+ metrics = parse_json(response)
169
+ @yesterday_metric_count = validate_metrics_and_count(metrics)
170
+ value = if @today_metric_count > 0
171
+ series = read_series_from_metrics(metrics)
172
+ @yesterday_metrics = store_metrics(series)
173
+ read_value_from_series(series)
174
+ end
175
+ value
140
176
  end
141
177
 
142
- def read_metrics(response)
143
- metrics = JSON.parse(response.to_str)['results']
144
- series = metrics[0]['series']
145
- values = series[0]['values'][0][1]
178
+ def metric_bigger_than_zero?(metric)
179
+ metric > 0
180
+ end
146
181
 
147
- if values.nil?
148
- values = 0
182
+ def using_regex?(using_regex)
183
+ using_regex == true
184
+ end
185
+
186
+ def read_series_from_metrics(metrics)
187
+ metrics[0]['series']
188
+ end
189
+
190
+ def validate_metrics_and_count(metrics)
191
+ if metrics.empty? || metrics.nil? || metrics[0].nil? || metrics[0]['series'].nil? || metrics[0]['series'][0]['values'][0][1].nil?
192
+ 0
193
+ else
194
+ metrics[0]['series'].count
195
+ end
196
+ end
197
+
198
+ def parse_json(response)
199
+ JSON.parse(response.to_str)['results']
200
+ end
201
+
202
+ def read_value_from_series(series)
203
+ if series.nil?
204
+ 0
205
+ elsif series[0]['values'][0][1].nil?
206
+ 0
207
+ else
208
+ values = series[0]['values'][0][1]
209
+ values.to_f
149
210
  end
211
+ end
150
212
 
151
- values.to_f
213
+ def store_metrics(series)
214
+ regex_metrics = Hash.new {}
215
+ if series.nil? == false && series.count > 0
216
+ series.each do |values|
217
+ value = values['values'][0][1]
218
+ regex_metrics[values['name']] = value.to_f
219
+ end
220
+ regex_metrics
221
+ end
152
222
  end
153
223
 
154
- def calculate_percentage_ofdifference(original, newnumber)
224
+ def display_metrics
225
+ puts 'Today metrics analysis: '
226
+ @today_metrics.each do |key, value|
227
+ puts 'For: ' + key + ' : ' + value.to_s
228
+ end
229
+ end
230
+
231
+ def difference_for_regex_and_notify
232
+ # different or more number of metrics for today could be a problem. Regex is designed to catch exceptions, so more exceptions -> alert
233
+ if @today_metric_count == 0 && @yesterday_metric_count == 0
234
+ ok 'no metrics found'
235
+ elsif @today_metric_count > @yesterday_metric_count
236
+ display_metrics
237
+ critical "For \"#{config[:metric]}\" more metrics were tracked today than yesterday. Check them out above"
238
+ elsif @today_metric_count == @yesterday_metric_count
239
+ compare_each_metric_in_regex
240
+ else
241
+ ok 'regex seems ok ' + @today_metric_count.to_s + ' metrics found today vs ' + @yesterday_metric_count.to_s + ' metrics found yesterday'
242
+ end
243
+ end
244
+
245
+ def compare_each_metric_in_regex
246
+ @today_metrics.each do |today_key, today_value|
247
+ @yesterday_metrics.each do |yesterday_key, yesterday_value|
248
+ if today_key.eql? yesterday_key
249
+ puts yesterday_value.to_s + ' vs ' + today_value.to_s + ' for ' + today_key
250
+ difference_for_standard_queries(today_value, yesterday_value)
251
+ end
252
+ end
253
+ end
254
+ end
255
+
256
+ def difference_between_two_metrics(original, newnumber)
155
257
  decrease = original - newnumber
156
258
  decrease.to_f / original.to_f * 100
157
259
  end
@@ -169,6 +271,7 @@ class CheckInfluxDbMetrics < Sensu::Plugin::Check::CLI
169
271
  end
170
272
 
171
273
  def evaluate_percentage_and_notify(difference)
274
+ puts 'Difference of: ' + difference.round(4).to_s + ' % for a period of ' + config[:period].to_s + 'm'
172
275
  if difference < config[:crit]
173
276
  critical "\"#{config[:metric]}\" difference is below allowed minimum of #{config[:crit]} %"
174
277
  elsif difference < config[:warn]
@@ -178,16 +281,39 @@ class CheckInfluxDbMetrics < Sensu::Plugin::Check::CLI
178
281
  end
179
282
  end
180
283
 
181
- def run
182
- difference = calculate_percentage_ofdifference(today_value, yesterday_value)
183
- puts 'Difference of: ' + difference.round(5).to_s + ' % for a period of ' + config[:period].to_s + 'm'
284
+ def calculate_difference_and_display_result(today, yesterday)
285
+ difference = if @is_using_regex
286
+ difference_for_regex_and_notify
287
+ else
288
+ difference_for_standard_queries(today, yesterday)
289
+ end
290
+ difference
291
+ end
292
+
293
+ def difference_for_standard_queries(today, yesterday)
294
+ difference = difference_between_two_metrics(today, yesterday)
184
295
  evaluate_percentage_and_notify(difference)
296
+ end
297
+
298
+ def difference_in_metrics
299
+ today = today_value
300
+ yesterday = yesterday_value
301
+ if today.nil? && yesterday.nil?
302
+ puts 'No results coming from InfluxDB either for Today nor Yesterday. Please check your query or try again'
303
+ else
304
+ calculate_difference_and_display_result(today, yesterday)
305
+ end
306
+ exit
307
+ end
308
+
309
+ def run
310
+ difference_in_metrics
185
311
 
186
312
  rescue Errno::ECONNREFUSED => e
187
313
  critical 'InfluxDB is not responding' + e.message
188
314
  rescue RestClient::RequestTimeout
189
315
  critical 'InfluxDB Connection timed out'
190
316
  rescue StandardError => e
191
- unknown 'An exception occurred:' + e.message
317
+ unknown 'An exception occurred: ' + e.message
192
318
  end
193
319
  end
@@ -2,7 +2,7 @@ module SensuPluginsInfluxDbMetricsChecker
2
2
  module Version
3
3
  MAJOR = 0
4
4
  MINOR = 3
5
- PATCH = 2
5
+ PATCH = 4
6
6
 
7
7
  VER_STRING = [MAJOR, MINOR, PATCH].compact.join('.')
8
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sensu-plugins-influxdb-metrics-checker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juanjo Guerrero Cerezuela
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-14 00:00:00.000000000 Z
11
+ date: 2017-01-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sensu-plugin