Sha256: fffdde999264ff06b470e741b4672aa9ce985f0f3266f9c5ce5f1377e5e1dbb9

Contents?: true

Size: 1.87 KB

Versions: 7

Compression:

Stored size: 1.87 KB

Contents

#! /usr/bin/env ruby
#  encoding: UTF-8
#
# Checks SWAP usage as a % of the total swap
#
# Date: 2016-11-21
# Author: Aleksandar Stojanov <aleksandar.stojanov@polarcape.com>
#
# PLATFORMS:
#   Linux
#
# DEPENDENCIES:
#   gem: sensu-plugin
#   commands: free, grep
#
# USAGE:
#   check-swap-percent.rb -w warn_percent -c critical_percent
#
# LICENSE:
#   Copyright 2016 Aleksandar Stojanov <aleksandar.stojanov@polarcape.com>
#   Released under the same terms as Sensu (the MIT license); see LICENSE
#   for details.

require 'sensu-plugin/check/cli'

class CheckSWAP < Sensu::Plugin::Check::CLI
  option :warn,
         short: '-w WARNING',
         long: '--warning',
         proc: proc(&:to_i),
         default: 70

  option :critical,
         short: '-c CRITICAL',
         long: '--critical',
         proc: proc(&:to_i),
         default: 80

  option :required,
         short: '-r',
         long: '--required',
         description: 'checks if swap is missing from the machine',
         boolean: true,
         default: false

  def run
    # Get output from free command
    output_swap = `free -m | grep "Swap"`

    # Prepare total and used swap
    total_swap = output_swap.split(' ')[1].to_i
    used_swap = output_swap.split(' ')[2].to_i

    # Check if swap exists
    if total_swap.zero?
      if config[:required]
        critical 'ERROR: SWAP is missing on this machine'
      else
        print 'There is no SWAP configured'
        exit
      end
    end

    # Prepare output message
    output = "#{used_swap}/#{total_swap}"

    # Calculate round percent from used and total
    swapp = (100.0 * (used_swap.nonzero? || 1) / total_swap).round(0)

    # Check conditions from config
    if swapp >= config[:critical]
      critical "#{swapp}% ; #{output}\n"
    elsif swapp >= config[:warn]
      warning "#{swapp}% ; #{output}\n"
    else
      ok "#{swapp}% ; #{output}\n"
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
sensu-plugins-memory-checks-3.1.1 bin/check-swap-percent.rb
sensu-plugins-memory-checks-3.0.3 bin/check-swap-percent.rb
sensu-plugins-memory-checks-3.0.2 bin/check-swap-percent.rb
sensu-plugins-memory-checks-3.0.1 bin/check-swap-percent.rb
sensu-plugins-memory-checks-3.0.0 bin/check-swap-percent.rb
sensu-plugins-memory-checks-2.1.0 bin/check-swap-percent.rb
sensu-plugins-memory-checks-2.0.0 bin/check-swap-percent.rb