Sha256: 431e1c655505d26213335d1220fb2ff2a118d4009869f6032c0e95f49e24dcbd

Contents?: true

Size: 1.78 KB

Versions: 4

Compression:

Stored size: 1.78 KB

Contents

#! /usr/bin/env ruby
#  encoding: UTF-8
#
#   check-uptime
#
# DESCRIPTION:
#
# OUTPUT:
#   plain text
#
# PLATFORMS:
#   Linux
#
# DEPENDENCIES:
#   gem: sensu-plugin
#
# USAGE:
#   check-uptime.rb --help
#
# NOTES:
#   Checks the systems uptime and warns if the system has been rebooted.
#   2017 Juan Moreno Martinez - Add reverse option
#   2017 Jeronimo Jose Diaz Garcia - Compatibility OS X
#
# LICENSE:
#   Copyright 2012 Kees Remmelzwaal <kees@fastmail.com>
#   Released under the same terms as Sensu (the MIT license); see LICENSE
#   for details.
#

require 'sensu-plugin/check/cli'

class CheckUptime < Sensu::Plugin::Check::CLI
  option :warn,
         short: '-w SEC ',
         description: 'Warn threshold in SEC',
         proc: proc(&:to_i),
         default: 180

  option :greater,
         short: '-g',
         long: '--greater-than',
         description: 'This compare uptime > threshold. Default behavior uptime < threshold',
         boolean: true,
         default: false

  def run
    os = `uname`.chomp
    if os == 'Darwin'
      uptime_timestamp = `sysctl kern.boottime | cut -d= -f2 | cut -d" " -f2 | cut -d, -f1`.to_i
      uptime_sec = `date +%s`.to_i - uptime_timestamp
    elsif os == 'Linux'
      uptime_sec = IO.read('/proc/uptime').split[0].to_i
    else
      unknown "platform: #{os} is not supported, please open an issue with the output of '`uname`'."
    end
    uptime_date = Time.now - uptime_sec

    if config[:greater] && uptime_sec > config[:warn]
      message "System boot detected (#{uptime_sec} seconds up), compared using '>'"
      warning
    elsif uptime_sec < config[:warn] && !config[:greater]
      message "System boot detected (#{uptime_sec} seconds up), compared using '<'"
      warning
    end

    message "System booted at #{uptime_date}"
    ok
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sensu-plugins-uptime-checks-3.2.0 bin/check-uptime.rb
sensu-plugins-uptime-checks-3.1.0 bin/check-uptime.rb
sensu-plugins-uptime-checks-3.0.0 bin/check-uptime.rb
sensu-plugins-uptime-checks-2.0.0 bin/check-uptime.rb