Sha256: 0c084f93f96f0e888767b7dd33500655acd52bb5346605a4e2094477de42a30e

Contents?: true

Size: 1.8 KB

Versions: 11

Compression:

Stored size: 1.8 KB

Contents

#!/usr/bin/env ruby
#
# metrics-sockstat
#
# DESCRIPTION:
#   This metric check parses /proc/net/sockstat and outputs all fields as metrics
#
# OUTPUT:
#   graphite metric data
#
# PLATFORMS:
#   Linux
#
# DEPENDENCIES:
#   gem: sensu-plugin
#
# USAGE:
#   Specify [-s|--scheme] SCHEME to change the text appended to the metric paths.
#
# NOTES:
#   It outputs the value in the first line ("sockets used") as SCHEME.total_used.
#   All other fields are output as SCHEME.type.field, i.e., SCHEME.TCP.inuse, SCHEME.UDP.mem
#
# LICENSE:
#   Copyright 2015 Contegix, LLC.
#   Released under the same terms as Sensu (the MIT license); see LICENSE for details.
#
require 'sensu-plugin/metric/cli'

# MetricsSockstat
class MetricsSockstat < Sensu::Plugin::Metric::CLI::Graphite
  option :scheme,
         description: 'Metric naming scheme, text to prepend to $protocol.$field',
         long: '--scheme SCHEME',
         short: '-s SCHEME',
         default: 'network.sockets'

  def output_metric(name, value)
    output "#{@config[:scheme]}.#{name} #{value} #{@timestamp}"
  end

  def socket_metrics(fields)
    name = 'total_used'
    value = fields[2]
    output_metric(name, value)
  end

  def generic_metrics(fields)
    proto = fields[0].sub(':', '')
    fields[1..-1].join(' ').scan(/([A-Za-z]+) (\d+)/).each do |tuple|
      output_metric("#{proto}.#{tuple[0]}", tuple[1])
    end
  end

  def read_sockstat
    return IO.read('/proc/net/sockstat')
  rescue => e
    unknown "Failed to read /proc/net/sockstat: #{e}"
  end

  def run
    sockstat = read_sockstat
    @config = config
    @timestamp = Time.now.to_i
    sockstat.split("\n").each do |line|
      fields = line.split
      if fields[0] == 'sockets:'
        socket_metrics(fields)
      elsif fields.length > 1
        generic_metrics(fields)
      end
    end
    ok
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
sensu-plugins-network-checks-2.3.1 bin/metrics-sockstat.rb
sensu-plugins-network-checks-2.3.0 bin/metrics-sockstat.rb
sensu-plugins-network-checks-2.2.0 bin/metrics-sockstat.rb
sensu-plugins-network-checks-2.1.1 bin/metrics-sockstat.rb
sensu-plugins-network-checks-2.1.0 bin/metrics-sockstat.rb
sensu-plugins-network-checks-2.0.1 bin/metrics-sockstat.rb
sensu-plugins-network-checks-2.0.0 bin/metrics-sockstat.rb
sensu-plugins-network-checks-1.2.0 bin/metrics-sockstat.rb
sensu-plugins-network-checks-1.1.0 bin/metrics-sockstat.rb
sensu-plugins-network-checks-1.0.0 bin/metrics-sockstat.rb
sensu-plugins-network-checks-0.2.4 bin/metrics-sockstat.rb