This repository has been archived by the owner on Jun 16, 2022. It is now read-only.
forked from devonharvey/newrelic_booking_rate_plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
booking_rate_agent
executable file
·66 lines (51 loc) · 1.81 KB
/
booking_rate_agent
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
#! /usr/bin/env ruby
# New Relic plugin to monitor FareHarbor booking rate.
require "rubygems"
require "bundler/setup"
require "newrelic_plugin"
require "pg"
module BookingRateAgent
class Agent < NewRelic::Plugin::Agent::Base
agent_guid "com.fareharbor.booking_rate"
agent_version "1.0.1"
agent_config_options :databases, :environment, :metrics
agent_human_labels("Booking Rate Agent") { "#{environment}" }
def poll_cycle
connections = {}
for db_name in databases.keys do
db = databases[db_name]
connections[db_name] = PGconn.open(
:host => db["host"],
:port => db["port"],
:dbname => db["database"],
:user => db["username"],
:password => db["password"],
)
end
for metric in metrics do
metric_total = 0
connection = connections[metric.fetch('database', 'replica')]
response = connection.exec(metric['query'])
# This assumes the query uses 1 aggregate and groups by 0 or 1 values.
if response.nfields == 1 and response.ntuples == 1
metric_total = response.getvalue(0, 0).to_i
else
for i in 0..response.ntuples - 1
aggregate_value = response.getvalue(i, 0).to_i
group = response.getvalue(i, 1)
metric_total += aggregate_value
report_metric "BookingRate/#{metric['name']}_#{group}", "Value", aggregate_value
end
end
report_metric "BookingRate/#{metric['name']}_total", "Value", metric_total
end
connections.each do |_, conn|
conn.close()
end
end
end
# Register this agent with the component.
NewRelic::Plugin::Setup.install_agent :booking_rate, BookingRateAgent
# Launch the agent; this never returns.
NewRelic::Plugin::Run.setup_and_run
end