sensu-plugins/sensu-plugins-lxc

View on GitHub
bin/check-lxc-status.rb

Summary

Maintainability
A
25 mins
Test Coverage
#! /usr/bin/env ruby
#
#   check-lxc-status
#
# DESCRIPTION:
#   This is a simple check script for Sensu to check the status of a Linux Container
#
# OUTPUT:
#   plain text, metric data, etc
#
# PLATFORMS:
#   Linux, Windows, BSD, Solaris, etc
#
# DEPENDENCIES:
#   gem: sensu-plugin
#   gem: lxc
#
# USAGE:
#   check-lxc-status.rb -n name    => name of the container
#
#   Default lxc is "testdebian", change to if you dont want to pass host
#   option
#
# NOTES:
#
# LICENSE:
#   Deepak Mohan Dass   <deepakmdass88@gmail.com>
#   Released under the same terms as Sensu (the MIT license); see LICENSE
#   for details.
#

require 'sensu-plugin/check/cli'
require 'lxc'

class CheckLXCSTATUS < Sensu::Plugin::Check::CLI
  option :name,
         short: '-n name',
         default: 'testdebian'

  def run
    lxc = LXC.new
    conn = LXC::Container.new(lxc: lxc, name: config[:name].to_s)
    if conn.exists?
      if conn.stopped?
        critical "container #{config[:name]} is Stopped"
      elsif conn.frozen?
        critical "container is #{config[:name]} in Frozen state"
      else
        ok "container  #{config[:name]} is Running"
      end
    else
      critical "container #{config[:name]} does not Exists"
    end
  end
end