Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
33 changes: 28 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 <cmcfee@kent.edu>
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,8 @@ 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 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]
service_hash['load'] = line_array[1]
Expand All @@ -66,8 +84,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
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