forked from lozzd/cdn_scripts
/
edgecast_to_graphite.rb
executable file
·97 lines (75 loc) · 2.72 KB
/
edgecast_to_graphite.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env ruby
# Pulls stats from the Edgecast reporting API and submits them to Graphite
# This outputs keys ready for graphite. Use it something like:
#
require 'net/http'
require 'json'
require 'pp'
# Configuration:
# Enter your customer ID in the URL below:
BASE_URI = 'https://api.edgecast.com/v2/realtimestats/customers/MYID/media/'
# Enter your API token for authentication
TOKEN = 'abcdefgh-abcd-1234-abcd-1234567890ab'
GRAPHITE_HOST = 'graphite.yourcompany.com'
GRAPHITE_PORT = '2003'
GRAPHITE_PREFIX = 'cdn.edgecast'
# Pulling stats for both the http_small and ADN platforms.
# Unfortunately the stats aren't more fine grained than tihs. If you have
# more than one 'service' using the platform(s), you'll get them added together.
# Remove or add platforms as applicable below if you do or don't use them.
platforms = { 'http_small' => '8', 'adn' => '14' }
timestamp = Time.now.to_i
headers = {
'Authorization' => "tok:#{TOKEN}",
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Host' => 'api.edgecast.com',
}
uri = URI(BASE_URI)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
def send_graphite(key, value, timestamp)
begin
socket = TCPSocket.open(GRAPHITE_HOST, GRAPHITE_PORT)
message = "#{key} #{value} #{timestamp}\n"
puts "Sending graphite data: #{message}"
socket.write(message)
rescue Exception => e
puts 'Graphite was unable to process the request.'
puts e.to_s
ensure
socket.close unless socket.nil?
end
end
platforms.each do |platform,id|
# Bandwidth
resp = http.get("#{uri.path}/#{id}/bandwidth", headers)
values = JSON.parse(resp.body)
if values.has_key?("Result")
bps = values["Result"]
else
bps = 0
end
send_graphite("#{GRAPHITE_PREFIX}.#{platform}.bandwidth", bps, timestamp)
# Connections
resp = http.get("#{uri.path}/#{id}/connections", headers)
values = JSON.parse(resp.body)
if values.has_key?("Result")
conns = values["Result"].to_f.ceil
else
conns = 0
end
send_graphite("#{GRAPHITE_PREFIX}.#{platform}.connections", conns, timestamp)
# Cache Status
resp = http.get("#{uri.path}/#{id}/cachestatus", headers)
values = JSON.parse(resp.body)
values.each do |status|
send_graphite("#{GRAPHITE_PREFIX}.#{platform}.cachestatus.#{status["CacheStatus"]}", status["Connections"], timestamp)
end
# Status Codes
resp = http.get("#{uri.path}/#{id}/statuscode", headers)
values = JSON.parse(resp.body)
values.each do |status|
send_graphite("#{GRAPHITE_PREFIX}.#{platform}.statuscode.#{status["StatusCode"]}", status["Connections"], timestamp)
end
end