sigar-test 0.7.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +201 -0
  3. data/NOTICE +117 -0
  4. data/README +2 -0
  5. data/Rakefile +105 -0
  6. data/bindings/SigarBuild.pm +301 -0
  7. data/bindings/SigarWrapper.pm +3025 -0
  8. data/bindings/ruby/extconf.rb +131 -0
  9. data/bindings/ruby/rbsigar.c +888 -0
  10. data/include/sigar.h +984 -0
  11. data/include/sigar_fileinfo.h +157 -0
  12. data/include/sigar_format.h +65 -0
  13. data/include/sigar_getline.h +18 -0
  14. data/include/sigar_log.h +80 -0
  15. data/include/sigar_private.h +429 -0
  16. data/include/sigar_ptql.h +53 -0
  17. data/include/sigar_util.h +197 -0
  18. data/src/os/aix/aix_sigar.c +2168 -0
  19. data/src/os/aix/sigar_os.h +73 -0
  20. data/src/os/darwin/Info.plist.in +27 -0
  21. data/src/os/darwin/darwin_sigar.c +3718 -0
  22. data/src/os/darwin/sigar_os.h +80 -0
  23. data/src/os/hpux/hpux_sigar.c +1361 -0
  24. data/src/os/hpux/sigar_os.h +49 -0
  25. data/src/os/linux/linux_sigar.c +2810 -0
  26. data/src/os/linux/sigar_os.h +82 -0
  27. data/src/os/solaris/get_mib2.c +321 -0
  28. data/src/os/solaris/get_mib2.h +127 -0
  29. data/src/os/solaris/kstats.c +181 -0
  30. data/src/os/solaris/procfs.c +97 -0
  31. data/src/os/solaris/sigar_os.h +224 -0
  32. data/src/os/solaris/solaris_sigar.c +2732 -0
  33. data/src/os/win32/peb.c +212 -0
  34. data/src/os/win32/sigar.rc.in +40 -0
  35. data/src/os/win32/sigar_os.h +685 -0
  36. data/src/os/win32/sigar_pdh.h +47 -0
  37. data/src/os/win32/win32_sigar.c +4109 -0
  38. data/src/sigar.c +2444 -0
  39. data/src/sigar_cache.c +253 -0
  40. data/src/sigar_fileinfo.c +815 -0
  41. data/src/sigar_format.c +696 -0
  42. data/src/sigar_getline.c +1849 -0
  43. data/src/sigar_ptql.c +1976 -0
  44. data/src/sigar_signal.c +216 -0
  45. data/src/sigar_util.c +1060 -0
  46. data/src/sigar_version.c.in +22 -0
  47. data/src/sigar_version_autoconf.c.in +22 -0
  48. data/version.properties +11 -0
  49. metadata +91 -0
