From 8bc213d9e371c55f1cee4129aee9f840d3c16808 Mon Sep 17 00:00:00 2001 From: Orien Vandenbergh Date: Tue, 23 Dec 2014 10:24:23 -0700 Subject: [PATCH] Initial commit of mrsh_reporter.rb --- mrsh/lib/mrsh_reporter.rb | 84 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 mrsh/lib/mrsh_reporter.rb diff --git a/mrsh/lib/mrsh_reporter.rb b/mrsh/lib/mrsh_reporter.rb new file mode 100644 index 0000000..10df7ac --- /dev/null +++ b/mrsh/lib/mrsh_reporter.rb @@ -0,0 +1,84 @@ +class Reporter + def initialize(settings) + @config = settings + commentwidth = 60 + + @formats = Hash.new + @formats[:ping] = "%15s ]-%s%4%s---------------------------------------------------[ %-4s / %-5s ]-\n" + @formats[:header] = "%15s ]-BEGIN-------------------------------------------------[ %-4s / %-5s ]-\n" + @formats[:content] = "%15s ]- %s\n" + @formats[:footer] = "%15s ]-END----------------------------------------------------[%3d]-[ %s%6.2f%s ]-\n" + + @colors = { + :online => '', + :offline => ''. + :nodns => '', + :loop => '', + :reset => ''. + :comment1 => '', + :comment2 => '' + } + if (@config.ansi) + @colors = { + :online => '\e[0;32m', + :offline => '\e[0;31m'. + :nodns => '\e[7;31;48m', + :loop => '\e[0;35m', + :reset => '\e[0m'. + :comment1 => '\e[0;37m', + :comment2 => '\e[0;36m' + } + end + end + def loop + puts + printf @formats[:ping], "LOOP", @colors[:loop], 'LOOP', @colors[:reset], "UP", "DOWN" + puts + end + def report(host) + if (@config.command.nil? or host.status != :online or not host.rpc?) + report_ping(host) + elsif (@config.output_path) + report_file(host) + else + report_plain(host) + end + end + def report_ping(host) + printf @formats[:ping], host.hostname, @colors[host.status], host_status(host), @colors[:reset], host.room, host.grid + if (host.ping_output) + host.ping_output.each do |line| + printf @formats[:content], host.hostname, line + end + end + end + def report_file(host) + printf @formats[:header], host.hostname, host.room, host.grid + File.open(File.join(@config.output_path, "#{host.hostname}.#{Time.now.strftime('%Y%m%d%H%M%S')}"),"w") do |file| + host.output.each do |time,line| + file.puts line + end + end + printf @formats[:footer], host.hostname, host.returncode, @colors[:comment1], host.duration, @colors[:reset] + end + def report_plain(host) + printf @formats[:header], host.hostname, host.room, host.grid + host.output.each do |time,line| + file.puts line + end + printf @formats[:footer], host.hostname, host.returncode, @colors[:comment1], host.duration, @colors[:reset] + end + def host_status(host) + case host.status + when :nodns + "!DNS" + when :offline + "DOWN" + when :online + " UP" + else + "UNKN" + end + end +end +end