peeping_tom 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,25 @@
1
+
2
+ require File.join(File.dirname(__FILE__), "peeping_tom", "peeper")
3
+ require File.join(File.dirname(__FILE__), "peeping_tom", "site")
4
+ require File.join(File.dirname(__FILE__), "peeping_tom", "dsl")
5
+
6
+ require File.join(File.dirname(__FILE__), "peeping_tom", "notifier", "email")
7
+
8
+ module PeepingTom
9
+ def self.peeper(name = nil)
10
+ @peepers ||= {}
11
+ @peepers[name || @name] ||= PeepingTom::Peeper.new
12
+ end
13
+
14
+ def self.set_peeper(name)
15
+ @name = name
16
+ end
17
+
18
+ def self.reset_peeper
19
+ @name = nil
20
+ end
21
+ end
22
+
23
+ include PeepingTom::DSL
24
+
25
+ $stdout.puts "[PeepingTom] Let me creep this place up a bit..."
@@ -0,0 +1,83 @@
1
+ require 'timeout'
2
+ require 'net/http'
3
+ require 'uri'
4
+
5
+ module PeepingTom
6
+ module DSL
7
+ def ping(site)
8
+ Timeout::timeout(site.timeout) do
9
+ url = URI.parse(site.url)
10
+ res = Net::HTTP.start(url.host, url.port) do |http|
11
+ file = "/" + url.to_s.sub(/http:\/\/.*?\//, '')
12
+ http.get(file)
13
+ end
14
+ return res.code.to_i >= 200 && res.code.to_i < 400
15
+ end
16
+ return error!
17
+ rescue Timeout::Error, Errno::ECONNREFUSED, SocketError
18
+ return error!
19
+ end
20
+
21
+ def channels
22
+ @channels
23
+ end
24
+
25
+ def watch(name)
26
+ PeepingTom.peeper(name).sites.each_value do |site|
27
+ @error = false
28
+ yield site
29
+ if @error
30
+ $stdout.puts "#{site.name} is DOWN"
31
+ @channels.each {|c| c.notify!(site)}
32
+ end
33
+ end
34
+ end
35
+
36
+ def errors
37
+ return unless @errors
38
+ yield
39
+ end
40
+
41
+ def no_errors
42
+ return if @errors
43
+ yield
44
+ end
45
+
46
+ def error!
47
+ @error = true
48
+ @errors = true
49
+ return false
50
+ end
51
+
52
+ def group(name, &block)
53
+ PeepingTom.set_peeper(name)
54
+ yield
55
+ PeepingTom.reset_peeper
56
+ end
57
+
58
+ def channel(type, *opts)
59
+ if type == :irc
60
+ require File.join(File.dirname(__FILE__), "notifier", "irc")
61
+ channel = PeepingTom::Notifier::IRC.new(*opts)
62
+ elsif type == :email
63
+ channel = PeepingTom::Notifier::Email.new(*opts)
64
+ else
65
+ raise ArgumentError, "unknown channel type: #{type}"
66
+ end
67
+ @channels ||= []
68
+ @channels << channel
69
+ end
70
+
71
+ def site(name, site)
72
+ PeepingTom.peeper.register(name, site)
73
+ end
74
+
75
+ def irc(msg)
76
+ PeepingTom::Notifier::IRC.notify(msg)
77
+ end
78
+
79
+ def email(msg)
80
+ PeepingTom::Notifier::Email.notify(msg)
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,29 @@
1
+ require 'net/smtp'
2
+
3
+ module PeepingTom
4
+ module Notifier
5
+ class Email
6
+ attr_reader :emails
7
+
8
+ def initialize(*sendmail_and_emails)
9
+ @sendmail_path = sendmail_and_emails.shift
10
+ @emails = sendmail_and_emails
11
+ end
12
+
13
+ def notify!(site)
14
+ self.emails.each do |email|
15
+ send_email(email, "#{site.name} is down", "#{site.name} was tested at #{site.url}, and it's busted. You should look into that.")
16
+ end
17
+ end
18
+
19
+ private
20
+ def send_email(to, subject, msg)
21
+ exec("#{@sendmail_path} #{to}
22
+ From: PeepingTom <peepingtom@creepy.com>
23
+ Subject: #{subject}
24
+
25
+ #{msg}")
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,20 @@
1
+ require 'shout-bot'
2
+
3
+ module PeepingTom
4
+ module Notifier
5
+ class IRC
6
+ def initialize(channel_name)
7
+ @channel = channel_name
8
+ end
9
+
10
+ def notify!(site)
11
+ msg = "#{site.name} was tested at #{site.url}, and it's busted. You should look into that."
12
+ ShoutBot.shout('irc://PeepingTom@irc.freenode.net:6667/#' + @channel) do |channel|
13
+ channel.say msg
14
+ end
15
+ rescue SocketError
16
+ #gulp gulp gulp
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,13 @@
1
+ module PeepingTom
2
+ class Peeper
3
+ attr_reader :sites
4
+
5
+ def initialize
6
+ @sites = {}
7
+ end
8
+
9
+ def register(name, url, opts = {})
10
+ @sites[name] = Site.new(name, url, opts)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ module PeepingTom
2
+ class Site
3
+ attr_reader :name, :timeout
4
+
5
+ def initialize(name, url, opts = {})
6
+ @name = name
7
+ @url = url
8
+ @timeout = opts[:timeout] || 10
9
+ end
10
+
11
+ def url
12
+ @url = @url + "/" unless @url =~ /\//
13
+ @url = "http://#{@url}" unless @url =~ /^http:\/\//
14
+ @url
15
+ end
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: peeping_tom
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ version: "0.1"
9
+ platform: ruby
10
+ authors:
11
+ - Terry Heath
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+
16
+ date: 2010-03-02 00:00:00 -06:00
17
+ default_executable:
18
+ dependencies: []
19
+
20
+ description: Allows for scripts that look at different applications and notify you when something looks wrong
21
+ email: theath@gmail.com
22
+ executables: []
23
+
24
+ extensions: []
25
+
26
+ extra_rdoc_files: []
27
+
28
+ files:
29
+ - lib/peeping_tom.rb
30
+ - lib/peeping_tom/dsl.rb
31
+ - lib/peeping_tom/peeper.rb
32
+ - lib/peeping_tom/site.rb
33
+ - lib/peeping_tom/notifier/email.rb
34
+ - lib/peeping_tom/notifier/irc.rb
35
+ has_rdoc: true
36
+ homepage: http://github.com/terrbear/peeping-tom
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ segments:
49
+ - 0
50
+ version: "0"
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.3.6
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: a gem that peeps on your apps
65
+ test_files: []
66
+