Class: Recmon::Monitor
- Inherits:
-
Object
- Object
- Recmon::Monitor
- Defined in:
- lib/recmon/monitor.rb
Overview
The Monitor manages the list of sensors, and calls on them periodically to report their results.
Instance Attribute Summary collapse
-
#sensors ⇒ Object
readonly
Returns an array of sensors.
Instance Method Summary collapse
-
#command(name, command, freq = 120) ⇒ Object
Adds a CommandSensor.
-
#diskfree(name, freq = 1200) ⇒ Object
Adds a DiskfreeSensor.
-
#diskspace(name, path, freq = 1200) ⇒ Object
Adds a DiskspaceSensor.
-
#filesize(name, path, freq = 1200) ⇒ Object
Adds a FilesizeSensor.
-
#initialize(log = "/var/log/recmon.log", name = "recmond", freq = 10) ⇒ Monitor
constructor
Creates a new monitor, writing to the given log file.
-
#log(message) ⇒ Object
Logs a message to the log file.
-
#monitor ⇒ Object
Checks the list of sensors to see if any are due to report.
-
#open_log ⇒ Object
opens the log file - occurs on starting, and if HUP signal is received.
-
#ping(name, ip, freq = 120) ⇒ Object
Adds a PingSensor.
-
#proc(name, pattern, freq = 60) ⇒ Object
Adds a ProcSensor.
-
#ssh(name, user, port = 22, freq = 120) ⇒ Object
Add an SSHSensor.
-
#start ⇒ Object
Starts the monitor running until an INT signal is received.
-
#stop ⇒ Object
Stops the monitor.
-
#web(name, url, freq = 120) ⇒ Object
Adds a WebSensor.
Constructor Details
#initialize(log = "/var/log/recmon.log", name = "recmond", freq = 10) ⇒ Monitor
Creates a new monitor, writing to the given log file. The default frequency of 10 seconds is frequent enough to be near-real-time without any burden on the processor. This is the frequency on which the monitor checks if any sensors are due - not the frequency for each sensor. The name is used in the process list (ps, top).
18 19 20 21 22 23 24 |
# File 'lib/recmon/monitor.rb', line 18 def initialize(log="/var/log/recmon.log", name="recmond", freq=10) @logfile = log @name = name @freq = freq # seconds @log = "" @sensors = [] end |
Instance Attribute Details
#sensors ⇒ Object (readonly)
Returns an array of sensors
11 12 13 |
# File 'lib/recmon/monitor.rb', line 11 def sensors @sensors end |
Instance Method Details
#command(name, command, freq = 120) ⇒ Object
Adds a CommandSensor
82 83 84 |
# File 'lib/recmon/monitor.rb', line 82 def command(name, command, freq=120) @sensors << Recmon::CommandSensor.new(name, command, freq) end |
#diskfree(name, freq = 1200) ⇒ Object
Adds a DiskfreeSensor
87 88 89 |
# File 'lib/recmon/monitor.rb', line 87 def diskfree(name, freq=1200) @sensors << Recmon::DiskfreeSensor.new(name, freq) end |
#diskspace(name, path, freq = 1200) ⇒ Object
Adds a DiskspaceSensor
92 93 94 |
# File 'lib/recmon/monitor.rb', line 92 def diskspace(name, path, freq=1200) @sensors << Recmon::DiskspaceSensor.new(name, path, freq) end |
#filesize(name, path, freq = 1200) ⇒ Object
Adds a FilesizeSensor
97 98 99 |
# File 'lib/recmon/monitor.rb', line 97 def filesize(name, path, freq=1200) @sensors << Recmon::FilesizeSensor.new(name, path, freq) end |
#log(message) ⇒ Object
Logs a message to the log file
77 78 79 |
# File 'lib/recmon/monitor.rb', line 77 def log() @log.puts("#{Time.now.iso8601} #{}") end |
#monitor ⇒ Object
Checks the list of sensors to see if any are due to report
71 72 73 74 |
# File 'lib/recmon/monitor.rb', line 71 def monitor() @sensors.each { |s| log(s.sense()) if s.check() } @log.flush() end |
#open_log ⇒ Object
opens the log file - occurs on starting, and if HUP signal is received
55 56 57 58 59 60 61 |
# File 'lib/recmon/monitor.rb', line 55 def open_log() begin @log = File.new(@logfile, 'a') rescue exit 1 end end |
#ping(name, ip, freq = 120) ⇒ Object
Adds a PingSensor
102 103 104 |
# File 'lib/recmon/monitor.rb', line 102 def ping(name, ip, freq=120) @sensors << Recmon::PingSensor.new(name, ip, freq) end |
#proc(name, pattern, freq = 60) ⇒ Object
Adds a ProcSensor
107 108 109 |
# File 'lib/recmon/monitor.rb', line 107 def proc(name, pattern, freq=60) @sensors << Recmon::ProcSensor.new(name, pattern, freq) end |
#ssh(name, user, port = 22, freq = 120) ⇒ Object
Add an SSHSensor
112 113 114 |
# File 'lib/recmon/monitor.rb', line 112 def ssh(name, user, port=22, freq=120) @sensors << Recmon::SSHSensor.new(name, user, port, freq) end |
#start ⇒ Object
Starts the monitor running until an INT signal is received.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/recmon/monitor.rb', line 27 def start() trap("HUP") { # defer monitoring for a minute and reopen log files @log.close() unless @log.nil? or @log.closed? sleep(60) open_log() } trap("INT") { stop() } open_log() # daemonise the process $0 = @name fork do Process.setsid exit if fork File.umask 0027 # default mode => 0750 STDIN.reopen "/dev/null" STDOUT.reopen @log STDERR.reopen @log log("Recmon is monitoring.") monitor() while sleep(@freq) monitor() end end end |
#stop ⇒ Object
Stops the monitor
64 65 66 67 68 |
# File 'lib/recmon/monitor.rb', line 64 def stop() log("Recmon is exiting.") @log.close() unless @log.nil? or @log.closed? exit 0 end |