Permalink
Browse files

add disk_usage conditon

  • Loading branch information...
Tom Werner
Tom Werner committed Oct 26, 2007
1 parent 43dd058 commit 7581ed838032c105d7e9abf21000784915da442e
Showing with 86 additions and 0 deletions.
  1. +2 −0 History.txt
  2. +1 −0 lib/god.rb
  3. +27 −0 lib/god/conditions/disk_usage.rb
  4. +56 −0 test/test_conditions_disk_usage.rb
View
@@ -4,6 +4,8 @@
* Move Syslog calls into God::Logger and clean up all calling code
* Remove god's pid file on user requested termination
* Better handling and cleanup of DRb server's unix domain socket
+* New Conditions
+ * DiskUsage < PollCondition - trigger if disk usage is above limit on mount [Rudy Desjardins]
== 0.5.2 / 2007-10-10
View
@@ -32,6 +32,7 @@
require 'god/conditions/degrading_lambda'
require 'god/conditions/flapping'
require 'god/conditions/http_response_code'
+require 'god/conditions/disk_usage'
require 'god/contact'
require 'god/contacts/email'
@@ -0,0 +1,27 @@
+module God
+ module Conditions
+
+ class DiskUsage < PollCondition
+ attr_accessor :above, :mount_point
+
+ def initialize
+ super
+ self.above = nil
+ self.mount_point = nil
+ end
+
+ def valid?
+ valid = true
+ valid &= complain("Attribute 'mount_point' must be specified", self) if self.mount_point.nil?
+ valid &= complain("Attribute 'above' must be specified", self) if self.above.nil?
+ valid
+ end
+
+ def test
+ usage = `df | grep -i " #{self.mount_point}$" | awk '{print $5}' | sed 's/%//'`
+ usage.to_i > self.above
+ end
+ end
+
+ end
+end
@@ -0,0 +1,56 @@
+require File.dirname(__FILE__) + '/helper'
+
+class TestConditionsDiskUsage < Test::Unit::TestCase
+ # valid?
+
+ def test_valid_should_return_false_if_no_above_given
+ c = Conditions::DiskUsage.new
+ c.mount_point = '/'
+ c.watch = stub(:name => 'foo')
+
+ no_stdout do
+ assert_equal false, c.valid?
+ end
+ end
+
+ def test_valid_should_return_false_if_no_mount_point_given
+ c = Conditions::DiskUsage.new
+ c.above = 90
+ c.watch = stub(:name => 'foo')
+
+ no_stdout do
+ assert_equal false, c.valid?
+ end
+ end
+
+ def test_valid_should_return_true_if_required_options_all_set
+ c = Conditions::DiskUsage.new
+ c.above = 90
+ c.mount_point = '/'
+ c.watch = stub(:name => 'foo')
+
+ assert_equal true, c.valid?
+ end
+
+ # test
+
+ def test_test_should_return_true_if_above_limit
+ c = Conditions::DiskUsage.new
+ c.above = 90
+ c.mount_point = '/'
+
+ c.expects(:`).returns('91')
+
+ assert_equal true, c.test
+ end
+
+ def test_test_should_return_false_if_below_limit
+ c = Conditions::DiskUsage.new
+ c.above = 90
+ c.mount_point = '/'
+
+ c.expects(:`).returns('90')
+
+ assert_equal false, c.test
+ end
+end

0 comments on commit 7581ed8

Please sign in to comment.