class SimpleServerMonitoring::Reporter
Attributes
report[R]
Public Class Methods
new(options = {})
click to toggle source
# File lib/simple_server_monitoring/reporter.rb, line 9 def initialize(options = {}) @to, @processes_file, @from = options.values_at(:to, :processes_file, :from) @report = build_report end
Public Instance Methods
send_report_email()
click to toggle source
# File lib/simple_server_monitoring/reporter.rb, line 14 def send_report_email send_email @to, subject: @report.subject, body: @report.body, content_type: "text/html", from: @from end
Private Instance Methods
build_report()
click to toggle source
# File lib/simple_server_monitoring/reporter.rb, line 20 def build_report running_processes = `ps -ef`.split("\n") hostname = `hostname` template_variables = { free_storage_space: `df -h /`, free_ram: `free 2> /dev/null`, # for cases where there is no `free` uptime: `uptime`, running_processes: running_processes, hostname: hostname, important_processes: [] } failed_process_constraints = [] load_process_constraints.each do |name_of_process, expected_processes_count| processes = running_processes.select { |process| process =~ /#{name_of_process}/i } unless processes.count == expected_processes_count failed_process_constraints.push(process: name_of_process, expected: expected_processes_count, actual: processes.count) end template_variables[:important_processes].push name: name_of_process, processes: processes end if failed_process_constraints.any? report_status = "errors: #{failed_process_constraints.map { |c| c[:process] }.join(", ")}" else report_status = "OK" end Report.new( subject: "#{report_status}, #{hostname}, #{Time.now.utc}, Server Monitoring".gsub("\n", ""), status: report_status, body: ERB.new(File.read(File.expand_path("../views/email_template.html.erb", __FILE__))).result(binding) ) end
load_process_constraints()
click to toggle source
# File lib/simple_server_monitoring/reporter.rb, line 69 def load_process_constraints YAML.load_file(File.expand_path(@processes_file, __FILE__)) end
send_email(receiver_emails, options = {})
click to toggle source
# File lib/simple_server_monitoring/reporter.rb, line 73 def send_email(receiver_emails, options = {}) options[:server] ||= "localhost" options[:from_alias] ||= "Simple Server Mailer" options[:content_type] ||= "text/plain" msg = <<-END_OF_MESSAGE From: #{options[:from_alias]} <#{options.fetch(:from)}> To: #{receiver_emails.join(", ")} Subject: #{options.fetch(:subject)} Content-Type: #{options[:content_type]}; charset=utf-8 #{options.fetch(:body)} END_OF_MESSAGE Net::SMTP.start(options[:server]) do |smtp| smtp.send_message msg, options[:from], receiver_emails end end