|
#!/usr/bin/env ruby |
|
|
|
require 'httparty' |
|
require 'json' |
|
require 'base64' |
|
require 'time' |
|
|
|
## |
|
# |
|
# Written by JB Farez ([email protected]) |
|
# Rev 0.3 |
|
# Last update : 2014/11/17 |
|
# |
|
## |
|
|
|
## Constants |
|
## -- |
|
DEFAULT_STATUS_RESP_TIME = "ok" |
|
DEFAULT_STATUS_AVAIL = "ok" |
|
|
|
## Vars |
|
## -- |
|
apiUrl = "https://io.catchpoint.com/ui/api/" |
|
apiVersion = "v1" |
|
|
|
testId = "<CATCHPOINT_TEST_ID>" |
|
clientId = "<CATCHPOINT_PULL_API_KEY>" |
|
clientSecret = "<CATCHPOINT_PULL_API_SECRET>" |
|
|
|
respWidgetId = "catchpoint-average-response" |
|
availWidgetId = "catchpoint-average-availability" |
|
|
|
graphPoints = [] |
|
|
|
## Job |
|
## -- |
|
SCHEDULER.every '180s', :first_in => 0 do |job| |
|
|
|
timeNow = Time.now.strftime('%Y-%m-%dT%H:%M').to_s |
|
timeMidnight = Time.now.strftime('%Y-%m-%dT00:00').to_s |
|
|
|
puts "#{timeNow} : INFO : Start polling average response time of webmobile from Catchpoint" |
|
|
|
# Get the access token to pull the API |
|
getTokenUri = "token" |
|
getTokenPost = "grant_type=client_credentials&client_id=#{clientId}&client_secret=#{clientSecret}" |
|
|
|
tokenResponse = HTTParty.post("#{apiUrl}#{getTokenUri}", |
|
{ |
|
:body => getTokenPost.to_s, |
|
:headers => { 'Accept' => 'application/json' } |
|
}) |
|
|
|
# Get the token himself and encode it in base64 |
|
accessToken = JSON.parse(tokenResponse.body)["access_token"] |
|
encodedToken = Base64.encode64(accessToken).gsub("\n", '') |
|
# Get the token type |
|
tokenType = JSON.parse(tokenResponse.body)["token_type"] |
|
|
|
# Get average response time and availability |
|
response = HTTParty.get("#{apiUrl}/#{apiVersion}/performance/favoriteCharts/#{testId}/data?startTime=#{timeMidnight}&endTime=#{timeNow}", |
|
{ |
|
:headers => { 'Accept' => 'application/json', "Authorization" => "#{tokenType} #{encodedToken}" } |
|
}) |
|
respCode = response.code |
|
data = response.body.to_s |
|
|
|
# Check there is a content |
|
if respCode == 200 |
|
# Average response time |
|
avgRespTime = JSON.parse(data)["summary"]["items"][0]["synthetic_metrics"][0].to_i |
|
if avgRespTime.to_i.between?(2000, 3000) |
|
respStatus = "warning" |
|
elsif avgRespTime.to_i > 3001 |
|
respStatus = "critical" |
|
else |
|
respStatus = DEFAULT_STATUS_RESP_TIME |
|
end |
|
graphPoints << { x:Time.now.to_i, y:avgRespTime } |
|
|
|
# Availability |
|
avgAvail = "%.2f" % JSON.parse(data)["summary"]["items"][0]["synthetic_metrics"][2].to_f |
|
if avgAvail.to_i.between?(98, 99) |
|
availStatus = "warning" |
|
elsif avgAvail.to_i < 98 |
|
availStatus = "critical" |
|
else |
|
availStatus = DEFAULT_STATUS_AVAIL |
|
end |
|
|
|
# If executed by dashing send the event to widget |
|
# If not, just print to stdout |
|
if defined?(send_event) |
|
send_event(respWidgetId, { points: graphPoints, status: respStatus }) |
|
send_event(availWidgetId, { value: avgAvail, status: availStatus }) |
|
else |
|
puts "Average response time : #{avgRespTime} (status : #{respStatus})" |
|
puts "Average availability : #{avgAvail} (status : #{availStatus})" |
|
puts "Queries : \n\t{ value: #{avgRespTime}, status: #{respStatus} }\n\t{ value: #{avgAvail}, status: #{availStatus} }" |
|
end |
|
|
|
puts "#{timeNow} : INFO : Polling average response time and availability of webmobile from Catchpoint is finished" |
|
|
|
else |
|
puts "#{timeNow} : FATAL : Response code = #{respCode}" |
|
puts "#{timeNow} : FATAL : Content = #{data}" |
|
end |
|
|
|
end |