@@ -0,0 +1,888 @@
1
+ /*
2
+ * Copyright (c) 2007-2009 Hyperic, Inc.
3
+ * Copyright (c) 2009 SpringSource, Inc.
4
+ * Copyright (c) 2009-2010 VMware, Inc.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ #include <ruby.h>
20
+ #ifdef RB_RUBY_19
21
+ #include <ruby/re.h>
22
+ #else
23
+ #include <re.h>
24
+ #endif
25
+
26
+ #include <errno.h>
27
+ #include "sigar.h"
28
+ #include "sigar_fileinfo.h"
29
+ #include "sigar_log.h"
30
+ #include "sigar_format.h"
31
+ #include "sigar_ptql.h"
32
+
33
+ #define RB_SIGAR_RAISE(msg) rb_raise(rb_eArgError, "%s", msg)
34
+ #define RB_SIGAR_CROAK RB_SIGAR_RAISE(sigar_strerror(sigar, status))
35
+ #define OBJ2PID(pid) rb_sigar_pid_get(rbsigar, pid)
36
+
37
+ #ifndef RSTRING_PTR
38
+ #define RSTRING_PTR(s) RSTRING(s)->ptr
39
+ #endif
40
+
41
+ #ifndef RSTRING_LEN
42
+ #define RSTRING_LEN(s) RSTRING(s)->len
43
+ #endif
44
+
45
+ #ifdef RB_HAS_RE_ERROR
46
+ # define RB_REGEX_ERROR rb_eRegexpError
47
+ #else
48
+ # define RB_REGEX_ERROR rb_eArgError
49
+ #endif
50
+
51
+ #define SIGAR \
52
+ sigar_t *sigar = rbsigar->sigar
53
+
54
+ #define SIGAR_GET \
55
+ rb_sigar_t *rbsigar = rb_sigar_get(obj); \
56
+ SIGAR
57
+
58
+ typedef struct {
59
+ sigar_t *sigar;
60
+ VALUE logger;
61
+ } rb_sigar_t;
62
+
63
+ static rb_sigar_t *rb_sigar_get(VALUE obj)
64
+ {
65
+ rb_sigar_t *rbsigar;
66
+ Data_Get_Struct(obj, rb_sigar_t, rbsigar);
67
+ return rbsigar;
68
+ }
69
+
70
+ static int rbsigar_ptql_re_impl(void *data,
71
+ char *haystack, char *needle)
72
+ {
73
+ #ifdef RB_RUBY_19
74
+ /* XXX no more regex.h */
75
+ return 0;
76
+ #else
77
+ struct re_pattern_buffer *regex;
78
+ int len = strlen(haystack);
79
+ int retval;
80
+ const char *err;
81
+
82
+ regex = ALLOC(struct re_pattern_buffer);
83
+ MEMZERO((char *)regex, struct re_pattern_buffer, 1);
84
+ /* XXX cache */
85
+ if ((err = re_compile_pattern(needle, strlen(needle), regex))) {
86
+ re_free_pattern(regex);
87
+ rb_raise(RB_REGEX_ERROR, "%s", err);
88
+ return 0;
89
+ }
90
+
91
+ retval = re_match(regex, haystack, len, 0, NULL);
92
+ re_free_pattern(regex);
93
+ return retval > 0;
94
+ #endif
95
+ }
96
+
97
+ #define sigar_isdigit(c) \
98
+ (isdigit(((unsigned char)(c))))
99
+
100
+ static sigar_pid_t rb_sigar_pid_get(rb_sigar_t *rbsigar, VALUE obj)
101
+ {
102
+ SIGAR;
103
+
104
+ if (TYPE(obj) == T_STRING) {
105
+ char *pid = StringValuePtr(obj);
106
+
107
+ if (sigar_isdigit(*pid)) {
108
+ obj = rb_str2inum(obj, 10);
109
+ /* fallthru */
110
+ }
111
+ else if ((RSTRING_LEN(obj) == 2) &&
112
+ (*pid == '$') && (*(pid + 1) == '$'))
113
+ {
114
+ return sigar_pid_get(sigar);
115
+ }
116
+ else {
117
+ /* XXX cache queries */
118
+ sigar_ptql_query_t *query;
119
+ sigar_ptql_error_t error;
120
+ int status =
121
+ sigar_ptql_query_create(&query, (char *)pid, &error);
122
+
123
+ if (status == SIGAR_OK) {
124
+ sigar_pid_t qpid;
125
+
126
+ sigar_ptql_re_impl_set(sigar, NULL, rbsigar_ptql_re_impl);
127
+ status = sigar_ptql_query_find_process(sigar, query, &qpid);
128
+ sigar_ptql_re_impl_set(sigar, NULL, NULL);
129
+ sigar_ptql_query_destroy(query);
130
+ if (status == SIGAR_OK) {
131
+ return qpid;
132
+ }
133
+ else {
134
+ RB_SIGAR_RAISE(sigar_strerror(sigar, status));
135
+ }
136
+ }
137
+ else {
138
+ RB_SIGAR_RAISE(error.message);
139
+ }
140
+ }
141
+ }
142
+ return NUM2UINT(obj);
143
+ }
144
+
145
+ static void rb_sigar_free(void *obj)
146
+ {
147
+ xfree(obj);
148
+ }
149
+
150
+ static void rb_sigar_close(rb_sigar_t *rbsigar)
151
+ {
152
+ sigar_close(rbsigar->sigar);
153
+ rb_sigar_free(rbsigar);
154
+ }
155
+
156
+ static void rb_sigar_mark(rb_sigar_t *rbsigar)
157
+ {
158
+ rb_gc_mark(rbsigar->logger);
159
+ }
160
+
161
+ static VALUE rb_sigar_new(VALUE module)
162
+ {
163
+ rb_sigar_t *rbsigar;
164
+ rbsigar = ALLOC(rb_sigar_t);
165
+ sigar_open(&(rbsigar->sigar));
166
+ return Data_Wrap_Struct(module, rb_sigar_mark, rb_sigar_close, rbsigar);
167
+ }
168
+
169
+ static VALUE rb_sigar_format_size(VALUE rclass, VALUE size)
170
+ {
171
+ char buffer[56];
172
+ return rb_str_new2(sigar_format_size(NUM2LL(size), buffer));
173
+ }
174
+
175
+ static VALUE rb_sigar_net_interface_flags_to_s(VALUE rclass, VALUE flags)
176
+ {
177
+ char buffer[1024];
178
+ return rb_str_new2(sigar_net_interface_flags_to_string(NUM2LL(flags), buffer));
179
+ }
180
+
181
+ static VALUE rb_sigar_net_connection_type_to_s(VALUE rclass, VALUE type)
182
+ {
183
+ return rb_str_new2(sigar_net_connection_type_get(NUM2INT(type)));
184
+ }
185
+
186
+ static VALUE rb_sigar_net_connection_state_to_s(VALUE rclass, VALUE state)
187
+ {
188
+ return rb_str_new2(sigar_net_connection_state_get(NUM2INT(state)));
189
+ }
190
+
191
+ static VALUE rb_sigar_net_address_to_string(sigar_net_address_t *address)
192
+ {
193
+ char addr_str[SIGAR_INET6_ADDRSTRLEN];
194
+ sigar_net_address_to_string(NULL, address, addr_str);
195
+ return rb_str_new2(addr_str);
196
+ }
197
+
198
+ static VALUE rb_sigar_net_scope_to_s(VALUE rclass, VALUE type)
199
+ {
200
+ return rb_str_new2(sigar_net_scope_to_string(NUM2INT(type)));
201
+ }
202
+
203
+ #define rb_sigar_net_address_to_s(a) rb_sigar_net_address_to_string(&a)
204
+
205
+ static VALUE rb_sigar_new_list(char *data, unsigned long number,
206
+ int size, VALUE rclass)
207
+ {
208
+ unsigned long i;
209
+ VALUE av = rb_ary_new2(number);
210
+
211
+ for (i=0; i<number; i++, data += size) {
212
+ void *ent = malloc(size);
213
+
214
+ memcpy(ent, data, size);
215
+ rb_ary_push(av, Data_Wrap_Struct(rclass, 0, free, ent));
216
+ }
217
+
218
+ return av;
219
+ }
220
+
221
+ static VALUE rb_sigar_new_strlist(char **data, unsigned long number)
222
+ {
223
+ unsigned long i;
224
+ VALUE av = rb_ary_new2(number);
225
+
226
+ for (i=0; i<number; i++) {
227
+ rb_ary_push(av, rb_str_new2(data[i]));
228
+ }
229
+
230
+ return av;
231
+ }
232
+
233
+ static VALUE rb_sigar_new_intlist(int *data, int number)
234
+ {
235
+ int i;
236
+ VALUE av = rb_ary_new2(number);
237
+
238
+ for (i=0; i<number; i++) {
239
+ rb_ary_push(av, rb_int2inum(data[i]));
240
+ }
241
+
242
+ return av;
243
+ }
244
+
245
+ static VALUE rb_sigar_new_doublelist(double *data, int number)
246
+ {
247
+ int i;
248
+ VALUE av = rb_ary_new2(number);
249
+
250
+ for (i=0; i<number; i++) {
251
+ rb_ary_push(av, rb_float_new(data[i]));
252
+ }
253
+
254
+ return av;
255
+ }
256
+
257
+ static VALUE rb_sigar_net_interface_list(VALUE obj)
258
+ {
259
+ SIGAR_GET;
260
+
261
+ int status;
262
+ sigar_net_interface_list_t iflist;
263
+ VALUE RETVAL;
264
+
265
+ status = sigar_net_interface_list_get(sigar, &iflist);
266
+ if (status != SIGAR_OK) {
267
+ RB_SIGAR_CROAK;
268
+ }
269
+
270
+ RETVAL = rb_sigar_new_strlist(iflist.data, iflist.number);
271
+
272
+ sigar_net_interface_list_destroy(sigar, &iflist);
273
+
274
+ return RETVAL;
275
+ }
276
+
277
+ static int rb_sigar_str2net_address(VALUE bytes, sigar_net_address_t *address)
278
+ {
279
+ long len = RSTRING_LEN(bytes);
280
+
281
+ switch (len) {
282
+ case 4:
283
+ address->family = SIGAR_AF_INET;
284
+ break;
285
+ case 4*4:
286
+ address->family = SIGAR_AF_INET6;
287
+ break;
288
+ default:
289
+ return EINVAL;
290
+ }
291
+
292
+ memcpy(RSTRING_PTR(bytes), &address->addr.in6, len);
293
+
294
+ return SIGAR_OK;
295
+ }
296
+
297
+ static VALUE rb_cSigarNetStat;
298
+
299
+ static VALUE rb_sigar_net_stat_get(VALUE obj, VALUE flags, VALUE bytes, int port)
300
+ {
301
+ SIGAR_GET;
302
+
303
+ int status;
304
+ int has_port = (port != -1);
305
+ sigar_net_stat_t *RETVAL = malloc(sizeof(*RETVAL));
306
+ sigar_net_address_t address;
307
+
308
+ if (has_port) {
309
+ status = rb_sigar_str2net_address(bytes, &address);
310
+ if (status == SIGAR_OK) {
311
+ status = sigar_net_stat_port_get(sigar, RETVAL, NUM2INT(flags),
312
+ &address, port);
313
+ }
314
+ }
315
+ else {
316
+ status = sigar_net_stat_get(sigar, RETVAL, NUM2INT(flags));
317
+ }
318
+
319
+ if (status != SIGAR_OK) {
320
+ free(RETVAL);
321
+ RB_SIGAR_CROAK;
322
+ }
323
+
324
+ return Data_Wrap_Struct(rb_cSigarNetStat, 0, rb_sigar_free, RETVAL);
325
+ }
326
+
327
+ static VALUE rb_sigar_net_stat(VALUE obj, VALUE flags)
328
+ {
329
+ return rb_sigar_net_stat_get(obj, flags, Qnil, -1);
330
+ }
331
+
332
+ static VALUE rb_sigar_net_stat_port(VALUE obj, VALUE flags, VALUE address, VALUE port)
333
+ {
334
+ return rb_sigar_net_stat_get(obj, flags, address, NUM2INT(port));
335
+ }
336
+
337
+ static VALUE rb_sigar_NetStat_tcp_states(VALUE self)
338
+ {
339
+ sigar_net_stat_t *net_stat;
340
+
341
+ Data_Get_Struct(self, sigar_net_stat_t, net_stat);
342
+
343
+ return rb_sigar_new_intlist(&net_stat->tcp_states[0], SIGAR_TCP_UNKNOWN);
344
+ }
345
+
346
+ static VALUE rb_cSigarNetConnection;
347
+
348
+ static VALUE rb_sigar_net_connection_list(VALUE obj, VALUE flags)
349
+ {
350
+ SIGAR_GET;
351
+
352
+ int status;
353
+ sigar_net_connection_list_t connlist;
354
+ VALUE RETVAL;
355
+
356
+ status = sigar_net_connection_list_get(sigar, &connlist, NUM2UINT(flags));
357
+
358
+ if (status != SIGAR_OK) {
359
+ RB_SIGAR_CROAK;
360
+ }
361
+
362
+ RETVAL = rb_sigar_new_list((char *)&connlist.data[0],
363
+ connlist.number,
364
+ sizeof(*connlist.data),
365
+ rb_cSigarNetConnection);
366
+
367
+ sigar_net_connection_list_destroy(sigar, &connlist);
368
+
369
+ return RETVAL;
370
+ }
371
+
372
+ static VALUE rb_sigar_net_services_name(VALUE obj, VALUE protocol, VALUE port)
373
+ {
374
+ SIGAR_GET;
375
+
376
+ char *name;
377
+
378
+ if ((name = sigar_net_services_name_get(sigar, NUM2UINT(protocol), NUM2UINT(port)))) {
379
+ return rb_str_new2(name);
380
+ }
381
+ else {
382
+ return Qnil;
383
+ }
384
+ }
385
+
386
+ static VALUE rb_cSigarCpuInfo;
387
+
388
+ static VALUE rb_sigar_cpu_info_list(VALUE obj)
389
+ {
390
+ SIGAR_GET;
391
+
392
+ int status;
393
+ sigar_cpu_info_list_t cpu_infos;
394
+ VALUE RETVAL;
395
+
396
+ status = sigar_cpu_info_list_get(sigar, &cpu_infos);
397
+ if (status != SIGAR_OK) {
398
+ RB_SIGAR_CROAK;
399
+ }
400
+
401
+ RETVAL = rb_sigar_new_list((char *)&cpu_infos.data[0],
402
+ cpu_infos.number,
403
+ sizeof(*cpu_infos.data),
404
+ rb_cSigarCpuInfo);
405
+
406
+ sigar_cpu_info_list_destroy(sigar, &cpu_infos);
407
+
408
+ return RETVAL;
409
+ }
410
+
411
+ static VALUE rb_sigar_loadavg(VALUE obj)
412
+ {
413
+ SIGAR_GET;
414
+
415
+ int status;
416
+ sigar_loadavg_t loadavg;
417
+
418
+ status = sigar_loadavg_get(sigar, &loadavg);
419
+ if (status != SIGAR_OK) {
420
+ RB_SIGAR_CROAK;
421
+ }
422
+
423
+ return rb_sigar_new_doublelist(&loadavg.loadavg[0], 3);
424
+ }
425
+
426
+ static VALUE rb_cSigarCpuPerc;
427
+
428
+ static VALUE rb_cSigarFileSystem;
429
+
430
+ static VALUE rb_sigar_file_system_list(VALUE obj)
431
+ {
432
+ SIGAR_GET;
433
+
434
+ int status;
435
+ sigar_file_system_list_t fslist;
436
+ VALUE RETVAL;
437
+
438
+ status = sigar_file_system_list_get(sigar, &fslist);
439
+ if (status != SIGAR_OK) {
440
+ RB_SIGAR_CROAK;
441
+ }
442
+
443
+ RETVAL = rb_sigar_new_list((char *)&fslist.data[0],
444
+ fslist.number,
445
+ sizeof(*fslist.data),
446
+ rb_cSigarFileSystem);
447
+
448
+ sigar_file_system_list_destroy(sigar, &fslist);
449
+
450
+ return RETVAL;
451
+ }
452
+
453
+ static VALUE rb_cSigarArp;
454
+
455
+ static VALUE rb_sigar_arp_list(VALUE obj)
456
+ {
457
+ SIGAR_GET;
458
+
459
+ int status;
460
+ sigar_arp_list_t list;
461
+ VALUE RETVAL;
462
+
463
+ status = sigar_arp_list_get(sigar, &list);
464
+ if (status != SIGAR_OK) {
465
+ RB_SIGAR_CROAK;
466
+ }
467
+
468
+ RETVAL = rb_sigar_new_list((char *)&list.data[0],
469
+ list.number,
470
+ sizeof(*list.data),
471
+ rb_cSigarArp);
472
+
473
+ sigar_arp_list_destroy(sigar, &list);
474
+
475
+ return RETVAL;
476
+ }
477
+
478
+ static VALUE rb_cSigarWho;
479
+
480
+ static VALUE rb_sigar_who_list(VALUE obj)
481
+ {
482
+ SIGAR_GET;
483
+
484
+ int status;
485
+ sigar_who_list_t list;
486
+ VALUE RETVAL;
487
+
488
+ status = sigar_who_list_get(sigar, &list);
489
+ if (status != SIGAR_OK) {
490
+ RB_SIGAR_CROAK;
491
+ }
492
+
493
+ RETVAL = rb_sigar_new_list((char *)&list.data[0],
494
+ list.number,
495
+ sizeof(*list.data),
496
+ rb_cSigarWho);
497
+
498
+ sigar_who_list_destroy(sigar, &list);
499
+
500
+ return RETVAL;
501
+ }
502
+
503
+ static VALUE rb_cSigarNetRoute;
504
+
505
+ static VALUE rb_sigar_net_route_list(VALUE obj)
506
+ {
507
+ SIGAR_GET;
508
+
509
+ int status;
510
+ sigar_net_route_list_t list;
511
+ VALUE RETVAL;
512
+
513
+ status = sigar_net_route_list_get(sigar, &list);
514
+ if (status != SIGAR_OK) {
515
+ RB_SIGAR_CROAK;
516
+ }
517
+
518
+ RETVAL = rb_sigar_new_list((char *)&list.data[0],
519
+ list.number,
520
+ sizeof(*list.data),
521
+ rb_cSigarNetRoute);
522
+
523
+ sigar_net_route_list_destroy(sigar, &list);
524
+
525
+ return RETVAL;
526
+ }
527
+
528
+ static VALUE rb_sigar_proc_list(int argc, VALUE *argv, VALUE obj)
529
+ {
530
+ SIGAR_GET;
531
+
532
+ int status;
533
+ sigar_proc_list_t list;
534
+ VALUE RETVAL;
535
+ VALUE vptql;
536
+
537
+ rb_scan_args(argc, argv, "01", &vptql);
538
+ if (NIL_P(vptql)) {
539
+ status = sigar_proc_list_get(sigar, &list);
540
+
541
+ if (status != SIGAR_OK) {
542
+ RB_SIGAR_CROAK;
543
+ }
544
+ }
545
+ else {
546
+ sigar_ptql_query_t *query;
547
+ sigar_ptql_error_t error;
548
+ char *ptql = StringValuePtr(vptql);
549
+
550
+ status = sigar_ptql_query_create(&query, ptql, &error);
551
+
552
+ if (status != SIGAR_OK) {
553
+ RB_SIGAR_RAISE(error.message);
554
+ }
555
+ sigar_ptql_re_impl_set(sigar, NULL, rbsigar_ptql_re_impl);
556
+ status = sigar_ptql_query_find(sigar, query, &list);
557
+ sigar_ptql_re_impl_set(sigar, NULL, NULL);
558
+ sigar_ptql_query_destroy(query);
559
+ if (status != SIGAR_OK) {
560
+ RB_SIGAR_RAISE(sigar_strerror(sigar, status));
561
+ }
562
+ }
563
+
564
+ RETVAL = rb_sigar_new_intlist(&list.data[0],
565
+ list.number);
566
+
567
+ sigar_proc_list_destroy(sigar, &list);
568
+
569
+ return RETVAL;
570
+ }
571
+
572
+ static VALUE rb_sigar_proc_args(VALUE obj, VALUE pid)
573
+ {
574
+ SIGAR_GET;
575
+
576
+ int status;
577
+ sigar_proc_args_t args;
578
+ VALUE RETVAL;
579
+
580
+ status = sigar_proc_args_get(sigar, OBJ2PID(pid), &args);
581
+
582
+ if (status != SIGAR_OK) {
583
+ RB_SIGAR_CROAK;
584
+ }
585
+
586
+ RETVAL = rb_sigar_new_strlist(args.data, args.number);
587
+
588
+ sigar_proc_args_destroy(sigar, &args);
589
+
590
+ return RETVAL;
591
+ }
592
+
593
+ static int rb_sigar_env_getall(void *data,
594
+ const char *key, int klen,
595
+ char *val, int vlen)
596
+ {
597
+ rb_hash_aset(*((VALUE*)data),
598
+ rb_str_new(key, klen),
599
+ rb_str_new(val, vlen));
600
+ return SIGAR_OK;
601
+ }
602
+
603
+ static VALUE rb_sigar_proc_port(VALUE obj, VALUE protocol, VALUE port)
604
+ {
605
+ SIGAR_GET;
606
+
607
+ int status;
608
+ sigar_pid_t RETVAL;
609
+
610
+ status = sigar_proc_port_get(sigar, NUM2INT(protocol), NUM2LL(port), &RETVAL);
611
+ if (status != SIGAR_OK) {
612
+ RB_SIGAR_CROAK;
613
+ }
614
+
615
+ return rb_int2inum(RETVAL);
616
+ }
617
+
618
+ static VALUE rb_sigar_proc_env(VALUE obj, VALUE pid)
619
+ {
620
+ SIGAR_GET;
621
+
622
+ int status;
623
+ sigar_proc_env_t procenv;
624
+ VALUE RETVAL = rb_hash_new();
625
+
626
+ procenv.type = SIGAR_PROC_ENV_ALL;
627
+ procenv.env_getter = rb_sigar_env_getall;
628
+ procenv.data = &RETVAL;
629
+
630
+ status = sigar_proc_env_get(sigar, OBJ2PID(pid), &procenv);
631
+ if (status != SIGAR_OK) {
632
+ RB_SIGAR_CROAK;
633
+ }
634
+
635
+ return RETVAL;
636
+ }
637
+
638
+ VALUE rb = Qnil;
639
+
640
+ static const char *logger_consts[] = {
641
+ "FATAL", /* SIGAR_LOG_FATAL */
642
+ "ERROR", /* SIGAR_LOG_ERROR */
643
+ "WARN", /* SIGAR_LOG_WARN */
644
+ "INFO", /* SIGAR_LOG_INFO */
645
+ "DEBUG", /* SIGAR_LOG_DEBUG */
646
+ "DEBUG", /* SIGAR_LOG_TRACE */
647
+ };
648
+
649
+ static void rb_sigar_logger_impl(sigar_t *sigar, void *data,
650
+ int level, char *message)
651
+ {
652
+ rb_sigar_t *rbsigar = ((rb_sigar_t*)data);
653
+ VALUE logger = rbsigar->logger;
654
+
655
+ /* XXX: cost of this, better way? */
656
+ VALUE logger_const = rb_const_get(rb_cObject, rb_intern("Logger"));
657
+ VALUE logger_level = rb_const_get(logger_const,
658
+ rb_intern(logger_consts[level]));
659
+ VALUE msg = rb_str_new2(message);
660
+
661
+ rb_funcall(logger, rb_intern ("add"), 2, logger_level, msg);
662
+
663
+ return;
664
+ }
665
+
666
+ static void rb_sigar_proc_impl(sigar_t *sigar, void *data,
667
+ int level, char *message)
668
+ {
669
+ rb_sigar_t *rbsigar = ((rb_sigar_t*)data);
670
+ VALUE logger = rbsigar->logger;
671
+
672
+ rb_funcall(logger, rb_intern("call"), 2, INT2FIX(level), rb_str_new2(message));
673
+
674
+ return;
675
+ }
676
+
677
+ static VALUE rb_sigar_logger(VALUE obj)
678
+ {
679
+ rb_sigar_t *rbsigar = rb_sigar_get(obj);
680
+
681
+ return rbsigar->logger;
682
+ }
683
+
684
+ static VALUE rb_sigar_set_logger(VALUE obj, VALUE logger)
685
+ {
686
+ SIGAR_GET;
687
+
688
+ if (rb_obj_is_kind_of(logger, rb_cProc) ||
689
+ rb_respond_to(logger, rb_intern("call"))) {
690
+
691
+ sigar_log_impl_set(sigar, rbsigar, rb_sigar_proc_impl);
692
+ rbsigar->logger = logger;
693
+
694
+ return obj;
695
+ }
696
+
697
+ /* Have to load Logger to test for it properly */
698
+ rb_require("logger");
699
+ if (rb_obj_is_kind_of(logger, rb_path2class("Logger"))) {
700
+ sigar_log_impl_set(sigar, rbsigar, rb_sigar_logger_impl);
701
+ rbsigar->logger = logger;
702
+ }
703
+ else {
704
+ rb_raise(rb_eArgError,
705
+ "value is not a proc object or subclass of Logger");
706
+ }
707
+
708
+ return obj;
709
+ }
710
+
711
+ static VALUE rb_sigar_log_level(VALUE obj)
712
+ {
713
+ SIGAR_GET;
714
+
715
+ return INT2FIX(sigar_log_level_get(sigar));
716
+ }
717
+
718
+ static VALUE rb_sigar_set_log_level(VALUE obj, VALUE level)
719
+ {
720
+ SIGAR_GET;
721
+
722
+ sigar_log_level_set(sigar, NUM2INT(level));
723
+
724
+ return obj;
725
+ }
726
+
727
+ static VALUE rb_sigar_fqdn(VALUE obj)
728
+ {
729
+ SIGAR_GET;
730
+
731
+ char fqdn[SIGAR_FQDN_LEN];
732
+ int status;
733
+
734
+ if ((status = sigar_fqdn_get(sigar, fqdn, sizeof(fqdn))) != SIGAR_OK) {
735
+ RB_SIGAR_CROAK;
736
+ }
737
+
738
+ return rb_str_new2(fqdn);
739
+ }
740
+
741
+ #include "./rbsigar_generated.rx"
742
+
743
+ static VALUE rb_sigar_cpu_list(VALUE obj)
744
+ {
745
+ SIGAR_GET;
746
+
747
+ int status;
748
+ sigar_cpu_list_t cpus;
749
+ VALUE RETVAL;
750
+
751
+ status = sigar_cpu_list_get(sigar, &cpus);
752
+ if (status != SIGAR_OK) {
753
+ RB_SIGAR_CROAK;
754
+ }
755
+
756
+ RETVAL = rb_sigar_new_list((char *)&cpus.data[0],
757
+ cpus.number,
758
+ sizeof(*cpus.data),
759
+ rb_cSigarCpu);
760
+
761
+ sigar_cpu_list_destroy(sigar, &cpus);
762
+
763
+ return RETVAL;
764
+ }
765
+
766
+ #define RB_SIGAR_CONST_INT(name) \
767
+ rb_define_const(rclass, #name, INT2FIX(SIGAR_##name))
768
+
769
+ #define RB_SIGAR_DEFINE_CONST_STR(name, value) \
770
+ rb_define_const(rclass, name, rb_obj_freeze(rb_str_new2(value)))
771
+
772
+ #define RB_SIGAR_CONST_STR(name) \
773
+ rb_define_const(rclass, #name, rb_obj_freeze(rb_str_new2(SIGAR_##name)))
774
+
775
+ static void Init_rbsigar_constants(VALUE rclass)
776
+ {
777
+ RB_SIGAR_CONST_INT(FSTYPE_NONE);
778
+ RB_SIGAR_CONST_INT(FSTYPE_LOCAL_DISK);
779
+ RB_SIGAR_CONST_INT(FSTYPE_NETWORK);
780
+ RB_SIGAR_CONST_INT(FSTYPE_RAM_DISK);
781
+ RB_SIGAR_CONST_INT(FSTYPE_CDROM);
782
+ RB_SIGAR_CONST_INT(FSTYPE_SWAP);
783
+
784
+ RB_SIGAR_CONST_INT(IFF_UP);
785
+ RB_SIGAR_CONST_INT(IFF_BROADCAST);
786
+ RB_SIGAR_CONST_INT(IFF_DEBUG);
787
+ RB_SIGAR_CONST_INT(IFF_LOOPBACK);
788
+ RB_SIGAR_CONST_INT(IFF_POINTOPOINT);
789
+ RB_SIGAR_CONST_INT(IFF_NOTRAILERS);
790
+ RB_SIGAR_CONST_INT(IFF_RUNNING);
791
+ RB_SIGAR_CONST_INT(IFF_NOARP);
792
+ RB_SIGAR_CONST_INT(IFF_PROMISC);
793
+ RB_SIGAR_CONST_INT(IFF_ALLMULTI);
794
+ RB_SIGAR_CONST_INT(IFF_MULTICAST);
795
+ RB_SIGAR_CONST_INT(IFF_SLAVE);
796
+ RB_SIGAR_CONST_INT(IFF_MASTER);
797
+ RB_SIGAR_CONST_INT(IFF_DYNAMIC);
798
+
799
+ RB_SIGAR_CONST_INT(NETCONN_CLIENT);
800
+ RB_SIGAR_CONST_INT(NETCONN_SERVER);
801
+ RB_SIGAR_CONST_INT(NETCONN_TCP);
802
+ RB_SIGAR_CONST_INT(NETCONN_UDP);
803
+ RB_SIGAR_CONST_INT(NETCONN_RAW);
804
+ RB_SIGAR_CONST_INT(NETCONN_UNIX);
805
+
806
+ RB_SIGAR_CONST_INT(TCP_ESTABLISHED);
807
+ RB_SIGAR_CONST_INT(TCP_SYN_SENT);
808
+ RB_SIGAR_CONST_INT(TCP_SYN_RECV);
809
+ RB_SIGAR_CONST_INT(TCP_FIN_WAIT1);
810
+ RB_SIGAR_CONST_INT(TCP_FIN_WAIT2);
811
+ RB_SIGAR_CONST_INT(TCP_TIME_WAIT);
812
+ RB_SIGAR_CONST_INT(TCP_CLOSE);
813
+ RB_SIGAR_CONST_INT(TCP_CLOSE_WAIT);
814
+ RB_SIGAR_CONST_INT(TCP_LAST_ACK);
815
+ RB_SIGAR_CONST_INT(TCP_LISTEN);
816
+ RB_SIGAR_CONST_INT(TCP_CLOSING);
817
+ RB_SIGAR_CONST_INT(TCP_IDLE);
818
+ RB_SIGAR_CONST_INT(TCP_BOUND);
819
+ RB_SIGAR_CONST_INT(TCP_UNKNOWN);
820
+
821
+ RB_SIGAR_CONST_INT(RTF_UP);
822
+ RB_SIGAR_CONST_INT(RTF_GATEWAY);
823
+ RB_SIGAR_CONST_INT(RTF_HOST);
824
+
825
+ RB_SIGAR_CONST_STR(NULL_HWADDR);
826
+
827
+ RB_SIGAR_CONST_INT(LOG_FATAL);
828
+ RB_SIGAR_CONST_INT(LOG_ERROR);
829
+ RB_SIGAR_CONST_INT(LOG_WARN);
830
+ RB_SIGAR_CONST_INT(LOG_INFO);
831
+ RB_SIGAR_CONST_INT(LOG_DEBUG);
832
+ RB_SIGAR_CONST_INT(LOG_TRACE);
833
+ }
834
+
835
+ static void Init_rbsigar_version(VALUE rclass)
836
+ {
837
+ sigar_version_t *sv = sigar_version_get();
838
+ RB_SIGAR_DEFINE_CONST_STR("BUILD_DATE", sv->build_date);
839
+ RB_SIGAR_DEFINE_CONST_STR("SCM_REVISION", sv->scm_revision);
840
+ RB_SIGAR_DEFINE_CONST_STR("VERSION", sv->version);
841
+ }
842
+
843
+ void Init_sigar(void)
844
+ {
845
+ VALUE rclass = rb_define_class("Sigar", rb_cObject);
846
+
847
+ rb_define_method(rclass, "logger", rb_sigar_logger, 0);
848
+ rb_define_method(rclass, "logger=", rb_sigar_set_logger, 1);
849
+
850
+ rb_define_method(rclass, "log_level", rb_sigar_log_level, 0);
851
+ rb_define_method(rclass, "log_level=", rb_sigar_set_log_level, 1);
852
+
853
+ rb_define_method(rclass, "cpu_info_list", rb_sigar_cpu_info_list, 0);
854
+ rb_define_method(rclass, "cpu_list", rb_sigar_cpu_list, 0);
855
+ rb_define_method(rclass, "loadavg", rb_sigar_loadavg, 0);
856
+ rb_define_method(rclass, "file_system_list", rb_sigar_file_system_list, 0);
857
+ rb_define_method(rclass, "net_connection_list", rb_sigar_net_connection_list, 1);
858
+ rb_define_method(rclass, "net_interface_list", rb_sigar_net_interface_list, 0);
859
+ rb_define_method(rclass, "net_services_name", rb_sigar_net_services_name, 2);
860
+ rb_define_method(rclass, "net_stat", rb_sigar_net_stat, 1);
861
+ rb_define_method(rclass, "net_stat_port", rb_sigar_net_stat_port, 3);
862
+ rb_define_method(rclass, "net_route_list", rb_sigar_net_route_list, 0);
863
+ rb_define_method(rclass, "arp_list", rb_sigar_arp_list, 0);
864
+ rb_define_method(rclass, "who_list", rb_sigar_who_list, 0);
865
+ rb_define_method(rclass, "proc_list", rb_sigar_proc_list, -1);
866
+ rb_define_method(rclass, "proc_args", rb_sigar_proc_args, 1);
867
+ rb_define_method(rclass, "proc_env", rb_sigar_proc_env, 1);
868
+ rb_define_method(rclass, "proc_port", rb_sigar_proc_port, 2);
869
+ rb_define_method(rclass, "fqdn", rb_sigar_fqdn, 0);
870
+
871
+ rb_define_singleton_method(rclass, "new", rb_sigar_new, 0);
872
+ rb_define_singleton_method(rclass, "format_size", rb_sigar_format_size, 1);
873
+ rb_define_singleton_method(rclass, "net_interface_flags_to_s",
874
+ rb_sigar_net_interface_flags_to_s, 1);
875
+ rb_define_singleton_method(rclass, "net_scope_to_s",
876
+ rb_sigar_net_scope_to_s, 1);
877
+ rb_define_singleton_method(rclass, "net_connection_type_to_s",
878
+ rb_sigar_net_connection_type_to_s, 1);
879
+ rb_define_singleton_method(rclass, "net_connection_state_to_s",
880
+ rb_sigar_net_connection_state_to_s, 1);
881
+
882
+ Init_rbsigar_constants(rclass);
883
+ Init_rbsigar_version(rclass);
884
+
885
+ /* generated */
886
+ rb_sigar_define_module_methods(rclass);
887
+ rb_define_method(rb_cSigarNetStat, "tcp_states", rb_sigar_NetStat_tcp_states, 0);
888
+ }