Skip to content

Instantly share code, notes, and snippets.

@jbfarez
Last active March 10, 2020 15:56
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbfarez/f94cc23a6e8c7f1f1328 to your computer and use it in GitHub Desktop.
Save jbfarez/f94cc23a6e8c7f1f1328 to your computer and use it in GitHub Desktop.
Dashing Catchpoint® widget

Preview

Screenshot

Description

Display your website availability / performances from Catchpoint® SaaS service.

This using the Catchpoint® Pull API.

This gather data for the current day (from midnight to now).

Requirements

Usage

Add this to your erb dashboard file :

<!-- Catchpoint response time -->
<li data-row="1" data-col="2" data-sizex="6" data-sizey="3">
  <div data-id="catchpoint-average-response" style="background: #555b7f;" data-title="Average response time" data-view="Rickshawgraph" data-renderer="area" data-stroke="true" data-colors="#898eab" data-raw-number="true" data-suffix=" ms"></div>
</li>

<!-- Catchpoint availability -->
<li data-row="1" data-col="3" data-sizex="3" data-sizey="3">
  <div data-id="catchpoint-average-availability" data-title="Availability" data-view="Meter" data-min="0" data-max="100" data-moreinfo="in % for today"></div>
</li>

Settings

Catchpoint ®

Log in to the Catchpoint® portal to configure the API

CatchpointMenu

Then set up the Pull API

CatchpointPullAPI

Job

There is only 3 mandatory settings :

testId = "<CATCHPOINT_TEST_ID>"
clientId = "<CATCHPOINT_PULL_API_KEY>"
clientSecret = "<CATCHPOINT_PULL_API_SECRET>"

You can customize as you wish the widgetId, but it must match with the data-id.

respWidgetId = "my-custom-catchpoint-average-response"
availWidgetId = "my-custom-catchpoint-average-availability"

NB :

This is for use with Catchpoint® Favorites charts so the testId must be the favorite chart id.

More info about this API call here

#!/usr/bin/env ruby
require 'httparty'
require 'json'
require 'base64'
require 'time'
##
#
# Written by JB Farez (jbfarez@viadeoteam.com)
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment