benotified 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ p�k�h�$�L%�3�9����Q\\��#߰�)#���T=��>��b��ta�ߗ�������Z�?c>
2
+ hϨY�ZLr&R���\ܮ���8.�z A��Jw3��ZR����f).�Tb1�
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source :rubygems
2
+
3
+ gem "actionmailer", "3.0.1"
4
+ gem "json_pure", "1.4.6"
5
+ gem "log4r", "1.1.9"
@@ -0,0 +1,51 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ abstract (1.0.0)
5
+ actionmailer (3.0.1)
6
+ actionpack (= 3.0.1)
7
+ mail (~> 2.2.5)
8
+ actionpack (3.0.1)
9
+ activemodel (= 3.0.1)
10
+ activesupport (= 3.0.1)
11
+ builder (~> 2.1.2)
12
+ erubis (~> 2.6.6)
13
+ i18n (~> 0.4.1)
14
+ rack (~> 1.2.1)
15
+ rack-mount (~> 0.6.12)
16
+ rack-test (~> 0.5.4)
17
+ tzinfo (~> 0.3.23)
18
+ activemodel (3.0.1)
19
+ activesupport (= 3.0.1)
20
+ builder (~> 2.1.2)
21
+ i18n (~> 0.4.1)
22
+ activesupport (3.0.1)
23
+ builder (2.1.2)
24
+ erubis (2.6.6)
25
+ abstract (>= 1.0.0)
26
+ i18n (0.4.2)
27
+ json_pure (1.4.6)
28
+ log4r (1.1.9)
29
+ mail (2.2.14)
30
+ activesupport (>= 2.3.6)
31
+ i18n (>= 0.4.0)
32
+ mime-types (~> 1.16)
33
+ treetop (~> 1.4.8)
34
+ mime-types (1.16)
35
+ polyglot (0.3.1)
36
+ rack (1.2.1)
37
+ rack-mount (0.6.13)
38
+ rack (>= 1.0.0)
39
+ rack-test (0.5.7)
40
+ rack (>= 1.0)
41
+ treetop (1.4.9)
42
+ polyglot (>= 0.3.1)
43
+ tzinfo (0.3.24)
44
+
45
+ PLATFORMS
46
+ ruby
47
+
48
+ DEPENDENCIES
49
+ actionmailer (= 3.0.1)
50
+ json_pure (= 1.4.6)
51
+ log4r (= 1.1.9)
@@ -0,0 +1,28 @@
1
+ Gemfile
2
+ Gemfile.lock
3
+ Manifest
4
+ README.md
5
+ Rakefile
6
+ benotified.gemspec
7
+ examples/monitor.rb
8
+ lib/be_notified.rb
9
+ lib/be_notified/commands.rb
10
+ lib/be_notified/commands/host_not_alive.rb
11
+ lib/be_notified/commands/number_of_files.rb
12
+ lib/be_notified/commands/program_running.rb
13
+ lib/be_notified/commands/size_of_file.rb
14
+ lib/be_notified/configuration.rb
15
+ lib/be_notified/monitor.rb
16
+ lib/be_notified/notifier.rb
17
+ lib/be_notified/version.rb
18
+ lib/benotified.rb
19
+ test/commands/test_host_not_alive.rb
20
+ test/commands/test_number_of_files.rb
21
+ test/commands/test_program_running.rb
22
+ test/commands/test_size_of_file.rb
23
+ test/notifiers/test_email_notifier.rb
24
+ test/notifiers/test_log_notifier.rb
25
+ test/test_configuration.rb
26
+ test/test_helper.rb
27
+ test/test_monitor.rb
28
+ test/test_notifier.rb
@@ -0,0 +1,132 @@
1
+ # BeNotified
2
+ ## Description
3
+
4
+ This library monitors your programs or resources and notifies you by email or log if anything goes wrong.
5
+
6
+ ## Installation
7
+
8
+ This gem is hosted on gemcutter.org
9
+
10
+ gem install be-notified
11
+
12
+ ## Usage
13
+
14
+ require 'be_notified'
15
+
16
+ BeNotified::Monitor.new do
17
+ alert_when "Something went wrong" do
18
+ # Put your code here
19
+ # Return true if something went wrong
20
+ # or false if everything was fine
21
+ end
22
+ end
23
+
24
+
25
+ This library can be used in two different ways:
26
+
27
+ __First:__ Run it from crontab to monitor some resources, for exmaple:
28
+
29
+ BeNotified::Monitor.new do
30
+ alert_when "Staging server is down" do
31
+ host_not_available?("staging.example.com")
32
+ end
33
+ end
34
+
35
+ or
36
+
37
+
38
+ BeNotified::Monitor.new do
39
+ alert_when "Log file does not exist" do
40
+ File.exists?('/var/log/messages') == false
41
+ end
42
+ end
43
+
44
+
45
+ __Second:__ Wrap your program around it and be informed when something goes wrong. I will use imaginary program called db_updater as an example:
46
+
47
+ BeNotified::Monitor.new do
48
+ alert_when "Db was not updated" do
49
+ # Require your program here
50
+ require 'db_updater'
51
+
52
+ # Get the count of the records before run
53
+ count_before_run = DBUpdater::Update::Record.all.count
54
+
55
+ # Run your application
56
+ DBUpdater::Update.run
57
+
58
+ # Get the count after the run
59
+ count_after_run = DBUpdater::Update::Record.all.count
60
+
61
+ # Now decide if anything went wrong
62
+ count_after_run == count_before_run
63
+ end
64
+ end
65
+
66
+ ## Configuration file
67
+
68
+ Options can be set up in the three different ways:
69
+
70
+ __First:__ By default program will log to standard output.
71
+
72
+ __Second:__ Define properties directly in the code. An example of setting email notification:
73
+
74
+ BeNotified::Monitor.new do
75
+ custom_options = {
76
+ :notifier_type => BeNotified::Notifiers::Email
77
+ :email => {
78
+ :smtp_address => 'localhost',
79
+ :smtp_port => 25,
80
+ :domain => 'example.com',
81
+ :username => 'login',
82
+ :password => 'pass',
83
+ :to => 'to@example.com',
84
+ :from => 'from@example.com',
85
+ :subject => 'The Subject'
86
+ }
87
+ }
88
+
89
+ # Make sure you merge the options
90
+ configuration(custom_options)
91
+ end
92
+
93
+ __Third:__ Create a file _.be\_notified_ in your home directory. It stores the configuration in JSON format. An example of setting application to log to the file:
94
+
95
+ {"logger_file":"/tmp/benotified.log"}
96
+
97
+ # Helper Methods
98
+
99
+ This library is shipped with some helper methods that can be used to monitor resources:
100
+
101
+ * host\_not\_alive? - checks if a given host if alive or not
102
+
103
+ alert_when "Staging server is down" do
104
+ host_not_available?("staging.example.com")
105
+ end
106
+
107
+ * number\_of\_files - checks the number of files in a given directory
108
+
109
+ alert_when "Number of files on the desktop is greater then 50" do
110
+ number_of_files("/Users/dominik/Desktop") > 50
111
+ end
112
+
113
+ * program\_running? - checks if a given program is running
114
+
115
+ alert_when "Application server is not running" do
116
+ ! program_running?("jboss")
117
+ end
118
+
119
+ * size\_of\_file - checks the size of a given file in GB, MB or KB
120
+
121
+ alert_when "JBoss log file is bigger then 1GB" do
122
+ size_of_file("/usr/local/jboss/server/default/log/server.log") > 1.GB
123
+ end
124
+
125
+ # How to contribute
126
+
127
+ 1. Revise the code and suggest improvements / changes
128
+ 2. Write more helper methods
129
+ 3. Add new ways of notifying user
130
+
131
+
132
+
@@ -0,0 +1,24 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ require File.expand_path('../lib/be_notified/version', __FILE__)
6
+
7
+ task :default => :test
8
+
9
+ require 'rake/testtask'
10
+
11
+ Rake::TestTask.new(:test) do |test|
12
+ test.libs << 'lib' << 'test'
13
+ test.pattern = 'test/**/test_*.rb'
14
+ test.verbose = true
15
+ end
16
+
17
+ Echoe.new('benotified', BeNotified::VERSION) do |p|
18
+ p.description = "Library to monitor your programs and resources. It will notify you by email or just log the event if something goes wrong."
19
+ p.url = "http://github.com/staskie/be-notified"
20
+ p.author = "Dominik Staskiewicz"
21
+ p.email = "stadominik @nospam@ gmail.com"
22
+ p.ignore_pattern = ["tmp/*", "script/*"]
23
+ p.development_dependencies = []
24
+ end
@@ -0,0 +1,33 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{benotified}
5
+ s.version = "0.0.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Dominik Staskiewicz"]
9
+ s.cert_chain = ["/Users/dominik/Documents/keys/gem-public_cert.pem"]
10
+ s.date = %q{2011-01-19}
11
+ s.description = %q{Library to monitor your programs and resources. It will notify you by email or just log the event if something goes wrong.}
12
+ s.email = %q{stadominik @nospam@ gmail.com}
13
+ s.extra_rdoc_files = ["README.md", "lib/be_notified.rb", "lib/be_notified/commands.rb", "lib/be_notified/commands/host_not_alive.rb", "lib/be_notified/commands/number_of_files.rb", "lib/be_notified/commands/program_running.rb", "lib/be_notified/commands/size_of_file.rb", "lib/be_notified/configuration.rb", "lib/be_notified/monitor.rb", "lib/be_notified/notifier.rb", "lib/be_notified/version.rb", "lib/benotified.rb"]
14
+ s.files = ["Gemfile", "Gemfile.lock", "Manifest", "README.md", "Rakefile", "benotified.gemspec", "examples/monitor.rb", "lib/be_notified.rb", "lib/be_notified/commands.rb", "lib/be_notified/commands/host_not_alive.rb", "lib/be_notified/commands/number_of_files.rb", "lib/be_notified/commands/program_running.rb", "lib/be_notified/commands/size_of_file.rb", "lib/be_notified/configuration.rb", "lib/be_notified/monitor.rb", "lib/be_notified/notifier.rb", "lib/be_notified/version.rb", "lib/benotified.rb", "test/commands/test_host_not_alive.rb", "test/commands/test_number_of_files.rb", "test/commands/test_program_running.rb", "test/commands/test_size_of_file.rb", "test/notifiers/test_email_notifier.rb", "test/notifiers/test_log_notifier.rb", "test/test_configuration.rb", "test/test_helper.rb", "test/test_monitor.rb", "test/test_notifier.rb"]
15
+ s.homepage = %q{http://github.com/staskie/be-notified}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Benotified", "--main", "README.md"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{benotified}
19
+ s.rubygems_version = %q{1.3.7}
20
+ s.signing_key = %q{/Users/dominik/Documents/keys/gem-private_key.pem}
21
+ s.summary = %q{Library to monitor your programs and resources. It will notify you by email or just log the event if something goes wrong.}
22
+ s.test_files = ["test/commands/test_host_not_alive.rb", "test/commands/test_number_of_files.rb", "test/commands/test_program_running.rb", "test/commands/test_size_of_file.rb", "test/notifiers/test_email_notifier.rb", "test/notifiers/test_log_notifier.rb", "test/test_configuration.rb", "test/test_helper.rb", "test/test_monitor.rb", "test/test_notifier.rb"]
23
+
24
+ if s.respond_to? :specification_version then
25
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
26
+ s.specification_version = 3
27
+
28
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
29
+ else
30
+ end
31
+ else
32
+ end
33
+ end
@@ -0,0 +1,27 @@
1
+ $:.push File.join(File.dirname(__FILE__), "../lib")
2
+
3
+ require 'be_notified'
4
+
5
+
6
+ BeNotified::Monitor.new do
7
+
8
+ alert_when "Clean your desktop." do
9
+ number_of_files("/Users/dominik/Desktop") > 5
10
+ end
11
+
12
+ alert_when "Number of jpg files on the desktop is greater then 2" do
13
+ number_of_files("/Users/dominik/Desktop", /jpg/i) > 2
14
+ end
15
+
16
+ alert_when "Size of abc.txt if greater then 4 KB" do
17
+ size_of_file("/Users/dominik/Desktop/abc.txt") > 4.KB
18
+ end
19
+
20
+ alert_when "Java prociess is not running" do
21
+ program_not_running?('java')
22
+ end
23
+
24
+ alert_when "Staging server is down" do
25
+ host_not_alive?("192.168.1.2", 2)
26
+ end
27
+ end
@@ -0,0 +1,9 @@
1
+ $:.push File.dirname(__FILE__)
2
+
3
+ require 'be_notified/version'
4
+ require 'be_notified/configuration'
5
+ require 'be_notified/notifier'
6
+ require 'be_notified/commands'
7
+ require 'be_notified/monitor'
8
+
9
+
@@ -0,0 +1,13 @@
1
+ module BeNotified
2
+ module Commands
3
+ # Hook method called when this module is included by some other class
4
+ # It loads all the command helpers
5
+ def self.included(base)
6
+ dir = File.join(File.dirname(__FILE__), 'commands')
7
+
8
+ Dir.glob("#{dir}/*.rb").each do |command|
9
+ require command
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,29 @@
1
+ require 'socket'
2
+
3
+ module BeNotified
4
+ module Commands
5
+
6
+ # Check if a given host is up
7
+ #
8
+ # host - The String represending either the domain name or IP address
9
+ # packets - number of PING packets to send to the host
10
+ #
11
+ # For example:
12
+ # host_not_alive?("example.com", 10)
13
+ #
14
+ # Returns true if no response from host
15
+ # Returns false if there was a response from host
16
+ def host_not_alive?(host, packets=5)
17
+ command = "ping -c #{packets} #{host}"
18
+
19
+ run_system(command) !~ /64 bytes from/
20
+ end
21
+
22
+ private
23
+
24
+ # For stubbing
25
+ def run_system(command)
26
+ `#{command}`
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,33 @@
1
+ module BeNotified
2
+ module Commands
3
+
4
+ # Check number of files in a given folder
5
+ #
6
+ # directory - The String representing the name of the directory
7
+ # mask - Regexp to match the files
8
+ #
9
+ # For example:
10
+ #
11
+ # number_of_files("/var/log")
12
+ # number_of_files("/var/log", /\.log$/)
13
+ #
14
+ # Returns Integer, count of files in a directory
15
+ def number_of_files(directory, mask = nil)
16
+ raise ArgumentError, "Directory '#{directory}' not found" if ! File.exists?(directory)
17
+ raise ArgumentError, "Mask should be a Regexp" if ! mask.nil? && ! (mask.kind_of? Regexp)
18
+
19
+ count = 0
20
+ Dir.glob("#{directory}/*").each do |file|
21
+ next if ! File.file?(file)
22
+
23
+ if mask.nil?
24
+ count += 1
25
+ else
26
+ count += 1 if file =~ Regexp.new(mask)
27
+ end
28
+ end
29
+
30
+ count
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,46 @@
1
+ module BeNotified
2
+ module Commands
3
+
4
+ # Check if process is running
5
+ #
6
+ # name - The String represending the name of the process
7
+ #
8
+ # For example:
9
+ #
10
+ # program_running?('netcat')
11
+ #
12
+ # Returns true if process is running
13
+ # Returns false if process is not running
14
+ # Raises RuntimeError if on Windows platform
15
+ def program_running?(name)
16
+ raise RuntimeError if running_on_windows?
17
+
18
+ command = "ps -ef | grep -i #{name} | grep -v grep"
19
+
20
+ # Run the command and check response
21
+ run_system(command) != ""
22
+ end
23
+
24
+ # Check if process is not running
25
+ # Argument and example the same as above
26
+ #
27
+ # Returns true if process is NOT running
28
+ # Returns false if process is running
29
+ # Raises RuntimeError if on Windows platform
30
+ def program_not_running?(name)
31
+ ! program_running?(name)
32
+ end
33
+
34
+ private
35
+
36
+ # For stubbing
37
+ def run_system(command)
38
+ `#{command}`
39
+ end
40
+
41
+ # Check if on windows platform
42
+ def running_on_windows?
43
+ RUBY_PLATFORM =~ /mswin32/
44
+ end
45
+ end
46
+ end