/
MetricApi.java
332 lines (312 loc) · 14 KB
/
MetricApi.java
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
*
* NOTE: This copyright does *not* cover user programs that use HQ
* program services by normal system calls through the application
* program interfaces provided as part of the Hyperic Plug-in Development
* Kit or the Hyperic Client Development Kit - this is merely considered
* normal use of the program, and does *not* fall under the heading of
* "derived work".
*
* Copyright (C) [2008, 2009], Hyperic, Inc.
* This file is part of HQ.
*
* HQ is free software; you can redistribute it and/or modify
* it under the terms version 2 of the GNU General Public License as
* published by the Free Software Foundation. This program is distributed
* in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
*/
package org.hyperic.hq.hqapi1;
import org.hyperic.hq.hqapi1.types.Metric;
import org.hyperic.hq.hqapi1.types.MetricDataResponse;
import org.hyperic.hq.hqapi1.types.MetricResponse;
import org.hyperic.hq.hqapi1.types.MetricTemplate;
import org.hyperic.hq.hqapi1.types.MetricTemplateResponse;
import org.hyperic.hq.hqapi1.types.MetricTemplatesRequest;
import org.hyperic.hq.hqapi1.types.MetricTemplatesResponse;
import org.hyperic.hq.hqapi1.types.MetricsDataResponse;
import org.hyperic.hq.hqapi1.types.MetricsRequest;
import org.hyperic.hq.hqapi1.types.MetricsResponse;
import org.hyperic.hq.hqapi1.types.Resource;
import org.hyperic.hq.hqapi1.types.ResourcePrototype;
import org.hyperic.hq.hqapi1.types.StatusResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* The Hyperic HQ Metric API.
* <br><br>
* This class provides access to the {@link org.hyperic.hq.hqapi1.types.Metric}s,
* {@link org.hyperic.hq.hqapi1.types.MetricTemplate}s, and
* {@link org.hyperic.hq.hqapi1.types.MetricData} within the HQ system.
* Each of the methods in this class return
* {@link org.hyperic.hq.hqapi1.types.Response} objects that wrap the result
* of the method with a
* {@link org.hyperic.hq.hqapi1.types.ResponseStatus} and a
* {@link org.hyperic.hq.hqapi1.types.ServiceError} that indicates the error
* if the response status is {@link org.hyperic.hq.hqapi1.types.ResponseStatus#FAILURE}.
*/
public class MetricApi extends BaseApi {
MetricApi(HQConnection conn) {
super(conn);
}
/**
* List all {@link org.hyperic.hq.hqapi1.types.Metric}s associated with a
* {@link org.hyperic.hq.hqapi1.types.Resource}
*
* @param resource The associated {@link org.hyperic.hq.hqapi1.types.Resource} which the metrics belong.
*
* @return {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS} if the
* metrics were successfully retrieved.
*
* @throws IOException If a network error occurs while making the request.
* @deprecated See {@link org.hyperic.hq.hqapi1.MetricApi#getMetrics(org.hyperic.hq.hqapi1.types.Resource, boolean)}
*/
public MetricsResponse getMetrics(Resource resource)
throws IOException
{
Map<String, String[]> params = new HashMap<String, String[]>();
params.put("resourceId", new String[] { Integer.toString(resource.getId()) });
return doGet("metric/getMetrics.hqu", params,
MetricsResponse.class);
}
/**
* List all enabled {@link org.hyperic.hq.hqapi1.types.Metric}s associated with a
* {@link org.hyperic.hq.hqapi1.types.Resource}
*
* @param resource The associated {@link org.hyperic.hq.hqapi1.types.Resource}
* which the metrics belong.
*
* @return {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS} if the
* metrics were successfully retrieved.
*
* @throws IOException If a network error occurs while making the request.
* @deprecated See {@link org.hyperic.hq.hqapi1.MetricApi#getMetrics(org.hyperic.hq.hqapi1.types.Resource, boolean)}
*/
public MetricsResponse getEnabledMetrics(Resource resource)
throws IOException
{
Map<String, String[]> params = new HashMap<String, String[]>();
params.put("resourceId", new String[] { Integer.toString(resource.getId()) });
params.put("enabled", new String[] { Boolean.toString(true) });
return doGet("metric/getMetrics.hqu", params,
MetricsResponse.class);
}
/**
* List all enabled {@link org.hyperic.hq.hqapi1.types.Metric}s associated with a
* {@link org.hyperic.hq.hqapi1.types.Resource}
*
* @param resource The associated {@link org.hyperic.hq.hqapi1.types.Resource}
* which the metrics belong.
* @param enabled If set to true, only Metrics which are enabled will be returned.
*
* @return {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS} if the
* metrics were successfully retrieved.
*
* @throws IOException If a network error occurs while making the request.
*/
public MetricsResponse getMetrics(Resource resource, boolean enabled)
throws IOException
{
Map<String, String[]> params = new HashMap<String, String[]>();
params.put("resourceId", new String[] { Integer.toString(resource.getId()) });
params.put("enabled", new String[] { Boolean.toString(enabled) });
return doGet("metric/getMetrics.hqu", params,
MetricsResponse.class);
}
/**
* Get the {@link org.hyperic.hq.hqapi1.types.Metric} associated
* with the metricId
*
* @param id The id used to retrieve the associated
* {@link org.hyperic.hq.hqapi1.types.Metric}.
*
* @return {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS} if the
* {@link org.hyperic.hq.hqapi1.types.Metric} was retrieved
* successfully.
*
* @throws IOException If a network error occurs while making the request.
*/
public MetricResponse getMetric(int id)
throws IOException
{
Map<String, String[]> params = new HashMap<String, String[]>();
params.put("id", new String[] { Integer.toString(id) });
return doGet("metric/getMetric.hqu", params,
MetricResponse.class);
}
/**
* Sync a List of {@link Metric}s.
*
* @param metrics The List of Metrics to sync.
*
* @return {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS} if the
* Metrics were synced successfully.
*
* @throws IOException If a network error occurs while making the request.
*/
public StatusResponse syncMetrics(List<Metric> metrics)
throws IOException
{
MetricsRequest syncRequest = new MetricsRequest();
syncRequest.getMetric().addAll(metrics);
return doPost("metric/syncMetrics.hqu", syncRequest,
StatusResponse.class);
}
/**
* Get the {@link org.hyperic.hq.hqapi1.types.MetricTemplate} associated
* with the metric id.
*
* @param id The id used to retrieve the associated
* {@link org.hyperic.hq.hqapi1.types.MetricTemplate}.
*
* @return {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS} if the
* {@link org.hyperic.hq.hqapi1.types.MetricTemplate} was retrieved
* successfully
*
* @throws IOException If a network error occurs while making the request.
*/
public MetricTemplateResponse getMetricTemplate(int id)
throws IOException
{
Map<String, String[]> params = new HashMap<String, String[]>();
params.put("id", new String[] { Integer.toString(id) });
return doGet("metric/getMetricTemplate.hqu", params,
MetricTemplateResponse.class);
}
/**
* List all {@link org.hyperic.hq.hqapi1.types.MetricTemplate}s associated
* with the given {@link org.hyperic.hq.hqapi1.types.ResourcePrototype}.
*
* @param prototype The associated {@link org.hyperic.hq.hqapi1.types.ResourcePrototype}
* to query.
*
* @return {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS} if the
* metric templates were successfully retrieved.
*
* @throws IOException If a network error occurs while making the request.
*/
public MetricTemplatesResponse getMetricTemplates(ResourcePrototype prototype)
throws IOException
{
Map<String, String[]> params = new HashMap<String, String[]>();
params.put("prototype", new String[] { prototype.getName() });
return doGet("metric/getTemplates.hqu", params,
MetricTemplatesResponse.class);
}
/**
* Sync a List of {@link MetricTemplate}s.
*
* @param templates The List of MetricTemplates to update.
*
* @return {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS} if the
* MetricTemplates were synced successfully.
*
* @throws IOException If a network error occurs while making the request.
*/
public StatusResponse syncMetricTemplates(List<MetricTemplate> templates)
throws IOException
{
MetricTemplatesRequest syncRequest = new MetricTemplatesRequest();
syncRequest.getMetricTemplate().addAll(templates);
return doPost("metric/syncTemplates.hqu", syncRequest,
StatusResponse.class);
}
/**
* Get the {@link org.hyperic.hq.hqapi1.types.MetricData} for the
* given {@link org.hyperic.hq.hqapi1.types.Metric}
*
* @param metricId The id of the {@link org.hyperic.hq.hqapi1.types.Metric} to query.
* @param start The start time to query, in epoch-millis.
* @param end The end time to query, in epoch-millis.
*
* @return {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS}
* if the data was succesfully queried. The returned data can be retrieved
* via {@link org.hyperic.hq.hqapi1.types.MetricDataResponse#getMetricData()}.
*
* @throws IOException If a network error occurs while making the request.
*/
public MetricDataResponse getMetricData(int metricId, long start, long end)
throws IOException
{
Map<String, String[]> params = new HashMap<String, String[]>();
params.put("metricId", new String[] { Integer.toString(metricId) });
params.put("start", new String[] { Long.toString(start) });
params.put("end", new String[] { Long.toString(end) });
return doGet("metric/getData.hqu", params, MetricDataResponse.class);
}
/**
* Get the {@link org.hyperic.hq.hqapi1.types.MetricData} for the given
* {@link org.hyperic.hq.hqapi1.types.Group} and {@link org.hyperic.hq.hqapi1.types.MetricTemplate}.
*
* @param groupId The id of the compatible {@link org.hyperic.hq.hqapi1.types.Group} to query.
* @param templateId The id of the {@link org.hyperic.hq.hqapi1.types.MetricTemplate}
* to query for data. The template must belong to the
* {@link org.hyperic.hq.hqapi1.types.ResourcePrototype} for the passed in
* compatible group.
* @param start The start time to query, in epoch-millis.
* @param end The end time to query, in epoch-millis.
*
* @return {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS}
* if the data was succesfully queried. The returned data can be retrieved
* via {@link org.hyperic.hq.hqapi1.types.MetricsDataResponse#getMetricData()}.
*
* @throws IOException If a network error occurs while making the request.
*/
public MetricsDataResponse getMetricData(int groupId, int templateId,
long start, long end)
throws IOException
{
Map<String, String[]> params = new HashMap<String, String[]>();
params.put("groupId", new String[] { Integer.toString(groupId) });
params.put("templateId", new String[] { Integer.toString(templateId) });
params.put("start", new String[] { Long.toString(start) });
params.put("end", new String[] { Long.toString(end) });
return doGet("metric/getGroupData.hqu", params,
MetricsDataResponse.class);
}
/**
* Get the {@link org.hyperic.hq.hqapi1.types.MetricData} for the given
* list of {@link Resource}s and {@link org.hyperic.hq.hqapi1.types.MetricTemplate}.
*
* @param resourceIds The list of {@link org.hyperic.hq.hqapi1.types.Resource}s to
* query. It is required that all Resources in this list are of the same
* {@link org.hyperic.hq.hqapi1.types.ResourcePrototype}.
* @param templateId The id of the {@link org.hyperic.hq.hqapi1.types.MetricTemplate}
* to query for data. The template must belong to the
* {@link org.hyperic.hq.hqapi1.types.ResourcePrototype} for the passed in
* resources.
* @param start The start time to query, in epoch-millis.
* @param end The end time to query, in epoch-millis.
*
* @return {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS}
* if the data was succesfully queried. The returned data can be retrieved
* via {@link org.hyperic.hq.hqapi1.types.MetricsDataResponse#getMetricData()}.
*
* @throws IOException If a network error occurs while making the request.
*/
public MetricsDataResponse getMetricData(int[] resourceIds, int templateId,
long start, long end)
throws IOException {
Map<String,String[]> params = new HashMap<String,String[]>();
String[] ids = new String[resourceIds.length];
for (int i = 0; i < resourceIds.length; i++) {
ids[i] = Integer.toString(resourceIds[i]);
}
params.put("ids", ids);
params.put("templateId", new String[] { Integer.toString(templateId) });
params.put("start", new String[] { Long.toString(start) });
params.put("end", new String[] { Long.toString(end) });
return doGet("metric/getResourceData.hqu", params,
MetricsDataResponse.class);
}
}