From cf81f6f32cea579930b1b29db97b2b5725bbc732 Mon Sep 17 00:00:00 2001 From: ynnt Date: Mon, 9 Jan 2017 05:09:00 +0200 Subject: [PATCH] Exit with zero datapoints message if no datapoints exist (#43) * Exit with zero datapoints message if no datapoints exist --- .../graphite_proxy/proxy.rb | 35 ++++++++----------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/lib/sensu-plugins-graphite/graphite_proxy/proxy.rb b/lib/sensu-plugins-graphite/graphite_proxy/proxy.rb index 5946dc5..e5cb459 100644 --- a/lib/sensu-plugins-graphite/graphite_proxy/proxy.rb +++ b/lib/sensu-plugins-graphite/graphite_proxy/proxy.rb @@ -3,10 +3,7 @@ module SensuPluginsGraphite module GraphiteProxy class ProxyError < StandardError - attr_accessor :exception - - def initialize(msg, args) - self.exception = args[:exception] + def initialize(msg) super msg end end @@ -87,24 +84,20 @@ def retrieve_data! handle = open(url, request_auth_options(config)) @raw_data = handle.gets - if @raw_data == '[]' - unknown 'Empty data received from Graphite - metric probably doesn\'t exists' - else - json_data = JSON.parse(@raw_data) - format_output(json_data) - end - rescue OpenURI::HTTPError => e - raise ProxyError.new('Failed to connect to Graphite server', exception: e) - rescue NoMethodError => e - raise ProxyError.new('No data for time period and/or target', exception: e) - rescue Errno::ECONNREFUSED => e - raise ProxyError.new('Connection refused when connecting to Graphite server', exception: e) - rescue Errno::ECONNRESET => e - raise ProxyError.new('Connection reset by peer when connecting to Graphite server', exception: e) - rescue EOFError => e - raise ProxyError.new('End of file error when reading from Graphite server', exception: e) + json_data = JSON.parse(@raw_data) + format_output(json_data) + rescue OpenURI::HTTPError + raise ProxyError, 'Failed to connect to Graphite server' + rescue NoMethodError, JSON::ParserError + raise ProxyError, 'No data for time period and/or target' + rescue Errno::ECONNREFUSED + raise ProxyError, 'Connection refused when connecting to Graphite server' + rescue Errno::ECONNRESET + raise ProxyError, 'Connection reset by peer when connecting to Graphite server' + rescue EOFError + raise ProxyError, 'End of file error when reading from Graphite server' rescue => e - raise ProxyError.new("An unknown error occurred: #{e.inspect}", exception: e) + raise ProxyError, "An unknown error occurred: #{e.inspect}" end end end