bin/check-service-consul.rb in sensu-plugins-consul-1.1.0 vs bin/check-service-consul.rb in sensu-plugins-consul-1.2.0

- old
+ new

@@ -44,12 +44,17 @@ description: 'a service managed by consul', short: '-s SERVICE', long: '--service SERVICE', default: 'consul' + option :tags, + description: 'filter services by a comma-separated list of tags (requires --service)', + short: '-t TAGS', + long: '--tags TAGS' + option :all, - description: 'get all services in a non-passing status', + description: 'get all services in a non-passing status (not compatible with --tags)', short: '-a', long: '--all' option :fail_if_not_found, description: 'fail if no service is found', @@ -57,11 +62,20 @@ long: '--fail-if-not-found' # Get the check data for the service from consul # def acquire_service_data - if config[:all] + if config[:tags] && config[:service] + tags = config[:tags].split(',').to_set + services = [] + Diplomat::Health.service(config[:service]).each do |s| + if s['Service']['Tags'].to_set.superset? tags + services.push(*s['Checks']) + end + end + return services + elsif config[:all] Diplomat::Health.state('any') else Diplomat::Health.checks(config[:service]) end rescue Faraday::ConnectionFailed => e @@ -71,10 +85,14 @@ end # Main function # def run + if config[:tags] && config[:all] + critical 'Cannot specify --tags and --all simultaneously (Consul health/service/ versus health/state/).' + end + Diplomat.configure do |dc| dc.url = config[:consul] end data = acquire_service_data @@ -90,16 +108,19 @@ failing << { 'node' => d['Node'], 'service' => d['ServiceName'], 'service_id' => d['ServiceID'], 'notes' => d['Notes'] - } if d['Status'] == 'failing' + } if d['Status'] == 'critical' end if failing.empty? && passing.empty? msg = 'Could not find checks for any services' if config[:service] msg = "Could not find checks for service #{config[:service]}" + if config[:tags] + msg += " with tags #{config[:tags]}" + end end if config[:fail_if_not_found] critical msg else unknown msg