SteelHouseLabs/apolo

View on GitHub
bin/apolo_cups

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby
#
# Script Name:: apolo_cups.rb
# @author Thomas Vincent <thomasvincent@steelhouselabs.com>
#
# Copyright 2014, Steel House Labs.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This script checks the quantity of connections on the database. The quantity
# can be filtered by a given criterion, and inverted if wanted.

require 'apolo'
require 'optparse'

options = { critical: 120, warning: 120 }

begin
  OptionParser.new do |opts|
    opts.on('-c', '--critical MAX', 'Max numbers of minutes on queue for critical') { |v| options[:critical] = v }
    opts.on('-w', '--warning MAX', 'Max numbers of minutes on queue for warning') { |v| options[:warning] = v }
    opts.on('-p' '--printer name', 'Printer name to check for jobs') { |v| options[:printer] = v }
  end.parse!
rescue Exception => msg
  puts msg
  exit
end

# Supress warning messages.
original_verbose, $VERBOSE = $VERBOSE, nil
@@options = options
# Activate warning messages again.
$VERBOSE = original_verbose


class CheckCups < Apolo::Metrics
  name 'CUPS'

  # Notifiers
  notify Apolo::Notifiers::Nagios, file: 'nagios.cmd',\
                                   host: 'localhost',\
                                   service: 'CUPS',\
                                   warning: @@options[:warning].to_i,\
                                   critical: @@options[:critical].to_i

  run do
    # create new Cups instance
    cups = Apolo::Domains::Cups.new(@@options[:printer_name])

    # send notify to nagios
    if cups.jobs_count > 0
      notify message: "Last job #{cups.minutes} minutes ago: #{cups.job[:id]} - #{cups.job[:dest]} - #{cups.job[:user]} - #{Time.at(cups.job[:creation_time])}", value: cups.minutes
    else
      notify message: 'Not jobs found', value: cups.minutes
    end

  end
end

# create monitor and run it
metrics = CheckCups.new
metrics.run