Class: SeyrenAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/seyren_api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(seyren_base_url) ⇒ SeyrenAPI

Returns a new instance of SeyrenAPI.



10
11
12
# File 'lib/seyren_api.rb', line 10

def initialize(seyren_base_url)
  @seyren_base_url = seyren_base_url
end

Instance Attribute Details

#seyren_base_urlObject

Returns the value of attribute seyren_base_url.



7
8
9
# File 'lib/seyren_api.rb', line 7

def seyren_base_url
  @seyren_base_url
end

Instance Method Details

#create_seyren_check(check_name, check_graphite_target, check_warn, check_error) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/seyren_api.rb', line 31

def create_seyren_check(check_name, check_graphite_target, check_warn, check_error)
  create_check = {
                  'enabled' => true,
                  'name'    => check_name,
                  'target'  => check_graphite_target,
                  'warn'    => check_warn.to_s,
                  'error'   => check_error.to_s
                 }

  json = create_check.to_json

  cmdout = `curl -si -X POST -d '#{json}' '#{@seyren_base_url}' --header 'Content-Type:application/json'`
  puts cmdout

  http_response_status_code = cmdout[/HTTP\/1\.1 (\d+)/,1]

  seyren_check_id = nil
  if http_response_status_code == '201'
    # e.g. 5192a13ee4b0fe0c215fa33e
    seyren_check_id = cmdout[/\/seyren\/api\/checks\/([0-9a-z]+)/,1]
  end

  return http_response_status_code, seyren_check_id
end

#create_standard_subscription_for_seyren_check(check_id, check_subscriber_email_address) ⇒ Object

note: standard subscription of type ‘enabled on ALL days, for ALL hours i.e. 24x7’



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/seyren_api.rb', line 58

def create_standard_subscription_for_seyren_check(check_id, check_subscriber_email_address)
  create_standard_subscription = {
                                  'enabled'  => true,
                                  'mo'       => true,
                                  'tu'       => true,
                                  'we'       => true,
                                  'th'       => true,
                                  'fr'       => true,
                                  'sa'       => true,
                                  'su'       => true,
                                  'fromTime' => '0000',
                                  'toTime'   => '2359',
                                  'target'   => check_subscriber_email_address,
                                  'type'     => 'EMAIL'
                                 }

  json = create_standard_subscription.to_json
  subscription_url = @seyren_base_url + '/' + check_id + '/subscriptions'

  cmdout = `curl -si -X POST -d '#{json}' '#{subscription_url}' --header 'Content-Type:application/json'`
  puts cmdout

  http_response_status_code = cmdout[/HTTP\/1\.1 (\d+)/,1]

  return http_response_status_code
end

#delete_seyren_check(check_id) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/seyren_api.rb', line 15

def delete_seyren_check(check_id)
  url = @seyren_base_url + '/' + check_id
  # Caution: "Brittle Code in all functions"
  # any single char change in output from seyren or possibly even
  # curl ver update will mess up the code in this API class.
  # TODO: Preferably switch from curl to any appropriate Ruby lib
  # that can help with the http call + parsing of results/codes/json response.
  cmdout = `curl -si -X DELETE #{url}`
  puts cmdout

  http_response_status_code = cmdout[/HTTP\/1\.1 (\d+)/,1]

  return http_response_status_code
end

#fetch_all_seyren_checksObject

sample output of curl command (incl response header)

HTTP/1.1 200 OK Date: Wed, 14 Aug 2013 22:08:00 GMT Server: Apache-Coyote/1.1 Content-Type: application/json Via: 1.1 seyren.staging.techco.com Transfer-Encoding: chunked

{“id”:“520ac8d1e4b0ab4e71f3c3f5”, “name”:“staging ui ui-staging ip-10-176-197-226_us-west-1_compute_internal cpu”, “target”:“averageSeries(groupByNode(collectd.ui.ip-10-176-197-226_us-west-1_compute_internal.*.cpu- {user,system,wait,nice,steal,softirq,interrupt,3,\"sumSeries\"))”,“warn”:“75”,“error”:“90”, “enabled”:true,“state”:“OK”,“subscriptions”:[“target”:“[email protected]”,“type”:“EMAIL”, “su”:true,“mo”:true,“tu”:true,“we”:true,“th”:true,“fr”:true,“sa”:true, “fromTime”:“0000”,“toTime”:“2359”,“enabled”:true]},

...<snip>similar as above</snip>...,"enabled":true]}],

“items”:0, “start”:0, “total”:2}



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/seyren_api.rb', line 110

def fetch_all_seyren_checks()
  cmdout = `curl -si '#{@seyren_base_url}'`
  puts cmdout

  json = cmdout[/^(\{\"values\":.*)/,1]
  http_response_status_code = cmdout[/HTTP\/1\.1 (\d+)/,1]

  seyren_id_to_check_hash = Hash.new
  if http_response_status_code == '200'
    parsed_checks = JSON.parse(json)
    parsed_checks['values'].each do |check|
      seyren_id_to_check_hash[check['id']] = check
    end
  end

  return http_response_status_code, seyren_id_to_check_hash
end

#fetch_seyren_check(check_id) ⇒ Object

sample output of curl command (incl response header)

HTTP/1.1 200 OK Date: Thu, 15 Aug 2013 01:55:48 GMT Server: Apache-Coyote/1.1 Content-Type: application/json Via: 1.1 seyren.staging.techco.com Transfer-Encoding: chunked

“name”:“staging ui ui-staging ip-10-176-197-226_us-west-1_compute_internal cpu”, “target”:“averageSeries(groupByNode(collectd.ui.ip-10-176-197-226_us-west-1_compute_internal.*.cpu- {user,system,wait,nice,steal,softirq,interrupt,3,\"sumSeries\"))”,“warn”:“75”,“error”:“90”, “enabled”:true,“state”:“OK”,“subscriptions”:[“target”:“[email protected]”,“type”:“EMAIL”, “su”:true,“mo”:true,“tu”:true,“we”:true,“th”:true,“fr”:true,“sa”:true, “fromTime”:“0000”,“toTime”:“2359”,“enabled”:true]}



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/seyren_api.rb', line 147

def fetch_seyren_check(check_id)
  url = @seyren_base_url + '/' + check_id

  cmdout = `curl -si '#{url}'`
  puts cmdout

  json = cmdout[/^(\{\"id\":.*)/,1]
  http_response_status_code = cmdout[/HTTP\/1\.1 (\d+)/,1]

  seyren_check_hash = Hash.new
  if http_response_status_code == '200'
    seyren_check_hash = JSON.parse(json)
  end

  return http_response_status_code, seyren_check_hash
end