-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVTMAgent.cs
221 lines (195 loc) · 10.2 KB
/
VTMAgent.cs
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
using System;
using System.Collections.Generic;
using NewRelic.Platform.Sdk;
using NewRelic.Platform.Sdk.Utils;
using NewRelic.Platform.Sdk.Processors;
using System.Reflection;
namespace Org.BeyondComputing.NewRelic.Brocade.VTM
{
class VTMAgent : Agent
{
public override string Guid
{
get
{
return "org.beyondcomputing.newrelic.brocade.vtm";
}
}
public override string Version
{
get
{
return Assembly.GetEntryAssembly().GetName().Version.ToString();
}
}
private string name;
private VTM VTM;
private decimal APIVersion;
// Create Dictionary of EpochProcessors to track rate over time for unknown number of items
private Dictionary<string,IProcessor> processors = new Dictionary<string,IProcessor>();
private Logger log = Logger.GetLogger(typeof(VTMAgent).Name);
public VTMAgent(string name, string host, int port, string username, string password, decimal APIVersion)
{
this.name = name;
this.VTM = new VTM(host, port, username, password);
this.APIVersion = APIVersion;
}
/// <summary>
/// Returns a human-readable string to differentiate different hosts/entities in the New Relic UI
/// </summary>
public override string GetAgentName()
{
return this.name;
}
/// <summary>
// This is where logic for fetching and reporting metrics should exist.
// Call off to a REST head, SQL DB, virtually anything you can programmatically
// get metrics from and then call ReportMetric.
/// </summary>
public override void PollCycle()
{
PollGlobal();
PollVirtualServer();
PollNodes();
PollPool();
}
private void PollGlobal()
{
try
{
// Setup new EpochProcessors for Global Stats
// Check for existance of first Dictionary Item for the Global Stats
if (!processors.ContainsKey("global_bytes_in"))
{
processors.Add("global_bytes_in", new EpochProcessor());
processors.Add("global_bytes_out", new EpochProcessor());
}
// Get Global Statistics and report to New Relic
GlobalStatistics globalStats = VTM.fetchVTMObject<GlobalStatObject>($"/api/tm/{APIVersion}/status/local_tm/statistics/globals").statistics;
// Calculate System Memory Percent Used
float systemMemoryPercentUsed = (((float)globalStats.sys_mem_in_use) / (globalStats.sys_mem_total))*100;
ReportMetric("global/throughput/Received", "mebibits/second", processors["global_bytes_in"].Process(globalStats.total_bytes_in) / 131072);
ReportMetric("global/throughput/Transmitted", "mebibits/second", processors["global_bytes_out"].Process(globalStats.total_bytes_out) / 131072);
ReportMetric("global/current_conn", "connections", globalStats.total_current_conn);
ReportMetric("global/sys_mem_used", "percent",systemMemoryPercentUsed);
ReportMetric("global/sys_cpu_busy_percent", "percent", globalStats.sys_cpu_busy_percent);
// Get Global Status API v3.7+
if (APIVersion >= 3.7m)
{
dynamic globalStatus = VTM.fetchVTMObject<dynamic>($"/api/tm/{APIVersion}/status/local_tm/state");
ReportMetric("global/errors", "count", globalStatus.state.errors.Count);
ReportMetric("global/failedNodes", "count", globalStatus.state.failed_nodes.Count);
}
}
catch(Exception e)
{
log.Error("Unable to fetch Global information from the Virtual Traffic Manager '{0}'", this.name);
log.Error("Exception Thrown:'{0}'", e.Message);
}
}
private void PollPool()
{
try
{
Children pools = VTM.fetchVTMObject<Children>($"/api/tm/{APIVersion}/status/local_tm/statistics/pools");
dynamic failedNodes = null;
// Get Global Status API v3.7+
if (APIVersion >= 3.7m)
{
failedNodes = VTM.fetchVTMObject<dynamic>($"/api/tm/{APIVersion}/status/local_tm/state").state.failed_nodes;
}
foreach (var pool in pools.children)
{
// Setup new EpochProcessors for each Pool
// Check for existance of first Dictionary Item for the Pool
if (!processors.ContainsKey("pool_" + pool.name + "_bytes_in"))
{
processors.Add("pool_" + pool.name + "_bytes_in", new EpochProcessor());
processors.Add("pool_" + pool.name + "_bytes_out", new EpochProcessor());
}
// Get Pool Statistics and report to New Relic
PoolStatistics poolStats = VTM.fetchVTMObject<PoolStatObject>(pool.href).statistics;
ReportMetric("pools/" + pool.name + "/throughput/Received", "mebibits/second", processors["pool_" + pool.name + "_bytes_in"].Process(poolStats.bytes_in) / 131072);
ReportMetric("pools/" + pool.name + "/throughput/Transmitted", "mebibits/second", processors["pool_" + pool.name + "_bytes_out"].Process(poolStats.bytes_out) / 131072);
ReportMetric("pools/" + pool.name + "/nodes", "nodes", poolStats.nodes);
ReportMetric("pools/" + pool.name + "/disabled", "nodes", poolStats.disabled);
ReportMetric("pools/" + pool.name + "/draining", "nodes", poolStats.draining);
// Get Global Status API v3.7+
if (failedNodes != null)
{
int failedCount = 0;
foreach (var node in failedNodes)
{
foreach (var p in node.pools)
{
if (p == pool.name)
{
failedCount += 1;
}
}
}
ReportMetric($"pools/{pool.name}/failed", "nodes", failedCount);
}
}
}
catch(Exception e)
{
log.Error("Unable to fetch Pool information from the Virtual Traffic Manager '{0}'", this.name);
log.Error("Exception Thrown:'{0}'", e.Message);
}
}
private void PollNodes()
{
Children nodes = VTM.fetchVTMObject<Children>($"/api/tm/{APIVersion}/status/local_tm/statistics/nodes/node");
foreach (var node in nodes.children)
{
try
{
// Setup new EpochProcessors for each Node
// Check for existance of first Dictionary Item for the Node
if (!processors.ContainsKey("node_" + node.name + "_errors"))
{
processors.Add("node_" + node.name + "_errors", new EpochProcessor());
processors.Add("node_" + node.name + "_failures", new EpochProcessor());
}
// Get Node Statistics and report to New Relic
NodeStatistics nodeStats = VTM.fetchVTMObject<NodeStatObject>(node.href).statistics;
ReportMetric("nodes/" + node.name + "/errors", "sec", processors["node_" + node.name + "_errors"].Process(nodeStats.errors));
ReportMetric("nodes/" + node.name + "/failures", "sec", processors["node_" + node.name + "_failures"].Process(nodeStats.failures));
ReportMetric("nodes/" + node.name + "/current_conn", "connections", nodeStats.current_conn);
ReportMetric("nodes/" + node.name + "/current_requests", "requests", nodeStats.current_requests);
}
catch
{
log.Error("Unable to fetch Node information from the Virtual Traffic Manager '{0}' for Node: '{1}'", this.name, node.name);
}
}
}
private void PollVirtualServer()
{
try
{
Children virtualServers = VTM.fetchVTMObject<Children>($"/api/tm/{APIVersion}/status/local_tm/statistics/virtual_servers");
foreach (var virtualServer in virtualServers.children)
{
// Setup new EpochProcessors for each Virtual Server
// Check for existance of first Dictionary Item for the Virtual Server
if (!processors.ContainsKey("vs_" + virtualServer.name + "_bytes_in"))
{
processors.Add("vs_" + virtualServer.name + "_bytes_in", new EpochProcessor());
processors.Add("vs_" + virtualServer.name + "_bytes_out", new EpochProcessor());
}
// Get Virtual Server Statistics and report to New Relic
VirtualServerStatistics virtualServerStats = VTM.fetchVTMObject<VirtualServerStatObject>(virtualServer.href).statistics;
ReportMetric("virtual_servers/" + virtualServer.name + "/throughput/Received", "mebibits/second", processors["vs_" + virtualServer.name + "_bytes_in"].Process(virtualServerStats.bytes_in) / 131072);
ReportMetric("virtual_servers/" + virtualServer.name + "/throughput/Transmitted", "mebibits/second", processors["vs_" + virtualServer.name + "_bytes_out"].Process(virtualServerStats.bytes_out) / 131072);
ReportMetric("virtual_servers/" + virtualServer.name + "/current_conn", "connections", virtualServerStats.current_conn);
}
}
catch
{
log.Error("Unable to fetch Virtual Server information from the Virtual Traffic Manager '{0}'", this.name);
}
}
}
}