Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Feature: Failed Mode #7

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 (@Seji64)

## [0.1.0] - 2017-09-10
### Added
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 27 additions & 5 deletions bin/check-systemd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
Expand All @@ -24,20 +26,35 @@
#

require 'sensu-plugin/check/cli'

#
# Check systemd services
#
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

Expand All @@ -53,7 +70,7 @@ 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) }
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]
Expand All @@ -66,8 +83,13 @@ def unit_services
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.none? && @failed == false
critical 'You must define services to check!'
end

unit_services.each do |service|
Expand Down
74 changes: 74 additions & 0 deletions check-systemd.rb.patch
Original file line number Diff line number Diff line change
@@ -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|
1 change: 0 additions & 1 deletion lib/sensu-plugins-systemd.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@

require 'sensu-plugins-systemd/version'
5 changes: 2 additions & 3 deletions sensu-plugins-systemd.gemspec
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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']
Expand All @@ -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