This repository has been archived by the owner on Apr 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
newrelic_wikipedia_agent
executable file
·85 lines (68 loc) · 2.24 KB
/
newrelic_wikipedia_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env ruby
require "rubygems"
require "bundler/setup"
require "newrelic_plugin"
require "net/http"
#
#
# The entire agent should be enclosed in a "WikipediaAgent" module
#
#
module WikipediaAgent
#
# Agent, Metric and PollCycle classes
#
# Each agent module must have an Agent, Metric and PollCycle class that inherits from their
# Component counterparts as you can see below.
#
class Agent < NewRelic::Plugin::Agent::Base
agent_guid "com.newrelic.examples.wikipedia"
agent_version "1.0.4"
#
# Each agent class must also include agent_human_labels. agent_human_labels requires:
# A friendly name of your component to appear in graphs.
# A block that returns a friendly name for this instance of your component.
#
# The block runs in the context of the agent instance.
#
agent_human_labels("Wikipedia") { "Wikipedia #{hostname}" }
#
# agent_config is a list of variables that the component will need
# from its instances. The Wikipedia agent is asking for the
# hostname to get the information from which it uses when it asks for the data.
#
agent_config_options :hostname
def setup_metrics
@WIKIPEDIA_STAT_URL = URI.parse("http://#{hostname}/w/api.php?action=query&format=json&meta=siteinfo&siprop=statistics")
@article_creation_rate = NewRelic::Processor::EpochCounter.new
end
def poll_cycle
num_articles = get_num_articles
if num_articles
report_metric("Articles/Created", "articles/sec", @article_creation_rate.process(num_articles))
report_metric("Articles/Count", "articles", num_articles)
end
end
private
def get_num_articles
begin
resp = ::Net::HTTP.get_response(@WIKIPEDIA_STAT_URL)
rescue
return nil
end
data = resp.body
begin
result = JSON.parse(data) # if server unavailable, then data may be html, not json
rescue
return nil
end
puts "returning #{result['query']['statistics']['articles']} total articles"
result['query']['statistics']['articles']
end
end
NewRelic::Plugin::Setup.install_agent :wikipedia,WikipediaAgent
#
# Launch the agent (never returns)
#
NewRelic::Plugin::Run.setup_and_run
end