Sha256: e9a8aae2923c1a781ef2284757bb2850d42b68df9b0ca31346c3d7365fae5f0e

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

#!/usr/bin/env ruby
Process.setproctitle($0)

# Gets the number of files present on a directory and submits it to riemann

require File.expand_path('../../lib/riemann/tools', __FILE__)

class Riemann::Tools::DirFilesCount
  include Riemann::Tools

  opt :directory, "", :default => '/var/log'
  opt :service_prefix, "The first part of the service name, before the directory path", :default => "dir-files-count"
  opt :warning, "Dir files number warning threshold", :type => Integer
  opt :critical, "Dir files number critical threshold", :type => Integer
  opt :alert_on_missing, "Send a critical metric if the directory is missing?", :default => true

  def initialize
    @dir = opts.fetch(:directory)
    @service_prefix = opts.fetch(:service_prefix)
    @warning = opts.fetch(:warning, nil)
    @critical = opts.fetch(:critical, nil)
    @alert_on_missing = opts.fetch(:alert_on_missing)
  end

  def tick
    if Dir.exists?(@dir)
      metric = Dir.entries(@dir).size - 2
      report(
        :service => "#{@service_prefix} #{@dir}",
        :metric => metric,
        :state => state(metric),
        :tags => ['dir_files_count']
      )
    elsif @alert_on_missing
      report(
        :service => "#{@service_prefix} #{@dir} missing",
        :description => "#{@service_prefix} #{@dir} does not exist",
        :metric => metric,
        :state => 'critical',
        :tags => ['dir_files_count']
      )
    end
  end

  def state(metric)
    if @critical && metric > @critical
      'critical'
    elsif @warning && metric > @warning
      'warning'
    else
      'ok'
    end
  end
end

Riemann::Tools::DirFilesCount.run

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
riemann-tools-1.0.0 bin/riemann-dir-files-count