forked from AdStage/newrelic-s3-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
newrelic_s3_agent
executable file
·88 lines (74 loc) · 2.22 KB
/
newrelic_s3_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
86
87
88
#! /usr/bin/env ruby
require "rubygems"
require "bundler/setup"
require "newrelic_plugin"
require "aws-sdk-core"
module S3Agent
class Agent < NewRelic::Plugin::Agent::Base
agent_guid "com.hightail.newrelic-s3-plugin"
agent_version "1.0.0"
agent_config_options :region, :bucket_name, :storage_type, :access_key_id, :secret_access_key
agent_human_labels("S3") { bucket_name }
def poll_cycle
period = 60 * 60 * 24
cloudwatch_delay = 60
time_offset = cloudwatch_delay
start_time = (Time.now.utc - (time_offset + period))
end_time = (Time.now.utc - time_offset)
cw = Aws::CloudWatch::Client.new(
credentials: Aws::Credentials.new(access_key_id, secret_access_key),
region: region)
size = cw.get_metric_statistics({
namespace: "AWS/S3",
metric_name: "BucketSizeBytes",
dimensions: [
{
name: "BucketName",
value: bucket_name,
},
{
name: "StorageType",
value: storage_type
}
],
start_time: start_time.iso8601,
end_time: end_time.iso8601,
period: period,
statistics: ["Average"],
unit: "Bytes"
})
count = cw.get_metric_statistics({
namespace: "AWS/S3",
metric_name: "NumberOfObjects",
dimensions: [
{
name: "BucketName",
value: bucket_name,
},
{
name: "StorageType",
value: "AllStorageTypes"
}
],
start_time: start_time.iso8601,
end_time: end_time.iso8601,
period: period,
statistics: ["Average"],
unit: "Count"
})
report_metric "ObjectCount", "Objects", count.datapoints[0].average
report_metric "Size", "Bytes", size.datapoints[0].average
end
end
#
# Register this agent with the component.
# The ExampleAgent is the name of the module that defines this
# driver (the module must contain at least three classes - a
# PollCycle, a Metric and an Agent class, as defined above).
#
NewRelic::Plugin::Setup.install_agent :s3, S3Agent
#
# Launch the agent; this never returns.
#
NewRelic::Plugin::Run.setup_and_run
end