Skip to content

Commit

Permalink
Version 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
kevynlebouille committed Jun 12, 2023
0 parents commit b9fa88a
Show file tree
Hide file tree
Showing 11 changed files with 284 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/assets/
/Gemfile.lock
32 changes: 32 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
AllCops:
SuggestExtensions: false
TargetRubyVersion: 3.2

Metrics/AbcSize:
Enabled: false

Metrics/MethodLength:
Enabled: false

Metrics/ClassLength:
Enabled: false

Metrics/CyclomaticComplexity:
Enabled: false

Metrics/PerceivedComplexity:
Enabled: false

Layout/LineLength:
Enabled: false

Style/Documentation:
Enabled: false

Style/StringLiterals:
Enabled: true
EnforcedStyle: single_quotes

Style/StringLiteralsInInterpolation:
Enabled: true
EnforcedStyle: double_quotes
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.2.2
10 changes: 10 additions & 0 deletions Dockerfile.debian10
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM sensu-ruby32-runtime-3.2.2-debian10:0.1.2
ARG ASSET_GEM=sensu-plugins-chrony
ARG GIT_REF=9247833830bf37d03456146588590333a447416a
ARG GIT_REPO=https://github.com/opsone/sensu-plugins-chrony.git

WORKDIR /assets/build/
RUN apt-get update && apt-get install -y git
RUN printf "source 'https://rubygems.org'\n\ngem '%s', git: '%s' , ref: '%s'\n" ${ASSET_GEM} ${GIT_REPO} ${GIT_REF} | tee Gemfile
RUN bundle install --path=lib/ --binstubs=bin/ --standalone
RUN tar -czf /assets/${ASSET_GEM}.tar.gz -C /assets/build/ .
10 changes: 10 additions & 0 deletions Dockerfile.debian11
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM sensu-ruby32-runtime-3.2.2-debian11:0.1.2
ARG ASSET_GEM=sensu-plugins-chrony
ARG GIT_REF=9247833830bf37d03456146588590333a447416a
ARG GIT_REPO=https://github.com/opsone/sensu-plugins-chrony.git

WORKDIR /assets/build/
RUN apt-get update && apt-get install -y git
RUN printf "source 'https://rubygems.org'\n\ngem '%s', git: '%s' , ref: '%s'\n" ${ASSET_GEM} ${GIT_REPO} ${GIT_REF} | tee Gemfile
RUN bundle install --path=lib/ --binstubs=bin/ --standalone
RUN tar -czf /assets/${ASSET_GEM}.tar.gz -C /assets/build/ .
9 changes: 9 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

source 'https://rubygems.org'

# Specify your gem's dependencies in sensu-plugins-postgres.gemspec
gemspec

gem 'rake', '~> 13.0'
gem 'rubocop', '~> 1.21'
21 changes: 21 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'rubocop/rake_task'

RuboCop::RakeTask.new

task default: :rubocop

task :build_assets do
version = Sensu::Plugins::Chrony::VERSION

%w[debian11 debian10].each do |platform|
`docker build -t ruby-plugin-#{platform} -f Dockerfile.#{platform} .`
`docker run -v "$PWD/assets:/tmp/assets" ruby-plugin-#{platform} cp /assets/sensu-plugins-chrony.tar.gz /tmp/assets/sensu-plugins-chrony_#{version}_#{platform}_linux_amd64.tar.gz`
`docker rm $(docker ps -a -q --filter ancestor=ruby-plugin-#{platform})`
`docker rmi ruby-plugin-#{platform}`
end

`cd assets && shasum -a 512 ./*.tar.gz > sensu-plugins-chrony_#{version}_sha512-checksums.txt`
end
142 changes: 142 additions & 0 deletions bin/check-chrony.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'sensu-plugin/check/cli'

class CheckChrony < Sensu::Plugin::Check::CLI
option :chronyc_cmd,
description: 'Path to chronyc executable (default: /usr/bin/chronyc)',
short: '-c <PATH>',
long: '--chronyc-cmd <PATH>',
default: '/usr/bin/chronyc'

option :warn_offset,
description: 'Warn if OFFSET exceeds current offset (ms)',
long: '--warn-offset <OFFSET>',
proc: proc(&:to_f),
default: 50

option :crit_offset,
description: 'Critical if OFFSET exceeds current offset (ms)',
long: '--crit-offset <OFFSET>',
proc: proc(&:to_f),
default: 100

option :warn_stratum,
description: 'Warn if STRATUM exceeds current stratum',
long: '--warn-stratum <STRATUM>',
proc: proc(&:to_i),
default: 10

option :crit_stratum,
description: 'Critical if STRATUM exceeds current stratum',
long: '--crit-stratum <STRATUM>',
proc: proc(&:to_i),
default: 16

