Sha256: fec46a623c614d14294ff0e6484e631c983f309f998dddaeee8244e24f1d8405

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

require 'tempfile'

class SadfInput < Fluent::Input
    Fluent::Plugin.register_input('sadf', self)

    config_param :sar_option,       :string,  default: ''
    config_param :tag,              :string,  default: 'sadf_result.tag'
    config_param :interval,         :integer, default: 5
    config_param :hostname_output,  :bool,    default: true 
    config_param :hostname,         :string,  default: Socket.gethostname

    def configure(conf)
        super
        @interval_m = @sar_option.split.size.zero? ? @interval * 60 : @interval * 60 - 1
        begin
           `sar -V`
        rescue
           raise Fluent::ConfigError, "sar(sysstat) is not installed."
        end
    end

    def start
        super
        @thread = Thread.new(&method(:run))
    end

    def shutdown
        @thread.kill
    end

    private

    def run
        loop do
            @result             = Hash.new
            @result["hostname"] = @hostname if @hostname_output
            @result["args"]     = @sar_option
            Fluent::Engine.emit(@tag, Fluent::Engine.now, @result.merge(sadf_execute(@sar_option)))
            sleep @interval_m
        end
    end

    def sadf_execute(opt)
        rec = Hash.new

        Tempfile.open('sar') {|tmpfile|
            `LANG=C sar -o #{tmpfile.path} 1 1`
            `LANG=C sadf -- #{opt} 1 1 #{tmpfile.path}`.split("\n").each {| line |
                array = line.split("\t")
                if !rec.has_key?(array[3]) then
                    rec[array[3]] = Hash.new
                end
                rec[array[3]][array[4]] = array[5]
            }
        }

        rec
    end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fluent-plugin-sadf-0.0.2 lib/fluent/plugin/in_sadf.rb