pcp_easy 0.0.3 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/ext/pcp_easy/agent.c +177 -0
- data/ext/pcp_easy/agent.h +26 -0
- data/ext/pcp_easy/metric.c +162 -2
- data/ext/pcp_easy/pcp_easy.c +2 -149
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 13fd0a230d5bd9d2d01e2f9502eb1d639574c251
|
4
|
+
data.tar.gz: 1d992b4b4f5580ed3d8e9ae7e37d53c76c75c4f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9269aa99c2030bec9430858fdc3fbba8e58c07d6d48949a6e1e20cc41054a06e0a79e5f4354f3bbffe815bbf00c564bc9707d19a2a8f3355b598964e746b6526
|
7
|
+
data.tar.gz: 67ba96db8ad09287dcad63f7987afee03efff3c33abe1259d0b5ed77fb3ff293b1702fa58004d9141669361665033153df2159ecbbded8460059c27f51af37dc
|
@@ -0,0 +1,177 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (C) 2016 Ryan Doyle
|
3
|
+
*
|
4
|
+
* This program is free software: you can redistribute it and/or modify
|
5
|
+
* it under the terms of the GNU General Public License as published by
|
6
|
+
* the Free Software Foundation, either version 3 of the License, or
|
7
|
+
* (at your option) any later version.
|
8
|
+
*
|
9
|
+
* This program is distributed in the hope that it will be useful,
|
10
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
* GNU General Public License for more details.
|
13
|
+
*
|
14
|
+
* You should have received a copy of the GNU General Public License
|
15
|
+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
16
|
+
*
|
17
|
+
*/
|
18
|
+
|
19
|
+
#include <ruby.h>
|
20
|
+
#include <pcp/pmapi.h>
|
21
|
+
|
22
|
+
#include "exceptions.h"
|
23
|
+
#include "metric.h"
|
24
|
+
|
25
|
+
VALUE pcpeasy_agent_class = Qnil;
|
26
|
+
|
27
|
+
|
28
|
+
typedef struct {
|
29
|
+
int pm_context;
|
30
|
+
} PcpEasyAgent;
|
31
|
+
|
32
|
+
static void deallocate(void *untyped_pcpeasy_agent) {
|
33
|
+
PcpEasyAgent *pcpqz_agent = (PcpEasyAgent*)untyped_pcpeasy_agent;
|
34
|
+
pmDestroyContext(pcpqz_agent->pm_context);
|
35
|
+
xfree(pcpqz_agent);
|
36
|
+
}
|
37
|
+
|
38
|
+
static VALUE allocate(VALUE self) {
|
39
|
+
PcpEasyAgent *pcpeasy_agent;
|
40
|
+
return Data_Make_Struct(self, PcpEasyAgent, 0, deallocate, pcpeasy_agent);
|
41
|
+
};
|
42
|
+
|
43
|
+
static VALUE initialize(VALUE self, VALUE hostname_rb) {
|
44
|
+
PcpEasyAgent *pcpeasy_agent;
|
45
|
+
const char *hostname;
|
46
|
+
int pm_context;
|
47
|
+
|
48
|
+
Data_Get_Struct(self, PcpEasyAgent, pcpeasy_agent);
|
49
|
+
hostname = StringValueCStr(hostname_rb);
|
50
|
+
|
51
|
+
if((pm_context = pmNewContext(PM_CONTEXT_HOST, hostname)) < 0) {
|
52
|
+
pcpeasy_raise_from_pmapi_error(pm_context);
|
53
|
+
}
|
54
|
+
|
55
|
+
rb_iv_set(self, "@host", hostname_rb);
|
56
|
+
|
57
|
+
/* Store away with this instance */
|
58
|
+
pcpeasy_agent->pm_context = pm_context;
|
59
|
+
|
60
|
+
return self;
|
61
|
+
}
|
62
|
+
|
63
|
+
static char* get_name_from_instance_id(int instance_id, int maximum_instances, int *instance_ids, char **instance_names) {
|
64
|
+
int i;
|
65
|
+
for(i=0; i<maximum_instances; i++) {
|
66
|
+
if(instance_id == instance_ids[i]) {
|
67
|
+
return instance_names[i];
|
68
|
+
}
|
69
|
+
}
|
70
|
+
return NULL;
|
71
|
+
}
|
72
|
+
|
73
|
+
static VALUE decode_multiple_instances(pmDesc pm_desc, pmValueSet *pm_value_set, char *metric_name) {
|
74
|
+
int error, i;
|
75
|
+
int number_of_instances = pm_value_set->numval;
|
76
|
+
int *instances = NULL;
|
77
|
+
char **instance_names = NULL;
|
78
|
+
VALUE result = rb_ary_new2(number_of_instances);
|
79
|
+
|
80
|
+
|
81
|
+
if((error = pmGetInDom(pm_desc.indom, &instances, &instance_names)) < 0) {
|
82
|
+
pcpeasy_raise_from_pmapi_error(error);
|
83
|
+
}
|
84
|
+
|
85
|
+
for(i = 0; i < number_of_instances; i++) {
|
86
|
+
char *instance_name = get_name_from_instance_id(pm_value_set->vlist[i].inst, number_of_instances, instances, instance_names);
|
87
|
+
rb_ary_push(result, pcpeasy_metric_new(metric_name, instance_name, &pm_value_set->vlist[i], &pm_desc, pm_value_set->valfmt));
|
88
|
+
}
|
89
|
+
|
90
|
+
free(instances);
|
91
|
+
free(instance_names);
|
92
|
+
|
93
|
+
return result;
|
94
|
+
}
|
95
|
+
|
96
|
+
static VALUE decode_single_instance(pmDesc pm_desc, pmValueSet *pm_value_set, char *metric_name) {
|
97
|
+
return pcpeasy_metric_new(metric_name, NULL, &pm_value_set->vlist[0], &pm_desc, pm_value_set->valfmt);
|
98
|
+
}
|
99
|
+
|
100
|
+
static VALUE decode_single_metric(pmID pmid, pmValueSet *pm_value_set, char *metric_name) {
|
101
|
+
int error;
|
102
|
+
pmDesc pm_desc;
|
103
|
+
|
104
|
+
/* No values, bail out */
|
105
|
+
if(pm_value_set->numval == 0) {
|
106
|
+
return Qnil;
|
107
|
+
}
|
108
|
+
|
109
|
+
/* Find out how to decode the metric */
|
110
|
+
if((error = pmLookupDesc(pmid, &pm_desc))) {
|
111
|
+
pcpeasy_raise_from_pmapi_error(error);
|
112
|
+
}
|
113
|
+
|
114
|
+
/* Do we have instances? */
|
115
|
+
if(pm_desc.indom == PM_INDOM_NULL) {
|
116
|
+
return decode_single_instance(pm_desc, pm_value_set, metric_name);
|
117
|
+
} else {
|
118
|
+
return decode_multiple_instances(pm_desc, pm_value_set, metric_name);
|
119
|
+
}
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
static VALUE decode_pm_result(pmID pmid, pmResult *pm_result, char *metric_name) {
|
124
|
+
/* No values (or somehow more than 1) */
|
125
|
+
if (pm_result->numpmid != 1) {
|
126
|
+
return Qnil;
|
127
|
+
}
|
128
|
+
|
129
|
+
return decode_single_metric(pmid, pm_result->vset[0], metric_name);
|
130
|
+
}
|
131
|
+
|
132
|
+
static VALUE metric(VALUE self, VALUE metric_string_rb) {
|
133
|
+
/* Get our context */
|
134
|
+
PcpEasyAgent *pcpeasy_agent;
|
135
|
+
Data_Get_Struct(self, PcpEasyAgent, pcpeasy_agent);
|
136
|
+
pmUseContext(pcpeasy_agent->pm_context);
|
137
|
+
|
138
|
+
/* Get the pmID */
|
139
|
+
int error;
|
140
|
+
pmID pmid;
|
141
|
+
pmID *pmid_list = ALLOC(pmID);
|
142
|
+
char **metric_list = ALLOC(char*);
|
143
|
+
char *metric_name = StringValueCStr(metric_string_rb);
|
144
|
+
metric_list[0] = metric_name;
|
145
|
+
if((error = pmLookupName(1, metric_list, pmid_list)) < 0) {
|
146
|
+
xfree(pmid_list);
|
147
|
+
xfree(metric_list);
|
148
|
+
pcpeasy_raise_from_pmapi_error(error);
|
149
|
+
}
|
150
|
+
pmid = pmid_list[0];
|
151
|
+
xfree(pmid_list);
|
152
|
+
xfree(metric_list);
|
153
|
+
|
154
|
+
|
155
|
+
/* Do the fetch */
|
156
|
+
pmResult *pm_result;
|
157
|
+
VALUE result;
|
158
|
+
if((error = pmFetch(1, &pmid, &pm_result))) {
|
159
|
+
pcpeasy_raise_from_pmapi_error(error);
|
160
|
+
}
|
161
|
+
|
162
|
+
/* Decode the result */
|
163
|
+
result = decode_pm_result(pmid, pm_result, metric_name);
|
164
|
+
pmFreeResult(pm_result);
|
165
|
+
|
166
|
+
return result;
|
167
|
+
}
|
168
|
+
|
169
|
+
|
170
|
+
void pcpeasy_agent_init(VALUE pcpeasy_class) {
|
171
|
+
pcpeasy_agent_class = rb_define_class_under(pcpeasy_class, "Agent", rb_cObject);
|
172
|
+
|
173
|
+
rb_define_alloc_func(pcpeasy_agent_class, allocate);
|
174
|
+
rb_define_method(pcpeasy_agent_class, "initialize", initialize, 1);
|
175
|
+
rb_define_method(pcpeasy_agent_class, "metric", metric, 1);
|
176
|
+
rb_define_attr(pcpeasy_agent_class, "host", 1, 0);
|
177
|
+
}
|
@@ -0,0 +1,26 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (C) 2016 Ryan Doyle
|
3
|
+
*
|
4
|
+
* This program is free software: you can redistribute it and/or modify
|
5
|
+
* it under the terms of the GNU General Public License as published by
|
6
|
+
* the Free Software Foundation, either version 3 of the License, or
|
7
|
+
* (at your option) any later version.
|
8
|
+
*
|
9
|
+
* This program is distributed in the hope that it will be useful,
|
10
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
* GNU General Public License for more details.
|
13
|
+
*
|
14
|
+
* You should have received a copy of the GNU General Public License
|
15
|
+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
16
|
+
*
|
17
|
+
*/
|
18
|
+
|
19
|
+
#ifndef PCP_EASY_RUBY_AGENT_H
|
20
|
+
#define PCP_EASY_RUBY_AGENT_H 1
|
21
|
+
|
22
|
+
#include <ruby.h>
|
23
|
+
|
24
|
+
void pcpeasy_agent_init(VALUE pcpeasy_class);
|
25
|
+
|
26
|
+
#endif
|
data/ext/pcp_easy/metric.c
CHANGED
@@ -22,7 +22,8 @@
|
|
22
22
|
#include "exceptions.h"
|
23
23
|
|
24
24
|
#define READ_ONLY 1, 0
|
25
|
-
#define CONSTRUCTOR_ARGS
|
25
|
+
#define CONSTRUCTOR_ARGS 6
|
26
|
+
#define rb_symbol_new(name) ID2SYM(rb_intern(name))
|
26
27
|
|
27
28
|
VALUE pcpeasy_metric_class;
|
28
29
|
VALUE pcpeasy_metric_semantics_counter;
|
@@ -30,11 +31,27 @@ VALUE pcpeasy_metric_semantics_instant;
|
|
30
31
|
VALUE pcpeasy_metric_semantics_discrete;
|
31
32
|
VALUE pcpeasy_metric_semantics_unknown;
|
32
33
|
|
33
|
-
|
34
|
+
VALUE pcpeasy_metric_type_nosupport;
|
35
|
+
VALUE pcpeasy_metric_type_32;
|
36
|
+
VALUE pcpeasy_metric_type_u32;
|
37
|
+
VALUE pcpeasy_metric_type_64;
|
38
|
+
VALUE pcpeasy_metric_type_u64;
|
39
|
+
VALUE pcpeasy_metric_type_float;
|
40
|
+
VALUE pcpeasy_metric_type_double;
|
41
|
+
VALUE pcpeasy_metric_type_string;
|
42
|
+
VALUE pcpeasy_metric_type_aggregate;
|
43
|
+
VALUE pcpeasy_metric_type_aggregate_static;
|
44
|
+
VALUE pcpeasy_metric_type_event;
|
45
|
+
VALUE pcpeasy_metric_type_highres_event;
|
46
|
+
VALUE pcpeasy_metric_type_unknown;
|
47
|
+
|
48
|
+
static VALUE initialize(VALUE self, VALUE name, VALUE value, VALUE instance, VALUE semantics, VALUE type, VALUE units) {
|
34
49
|
rb_iv_set(self, "@name", name);
|
35
50
|
rb_iv_set(self, "@value", value);
|
36
51
|
rb_iv_set(self, "@instance", instance);
|
37
52
|
rb_iv_set(self, "@semantics", semantics);
|
53
|
+
rb_iv_set(self, "@type", type);
|
54
|
+
rb_iv_set(self, "@units", units);
|
38
55
|
|
39
56
|
return self;
|
40
57
|
}
|
@@ -102,16 +119,143 @@ static VALUE equal(VALUE self, VALUE other) {
|
|
102
119
|
return Qfalse;
|
103
120
|
if(!is_field_equal("@semantics", self, other))
|
104
121
|
return Qfalse;
|
122
|
+
if(!is_field_equal("@type", self, other))
|
123
|
+
return Qfalse;
|
124
|
+
if(!is_field_equal("@units", self, other))
|
125
|
+
return Qfalse;
|
105
126
|
|
106
127
|
return Qtrue;
|
107
128
|
}
|
108
129
|
|
130
|
+
static VALUE type(int type) {
|
131
|
+
switch(type) {
|
132
|
+
case PM_TYPE_32:
|
133
|
+
return pcpeasy_metric_type_32;
|
134
|
+
case PM_TYPE_U32:
|
135
|
+
return pcpeasy_metric_type_u32;
|
136
|
+
case PM_TYPE_64:
|
137
|
+
return pcpeasy_metric_type_64;
|
138
|
+
case PM_TYPE_U64:
|
139
|
+
return pcpeasy_metric_type_u64;
|
140
|
+
case PM_TYPE_FLOAT:
|
141
|
+
return pcpeasy_metric_type_float;
|
142
|
+
case PM_TYPE_DOUBLE:
|
143
|
+
return pcpeasy_metric_type_double;
|
144
|
+
case PM_TYPE_STRING:
|
145
|
+
return pcpeasy_metric_type_string;
|
146
|
+
case PM_TYPE_AGGREGATE:
|
147
|
+
return pcpeasy_metric_type_aggregate;
|
148
|
+
case PM_TYPE_AGGREGATE_STATIC:
|
149
|
+
return pcpeasy_metric_type_aggregate_static;
|
150
|
+
case PM_TYPE_EVENT:
|
151
|
+
return pcpeasy_metric_type_event;
|
152
|
+
case PM_TYPE_HIGHRES_EVENT:
|
153
|
+
return pcpeasy_metric_type_highres_event;
|
154
|
+
case PM_TYPE_NOSUPPORT:
|
155
|
+
return pcpeasy_metric_type_nosupport;
|
156
|
+
case PM_TYPE_UNKNOWN:
|
157
|
+
default:
|
158
|
+
return pcpeasy_metric_type_unknown;
|
159
|
+
}
|
160
|
+
}
|
161
|
+
|
162
|
+
static VALUE units(pmUnits pm_units) {
|
163
|
+
VALUE units = rb_hash_new();
|
164
|
+
VALUE dimension = Qnil;
|
165
|
+
|
166
|
+
if(pm_units.dimSpace == 1 && pm_units.dimTime == 0 && pm_units.dimCount == 0)
|
167
|
+
dimension = rb_symbol_new("space");
|
168
|
+
if(pm_units.dimSpace == 1 && pm_units.dimTime == -1 && pm_units.dimCount == 0)
|
169
|
+
dimension = rb_symbol_new("space_time");
|
170
|
+
if(pm_units.dimSpace == 1 && pm_units.dimTime == 0 && pm_units.dimCount == -1)
|
171
|
+
dimension = rb_symbol_new("space_count");
|
172
|
+
if(pm_units.dimSpace == 0 && pm_units.dimTime == 1 && pm_units.dimCount == 0)
|
173
|
+
dimension = rb_symbol_new("time");
|
174
|
+
if(pm_units.dimSpace == -1 && pm_units.dimTime == 1 && pm_units.dimCount == 0)
|
175
|
+
dimension = rb_symbol_new("time_space");
|
176
|
+
if(pm_units.dimSpace == 0 && pm_units.dimTime == 1 && pm_units.dimCount == -1)
|
177
|
+
dimension = rb_symbol_new("time_count");
|
178
|
+
if(pm_units.dimSpace == 0 && pm_units.dimTime == 0 && pm_units.dimCount == 1)
|
179
|
+
dimension = rb_symbol_new("count");
|
180
|
+
if(pm_units.dimSpace == -1 && pm_units.dimTime == 0 && pm_units.dimCount == 1)
|
181
|
+
dimension = rb_symbol_new("count_space");
|
182
|
+
if(pm_units.dimSpace == 0 && pm_units.dimTime == -1 && pm_units.dimCount == 1)
|
183
|
+
dimension = rb_symbol_new("count_time");
|
184
|
+
|
185
|
+
rb_hash_aset(units, rb_symbol_new("dimension"), dimension);
|
186
|
+
|
187
|
+
if(pm_units.dimSpace != 0) {
|
188
|
+
VALUE scale_space;
|
189
|
+
switch(pm_units.scaleSpace) {
|
190
|
+
case PM_SPACE_BYTE:
|
191
|
+
scale_space = rb_symbol_new("bytes");
|
192
|
+
break;
|
193
|
+
case PM_SPACE_KBYTE:
|
194
|
+
scale_space = rb_symbol_new("kilobytes");
|
195
|
+
break;
|
196
|
+
case PM_SPACE_MBYTE:
|
197
|
+
scale_space = rb_symbol_new("megabytes");
|
198
|
+
break;
|
199
|
+
case PM_SPACE_GBYTE:
|
200
|
+
scale_space = rb_symbol_new("gigabytes");
|
201
|
+
break;
|
202
|
+
case PM_SPACE_TBYTE:
|
203
|
+
scale_space = rb_symbol_new("terabytes");
|
204
|
+
break;
|
205
|
+
case PM_SPACE_PBYTE:
|
206
|
+
scale_space = rb_symbol_new("petabytes");
|
207
|
+
break;
|
208
|
+
case PM_SPACE_EBYTE:
|
209
|
+
scale_space = rb_symbol_new("exabytes");
|
210
|
+
break;
|
211
|
+
default:
|
212
|
+
scale_space = Qnil;
|
213
|
+
}
|
214
|
+
rb_hash_aset(units, rb_symbol_new("space"), scale_space);
|
215
|
+
}
|
216
|
+
|
217
|
+
if(pm_units.dimTime != 0) {
|
218
|
+
VALUE scale_time;
|
219
|
+
switch(pm_units.scaleTime) {
|
220
|
+
case PM_TIME_NSEC:
|
221
|
+
scale_time = rb_symbol_new("nanoseconds");
|
222
|
+
break;
|
223
|
+
case PM_TIME_USEC:
|
224
|
+
scale_time = rb_symbol_new("microseconds");
|
225
|
+
break;
|
226
|
+
case PM_TIME_MSEC:
|
227
|
+
scale_time = rb_symbol_new("milliseconds");
|
228
|
+
break;
|
229
|
+
case PM_TIME_SEC:
|
230
|
+
scale_time = rb_symbol_new("seconds");
|
231
|
+
break;
|
232
|
+
case PM_TIME_MIN:
|
233
|
+
scale_time = rb_symbol_new("minutes");
|
234
|
+
break;
|
235
|
+
case PM_TIME_HOUR:
|
236
|
+
scale_time = rb_symbol_new("hour");
|
237
|
+
break;
|
238
|
+
default:
|
239
|
+
scale_time = Qnil;
|
240
|
+
}
|
241
|
+
rb_hash_aset(units, rb_symbol_new("time"), scale_time);
|
242
|
+
}
|
243
|
+
|
244
|
+
if(pm_units.dimCount != 0) {
|
245
|
+
rb_hash_aset(units, rb_symbol_new("count_scaling"), INT2NUM(pm_units.scaleCount));
|
246
|
+
}
|
247
|
+
|
248
|
+
return units;
|
249
|
+
}
|
250
|
+
|
109
251
|
VALUE pcpeasy_metric_new(char *metric_name, char *instance, pmValue *pm_value, pmDesc *pm_desc, int value_format) {
|
110
252
|
VALUE args[CONSTRUCTOR_ARGS];
|
111
253
|
args[0] = rb_tainted_str_new_cstr(metric_name);
|
112
254
|
args[1] = value(value_format, pm_value, pm_desc->type);
|
113
255
|
args[2] = instance_name(instance);
|
114
256
|
args[3] = semantics_symbol(pm_desc->sem);
|
257
|
+
args[4] = type(pm_desc->type);
|
258
|
+
args[5] = units(pm_desc->units);
|
115
259
|
|
116
260
|
return rb_class_new_instance(CONSTRUCTOR_ARGS, args, pcpeasy_metric_class);
|
117
261
|
}
|
@@ -124,10 +268,26 @@ void pcpeasy_metric_init(VALUE pcpeasy_class) {
|
|
124
268
|
pcpeasy_metric_semantics_instant = ID2SYM(rb_intern("instant"));
|
125
269
|
pcpeasy_metric_semantics_unknown = ID2SYM(rb_intern("unknown"));
|
126
270
|
|
271
|
+
pcpeasy_metric_type_nosupport = ID2SYM(rb_intern("nosupport"));
|
272
|
+
pcpeasy_metric_type_32 = ID2SYM(rb_intern("int32"));
|
273
|
+
pcpeasy_metric_type_u32 = ID2SYM(rb_intern("uint32"));
|
274
|
+
pcpeasy_metric_type_64 = ID2SYM(rb_intern("int64"));
|
275
|
+
pcpeasy_metric_type_u64 = ID2SYM(rb_intern("uint64"));
|
276
|
+
pcpeasy_metric_type_float = ID2SYM(rb_intern("float"));
|
277
|
+
pcpeasy_metric_type_double = ID2SYM(rb_intern("double"));
|
278
|
+
pcpeasy_metric_type_string = ID2SYM(rb_intern("string"));
|
279
|
+
pcpeasy_metric_type_aggregate = ID2SYM(rb_intern("aggregate"));
|
280
|
+
pcpeasy_metric_type_aggregate_static = ID2SYM(rb_intern("aggregate_static"));
|
281
|
+
pcpeasy_metric_type_event = ID2SYM(rb_intern("event"));
|
282
|
+
pcpeasy_metric_type_highres_event = ID2SYM(rb_intern("highres_event"));
|
283
|
+
pcpeasy_metric_type_unknown = ID2SYM(rb_intern("unknown"));
|
284
|
+
|
127
285
|
rb_define_method(pcpeasy_metric_class, "initialize", initialize, CONSTRUCTOR_ARGS);
|
128
286
|
rb_define_method(pcpeasy_metric_class, "==", equal, 1);
|
129
287
|
rb_define_attr(pcpeasy_metric_class, "name", READ_ONLY);
|
130
288
|
rb_define_attr(pcpeasy_metric_class, "value", READ_ONLY);
|
131
289
|
rb_define_attr(pcpeasy_metric_class, "instance", READ_ONLY);
|
132
290
|
rb_define_attr(pcpeasy_metric_class, "semantics", READ_ONLY);
|
291
|
+
rb_define_attr(pcpeasy_metric_class, "type", READ_ONLY);
|
292
|
+
rb_define_attr(pcpeasy_metric_class, "units", READ_ONLY);
|
133
293
|
}
|
data/ext/pcp_easy/pcp_easy.c
CHANGED
@@ -17,165 +17,18 @@
|
|
17
17
|
*/
|
18
18
|
|
19
19
|
#include <ruby.h>
|
20
|
-
#include <pcp/pmapi.h>
|
21
20
|
|
22
21
|
#include "exceptions.h"
|
23
22
|
#include "metric.h"
|
23
|
+
#include "agent.h"
|
24
24
|
|
25
25
|
VALUE pcpeasy_class = Qnil;
|
26
|
-
VALUE pcpeasy_agent_class = Qnil;
|
27
26
|
|
28
27
|
|
29
|
-
typedef struct {
|
30
|
-
int pm_context;
|
31
|
-
} PcpEasyAgent;
|
32
|
-
|
33
|
-
static void pcpeasy_agent_deallocate(void *untyped_pcpeasy_agent) {
|
34
|
-
PcpEasyAgent *pcpqz_agent = (PcpEasyAgent*)untyped_pcpeasy_agent;
|
35
|
-
pmDestroyContext(pcpqz_agent->pm_context);
|
36
|
-
xfree(pcpqz_agent);
|
37
|
-
}
|
38
|
-
|
39
|
-
static VALUE pcpeasy_agent_allocate(VALUE self) {
|
40
|
-
PcpEasyAgent *pcpeasy_agent;
|
41
|
-
return Data_Make_Struct(self, PcpEasyAgent, 0, pcpeasy_agent_deallocate, pcpeasy_agent);
|
42
|
-
};
|
43
|
-
|
44
|
-
static VALUE pcpeasy_agent_initialize(VALUE self, VALUE hostname_rb) {
|
45
|
-
PcpEasyAgent *pcpeasy_agent;
|
46
|
-
const char *hostname;
|
47
|
-
int pm_context;
|
48
|
-
|
49
|
-
Data_Get_Struct(self, PcpEasyAgent, pcpeasy_agent);
|
50
|
-
hostname = StringValueCStr(hostname_rb);
|
51
|
-
|
52
|
-
if((pm_context = pmNewContext(PM_CONTEXT_HOST, hostname)) < 0) {
|
53
|
-
pcpeasy_raise_from_pmapi_error(pm_context);
|
54
|
-
}
|
55
|
-
|
56
|
-
rb_iv_set(self, "@host", hostname_rb);
|
57
|
-
|
58
|
-
/* Store away with this instance */
|
59
|
-
pcpeasy_agent->pm_context = pm_context;
|
60
|
-
|
61
|
-
return self;
|
62
|
-
}
|
63
|
-
|
64
|
-
static char* get_name_from_instance_id(int instance_id, int maximum_instances, int *instance_ids, char **instance_names) {
|
65
|
-
int i;
|
66
|
-
for(i=0; i<maximum_instances; i++) {
|
67
|
-
if(instance_id == instance_ids[i]) {
|
68
|
-
return instance_names[i];
|
69
|
-
}
|
70
|
-
}
|
71
|
-
return NULL;
|
72
|
-
}
|
73
|
-
|
74
|
-
static VALUE decode_multiple_instances(pmDesc pm_desc, pmValueSet *pm_value_set, char *metric_name) {
|
75
|
-
int error, i;
|
76
|
-
int number_of_instances = pm_value_set->numval;
|
77
|
-
int *instances = NULL;
|
78
|
-
char **instance_names = NULL;
|
79
|
-
VALUE result = rb_ary_new2(number_of_instances);
|
80
|
-
|
81
|
-
|
82
|
-
if((error = pmGetInDom(pm_desc.indom, &instances, &instance_names)) < 0) {
|
83
|
-
pcpeasy_raise_from_pmapi_error(error);
|
84
|
-
}
|
85
|
-
|
86
|
-
for(i = 0; i < number_of_instances; i++) {
|
87
|
-
char *instance_name = get_name_from_instance_id(pm_value_set->vlist[i].inst, number_of_instances, instances, instance_names);
|
88
|
-
rb_ary_push(result, pcpeasy_metric_new(metric_name, instance_name, &pm_value_set->vlist[i], &pm_desc, pm_value_set->valfmt));
|
89
|
-
}
|
90
|
-
|
91
|
-
free(instances);
|
92
|
-
free(instance_names);
|
93
|
-
|
94
|
-
return result;
|
95
|
-
}
|
96
|
-
|
97
|
-
static VALUE decode_single_instance(pmDesc pm_desc, pmValueSet *pm_value_set, char *metric_name) {
|
98
|
-
return pcpeasy_metric_new(metric_name, NULL, &pm_value_set->vlist[0], &pm_desc, pm_value_set->valfmt);
|
99
|
-
}
|
100
|
-
|
101
|
-
static VALUE decode_single_metric(pmID pmid, pmValueSet *pm_value_set, char *metric_name) {
|
102
|
-
int error;
|
103
|
-
pmDesc pm_desc;
|
104
|
-
|
105
|
-
/* No values, bail out */
|
106
|
-
if(pm_value_set->numval == 0) {
|
107
|
-
return Qnil;
|
108
|
-
}
|
109
|
-
|
110
|
-
/* Find out how to decode the metric */
|
111
|
-
if((error = pmLookupDesc(pmid, &pm_desc))) {
|
112
|
-
pcpeasy_raise_from_pmapi_error(error);
|
113
|
-
}
|
114
|
-
|
115
|
-
/* Do we have instances? */
|
116
|
-
if(pm_desc.indom == PM_INDOM_NULL) {
|
117
|
-
return decode_single_instance(pm_desc, pm_value_set, metric_name);
|
118
|
-
} else {
|
119
|
-
return decode_multiple_instances(pm_desc, pm_value_set, metric_name);
|
120
|
-
}
|
121
|
-
|
122
|
-
}
|
123
|
-
|
124
|
-
static VALUE decode_pm_result(pmID pmid, pmResult *pm_result, char *metric_name) {
|
125
|
-
/* No values (or somehow more than 1) */
|
126
|
-
if (pm_result->numpmid != 1) {
|
127
|
-
return Qnil;
|
128
|
-
}
|
129
|
-
|
130
|
-
return decode_single_metric(pmid, pm_result->vset[0], metric_name);
|
131
|
-
}
|
132
|
-
|
133
|
-
static VALUE pcpeasy_agent_metric(VALUE self, VALUE metric_string_rb) {
|
134
|
-
/* Get our context */
|
135
|
-
PcpEasyAgent *pcpeasy_agent;
|
136
|
-
Data_Get_Struct(self, PcpEasyAgent, pcpeasy_agent);
|
137
|
-
pmUseContext(pcpeasy_agent->pm_context);
|
138
|
-
|
139
|
-
/* Get the pmID */
|
140
|
-
int error;
|
141
|
-
pmID pmid;
|
142
|
-
pmID *pmid_list = ALLOC(pmID);
|
143
|
-
char **metric_list = ALLOC(char*);
|
144
|
-
char *metric_name = StringValueCStr(metric_string_rb);
|
145
|
-
metric_list[0] = metric_name;
|
146
|
-
if((error = pmLookupName(1, metric_list, pmid_list)) < 0) {
|
147
|
-
xfree(pmid_list);
|
148
|
-
xfree(metric_list);
|
149
|
-
pcpeasy_raise_from_pmapi_error(error);
|
150
|
-
}
|
151
|
-
pmid = pmid_list[0];
|
152
|
-
xfree(pmid_list);
|
153
|
-
xfree(metric_list);
|
154
|
-
|
155
|
-
|
156
|
-
/* Do the fetch */
|
157
|
-
pmResult *pm_result;
|
158
|
-
VALUE result;
|
159
|
-
if((error = pmFetch(1, &pmid, &pm_result))) {
|
160
|
-
pcpeasy_raise_from_pmapi_error(error);
|
161
|
-
}
|
162
|
-
|
163
|
-
/* Decode the result */
|
164
|
-
result = decode_pm_result(pmid, pm_result, metric_name);
|
165
|
-
pmFreeResult(pm_result);
|
166
|
-
|
167
|
-
return result;
|
168
|
-
}
|
169
28
|
|
170
29
|
void Init_pcp_easy() {
|
171
30
|
pcpeasy_class = rb_define_module("PCPEasy");
|
172
|
-
pcpeasy_agent_class = rb_define_class_under(pcpeasy_class, "Agent", rb_cObject);
|
173
31
|
pcpeasy_exceptions_init(pcpeasy_class);
|
174
32
|
pcpeasy_metric_init(pcpeasy_class);
|
175
|
-
|
176
|
-
rb_define_alloc_func(pcpeasy_agent_class, pcpeasy_agent_allocate);
|
177
|
-
rb_define_method(pcpeasy_agent_class, "initialize", pcpeasy_agent_initialize, 1);
|
178
|
-
rb_define_method(pcpeasy_agent_class, "metric", pcpeasy_agent_metric, 1);
|
179
|
-
rb_define_attr(pcpeasy_agent_class, "host", 1, 0);
|
180
|
-
|
33
|
+
pcpeasy_agent_init(pcpeasy_class);
|
181
34
|
}
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pcp_easy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Doyle
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-03-
|
11
|
+
date: 2016-03-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake-compiler
|
@@ -45,6 +45,8 @@ extensions:
|
|
45
45
|
- ext/pcp_easy/extconf.rb
|
46
46
|
extra_rdoc_files: []
|
47
47
|
files:
|
48
|
+
- ext/pcp_easy/agent.c
|
49
|
+
- ext/pcp_easy/agent.h
|
48
50
|
- ext/pcp_easy/exceptions.c
|
49
51
|
- ext/pcp_easy/exceptions.h
|
50
52
|
- ext/pcp_easy/extconf.rb
|