Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added generic shell command plugin

  • Loading branch information...
commit ec85ec913e5ba50bae2f11e2b1d84a03005b36a7 1 parent 67b97db
Andre Lewis authored
View
36 generic_shell_command/generic_shell_command.rb
@@ -0,0 +1,36 @@
+# Provide a shell command that yields a number. For Example:
+#
+# ls -1 /Users/andre | wc -l
+#
+# If you want the resulting metric to have a name, place the name after a comment (#):
+#
+# ls -1 /Users/andre | wc -l # num_files_in_home
+#
+# For multiple commands, separate with a semicolon:
+#
+# ps -ef | grep ruby | wc -l # num_ruby_processes; ls -1 /Users/andre | wc -l # files_in_directory
+#
+# Notes:
+# * to configure this plugin, download a local copy and try in test mode! Example:
+# scout test generic_shell_command.rb args="ls -1 /some/dir | wc -l"
+# * all results will be cast to integers. If you forget to pipe to wc, you will get unexpected results
+# * make sure you know what you're doing with this plugin!
+
+class GenericShellCommand < Scout::Plugin
+ OPTIONS=<<-EOS
+ args:
+ name: args
+ notes: "one or more shell commands, in the format 'command_1 #optional_label; command_2 #label2' "
+ EOS
+
+ def build_report
+ res={}
+ lines = (option(:args) || "").split(";")
+ lines.each_with_index do |line,i|
+ command,label=line.split("#",2).map{|s|s.strip}
+ label = (label && label != "") ? label.gsub(/\W+/,'_') : "value_#{i+1}"
+ res[label]=`#{command}`.strip.to_i
+ end
+ report res
+ end
+end
View
46 simple_process_check/simple_process_check.rb
@@ -1,17 +1,24 @@
-
-
# Provide a comma-delimited list of process names (names only, no paths). This plugin checks that at least
# one instance of each named process is running, and alerts you if any of the processes have NO instances running.
# It alerts you again when one or more of the non-running processes is detected again.
+#
+# You can also check that a process name exists with a certain substring included in its arguments. For example, to
+# check for two instances of node (one with "emailer" in its args, and one with "eventLogger" in its args), set this
+# in process_names: node/emailer,node/eventLogger
+#
+# You can mix and match pure process_names and process_name/args. Note that the process names are always full string matches,
+# and the args are always partial string matches.
+#
class ProcessUsage < Scout::Plugin
OPTIONS=<<-EOS
process_names:
- notes: "comma-delimited list of process names to monitor. Example: sshd,apache2"
+ notes: "comma-delimited list of process names to monitor. Example: sshd,apache2,node"
ps_command:
label: ps command
- default: ps axco command
- notes: Leave the default in most cases. The commmand should return all processes running, one line per process, without any other info.
+ default: ps -eo comm,args
+ notes: Leave the default in most cases.
+ attributes: advanced
EOS
def build_report
@@ -19,20 +26,31 @@ def build_report
if process_names.nil? or process_names == ""
return error("Please specify the names of the processes you want to monitor. Example: sshd,apache2")
end
- ps_command = option(:ps_command) || "ps axco command"
- ps_output = `#{ps_command}`
+ ps_output = `#{option(:ps_command)}`
+
unless $?.success?
return error("Couldn't use `ps` as expected.", error.message)
end
- ps_output=ps_output.downcase.split("\n")
+ # this makes ps_output an array of two-element arrays:
+ # [ ["smtpd", "smtpd -n smtp -t inet -u -c"],
+ # ["proxymap", "proxymap -t unix -u"],
+ # ["apache2", "usr/sbin/apache2 -k start"] ]
+ ps_output=ps_output.downcase.split("\n").map{|line| line.split(/\W+/,2)}
+ processes_to_watch = process_names.split(",").uniq
+ process_counts = processes_to_watch.map do |p|
+ name, arg_string = p.split("/").map{|s|s.strip}
+ if arg_string
+ res = ps_output.select{|row| row[0] == name && row[1].include?(arg_string) }.size
+ else
+ res = ps_output.select{|row| row[0] == name }.size
+ end
+ res
+ end
- processes=process_names.split(",").uniq
- process_counts=processes.map{|name| ps_output.count{|line_item|line_item==name} }
-
- num_processes=processes.size
+ num_processes=processes_to_watch.size
num_processes_present = process_counts.count{|count| count > 0}
previous_num_processes=memory(:num_processes)
@@ -40,9 +58,9 @@ def build_report
# alert if the number of processes monitored or the number of processes present has changed since last time
if num_processes !=previous_num_processes || num_processes_present != previous_num_processes_present
- subject = "Process check: #{num_processes_present} of #{processes.size} processes are present"
+ subject = "Process check: #{num_processes_present} of #{processes_to_watch.size} processes are present"
body=""
- processes.each_with_index do |process,index|
+ processes_to_watch.each_with_index do |process,index|
body<<"#{index+1}) #{process} - #{process_counts[index]} instance(s) running \n"
end
alert(subject,body)
Please sign in to comment.
Something went wrong with that request. Please try again.