From cbe74cd2e89647b92526f8df697afb2cb265b484 Mon Sep 17 00:00:00 2001 From: Difrex Date: Tue, 8 Dec 2015 20:53:31 +0300 Subject: [PATCH] Sensu graphite metric check plugin --- plugins/graphite/README.md | 34 +++++++ plugins/graphite/graphite-metric-check.py | 108 ++++++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 plugins/graphite/README.md create mode 100755 plugins/graphite/graphite-metric-check.py diff --git a/plugins/graphite/README.md b/plugins/graphite/README.md new file mode 100644 index 0000000..1640381 --- /dev/null +++ b/plugins/graphite/README.md @@ -0,0 +1,34 @@ +# sensu-plugin-graphite-metric-check + +Check average value from graphite metric. + +## Usage +``` +usage: graphite-metric-check.py [-h] -w WARNING -c CRITICAL -s HOST [-p PORT] + -t TARGET [-f FROM_TIME] [-o TO_TIME] + [-m MESSAGE] + +optional arguments: + -h, --help show this help message and exit + -w WARNING, --warning WARNING + Integer warning level to output + -c CRITICAL, --critical CRITICAL + Integer critical level to output + -s HOST, --host HOST graphite host. http://graphite.example.com + -p PORT, --port PORT Port + -t TARGET, --target TARGET + metric target: com.example.some.metric + -f FROM_TIME, --from_time FROM_TIME + from time. Default = -5min + -o TO_TIME, --to_time TO_TIME + to time. Default = now + -m MESSAGE, --message MESSAGE + message to display +``` + +## Dependencies + +* sensu-python-plugin +* libjson-python + + diff --git a/plugins/graphite/graphite-metric-check.py b/plugins/graphite/graphite-metric-check.py new file mode 100755 index 0000000..bdfdef0 --- /dev/null +++ b/plugins/graphite/graphite-metric-check.py @@ -0,0 +1,108 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +from sensu_plugin import SensuPluginCheck +import json +import requests + +def getGraphiteValue(host, port, target, from_time, to_time): + + request_url = 'http://' + host + ':' + str(port) + '/render' + ranges = { 'from': from_time, 'to': to_time, 'target': target, 'format': 'json' } + + + r = requests.get(request_url, params=ranges) + graphite_dict = json.loads(r.content) + + counter = 0 + point_sum = 0 + for point in graphite_dict: + for value, timestamp in point['datapoints']: + if value is not None: + point_sum = int(point_sum) + int(value) + counter += 1 + + avg_value = point_sum / counter + + # return str(graphite_dict) + + return avg_value + + +class GraphiteMetricCheck(SensuPluginCheck): + def setup(self): + self.parser.add_argument( + '-w', + '--warning', + required=True, + type=int, + help='Integer warning level to output' + ) + self.parser.add_argument( + '-c', + '--critical', + required=True, + type=int, + help='Integer critical level to output' + ) + self.parser.add_argument( + '-s', + '--host', + required=True, + type=str, + help='graphite host. http://graphite.example.com' + ) + self.parser.add_argument( + '-p', + '--port', + type=int, + default=8080, + help='Port' + ) + self.parser.add_argument( + '-t', + '--target', + required=True, + type=str, + help='metric target: com.example.some.metric' + ) + self.parser.add_argument( + '-f', + '--from_time', + default='-5min', + type=str, + help='from time. Default = -5min' + ) + self.parser.add_argument( + '-o', + '--to_time', + type=str, + default='now', + help='to time. Default = now' + ) + self.parser.add_argument( + '-m', + '--message', + type=str, + default=None, + help='message to display' + ) + + def run(self): + # this method is called to_time perform the actual check + + self.check_name('GraphiteMetricCheck') # defaults to_time class name + + avg = getGraphiteValue(self.options.host, self.options.port, self.options.target, self.options.from_time, self.options.to_time) + + if avg > self.options.warning and avg < self.options.critical: + self.warning(self.options.target + ' ' + str(avg)) + elif avg < self.options.warning: + self.ok(self.options.target + ' ' + str(avg)) + elif avg >= self.options.critical: + self.critical(self.options.target + ' ' + str(avg)) + else: + self.unknown(self.options.message) + +if __name__ == "__main__": + f = GraphiteMetricCheck()