class WdTest

Attributes

expected_max[RW]
expected_min[RW]
expected_regex[RW]
expected_string[RW]
name[RW]
notify_on_output_change[RW]
previous_output[RW]
restore_cmd[RW]
status[RW]
test_cmd[RW]
test_url[RW]

Public Class Methods

new(name, params, logger) click to toggle source
# File lib/sys_watchdog/wd_test.rb, line 7
def initialize name, params, logger
    @logger = logger

    @name = name

    @test_cmd = params[:test_cmd]
    @test_url = params[:test_url]
    @notify_on_output_change = params[:notify_on_output_change]
    @restore_cmd = params[:restore_cmd]

    @expected_regex = params[:expected_regex]
    @expected_string = params[:expected_string]
    @expected_max = params[:expected_max]
    @expected_min = params[:expected_min]

    @status = true
    @previous_output = nil

    setup
end

Public Instance Methods

restore() click to toggle source
# File lib/sys_watchdog/wd_test.rb, line 28
def restore
    exitstatus, output = run_cmd @restore_cmd
    unless exitstatus == 0
        @logger.error "restore exited with non-zero: #{exitstatus} #{output}"
    end
end
run() click to toggle source
# File lib/sys_watchdog/wd_test.rb, line 35
def run
    @logger.info "========== testing #{@name}"

    unless @test_cmd
        @logger.error "test cmd or url required"
        return
    end

    exitstatus, output = run_cmd @test_cmd

    success = check_result exitstatus, output
    @logger.info "success: #{success}"

    [success, exitstatus, output]
end

Private Instance Methods

check_result(exitstatus, output) click to toggle source
# File lib/sys_watchdog/wd_test.rb, line 78
def check_result exitstatus, output
    success = 
        if @expected_regex
            output =~ @expected_regex
        elsif @expected_string
            output.index @expected_string
        elsif @expected_max
            output.to_f <= @expected_max
        elsif @expected_min
            output.to_f >= @expected_min
        else
            exitstatus == 0
        end
    !!success
end
run_cmd(cmd) click to toggle source
# File lib/sys_watchdog/wd_test.rb, line 53
def run_cmd cmd
    @logger.info "run: #{ cmd }"

    output = IO.popen(cmd, "r") {|pipe| pipe.read}
    exitstatus = $?.exitstatus

    if exitstatus != 0
        @logger.error "#{cmd} -> #{output}"
    end

    [exitstatus, output]
end
setup() click to toggle source
# File lib/sys_watchdog/wd_test.rb, line 66
def setup
    if @test_url
        @test_cmd = "wget -O - '#{ @test_url }' > /dev/null 2>&1"
    end
    if @expected_regex
        @expected_regex = Regexp.new @expected_regex
    end
    if @notify_on_output_change
        @test_cmd = @notify_on_output_change
    end
end