seyren_api 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/seyren_api.rb +164 -0
  2. metadata +65 -0
@@ -0,0 +1,164 @@
1
+ # Seyren API module
2
+ require 'json'
3
+
4
+ class SeyrenAPI
5
+
6
+
7
+ attr_accessor :seyren_base_url
8
+
9
+
10
+ def initialize(seyren_base_url)
11
+ @seyren_base_url = seyren_base_url
12
+ end
13
+
14
+
15
+ def delete_seyren_check(check_id)
16
+ url = @seyren_base_url + '/' + check_id
17
+ # Caution: "Brittle Code in all functions"
18
+ # any single char change in output from seyren or possibly even
19
+ # curl ver update will mess up the code in this API class.
20
+ # TODO: Preferably switch from curl to any appropriate Ruby lib
21
+ # that can help with the http call + parsing of results/codes/json response.
22
+ cmdout = `curl -si -X DELETE #{url}`
23
+ puts cmdout
24
+
25
+ http_response_status_code = cmdout[/HTTP\/1\.1 (\d+)/,1]
26
+
27
+ return http_response_status_code
28
+ end
29
+
30
+
31
+ def create_seyren_check(check_name, check_graphite_target, check_warn, check_error)
32
+ create_check = {
33
+ 'enabled' => true,
34
+ 'name' => check_name,
35
+ 'target' => check_graphite_target,
36
+ 'warn' => check_warn.to_s,
37
+ 'error' => check_error.to_s
38
+ }
39
+
40
+ json = create_check.to_json
41
+
42
+ cmdout = `curl -si -X POST -d '#{json}' '#{@seyren_base_url}' --header 'Content-Type:application/json'`
43
+ puts cmdout
44
+
45
+ http_response_status_code = cmdout[/HTTP\/1\.1 (\d+)/,1]
46
+
47
+ seyren_check_id = nil
48
+ if http_response_status_code == '201'
49
+ # e.g. 5192a13ee4b0fe0c215fa33e
50
+ seyren_check_id = cmdout[/\/seyren\/api\/checks\/([0-9a-z]+)/,1]
51
+ end
52
+
53
+ return http_response_status_code, seyren_check_id
54
+ end
55
+
56
+
57
+ #note: standard subscription of type 'enabled on ALL days, for ALL hours i.e. 24x7'
58
+ def create_standard_subscription_for_seyren_check(check_id, check_subscriber_email_address)
59
+ create_standard_subscription = {
60
+ 'enabled' => true,
61
+ 'mo' => true,
62
+ 'tu' => true,
63
+ 'we' => true,
64
+ 'th' => true,
65
+ 'fr' => true,
66
+ 'sa' => true,
67
+ 'su' => true,
68
+ 'fromTime' => '0000',
69
+ 'toTime' => '2359',
70
+ 'target' => check_subscriber_email_address,
71
+ 'type' => 'EMAIL'
72
+ }
73
+
74
+ json = create_standard_subscription.to_json
75
+ subscription_url = @seyren_base_url + '/' + check_id + '/subscriptions'
76
+
77
+ cmdout = `curl -si -X POST -d '#{json}' '#{subscription_url}' --header 'Content-Type:application/json'`
78
+ puts cmdout
79
+
80
+ http_response_status_code = cmdout[/HTTP\/1\.1 (\d+)/,1]
81
+
82
+ return http_response_status_code
83
+ end
84
+
85
+
86
+ # sample output of curl command (incl response header)
87
+ #
88
+ # HTTP/1.1 200 OK
89
+ # Date: Wed, 14 Aug 2013 22:08:00 GMT
90
+ # Server: Apache-Coyote/1.1
91
+ # Content-Type: application/json
92
+ # Via: 1.1 seyren.staging.techco.com
93
+ # Transfer-Encoding: chunked
94
+ #
95
+ # {"values":[
96
+ # {"id":"520ac8d1e4b0ab4e71f3c3f5",
97
+ # "name":"staging ui ui-staging ip-10-176-197-226_us-west-1_compute_internal cpu",
98
+ # "target":"averageSeries(groupByNode(collectd.ui.ip-10-176-197-226_us-west-1_compute_internal.*.cpu-
99
+ # {user,system,wait,nice,steal,softirq,interrupt},3,\\\"sumSeries\\\"))","warn":"75","error":"90",
100
+ # "enabled":true,"state":"OK","subscriptions":[{"id":"520ac8d1e4b0ab4e71f3c3f6",
101
+ # "target":"me@techco.com","type":"EMAIL",
102
+ # "su":true,"mo":true,"tu":true,"we":true,"th":true,"fr":true,"sa":true,
103
+ # "fromTime":"0000","toTime":"2359","enabled":true}]},
104
+ # {"id":"520bfe82e4b0ab4e71f3c403",
105
+ # ...<snip>similar as above</snip>...,"enabled":true}]}],
106
+ # "items":0,
107
+ # "start":0,
108
+ # "total":2}
109
+ #
110
+ def fetch_all_seyren_checks()
111
+ cmdout = `curl -si '#{@seyren_base_url}'`
112
+ puts cmdout
113
+
114
+ json = cmdout[/^(\{\"values\":.*)/,1]
115
+ http_response_status_code = cmdout[/HTTP\/1\.1 (\d+)/,1]
116
+
117
+ seyren_id_to_check_hash = Hash.new
118
+ if http_response_status_code == '200'
119
+ parsed_checks = JSON.parse(json)
120
+ parsed_checks['values'].each do |check|
121
+ seyren_id_to_check_hash[check['id']] = check
122
+ end
123
+ end
124
+
125
+ return http_response_status_code, seyren_id_to_check_hash
126
+ end
127
+
128
+
129
+ # sample output of curl command (incl response header)
130
+ #
131
+ # HTTP/1.1 200 OK
132
+ # Date: Thu, 15 Aug 2013 01:55:48 GMT
133
+ # Server: Apache-Coyote/1.1
134
+ # Content-Type: application/json
135
+ # Via: 1.1 seyren.staging.techco.com
136
+ # Transfer-Encoding: chunked
137
+ #
138
+ # {"id":"520ac8d1e4b0ab4e71f3c3f5",
139
+ # "name":"staging ui ui-staging ip-10-176-197-226_us-west-1_compute_internal cpu",
140
+ # "target":"averageSeries(groupByNode(collectd.ui.ip-10-176-197-226_us-west-1_compute_internal.*.cpu-
141
+ # {user,system,wait,nice,steal,softirq,interrupt},3,\\\"sumSeries\\\"))","warn":"75","error":"90",
142
+ # "enabled":true,"state":"OK","subscriptions":[{"id":"520ac8d1e4b0ab4e71f3c3f6",
143
+ # "target":"me@techco.com","type":"EMAIL",
144
+ # "su":true,"mo":true,"tu":true,"we":true,"th":true,"fr":true,"sa":true,
145
+ # "fromTime":"0000","toTime":"2359","enabled":true}]}
146
+ #
147
+ def fetch_seyren_check(check_id)
148
+ url = @seyren_base_url + '/' + check_id
149
+
150
+ cmdout = `curl -si '#{url}'`
151
+ puts cmdout
152
+
153
+ json = cmdout[/^(\{\"id\":.*)/,1]
154
+ http_response_status_code = cmdout[/HTTP\/1\.1 (\d+)/,1]
155
+
156
+ seyren_check_hash = Hash.new
157
+ if http_response_status_code == '200'
158
+ seyren_check_hash = JSON.parse(json)
159
+ end
160
+
161
+ return http_response_status_code, seyren_check_hash
162
+ end
163
+
164
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: seyren_api
3
+ version: !ruby/object:Gem::Version
4
+ hash: 21
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 1
10
+ version: 1.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Ajay Mamtura
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2013-09-25 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: A gem to create/delete/fetch etc seyren checks. Tested with https://github.com/scobal/seyren.git:23983ce38e98ba3cb89c1dcacc3f2bd42db553db
22
+ email: ajay_pm@hotmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - lib/seyren_api.rb
31
+ homepage: http://rubygems.org/gems/seyren_api
32
+ licenses:
33
+ - MIT
34
+ post_install_message:
35
+ rdoc_options: []
36
+
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ hash: 3
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.8.10
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: Seyren API
64
+ test_files: []
65
+