Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

monitor a collection of process names

  • Loading branch information...
commit 345a42374c1444bd27ae19bbfb222857eef50555 1 parent f51fca2
Andre Lewis authored
Showing with 40 additions and 39 deletions.
  1. +40 −39 stale_daemon_monitor/stale_daemon_monitor.rb
79 stale_daemon_monitor/stale_daemon_monitor.rb
View
@@ -4,20 +4,18 @@
# workers and custom scripts. Sometimes we've seen processes fail to
# restart (maybe the process was busy and didn't trap the SIGHUP?). The
# result is stale processes whose code is out of sync with the database.
-#
+#
# So this is a script that will check the timestamp of our current Rails
# deploy and alert us if the start time of any daemons are older than
# that.
$VERBOSE=false
require 'time'
-class StaleDaemonMonitor < Scout::Plugin
+class StaleDaemonMonitor < Scout::Plugin
- START_TIME_HEADER = 'start'
-
OPTIONS=<<-EOS
- command_name:
- name: Command Name
+ command_names:
+ name: Comma Separated Process Names
rails_root:
name: Rails Root Path
notes: "Full path to the root directory of the Rails application"
@@ -27,47 +25,50 @@ class StaleDaemonMonitor < Scout::Plugin
attributes: advanced
ps_regex:
name: "PS Regular Expression"
- default: "(?i:\\bCOMMAND\\b)"
+ attributes: advanced
+ start_time_header:
+ name: "Start time header from ps command"
attributes: advanced
EOS
- def build_report
- if option(:command_name).nil?
- return error("Please specify the name of the process you want to monitor.")
- end
+ def build_report
+ if option(:command_names).nil?
+ return error("Please specify the name of the process(es) you want to monitor.")
+ end
+
+ if option(:rails_root).nil? || !File.readable?(option(:rails_root))
+ return error("Please specify readable Rails root.")
+ end
+ current_deployment_time = File.ctime(option(:rails_root))
- if option(:rails_root).nil? || !File.readable?(option(:rails_root))
- return error("Please specify readable Rails root.")
- end
- current_deployment_time = File.ctime(option(:rails_root))
-
- ps_command = option(:ps_command) || "ps auxww"
- ps_regex = (option(:ps_regex) || "(?i:\\bCOMMAND\\b)").to_s.gsub("COMMAND") { Regexp.escape(option(:command_name)) }
+ ps_command = option(:ps_command) || "ps auxww"
+ ps_regex = (option(:ps_regex) || "(?i:\\b(COMMAND)\\b)").to_s.gsub("COMMAND") { option(:command_names).split(',').join('|') }
+ start_time_header = option(:start_time_header) || 'start'
- ps_output = `#{ps_command}`
- unless $?.success?
- return error("Couldn't use `ps` as expected.", error.message)
- end
+ ps_output = `#{ps_command}`
+ unless $?.success?
+ return error("Couldn't use `ps` as expected.", error.message)
+ end
- ps_lines = ps_output.split(/\n/)
- fields = ps_lines.shift.downcase.split
- start_time_index = fields.index(START_TIME_HEADER)
- if start_time_index.nil?
- return error("The output from `#{ps_command}` did not include the #{START_TIME_HEADER} fields." )
- end
+ ps_lines = ps_output.split(/\n/)
+ fields = ps_lines.shift.downcase.split
+ start_time_index = fields.index(start_time_header)
+ if start_time_index.nil?
+ return error("The output from `#{ps_command}` did not include the '#{start_time_header}' field." )
+ end
+
+ process_lines = ps_lines.grep(Regexp.new(ps_regex))
+ stale_processes = process_lines.select do |line|
+ process_started_at = Time.parse(line.split[start_time_index])
+ process_started_at < current_deployment_time
+ end
- process_lines = ps_lines.grep(Regexp.new(ps_regex))
- stale_processes = process_lines.select do |line|
- process_started_at = Time.parse(line.split[start_time_index])
- process_started_at < current_deployment_time
- end
+ if stale_processes.any?
+ alert("Found Process Older Than #{current_deployment_time}", stale_processes.join("\n"))
+ end
- if stale_processes.any?
- alert("Found Stale Process", stale_processes.join("\n"))
- end
-
rescue Exception => e
error("Error when executing: #{e.class}", e.message)
- end
+ end
-end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.