class SeyrenAPI
Attributes
Public Class Methods
# File lib/seyren_api.rb, line 10 def initialize(seyren_base_url) @seyren_base_url = seyren_base_url end
Public Instance Methods
# 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
note: standard subscription of type ‘enabled on ALL days, for ALL hours i.e. 24x7’
# 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
# 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
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
{“values”:[ {“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”:[{“id”:“520ac8d1e4b0ab4e71f3c3f6”, “target”:“me@techco.com”,“type”:“EMAIL”, “su”:true,“mo”:true,“tu”:true,“we”:true,“th”:true,“fr”:true,“sa”:true, “fromTime”:“0000”,“toTime”:“2359”,“enabled”:true}]}, {“id”:“520bfe82e4b0ab4e71f3c403”,
...<snip>similar as above</snip>...,"enabled":true}]}],
“items”:0, “start”:0, “total”:2}
# 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
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
{“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”:[{“id”:“520ac8d1e4b0ab4e71f3c3f6”, “target”:“me@techco.com”,“type”:“EMAIL”, “su”:true,“mo”:true,“tu”:true,“we”:true,“th”:true,“fr”:true,“sa”:true, “fromTime”:“0000”,“toTime”:“2359”,“enabled”:true}]}
# 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