Class: UbcMonitor::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/ubc_monitor/cli.rb

Overview

Run the monitor

Class Method Summary collapse

Class Method Details

.execute(arguments = []) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ubc_monitor/cli.rb', line 8

def self.execute(arguments=[])
  options = { :file => File.expand_path('~/.ubc_monitor'),
              :email_recipient => nil,
              :email_subject => 'User beancounters has new errors' }

  OptionParser.new do |opts|
    opts.banner = "      Monitor /proc/user_beancounters and send an email to the systems\n      administrator if failcnt has increased since last time ubc_monitor\n      was run.\n\n      ubc_monitor uses a file (by default ~/.ubc_monitor) to keep track of\n      user_beancounter fail counts. When running without this file (such\n      as the first run) any fail count except 0 will be reported.\n\n      Usage: \#{File.basename(__FILE__)} [options]\n    BANNER\n    opts.on(\"-f\", \"--file [FILE]\", \"Log failcounts to this file, default\\n\" +\n            \"                                     is \#{options[:file]}\") { |f| options[:file] = File.expand_path f }\n    opts.on(\"-n\", \"--no-log\", \"Don't log fail counts\") { |r| options[:file] = nil }\n    opts.on(\"-t\", \"--email-recipient [EMAIL]\", \"The recipient of report emails.\\n\" +\n            \"                                     If this is not set, no email is sent\") { |r| options[:email_recipient] = r }\n    opts.on(\"-s\", \"--email-subject [SUBJECT]\", \"Email subject. Default\\n\" +\n            \"                                     'value is \#{options[:email_subject]}'\") { |s| options[:email_subject] = s }\n  end.parse!\n\n  monitor = UbcMonitor::Monitor.new(options)\n  report = monitor.run\n\n  if report.updated && !options[:email_recipient].nil?\n    `echo '\"\#{report.to_s}\"' | mail -s \"\#{options[:email_subject]}\" \"\#{options[:email_recipient]}\"`\n  elsif report.updated\n    puts report.to_s\n  else\n    puts \"All's swell!\"\n  end\nend\n".gsub(/^          /,'')