dogeventer 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,131 @@
1
+ module DogEventer
2
+ class EventEmitter
3
+ attr_reader :events
4
+
5
+ def wait(seconds)
6
+ @start_time += seconds
7
+ end
8
+ end
9
+
10
+ class NagiosCheck < EventEmitter
11
+ attr_reader :host
12
+
13
+ def initialize(check_name, host, start_time)
14
+ @check_name = check_name
15
+ @host = host
16
+ @start_time = start_time
17
+ @events = []
18
+ end
19
+
20
+ def msg_title(alert_state)
21
+ "#{@check_name} is #{alert_state.to_s} on #{@host}"
22
+ end
23
+
24
+ def event_object()
25
+ @check_name
26
+ end
27
+
28
+ def event_type()
29
+ "monitoring.alert"
30
+ end
31
+
32
+ def alert_type(state)
33
+ {
34
+ :critical => :error,
35
+ :ok => :success,
36
+ :warning => :warning,
37
+ :warn => :warn
38
+ }[state]
39
+ end
40
+
41
+ def date_happened(date=nil)
42
+ (date || @start_time).to_i
43
+ end
44
+
45
+ def source_type_name()
46
+ "Nagios"
47
+ end
48
+
49
+ def to_event(alert_state, date=nil)
50
+ @events << {
51
+ :event_object => event_object,
52
+ :event_type => event_type,
53
+ :alert_type => alert_type(alert_state),
54
+ :date_happened => date_happened(date),
55
+ :msg_title => msg_title(alert_state),
56
+ :source_type_name => source_type_name,
57
+ :host => host
58
+ }
59
+ end
60
+
61
+ def critical(date=nil)
62
+ to_event :critical
63
+ end
64
+
65
+ def warning(date=nil)
66
+ to_event :warning
67
+ end
68
+
69
+ def ok(date=nil)
70
+ to_event :ok
71
+ end
72
+ end
73
+
74
+ class PingdomCheck < EventEmitter
75
+ attr_reader :url
76
+
77
+ def initialize(check_name, url, start_time)
78
+ @check_name = check_name
79
+ @url = url
80
+ @start_time = start_time
81
+ @events = []
82
+ end
83
+
84
+ def msg_title(alert_state)
85
+ "#{@check_name} is #{alert_state.to_s}"
86
+ end
87
+
88
+ def event_object()
89
+ @check_name
90
+ end
91
+
92
+ def event_type()
93
+ "monitoring.alert"
94
+ end
95
+
96
+ def alert_type(state)
97
+ {
98
+ :down => :error,
99
+ :up => :success
100
+ }[state]
101
+ end
102
+
103
+ def date_happened(date=nil)
104
+ (date || @start_time).to_i
105
+ end
106
+
107
+ def source_type_name()
108
+ "Pingdom"
109
+ end
110
+
111
+ def to_event(alert_state, date=nil)
112
+ @events << {
113
+ :event_object => event_object,
114
+ :event_type => event_type,
115
+ :alert_type => alert_type(alert_state),
116
+ :date_happened => date_happened(date),
117
+ :msg_title => msg_title(alert_state),
118
+ :source_type_name => source_type_name,
119
+ :host => "Pingdom:#{@check_name}"
120
+ }
121
+ end
122
+
123
+ def down(date=nil)
124
+ to_event :down
125
+ end
126
+ def up(date=nil)
127
+ to_event :up
128
+ end
129
+
130
+ end
131
+ end
@@ -0,0 +1,36 @@
1
+ module DogEventer
2
+ class Scope
3
+ attr_reader :events
4
+ def initialize(start_time, events=nil)
5
+ @start_time = start_time
6
+ @events = events || []
7
+ end
8
+ end
9
+
10
+ class Host < Scope
11
+ def initialize(start_time, host_name)
12
+ super start_time
13
+ @host_name = host_name
14
+ end
15
+
16
+ def nagios_check(check_name, &block)
17
+ n = NagiosCheck.new(check_name, @host_name, @start_time)
18
+ n.instance_eval &block
19
+ @events += n.events
20
+ end
21
+ end
22
+
23
+ class Url < Scope
24
+ def initialize(start_time, url)
25
+ super start_time
26
+ @url = url
27
+ end
28
+
29
+ def pingdom_check(check_name, &block)
30
+ p = PingdomCheck.new(check_name, @url, @start_time)
31
+ p.instance_eval &block
32
+ @events += p.events
33
+ end
34
+ end
35
+ end
36
+
data/lib/dogeventer.rb ADDED
@@ -0,0 +1,91 @@
1
+ require 'dogapi'
2
+ require 'dogeventer/scope'
3
+ require 'dogeventer/event_emitter'
4
+
5
+ module DogEventer
6
+ class DogEventer
7
+ def initialize(api_key, application_key=nil)
8
+ @dog = Dogapi::Client.new(api_key, application_key)
9
+ @events = []
10
+ end
11
+
12
+ def generate(start_time, &block)
13
+ @start_time = start_time
14
+ instance_eval &block
15
+
16
+ @events.each do |event|
17
+ puts event.inspect
18
+ @dog.emit_event(Dogapi::Event.new('', event), :host => event[:host])
19
+ end
20
+ end
21
+
22
+ def host(host_name, &block)
23
+ h = Host.new(@start_time, host_name)
24
+ @events += (h.instance_eval &block)
25
+ end
26
+
27
+ def url(url, &block)
28
+ u = Url.new(@start_time, url)
29
+ @events += (u.instance_eval &block)
30
+ end
31
+
32
+ end
33
+ end
34
+
35
+ def dogeventer(start_time=nil, api_key=nil, application_key=nil, &block)
36
+ require 'optparse'
37
+
38
+ all_config = {
39
+ :default => {:start_time => Time.now.to_i - 60*60},
40
+ :cli => {},
41
+ :file => {}
42
+ }
43
+ config_file = "~/.dogrc"
44
+
45
+ optparse = OptionParser.new do |opts|
46
+ opts.banner = "Usage: ruby #{$0} [options]"
47
+ opts.on("-t", "--time TIME", Float, "Start time in seconds since unix epoch. Defaults to the current time minus an hour.") { |v|
48
+ all_config[:cli][:start_time] = start_time
49
+ }
50
+ opts.on("-c", "--config CONFIG_FILE", "Path to config file. Defaults to #{config_file}") { |v|
51
+ config_file = v
52
+ }
53
+ opts.on("--api_key API_KEY", "Api key. Overrides config values.") { |v|
54
+ all_config[:cli][:api_key] = v
55
+ }
56
+ opts.on("--app_key [APP_KEY]", "App key. Overrides config values.") { |v|
57
+ all_config[:cli][:app_key] = v
58
+ }
59
+
60
+ end
61
+ optparse.parse!
62
+
63
+ if not config_file
64
+ config_file = default_config_file
65
+ end
66
+
67
+ config_file = File.expand_path config_file
68
+ if File.exists? config_file
69
+ File.open(config_file) do |f|
70
+ f.readlines.each do |line|
71
+ keyval = line.split "="
72
+ if keyval.length == 2
73
+ key = keyval[0].strip
74
+ val = keyval[1].strip
75
+ if key == 'apikey'
76
+ all_config[:file][:api_key] = val
77
+ elsif key == 'appkey'
78
+ all_config[:file][:app_key] = val
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
84
+
85
+ config = all_config.reduce({}) do |merged, to_merge|
86
+ merged.merge to_merge[1]
87
+ end
88
+
89
+ DogEventer::DogEventer.new(config[:api_key], config[:app_key]).generate config[:start_time], &block
90
+ end
91
+
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dogeventer
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Datadog, Inc.
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-12-02 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: dogapi
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 19
29
+ segments:
30
+ - 1
31
+ - 2
32
+ - 6
33
+ version: 1.2.6
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ description: A DSL for generating Datadog events
37
+ email: packages@datadoghq.com
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ files:
45
+ - lib/dogeventer/event_emitter.rb
46
+ - lib/dogeventer/scope.rb
47
+ - lib/dogeventer.rb
48
+ homepage: http://datadoghq.com/
49
+ licenses:
50
+ - BSD
51
+ post_install_message:
52
+ rdoc_options: []
53
+
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ requirements: []
75
+
76
+ rubyforge_project:
77
+ rubygems_version: 1.8.11
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: A DSL for generating Datadog events
81
+ test_files: []
82
+