Permalink
Browse files

added the cpu and disk tracking

  • Loading branch information...
draper
draper committed Apr 12, 2010
1 parent a76dec4 commit 27773d9cbe2b9130eec35d9522ce37e80a45d30b
Showing with 112 additions and 0 deletions.
  1. +59 −0 check_cpu.rb
  2. +53 −0 check_disk.rb
View
@@ -0,0 +1,59 @@
+#!/usr/bin/ruby
+#this is a script for checking free ram left
+#the script should be used as follows: check_memory.rb <lowest percent for warning> <lowest percent for critical>
+#Example check_memory.rb 25 15
+# this would warn you when 25% of the memory is not free and would be critical when 25% of it is not free
+#the file should be excuted using the following: basic_check.rb <warning> <critical> <min> <max>
+#min and max are not required
+#some scripts may not having a warning and may have other stuff
+MEGABYTE = 1024.0 * 1024.0
+def bytes_to_megabytes bytes
+ bytes / MEGABYTE
+end
+
+exit_code = 0
+status = "OK"
+output_text = ""
+using_performance_data = true
+performance_data = ""
+perf_label = "cpu_usage"
+value = 0
+uom = ""
+warning = ARGV[0]
+critical = ARGV[1]
+min = ARGV[2]
+max = ARGV[3]
+
+#top -l 1| awk '/CPU usage/ {print $3, $5}'
+user_cpu = `top -l 1| awk '/CPU usage/ {print $3}'`
+user_cpu = user_cpu[0,(user_cpu.length-2)].to_f
+system_cpu = `top -l 1| awk '/CPU usage/ {print $5}'`
+system_cpu = system_cpu[0,(system_cpu.length-2)].to_f
+total_cpu = system_cpu+user_cpu
+
+
+
+#check for the max and such
+
+if total_cpu > warning.to_f
+ exit_code = 1
+ status = "WARNING"
+ if total_cpu > critical.to_f
+ exit_code = 2
+ status = "CRITICAL"
+ end
+end
+
+#testing percents
+if using_performance_data
+ #setup the performance data
+ value = total_cpu.to_i
+ uom="%"
+ performance_data= "#{perf_label}=#{value}#{uom};#{warning};#{critical};#{min};#{max}"
+end
+output_text = "#{status} CPU: #{total_cpu}% User:#{user_cpu}% Sytem: #{system_cpu}%"
+
+puts output_text + " | "+performance_data
+exit exit_code
+
+
View
@@ -0,0 +1,53 @@
+#!/usr/bin/ruby
+#this script checks for free disk space on a drive
+#the command is ran as so
+#check_disk.rb <warning> <critical> <diskname>
+#example: check_disk.rb 10 5 disk0s2
+#this would be a warning when there is only 10% free on disk0s2
+
+
+exit_code = 0
+status = "OK"
+output_text = ""
+using_performance_data = true
+performance_data = ""
+perf_label = "cpu_usage"
+value = 0
+uom = ""
+warning = ARGV[0]
+critical = ARGV[1]
+disk = ARGV[2]
+
+#df -l | grep 'disk0s2' | awk '{print $4"/"$2" free ("$5" used)"}'
+
+size = `df -l | grep '#{disk}' | awk '{print $2}'`.to_f
+free = `df -l | grep '#{disk}' | awk '{print $4}'`.to_f
+percent = ((free/size) * 100)
+
+
+#check for the max and such
+
+if percent < warning.to_f
+ exit_code = 1
+ status = "WARNING"
+ if percent < critical.to_f
+ exit_code = 2
+ status = "CRITICAL"
+ end
+end
+
+#testing percents
+if using_performance_data
+ #setup the performance data
+ value = percent.to_i
+ uom="%"
+ performance_data= "#{perf_label}=#{value}#{uom};#{warning};#{critical};"
+end
+free_gb = `df -hl | grep '#{disk}' | awk '{print $4}'`.chomp.chop.chop
+total_gb = `df -hl | grep '#{disk}' | awk '{print $2}'`.chomp.chop.chop
+output_text = "#{status} Free: #{percent.to_i}% #{free_gb}GB/#{total_gb}GB"
+
+puts output_text + " | "+performance_data
+exit exit_code
+
+

0 comments on commit 27773d9

Please sign in to comment.