def run
stratum = nil
offset = nil
status = nil

`#{config[:chronyc_cmd]} tracking`.each_line do |line|
case line.downcase
when /^stratum\s*:\s*(\d+)$/
stratum = Regexp.last_match(1).to_i
when /^last offset\s*:\s*([\-\+]?[.\d]+)\s*seconds$/
# convert from seconds to milliseconds
offset = Regexp.last_match(1).to_f * 1000
when /^leap status\s*:\s*(.*?)$/
status = Regexp.last_match(1)
end
end

if stratum
msg = "NTP stratum is #{stratum}"

if stratum >= config[:crit_stratum]
msg += ", expected < #{config[:crit_stratum]}"
critical msg
elsif stratum >= config[:warn_stratum]
msg += ", expected < #{config[:warn_stratum]}"
warning msg
else
ok msg
end
else
unknown 'Failed to look up NTP stratum'
end

if offset
msg = "NTP offset is #{offset.round(4)}ms"

if offset >= config[:crit_offset] || offset <= -config[:crit_offset]
msg += ", expected > -#{config[:crit_offset]} and < #{config[:crit_offset]}"
critical msg
elsif offset >= config[:warn_offset] || offset < -config[:warn_offset]
msg += ", expected > -#{config[:warn_offset]} and < #{config[:warn_offset]}"
warning msg
else
ok msg
end
else
unknown 'Failed to look up NTP offset'
end

if status
msg = "NTP status is '#{status}'"

if status != 'normal'
msg += ', expected \'Normal\''
critical msg
else
ok msg
end
else
unknown 'Failed to look up NTP status'
end
end

private

def send_client_socket(data)
sock = UDPSocket.new
sock.send("#{data}\n", 0, '127.0.0.1', 3030)
end

def send_ok(check_name, msg)
event = {
'name' => check_name,
'status' => 0,
'output' => "#{self.class.name} OK: #{msg}"
}
send_client_socket(event.to_json)
end

def send_warning(check_name, msg)
event = {
'name' => check_name,
'status' => 1,
'output' => "#{self.class.name} WARNING: #{msg}"
}
send_client_socket(event.to_json)
end

def send_critical(check_name, msg)
event = {
'name' => check_name,
'status' => 2,
'output' => "#{self.class.name} CRITICAL: #{msg}"
}
send_client_socket(event.to_json)
end

def send_unknown(check_name, msg)
event = {
'name' => check_name,
'status' => 3,
'output' => "#{self.class.name} UNKNOWN: #{msg}"
}
send_client_socket(event.to_json)
end
end
11 changes: 11 additions & 0 deletions lib/sensu/plugins/chrony.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

require_relative 'chrony/version'

module Sensu
module Plugins
module Chrony
class Error < StandardError; end
end
end
end
9 changes: 9 additions & 0 deletions lib/sensu/plugins/chrony/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module Sensu
module Plugins
module Chrony
VERSION = '0.1.0'
end
end
end
37 changes: 37 additions & 0 deletions sensu-plugins-chrony.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

require_relative 'lib/sensu/plugins/chrony'

Gem::Specification.new do |spec|
spec.name = 'sensu-plugins-chrony'
spec.version = Sensu::Plugins::Chrony::VERSION
spec.authors = ['Kevyn Lebouille']
spec.email = ['[email protected]']

spec.summary = 'This plugin provides facilities for monitoring Chrony NTP'
spec.homepage = 'https://github.com/opsone/sensu-plugins-chrony'
spec.required_ruby_version = '>= 3.2.0'

spec.metadata['allowed_push_host'] = "TODO: Set to your gem server 'https://example.com'"

spec.metadata['homepage_uri'] = spec.homepage
spec.metadata['source_code_uri'] = 'https://github.com/opsone/sensu-plugins-chrony'
spec.metadata['changelog_uri'] = 'https://github.com/opsone/sensu-plugins-chrony/CHANGELOG.md'

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
spec.files = Dir.chdir(__dir__) do
`git ls-files -z`.split("\x0").reject do |f|
(File.expand_path(f) == __FILE__) || f.start_with?(*%w[test/ spec/ features/ .git .circleci appveyor])
end
end
spec.bindir = 'bin'
spec.executables = spec.files.grep(%r{\Abin/}) { |f| File.basename(f) }
spec.require_paths = ['lib']

# Uncomment to register a new dependency of your gem
spec.add_dependency 'sensu-plugin', '~> 4.0'

# For more information and examples about making a new gem, check out our
# guide at: https://bundler.io/guides/creating_gem.html
end

0 comments on commit b9fa88a

Please sign in to comment.