From 150d93e2e4a20b08cb3aa0962aee4494d7e05f6f Mon Sep 17 00:00:00 2001 From: Seji64 Date: Mon, 18 Jun 2018 12:04:24 +0200 Subject: [PATCH 1/7] Added 'failed' Mode. This Mode checks all systemd service and reports only CRITICAL if any service has state "failed" This mode can be combined with '-i' to set services which should be ignored. Example: check-systemd.rb -f -i kdump.service --- bin/check-systemd.rb | 45 +++++++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/bin/check-systemd.rb b/bin/check-systemd.rb index 999086d..9f6a6a8 100755 --- a/bin/check-systemd.rb +++ b/bin/check-systemd.rb @@ -31,13 +31,29 @@ class CheckSystemd < Sensu::Plugin::Check::CLI option :services, short: '-s SERVICES', - proc: proc { |a| a.split(',') } + proc: proc { |a| a.split(',')}, + default: [], + description: 'comma seperated list of services to check. ignored if --failed is set' + option :failed, + short: '-f', + boolean: true, + default: false, + long: '--failed', + description: 'all services are being checked. One or more failed => CRITICAL' + option :failed_ignore, + short: '-i SERVICES', + proc: proc { |a| a.split(',')}, + default: [], + long: '--failed-ignore', + description: 'comma seperated list of services which should be ignored when using --failed mode' # Setup variables # def initialize super @services = config[:services] + @failed = config[:failed] + @failed_ignore = config[:failed_ignore] @crit_service = [] end @@ -53,21 +69,28 @@ def unit_services service_array = [] systemd_output.split("\n").each do |line| line_array = line.split(' ') - next unless @services.any? { |service| line_array[0].include?(service) } - service_hash = {} - service_hash['name'] = line_array[0] - service_hash['load'] = line_array[1] - service_hash['active'] = line_array[2] - service_hash['sub'] = line_array[3] - service_hash['description'] = line_array[4] - service_array.push(service_hash) + next unless @services.any? { |service| line_array[0].include?(service) } || @failed == true + unless @failed_ignore.any? { |service| line_array[0].include?(service) } && @failed == true + service_hash = {} + service_hash['name'] = line_array[0] + service_hash['load'] = line_array[1] + service_hash['active'] = line_array[2] + service_hash['sub'] = line_array[3] + service_hash['description'] = line_array[4] + service_array.push(service_hash) + end end service_array end def check_systemd - @services.reject { |service| validate_presence_of(service) }.each do |gone| - @crit_service << "#{gone} - Not Present" + unless @services.nil? + @services.reject { |service| validate_presence_of(service) }.each do |gone| + @crit_service << "#{gone} - Not Present" + end + end + if @services.nil? && @failed == false + critical "You must define services to check!" end unit_services.each do |service| From e6e513ad9739a9cb987db2e8b76dd9a170492e65 Mon Sep 17 00:00:00 2001 From: Seji64 Date: Mon, 18 Jun 2018 12:15:16 +0200 Subject: [PATCH 2/7] Updated Changelog und README --- CHANGELOG.md | 5 +++++ README.md | 7 +++++++ bin/check-systemd.rb | 2 ++ 3 files changed, 14 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fd69c8f..76407a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ Which is based on [Keep A Changelog](http://keepachangelog.com/) This CHANGELOG follows the format listed at [Keep A Changelog](http://keepachangelog.com/) ## [Unreleased] +### Added +- added 'failed mode'. This Mode checks all systemd service and reports only CRITICAL if any service has state "failed" (@Seji64) + +### Fixed +- check-systemd.rb: NilClass Exception when no service (-s) is set ## [0.1.0] - 2017-09-10 ### Added diff --git a/README.md b/README.md index 94d255b..1216bfc 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,13 @@ Pass services as a comma delimited -s option check-systemd.rb -s SERVICE1.service,SERVICE2.service ``` +### Usage/example failed mode +All services with state 'failed' will be reported as CRITICAL except kdump.service + +``` +check-systemd.rb -f -i kdump.service +``` + ## Installation [Installation and Setup](http://sensu-plugins.io/docs/installation_instructions.html) diff --git a/bin/check-systemd.rb b/bin/check-systemd.rb index 9f6a6a8..277d267 100755 --- a/bin/check-systemd.rb +++ b/bin/check-systemd.rb @@ -16,6 +16,8 @@ # # USAGE: # -s SERVICE - Services to check delimited by commas +# -f Toogle 'failed mode' +# -i SERVICES - Services to ignore in 'failed mode'. delimited by commas # # LICENSE: # Chris McFee From 06f7cf6316fb318fec4c235610979b475689b5a1 Mon Sep 17 00:00:00 2001 From: Seji64 Date: Mon, 18 Jun 2018 16:53:45 +0200 Subject: [PATCH 3/7] Some RoboCop fixed --- bin/check-systemd.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/bin/check-systemd.rb b/bin/check-systemd.rb index 277d267..2dc834b 100755 --- a/bin/check-systemd.rb +++ b/bin/check-systemd.rb @@ -33,7 +33,7 @@ class CheckSystemd < Sensu::Plugin::Check::CLI option :services, short: '-s SERVICES', - proc: proc { |a| a.split(',')}, + proc: proc { |a| a.split(',') }, default: [], description: 'comma seperated list of services to check. ignored if --failed is set' option :failed, @@ -55,7 +55,7 @@ def initialize super @services = config[:services] @failed = config[:failed] - @failed_ignore = config[:failed_ignore] + @failed_ignore = config[:failed_ignore] @crit_service = [] end @@ -86,13 +86,13 @@ def unit_services end def check_systemd - unless @services.nil? - @services.reject { |service| validate_presence_of(service) }.each do |gone| + unless @services.nil? + @services.reject { |service| validate_presence_of(service) }.each do |gone| @crit_service << "#{gone} - Not Present" end end - if @services.nil? && @failed == false - critical "You must define services to check!" + if !@services.any? && @failed == false + critical "You must define services to check!" end unit_services.each do |service| From cd701b2f401a6d0c0b462c05e22dcfde83c468cb Mon Sep 17 00:00:00 2001 From: Seji64 Date: Mon, 18 Jun 2018 16:59:40 +0200 Subject: [PATCH 4/7] RoboCop fixes Part 2 --- bin/check-systemd.rb | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/bin/check-systemd.rb b/bin/check-systemd.rb index 2dc834b..b5d2383 100755 --- a/bin/check-systemd.rb +++ b/bin/check-systemd.rb @@ -26,7 +26,6 @@ # require 'sensu-plugin/check/cli' - # # Check systemd services # @@ -44,7 +43,7 @@ class CheckSystemd < Sensu::Plugin::Check::CLI description: 'all services are being checked. One or more failed => CRITICAL' option :failed_ignore, short: '-i SERVICES', - proc: proc { |a| a.split(',')}, + proc: proc { |a| a.split(',') }, default: [], long: '--failed-ignore', description: 'comma seperated list of services which should be ignored when using --failed mode' @@ -88,11 +87,11 @@ def unit_services def check_systemd unless @services.nil? @services.reject { |service| validate_presence_of(service) }.each do |gone| - @crit_service << "#{gone} - Not Present" - end + @crit_service << "#{gone} - Not Present" + end end - if !@services.any? && @failed == false - critical "You must define services to check!" + if @services.none? && @failed == false + critical 'You must define services to check!' end unit_services.each do |service| From bcf8af81f3b0965da58c7420c2aa487160b34317 Mon Sep 17 00:00:00 2001 From: Ben Abrams Date: Thu, 21 Jun 2018 21:52:18 -0700 Subject: [PATCH 5/7] appease the cops --- CHANGELOG.md | 2 +- bin/check-systemd.rb | 19 +++++++++---------- lib/sensu-plugins-systemd.rb | 1 - sensu-plugins-systemd.gemspec | 5 ++--- 4 files changed, 12 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 76407a2..84a6539 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ This CHANGELOG follows the format listed at [Keep A Changelog](http://keepachang - added 'failed mode'. This Mode checks all systemd service and reports only CRITICAL if any service has state "failed" (@Seji64) ### Fixed -- check-systemd.rb: NilClass Exception when no service (-s) is set +- check-systemd.rb: NilClass Exception when no service (-s) is set (@Seji64) ## [0.1.0] - 2017-09-10 ### Added diff --git a/bin/check-systemd.rb b/bin/check-systemd.rb index b5d2383..1a4e2d9 100755 --- a/bin/check-systemd.rb +++ b/bin/check-systemd.rb @@ -71,15 +71,14 @@ def unit_services systemd_output.split("\n").each do |line| line_array = line.split(' ') next unless @services.any? { |service| line_array[0].include?(service) } || @failed == true - unless @failed_ignore.any? { |service| line_array[0].include?(service) } && @failed == true - service_hash = {} - service_hash['name'] = line_array[0] - service_hash['load'] = line_array[1] - service_hash['active'] = line_array[2] - service_hash['sub'] = line_array[3] - service_hash['description'] = line_array[4] - service_array.push(service_hash) - end + next if @failed_ignore.any? { |service| line_array[0].include?(service) } && @failed == true + service_hash = {} + service_hash['name'] = line_array[0] + service_hash['load'] = line_array[1] + service_hash['active'] = line_array[2] + service_hash['sub'] = line_array[3] + service_hash['description'] = line_array[4] + service_array.push(service_hash) end service_array end @@ -87,7 +86,7 @@ def unit_services def check_systemd unless @services.nil? @services.reject { |service| validate_presence_of(service) }.each do |gone| - @crit_service << "#{gone} - Not Present" + @crit_service << "#{gone} - Not Present" end end if @services.none? && @failed == false diff --git a/lib/sensu-plugins-systemd.rb b/lib/sensu-plugins-systemd.rb index 473f8fe..65c1aab 100644 --- a/lib/sensu-plugins-systemd.rb +++ b/lib/sensu-plugins-systemd.rb @@ -1,2 +1 @@ - require 'sensu-plugins-systemd/version' diff --git a/sensu-plugins-systemd.gemspec b/sensu-plugins-systemd.gemspec index a0cbd9d..316100b 100644 --- a/sensu-plugins-systemd.gemspec +++ b/sensu-plugins-systemd.gemspec @@ -1,4 +1,4 @@ -lib = File.expand_path('../lib', __FILE__) +lib = File.expand_path('lib', __dir__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'date' @@ -24,7 +24,6 @@ Gem::Specification.new do |s| # rubocop:disable Metrics/BlockLength s.summary = 'This provides functionality to check systemd services.' s.description = 'Plugins to provide functionality to check systemd services for Sensu, a monitoring framework' s.license = 'MIT' - s.has_rdoc = false s.require_paths = ['lib'] s.files = Dir.glob('{bin,lib}/**/*') + %w[LICENSE README.md CHANGELOG.md] # s.test_files = Dir['test/*.rb'] @@ -40,7 +39,7 @@ Gem::Specification.new do |s| # rubocop:disable Metrics/BlockLength s.add_development_dependency 'pry', '~> 0.10' s.add_development_dependency 'rake', '~> 10.5' s.add_development_dependency 'redcarpet', '~> 3.2' - s.add_development_dependency 'rubocop', '~> 0.37' s.add_development_dependency 'rspec', '~> 3.4' + s.add_development_dependency 'rubocop', '~> 0.37' s.add_development_dependency 'yard', '~> 0.8' end From 4eee6c5e9b3be38dfde2f71b484366bf87d25cae Mon Sep 17 00:00:00 2001 From: Seji64 Date: Fri, 22 Jun 2018 08:42:50 +0200 Subject: [PATCH 6/7] some robocop fixes --- bin/check-systemd.rb | 2 +- check-systemd.rb.patch | 74 +++++++++++++++++++++++++++++++++++ lib/sensu-plugins-systemd.rb | 1 - sensu-plugins-systemd.gemspec | 4 +- 4 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 check-systemd.rb.patch diff --git a/bin/check-systemd.rb b/bin/check-systemd.rb index b5d2383..2c0346c 100755 --- a/bin/check-systemd.rb +++ b/bin/check-systemd.rb @@ -87,7 +87,7 @@ def unit_services def check_systemd unless @services.nil? @services.reject { |service| validate_presence_of(service) }.each do |gone| - @crit_service << "#{gone} - Not Present" + @crit_service << "#{gone} - Not Present" end end if @services.none? && @failed == false diff --git a/check-systemd.rb.patch b/check-systemd.rb.patch new file mode 100644 index 0000000..93e6dbb --- /dev/null +++ b/check-systemd.rb.patch @@ -0,0 +1,74 @@ +diff --git a/bin/check-systemd.rb b/bin/check-systemd.rb +index 999086d..9f6a6a8 100755 +--- a/bin/check-systemd.rb ++++ b/bin/check-systemd.rb +@@ -31,13 +31,29 @@ require 'sensu-plugin/check/cli' + class CheckSystemd < Sensu::Plugin::Check::CLI + option :services, + short: '-s SERVICES', +- proc: proc { |a| a.split(',') } ++ proc: proc { |a| a.split(',')}, ++ default: [], ++ description: 'comma seperated list of services to check. ignored if --failed is set' ++ option :failed, ++ short: '-f', ++ boolean: true, ++ default: false, ++ long: '--failed', ++ description: 'all services are being checked. One or more failed => CRITICAL' ++ option :failed_ignore, ++ short: '-i SERVICES', ++ proc: proc { |a| a.split(',')}, ++ default: [], ++ long: '--failed-ignore', ++ description: 'comma seperated list of services which should be ignored when using --failed mode' + + # Setup variables + # + def initialize + super + @services = config[:services] ++ @failed = config[:failed] ++ @failed_ignore = config[:failed_ignore] + @crit_service = [] + end + +@@ -53,21 +69,28 @@ class CheckSystemd < Sensu::Plugin::Check::CLI + service_array = [] + systemd_output.split("\n").each do |line| + line_array = line.split(' ') +- next unless @services.any? { |service| line_array[0].include?(service) } +- service_hash = {} +- service_hash['name'] = line_array[0] +- service_hash['load'] = line_array[1] +- service_hash['active'] = line_array[2] +- service_hash['sub'] = line_array[3] +- service_hash['description'] = line_array[4] +- service_array.push(service_hash) ++ next unless @services.any? { |service| line_array[0].include?(service) } || @failed == true ++ unless @failed_ignore.any? { |service| line_array[0].include?(service) } && @failed == true ++ service_hash = {} ++ service_hash['name'] = line_array[0] ++ service_hash['load'] = line_array[1] ++ service_hash['active'] = line_array[2] ++ service_hash['sub'] = line_array[3] ++ service_hash['description'] = line_array[4] ++ service_array.push(service_hash) ++ end + end + service_array + end + + def check_systemd +- @services.reject { |service| validate_presence_of(service) }.each do |gone| +- @crit_service << "#{gone} - Not Present" ++ unless @services.nil? ++ @services.reject { |service| validate_presence_of(service) }.each do |gone| ++ @crit_service << "#{gone} - Not Present" ++ end ++ end ++ if @services.nil? && @failed == false ++ critical "You must define services to check!" + end + + unit_services.each do |service| diff --git a/lib/sensu-plugins-systemd.rb b/lib/sensu-plugins-systemd.rb index 473f8fe..65c1aab 100644 --- a/lib/sensu-plugins-systemd.rb +++ b/lib/sensu-plugins-systemd.rb @@ -1,2 +1 @@ - require 'sensu-plugins-systemd/version' diff --git a/sensu-plugins-systemd.gemspec b/sensu-plugins-systemd.gemspec index a0cbd9d..32d1139 100644 --- a/sensu-plugins-systemd.gemspec +++ b/sensu-plugins-systemd.gemspec @@ -1,4 +1,4 @@ -lib = File.expand_path('../lib', __FILE__) +lib = File.expand_path('lib', __dir__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'date' @@ -40,7 +40,7 @@ Gem::Specification.new do |s| # rubocop:disable Metrics/BlockLength s.add_development_dependency 'pry', '~> 0.10' s.add_development_dependency 'rake', '~> 10.5' s.add_development_dependency 'redcarpet', '~> 3.2' - s.add_development_dependency 'rubocop', '~> 0.37' s.add_development_dependency 'rspec', '~> 3.4' + s.add_development_dependency 'rubocop', '~> 0.37' s.add_development_dependency 'yard', '~> 0.8' end From d434c5f342e02f0d937781955f1fdb7960696754 Mon Sep 17 00:00:00 2001 From: Seji64 Date: Fri, 22 Jun 2018 08:56:41 +0200 Subject: [PATCH 7/7] messed up some changes during merge - sorry --- bin/check-systemd.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/bin/check-systemd.rb b/bin/check-systemd.rb index 1a4e2d9..d65be3f 100755 --- a/bin/check-systemd.rb +++ b/bin/check-systemd.rb @@ -70,7 +70,6 @@ def unit_services service_array = [] systemd_output.split("\n").each do |line| line_array = line.split(' ') - next unless @services.any? { |service| line_array[0].include?(service) } || @failed == true next if @failed_ignore.any? { |service| line_array[0].include?(service) } && @failed == true service_hash = {} service_hash['name'] = line_array[0]