dag-statgrab 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/LICENSE +202 -0
  2. data/README.mkdn +54 -0
  3. data/extconf.rb +3 -0
  4. data/statgrab.c +1136 -0
  5. metadata +64 -0
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/README.mkdn ADDED
@@ -0,0 +1,54 @@
1
+ Statgrab for Ruby
2
+ =================
3
+
4
+ Ruby bindings to the libstatgrab portable system statistics library.
5
+
6
+ Building and installing
7
+ -----------------------
8
+
9
+ First, make sure libstatgrab is installed on your system.
10
+
11
+ Follow these procedures to build and install manually:
12
+
13
+ ruby extconf.rb
14
+ make
15
+ su
16
+ make install
17
+
18
+ Optionally, you may build a Rubygem:
19
+
20
+ gem build statgrab.gemspec
21
+ su
22
+ gem install statgrab-0.1.gem
23
+
24
+ API Documentation
25
+ -----------------
26
+
27
+ See <http://statgrab.rubyforge.org/doc/>.
28
+
29
+ Sample program
30
+ --------------
31
+
32
+ require 'statgrab'
33
+ begin
34
+ sg = Statgrab.new
35
+ puts "CPU Usage: %.1f%%" % sg.cpu_percents[:user]
36
+ rescue Statgrab::Exception => e
37
+ puts "Error! %s" % e.message
38
+ end
39
+
40
+ Licensing
41
+ ---------
42
+
43
+ Copyright 2008 Dag Odenhall <dag.odenhall@gmail.com>
44
+
45
+ Licensed under the Apache License, Version 2.0 (the "License").
46
+ You may obtain a copy of the License at
47
+
48
+ <http://www.apache.org/licenses/LICENSE-2.0>
49
+
50
+ Unless required by applicable law or agreed to in writing, software
51
+ distributed under the License is distributed on an "AS IS" BASIS,
52
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
53
+ See the License for the specific language governing permissions and
54
+ limitations under the License.
data/extconf.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'mkmf'
2
+ have_library("statgrab")
3
+ create_makefile("statgrab")
data/statgrab.c ADDED
@@ -0,0 +1,1136 @@
1
+ /*
2
+ * Copyright 2008 Dag Odenhall <dag.odenhall@gmail.com>
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ #include <ruby.h>
18
+ #include <statgrab.h>
19
+ #include <string.h>
20
+
21
+ VALUE cStatgrab;
22
+ VALUE eStatgrabException;
23
+ VALUE eStatgrabAsprintfError;
24
+ VALUE eStatgrabDevicesError;
25
+ VALUE eStatgrabDevstatGetdevsError;
26
+ VALUE eStatgrabDevstatSelectdevsError;
27
+ VALUE eStatgrabDiskinfoError;
28
+ VALUE eStatgrabEnoentError;
29
+ VALUE eStatgrabGetifaddrsError;
30
+ VALUE eStatgrabGetmntinfoError;
31
+ VALUE eStatgrabGetpagesizeError;
32
+ VALUE eStatgrabHostError;
33
+ VALUE eStatgrabKstatDataLookupError;
34
+ VALUE eStatgrabKstatLookupError;
35
+ VALUE eStatgrabKstatOpenError;
36
+ VALUE eStatgrabKstatReadError;
37
+ VALUE eStatgrabKvmGetswapinfoError;
38
+ VALUE eStatgrabKvmOpenfilesError;
39
+ VALUE eStatgrabMallocError;
40
+ VALUE eStatgrabMemstatusError;
41
+ VALUE eStatgrabOpenError;
42
+ VALUE eStatgrabOpendirError;
43
+ VALUE eStatgrabParseError;
44
+ VALUE eStatgrabPdhaddError;
45
+ VALUE eStatgrabPdhcollectError;
46
+ VALUE eStatgrabPdhopenError;
47
+ VALUE eStatgrabPdhreadError;
48
+ VALUE eStatgrabPermissionError;
49
+ VALUE eStatgrabPstatError;
50
+ VALUE eStatgrabSetegidError;
51
+ VALUE eStatgrabSeteuidError;
52
+ VALUE eStatgrabSetmntentError;
53
+ VALUE eStatgrabSocketError;
54
+ VALUE eStatgrabSwapctlError;
55
+ VALUE eStatgrabSysconfError;
56
+ VALUE eStatgrabSysctlError;
57
+ VALUE eStatgrabSysctlbynameError;
58
+ VALUE eStatgrabSysctlnametomibError;
59
+ VALUE eStatgrabUnameError;
60
+ VALUE eStatgrabUnsupportedError;
61
+ VALUE eStatgrabXswVerMismatchError;
62
+
63
+ /*
64
+ * Get the latest error from statgrab and, if there is one,
65
+ * raise the corresponding Ruby exception with the error message.
66
+ * Only used internally.
67
+ */
68
+ static void
69
+ statgrab_handle_error()
70
+ {
71
+ sg_error err_num;
72
+ err_num = sg_get_error();
73
+
74
+ switch(err_num) {
75
+ case SG_ERROR_NONE:
76
+ return;
77
+ case SG_ERROR_ASPRINTF:
78
+ rb_raise(eStatgrabAsprintfError, "%s",
79
+ sg_str_error(err_num));
80
+ break;
81
+ case SG_ERROR_DEVICES:
82
+ rb_raise(eStatgrabDevicesError, "%s", sg_str_error(err_num));
83
+ break;
84
+ case SG_ERROR_DEVSTAT_GETDEVS:
85
+ rb_raise(eStatgrabDevstatGetdevsError, "%s",
86
+ sg_str_error(err_num));
87
+ break;
88
+ case SG_ERROR_DEVSTAT_SELECTDEVS:
89
+ rb_raise(eStatgrabDevstatSelectdevsError, "%s",
90
+ sg_str_error(err_num));
91
+ break;
92
+ case SG_ERROR_DISKINFO:
93
+ rb_raise(eStatgrabDiskinfoError, "%s",
94
+ sg_str_error(err_num));
95
+ break;
96
+ case SG_ERROR_ENOENT:
97
+ rb_raise(eStatgrabEnoentError, "%s", sg_str_error(err_num));
98
+ break;
99
+ case SG_ERROR_GETIFADDRS:
100
+ rb_raise(eStatgrabGetifaddrsError, "%s",
101
+ sg_str_error(err_num));
102
+ break;
103
+ case SG_ERROR_GETMNTINFO:
104
+ rb_raise(eStatgrabGetmntinfoError, "%s",
105
+ sg_str_error(err_num));
106
+ break;
107
+ case SG_ERROR_GETPAGESIZE:
108
+ rb_raise(eStatgrabGetpagesizeError, "%s",
109
+ sg_str_error(err_num));
110
+ break;
111
+ case SG_ERROR_HOST:
112
+ rb_raise(eStatgrabHostError, "%s", sg_str_error(err_num));
113
+ break;
114
+ case SG_ERROR_KSTAT_DATA_LOOKUP:
115
+ rb_raise(eStatgrabKstatDataLookupError, "%s",
116
+ sg_str_error(err_num));
117
+ break;
118
+ case SG_ERROR_KSTAT_LOOKUP:
119
+ rb_raise(eStatgrabKstatLookupError, "%s",
120
+ sg_str_error(err_num));
121
+ break;
122
+ case SG_ERROR_KSTAT_OPEN:
123
+ rb_raise(eStatgrabKstatOpenError, "%s",
124
+ sg_str_error(err_num));
125
+ break;
126
+ case SG_ERROR_KSTAT_READ:
127
+ rb_raise(eStatgrabKstatReadError, "%s",
128
+ sg_str_error(err_num));
129
+ break;
130
+ case SG_ERROR_KVM_GETSWAPINFO:
131
+ rb_raise(eStatgrabKvmGetswapinfoError, "%s",
132
+ sg_str_error(err_num));
133
+ break;
134
+ case SG_ERROR_KVM_OPENFILES:
135
+ rb_raise(eStatgrabKvmOpenfilesError, "%s",
136
+ sg_str_error(err_num));
137
+ break;
138
+ case SG_ERROR_MALLOC:
139
+ rb_raise(eStatgrabMallocError, "%s", sg_str_error(err_num));
140
+ break;
141
+ case SG_ERROR_MEMSTATUS:
142
+ rb_raise(eStatgrabMemstatusError, "%s",
143
+ sg_str_error(err_num));
144
+ break;
145
+ case SG_ERROR_OPEN:
146
+ rb_raise(eStatgrabOpenError, "%s", sg_str_error(err_num));
147
+ break;
148
+ case SG_ERROR_OPENDIR:
149
+ rb_raise(eStatgrabOpendirError, "%s", sg_str_error(err_num));
150
+ break;
151
+ case SG_ERROR_PARSE:
152
+ rb_raise(eStatgrabParseError, "%s", sg_str_error(err_num));
153
+ break;
154
+ case SG_ERROR_PDHADD:
155
+ rb_raise(eStatgrabPdhaddError, "%s", sg_str_error(err_num));
156
+ break;
157
+ case SG_ERROR_PDHCOLLECT:
158
+ rb_raise(eStatgrabPdhcollectError, "%s",
159
+ sg_str_error(err_num));
160
+ break;
161
+ case SG_ERROR_PDHOPEN:
162
+ rb_raise(eStatgrabPdhopenError, "%s", sg_str_error(err_num));
163
+ break;
164
+ case SG_ERROR_PDHREAD:
165
+ rb_raise(eStatgrabPdhreadError, "%s", sg_str_error(err_num));
166
+ break;
167
+ case SG_ERROR_PERMISSION:
168
+ rb_raise(eStatgrabPermissionError, "%s",
169
+ sg_str_error(err_num));
170
+ break;
171
+ case SG_ERROR_PSTAT:
172
+ rb_raise(eStatgrabPstatError, "%s", sg_str_error(err_num));
173
+ break;
174
+ case SG_ERROR_SETEGID:
175
+ rb_raise(eStatgrabSetegidError, "%s", sg_str_error(err_num));
176
+ break;
177
+ case SG_ERROR_SETEUID:
178
+ rb_raise(eStatgrabSeteuidError, "%s", sg_str_error(err_num));
179
+ break;
180
+ case SG_ERROR_SETMNTENT:
181
+ rb_raise(eStatgrabSetmntentError, "%s",
182
+ sg_str_error(err_num));
183
+ break;
184
+ case SG_ERROR_SOCKET:
185
+ rb_raise(eStatgrabSocketError, "%s", sg_str_error(err_num));
186
+ break;
187
+ case SG_ERROR_SWAPCTL:
188
+ rb_raise(eStatgrabSwapctlError, "%s", sg_str_error(err_num));
189
+ break;
190
+ case SG_ERROR_SYSCONF:
191
+ rb_raise(eStatgrabSysconfError, "%s", sg_str_error(err_num));
192
+ break;
193
+ case SG_ERROR_SYSCTL:
194
+ rb_raise(eStatgrabSysctlError, "%s", sg_str_error(err_num));
195
+ break;
196
+ case SG_ERROR_SYSCTLBYNAME:
197
+ rb_raise(eStatgrabSysctlbynameError, "%s",
198
+ sg_str_error(err_num));
199
+ break;
200
+ case SG_ERROR_SYSCTLNAMETOMIB:
201
+ rb_raise(eStatgrabSysctlnametomibError, "%s",
202
+ sg_str_error(err_num));
203
+ break;
204
+ case SG_ERROR_UNAME:
205
+ rb_raise(eStatgrabUnameError, "%s", sg_str_error(err_num));
206
+ break;
207
+ case SG_ERROR_UNSUPPORTED:
208
+ rb_raise(eStatgrabUnsupportedError, "%s",
209
+ sg_str_error(err_num));
210
+ break;
211
+ case SG_ERROR_XSW_VER_MISMATCH:
212
+ rb_raise(eStatgrabXswVerMismatchError, "%s",
213
+ sg_str_error(err_num));
214
+ break;
215
+ }
216
+ }
217
+
218
+ /*
219
+ * call-seq:
220
+ * Statgrab.new(drop_privileges=true)
221
+ *
222
+ * Set privileges and prepare connections, and then drop privileges.
223
+ * See <tt>statgrab(3)</tt> manpage.
224
+ */
225
+ static VALUE
226
+ statgrab_initialize(VALUE self, VALUE args)
227
+ {
228
+ if (sg_init())
229
+ statgrab_handle_error();
230
+
231
+ if (rb_ary_shift(args) != Qfalse && sg_drop_privileges())
232
+ statgrab_handle_error();
233
+
234
+ return self;
235
+ }
236
+
237
+ /*
238
+ * CPU usage as ticks spent in different states,
239
+ * see <tt>sg_get_cpu_stats(3)</tt> manpage.
240
+ */
241
+ static VALUE
242
+ statgrab_cpu_stats(VALUE self)
243
+ {
244
+ sg_cpu_stats *stats;
245
+ VALUE info, time_at;
246
+
247
+ stats = sg_get_cpu_stats();
248
+ if (stats == NULL)
249
+ statgrab_handle_error();
250
+
251
+ info = rb_hash_new();
252
+ rb_hash_aset(info, ID2SYM(rb_intern("user")), INT2NUM(stats->user));
253
+ rb_hash_aset(info, ID2SYM(rb_intern("kernel")),
254
+ INT2NUM(stats->kernel));
255
+ rb_hash_aset(info, ID2SYM(rb_intern("idle")), INT2NUM(stats->idle));
256
+ rb_hash_aset(info, ID2SYM(rb_intern("iowait")),
257
+ INT2NUM(stats->iowait));
258
+ rb_hash_aset(info, ID2SYM(rb_intern("swap")), INT2NUM(stats->swap));
259
+ rb_hash_aset(info, ID2SYM(rb_intern("nice")), INT2NUM(stats->nice));
260
+ rb_hash_aset(info, ID2SYM(rb_intern("systime")),
261
+ INT2NUM(stats->systime));
262
+
263
+ time_at = rb_funcall(rb_cTime, rb_intern("at"), 1,
264
+ INT2NUM(stats->systime));
265
+ rb_hash_aset(info, ID2SYM(rb_intern("time")), time_at);
266
+
267
+ return info;
268
+ }
269
+
270
+ /*
271
+ * CPU usage as ticks spent since last call,
272
+ * see <tt>sg_get_cpu_stats_diff(3)</tt> manpage.
273
+ */
274
+ static VALUE
275
+ statgrab_cpu_stats_diff(VALUE self)
276
+ {
277
+ sg_cpu_stats *stats;
278
+ VALUE info, time_at;
279
+
280
+ stats = sg_get_cpu_stats_diff();
281
+ if (stats == NULL)
282
+ statgrab_handle_error();
283
+
284
+ info = rb_hash_new();
285
+ rb_hash_aset(info, ID2SYM(rb_intern("user")), INT2NUM(stats->user));
286
+ rb_hash_aset(info, ID2SYM(rb_intern("kernel")),
287
+ INT2NUM(stats->kernel));
288
+ rb_hash_aset(info, ID2SYM(rb_intern("idle")), INT2NUM(stats->idle));
289
+ rb_hash_aset(info, ID2SYM(rb_intern("iowait")),
290
+ INT2NUM(stats->iowait));
291
+ rb_hash_aset(info, ID2SYM(rb_intern("swap")), INT2NUM(stats->swap));
292
+ rb_hash_aset(info, ID2SYM(rb_intern("nice")), INT2NUM(stats->nice));
293
+ rb_hash_aset(info, ID2SYM(rb_intern("systime")),
294
+ INT2NUM(stats->systime));
295
+
296
+ time_at = rb_funcall(rb_cTime, rb_intern("at"), 1,
297
+ INT2NUM(stats->systime));
298
+ rb_hash_aset(info, ID2SYM(rb_intern("time")), time_at);
299
+
300
+ return info;
301
+ }
302
+
303
+ /*
304
+ * CPU usage as ticks difference in percents,
305
+ * see <tt>sg_get_cpu_percents(3)</tt> manpage.
306
+ */
307
+ static VALUE
308
+ statgrab_cpu_percents(VALUE self)
309
+ {
310
+ sg_cpu_percents *percents;
311
+ VALUE info, time_now;
312
+
313
+ percents = sg_get_cpu_percents();
314
+ if (percents == NULL)
315
+ statgrab_handle_error();
316
+
317
+ info = rb_hash_new();
318
+ rb_hash_aset(info, ID2SYM(rb_intern("user")),
319
+ rb_float_new(percents->user));
320
+ rb_hash_aset(info, ID2SYM(rb_intern("kernel")),
321
+ rb_float_new(percents->kernel));
322
+ rb_hash_aset(info, ID2SYM(rb_intern("idle")),
323
+ rb_float_new(percents->idle));
324
+ rb_hash_aset(info, ID2SYM(rb_intern("iowait")),
325
+ rb_float_new(percents->iowait));
326
+ rb_hash_aset(info, ID2SYM(rb_intern("swap")),
327
+ rb_float_new(percents->swap));
328
+ rb_hash_aset(info, ID2SYM(rb_intern("nice")),
329
+ rb_float_new(percents->nice));
330
+ rb_hash_aset(info, ID2SYM(rb_intern("time_taken")),
331
+ INT2NUM(percents->time_taken));
332
+
333
+ time_now = rb_funcall(rb_cTime, rb_intern("now"), 0);
334
+ rb_hash_aset(info, ID2SYM(rb_intern("last_call")),
335
+ rb_funcall(time_now, rb_intern("-"), 1,
336
+ INT2NUM(percents->time_taken)));
337
+
338
+ return info;
339
+ }
340
+
341
+ /*
342
+ * Disk I/O statistics, see <tt>sg_get_disk_io_stats(3)</tt> manpage.
343
+ */
344
+ static VALUE
345
+ statgrab_disk_io_stats(VALUE self)
346
+ {
347
+ int entries, i;
348
+ sg_disk_io_stats *stats;
349
+ VALUE arr, info, time_at;
350
+
351
+ stats = sg_get_disk_io_stats(&entries);
352
+ if (stats == NULL)
353
+ statgrab_handle_error();
354
+
355
+ arr = rb_ary_new();
356
+ for (i = 0; i < entries; i++) {
357
+ info = rb_hash_new();
358
+ rb_hash_aset(info, ID2SYM(rb_intern("disk_name")),
359
+ rb_str_new2(stats[i].disk_name));
360
+ rb_hash_aset(info, ID2SYM(rb_intern("read_bytes")),
361
+ INT2NUM(stats[i].read_bytes/1024));
362
+ rb_hash_aset(info, ID2SYM(rb_intern("write_bytes")),
363
+ INT2NUM(stats[i].write_bytes/1024));
364
+ rb_hash_aset(info, ID2SYM(rb_intern("systime")),
365
+ INT2NUM(stats[i].systime));
366
+
367
+ time_at = rb_funcall(rb_cTime, rb_intern("at"), 1,
368
+ INT2NUM(stats[i].systime));
369
+ rb_hash_aset(info, ID2SYM(rb_intern("time")), time_at);
370
+
371
+ rb_ary_push(arr, info);
372
+ }
373
+
374
+ return arr;
375
+ }
376
+
377
+ /*
378
+ * Disk I/O statistics difference since last call,
379
+ * see <tt>sg_get_disk_io_stats_diff(3)</tt> manpage.
380
+ */
381
+ static VALUE
382
+ statgrab_disk_io_stats_diff(VALUE self)
383
+ {
384
+ int entries, i;
385
+ sg_disk_io_stats *stats;
386
+ VALUE arr, info, time_now;
387
+
388
+ stats = sg_get_disk_io_stats_diff(&entries);
389
+ if (stats == NULL)
390
+ statgrab_handle_error();
391
+
392
+ arr = rb_ary_new();
393
+ for(i = 0; i < entries; i++) {
394
+ info = rb_hash_new();
395
+ rb_hash_aset(info, ID2SYM(rb_intern("disk_name")),
396
+ rb_str_new2(stats[i].disk_name));
397
+ rb_hash_aset(info, ID2SYM(rb_intern("read_bytes")),
398
+ INT2NUM(stats[i].read_bytes/1024));
399
+ rb_hash_aset(info, ID2SYM(rb_intern("write_bytes")),
400
+ INT2NUM(stats[i].write_bytes/1024));
401
+ rb_hash_aset(info, ID2SYM(rb_intern("systime")),
402
+ INT2NUM(stats[i].systime));
403
+
404
+ time_now = rb_funcall(rb_cTime, rb_intern("now"), 0);
405
+ rb_hash_aset(info, ID2SYM(rb_intern("last_call")),
406
+ rb_funcall(time_now, rb_intern("-"), 1,
407
+ INT2NUM(stats[i].systime)));
408
+
409
+ rb_ary_push(arr, info);
410
+ }
411
+
412
+ return arr;
413
+ }
414
+
415
+ /*
416
+ * Filesystem statistics, see <tt>sg_get_fs_stats(3)</tt> manpage.
417
+ */
418
+ static VALUE
419
+ statgrab_fs_stats(VALUE self)
420
+ {
421
+ int entries, i;
422
+ sg_fs_stats *stats;
423
+ VALUE arr, info;
424
+
425
+ stats = sg_get_fs_stats(&entries);
426
+ if (stats == NULL)
427
+ statgrab_handle_error();
428
+
429
+ arr = rb_ary_new();
430
+ for (i = 0; i < entries; i++) {
431
+ info = rb_hash_new();
432
+ rb_hash_aset(info, ID2SYM(rb_intern("device_name")),
433
+ rb_str_new2(stats[i].device_name));
434
+ rb_hash_aset(info, ID2SYM(rb_intern("fs_type")),
435
+ rb_str_new2(stats[i].fs_type));
436
+ rb_hash_aset(info, ID2SYM(rb_intern("mnt_point")),
437
+ rb_str_new2(stats[i].mnt_point));
438
+ rb_hash_aset(info, ID2SYM(rb_intern("size")),
439
+ INT2NUM(stats[i].size/1024));
440
+ rb_hash_aset(info, ID2SYM(rb_intern("used")),
441
+ INT2NUM(stats[i].used/1024));
442
+ rb_hash_aset(info, ID2SYM(rb_intern("avail")),
443
+ INT2NUM(stats[i].avail/1024));
444
+ rb_hash_aset(info, ID2SYM(rb_intern("total_inodes")),
445
+ INT2NUM(stats[i].total_inodes));
446
+ rb_hash_aset(info, ID2SYM(rb_intern("used_inodes")),
447
+ INT2NUM(stats[i].used_inodes));
448
+ rb_hash_aset(info, ID2SYM(rb_intern("free_inodes")),
449
+ INT2NUM(stats[i].free_inodes));
450
+ rb_hash_aset(info, ID2SYM(rb_intern("avail_blocks")),
451
+ INT2NUM(stats[i].avail_blocks));
452
+ rb_hash_aset(info, ID2SYM(rb_intern("io_size")),
453
+ INT2NUM(stats[i].io_size));
454
+ rb_hash_aset(info, ID2SYM(rb_intern("block_size")),
455
+ INT2NUM(stats[i].block_size));
456
+ rb_hash_aset(info, ID2SYM(rb_intern("total_blocks")),
457
+ INT2NUM(stats[i].total_blocks));
458
+ rb_hash_aset(info, ID2SYM(rb_intern("free_blocks")),
459
+ INT2NUM(stats[i].free_blocks));
460
+ rb_hash_aset(info, ID2SYM(rb_intern("used_blocks")),
461
+ INT2NUM(stats[i].used_blocks));
462
+ rb_hash_aset(info, ID2SYM(rb_intern("avail_blocks")),
463
+ INT2NUM(stats[i].avail_blocks));
464
+
465
+ rb_ary_push(arr, info);
466
+ }
467
+
468
+ return arr;
469
+ }
470
+
471
+ /*
472
+ * General operating system statistics,
473
+ * see <tt>sg_get_host_info(3)</tt> manpage.
474
+ */
475
+ static VALUE
476
+ statgrab_host_info(VALUE self)
477
+ {
478
+ sg_host_info *host;
479
+ VALUE info, time_now;
480
+
481
+ host = sg_get_host_info();
482
+ if (host == NULL)
483
+ statgrab_handle_error();
484
+
485
+ info = rb_hash_new();
486
+ rb_hash_aset(info, ID2SYM(rb_intern("os_name")),
487
+ rb_str_new2(host->os_name));
488
+ rb_hash_aset(info, ID2SYM(rb_intern("os_release")),
489
+ rb_str_new2(host->os_release));
490
+ rb_hash_aset(info, ID2SYM(rb_intern("os_version")),
491
+ rb_str_new2(host->os_version));
492
+ rb_hash_aset(info, ID2SYM(rb_intern("platform")),
493
+ rb_str_new2(host->platform));
494
+ rb_hash_aset(info, ID2SYM(rb_intern("hostname")),
495
+ rb_str_new2(host->hostname));
496
+ rb_hash_aset(info, ID2SYM(rb_intern("uptime")),
497
+ INT2NUM(host->uptime));
498
+
499
+ time_now = rb_funcall(rb_cTime, rb_intern("now"), 0);
500
+ rb_hash_aset(info, ID2SYM(rb_intern("booted")),
501
+ rb_funcall(time_now, rb_intern("-"), 1,
502
+ INT2NUM(host->uptime)));
503
+
504
+ return info;
505
+ }
506
+
507
+ /*
508
+ * System load, see <tt>sg_get_load_stats(3)</tt> manpage.
509
+ */
510
+ static VALUE
511
+ statgrab_load_stats(VALUE self)
512
+ {
513
+ sg_load_stats *load;
514
+ VALUE info;
515
+
516
+ load = sg_get_load_stats();
517
+ if (load == NULL)
518
+ statgrab_handle_error();
519
+
520
+ info = rb_hash_new();
521
+ rb_hash_aset(info, ID2SYM(rb_intern("min1")),
522
+ rb_float_new(load->min1));
523
+ rb_hash_aset(info, ID2SYM(rb_intern("min5")),
524
+ rb_float_new(load->min5));
525
+ rb_hash_aset(info, ID2SYM(rb_intern("min15")),
526
+ rb_float_new(load->min15));
527
+
528
+ return info;
529
+ }
530
+
531
+ /*
532
+ * Memory statistics, see <tt>sg_get_mem_stats(3)</tt> manpage.
533
+ */
534
+ static VALUE
535
+ statgrab_mem_stats(VALUE self)
536
+ {
537
+ sg_mem_stats *stats;
538
+ VALUE info;
539
+
540
+ stats = sg_get_mem_stats();
541
+ if (stats == NULL)
542
+ statgrab_handle_error();
543
+
544
+ info = rb_hash_new();
545
+ rb_hash_aset(info, ID2SYM(rb_intern("total")),
546
+ INT2NUM(stats->total/1024));
547
+ rb_hash_aset(info, ID2SYM(rb_intern("free")),
548
+ INT2NUM(stats->free/1024));
549
+ rb_hash_aset(info, ID2SYM(rb_intern("used")),
550
+ INT2NUM(stats->used/1024));
551
+ rb_hash_aset(info, ID2SYM(rb_intern("cache")),
552
+ INT2NUM(stats->cache/1024));
553
+
554
+ return info;
555
+ }
556
+
557
+ /*
558
+ * Swap statistics, see <tt>sg_get_swap_stats(3)</tt> manpage.
559
+ */
560
+ static VALUE
561
+ statgrab_swap_stats(VALUE self)
562
+ {
563
+ sg_swap_stats *stats;
564
+ VALUE info;
565
+
566
+ stats = sg_get_swap_stats();
567
+ if (stats == NULL)
568
+ statgrab_handle_error();
569
+
570
+ info = rb_hash_new();
571
+ rb_hash_aset(info, ID2SYM(rb_intern("total")),
572
+ INT2NUM(stats->total/1024));
573
+ rb_hash_aset(info, ID2SYM(rb_intern("used")),
574
+ INT2NUM(stats->used/1024));
575
+ rb_hash_aset(info, ID2SYM(rb_intern("free")),
576
+ INT2NUM(stats->free/1024));
577
+
578
+ return info;
579
+ }
580
+
581
+ /*
582
+ * Network statistics, see <tt>sg_get_network_io_stats(3)</tt> manpage.
583
+ */
584
+ static VALUE
585
+ statgrab_network_io_stats(VALUE self)
586
+ {
587
+ int entries, i;
588
+ sg_network_io_stats *stats;
589
+ VALUE arr, info, time_at;
590
+
591
+ stats = sg_get_network_io_stats(&entries);
592
+ if (stats == NULL)
593
+ statgrab_handle_error();
594
+
595
+ arr = rb_ary_new();
596
+ for (i = 0; i < entries; i++) {
597
+ info = rb_hash_new();
598
+ rb_hash_aset(info, ID2SYM(rb_intern("interface_name")),
599
+ rb_str_new2(stats[i].interface_name));
600
+ rb_hash_aset(info, ID2SYM(rb_intern("tx")),
601
+ INT2NUM(stats[i].tx/1024));
602
+ rb_hash_aset(info, ID2SYM(rb_intern("rx")),
603
+ INT2NUM(stats[i].rx/1024));
604
+ rb_hash_aset(info, ID2SYM(rb_intern("ipackets")),
605
+ INT2NUM(stats[i].ipackets));
606
+ rb_hash_aset(info, ID2SYM(rb_intern("opackets")),
607
+ INT2NUM(stats[i].opackets));
608
+ rb_hash_aset(info, ID2SYM(rb_intern("ierrors")),
609
+ INT2NUM(stats[i].ierrors));
610
+ rb_hash_aset(info, ID2SYM(rb_intern("oerrors")),
611
+ INT2NUM(stats[i].oerrors));
612
+ rb_hash_aset(info, ID2SYM(rb_intern("collisions")),
613
+ INT2NUM(stats[i].collisions));
614
+ rb_hash_aset(info, ID2SYM(rb_intern("systime")),
615
+ INT2NUM(stats[i].systime));
616
+
617
+ time_at = rb_funcall(rb_cTime, rb_intern("at"), 1,
618
+ INT2NUM(stats[i].systime));
619
+ rb_hash_aset(info, ID2SYM(rb_intern("time")), time_at);
620
+
621
+ rb_ary_push(arr, info);
622
+ }
623
+
624
+ return arr;
625
+ }
626
+
627
+ /*
628
+ * Network statistics difference since last call,
629
+ * see <tt>sg_get_network_io_stats_diff(3)</tt> manpage.
630
+ */
631
+ static VALUE
632
+ statgrab_network_io_stats_diff(VALUE self)
633
+ {
634
+ int entries, i;
635
+ sg_network_io_stats *stats;
636
+ VALUE arr, info, time_now;
637
+
638
+ stats = sg_get_network_io_stats_diff(&entries);
639
+ if (stats == NULL)
640
+ statgrab_handle_error();
641
+
642
+ arr = rb_ary_new();
643
+ for (i = 0; i < entries; i++) {
644
+ info = rb_hash_new();
645
+ rb_hash_aset(info, ID2SYM(rb_intern("interface_name")),
646
+ rb_str_new2(stats[i].interface_name));
647
+ rb_hash_aset(info, ID2SYM(rb_intern("tx")),
648
+ INT2NUM(stats[i].tx/1024));
649
+ rb_hash_aset(info, ID2SYM(rb_intern("rx")),
650
+ INT2NUM(stats[i].rx/1024));
651
+ rb_hash_aset(info, ID2SYM(rb_intern("ipackets")),
652
+ INT2NUM(stats[i].ipackets));
653
+ rb_hash_aset(info, ID2SYM(rb_intern("opackets")),
654
+ INT2NUM(stats[i].opackets));
655
+ rb_hash_aset(info, ID2SYM(rb_intern("ierrors")),
656
+ INT2NUM(stats[i].ierrors));
657
+ rb_hash_aset(info, ID2SYM(rb_intern("oerrors")),
658
+ INT2NUM(stats[i].oerrors));
659
+ rb_hash_aset(info, ID2SYM(rb_intern("collisions")),
660
+ INT2NUM(stats[i].collisions));
661
+ rb_hash_aset(info, ID2SYM(rb_intern("systime")),
662
+ INT2NUM(stats[i].systime));
663
+
664
+ time_now = rb_funcall(rb_cTime, rb_intern("now"), 0);
665
+ rb_hash_aset(info, ID2SYM(rb_intern("last_call")),
666
+ rb_funcall(time_now, rb_intern("-"), 1,
667
+ INT2NUM(stats[i].systime)));
668
+
669
+ rb_ary_push(arr, info);
670
+ }
671
+
672
+ return arr;
673
+ }
674
+
675
+ /*
676
+ * Network interface statistics,
677
+ * see <tt>sg_get_network_iface_stats(3)</tt> manpage.
678
+ */
679
+ static VALUE
680
+ statgrab_network_iface_stats(VALUE self)
681
+ {
682
+ int entries, i;
683
+ sg_network_iface_stats *stats;
684
+ VALUE arr, info;
685
+
686
+ stats = sg_get_network_iface_stats(&entries);
687
+ if (stats == NULL)
688
+ statgrab_handle_error();
689
+
690
+ arr = rb_ary_new();
691
+ for (i = 0; i < entries; i++) {
692
+ info = rb_hash_new();
693
+ rb_hash_aset(info, ID2SYM(rb_intern("interface_name")),
694
+ rb_str_new2(stats[i].interface_name));
695
+ rb_hash_aset(info, ID2SYM(rb_intern("speed")),
696
+ INT2FIX(stats[i].speed));
697
+
698
+ switch(stats[i].duplex) {
699
+ case SG_IFACE_DUPLEX_FULL:
700
+ rb_hash_aset(info, ID2SYM(rb_intern("duplex")),
701
+ ID2SYM(rb_intern("full")));
702
+ break;
703
+ case SG_IFACE_DUPLEX_HALF:
704
+ rb_hash_aset(info, ID2SYM(rb_intern("duplex")),
705
+ ID2SYM(rb_intern("half")));
706
+ break;
707
+ case SG_IFACE_DUPLEX_UNKNOWN:
708
+ rb_hash_aset(info, ID2SYM(rb_intern("duplex")),
709
+ ID2SYM(rb_intern("unknown")));
710
+ break;
711
+ }
712
+
713
+ if (stats[i].up)
714
+ rb_hash_aset(info, ID2SYM(rb_intern("up")), Qtrue);
715
+ else
716
+ rb_hash_aset(info, ID2SYM(rb_intern("up")), Qfalse);
717
+
718
+ rb_ary_push(arr, info);
719
+ }
720
+
721
+ return arr;
722
+ }
723
+
724
+ /*
725
+ * Paging statistics, see <tt>sg_get_page_stats(3)</tt> manpage.
726
+ */
727
+ static VALUE
728
+ statgrab_page_stats(VALUE self)
729
+ {
730
+ sg_page_stats *stats;
731
+ VALUE info, time_at;
732
+
733
+ stats = sg_get_page_stats();
734
+ if (stats == NULL)
735
+ statgrab_handle_error();
736
+
737
+ info = rb_hash_new();
738
+ rb_hash_aset(info, ID2SYM(rb_intern("pages_pagein")),
739
+ INT2NUM(stats->pages_pagein));
740
+ rb_hash_aset(info, ID2SYM(rb_intern("pages_pageout")),
741
+ INT2NUM(stats->pages_pageout));
742
+ rb_hash_aset(info, ID2SYM(rb_intern("systime")),
743
+ INT2NUM(stats->systime));
744
+
745
+ time_at = rb_funcall(rb_cTime, rb_intern("at"), 1,
746
+ INT2NUM(stats->systime));
747
+ rb_hash_aset(info, ID2SYM(rb_intern("time")), time_at);
748
+
749
+ return info;
750
+ }
751
+
752
+ /*
753
+ * Paging statistics difference since last call,
754
+ * see <tt>sg_get_page_stats(3)</tt> manpage.
755
+ */
756
+ static VALUE
757
+ statgrab_page_stats_diff(VALUE self)
758
+ {
759
+ sg_page_stats *stats;
760
+ VALUE info, time_now;
761
+
762
+ stats = sg_get_page_stats_diff();
763
+ if (stats == NULL)
764
+ statgrab_handle_error();
765
+
766
+ info = rb_hash_new();
767
+ rb_hash_aset(info, ID2SYM(rb_intern("pages_pagein")),
768
+ INT2NUM(stats->pages_pagein));
769
+ rb_hash_aset(info, ID2SYM(rb_intern("pages_pageout")),
770
+ INT2NUM(stats->pages_pageout));
771
+ rb_hash_aset(info, ID2SYM(rb_intern("systime")),
772
+ INT2NUM(stats->systime));
773
+
774
+ time_now = rb_funcall(rb_cTime, rb_intern("now"), 0);
775
+ rb_hash_aset(info, ID2SYM(rb_intern("last_call")),
776
+ rb_funcall(time_now, rb_intern("-"), 1,
777
+ INT2NUM(stats->systime)));
778
+
779
+ return info;
780
+ }
781
+
782
+ /*
783
+ * Process statistics, see <tt>sg_get_process_stats(3)</tt> manpage.
784
+ *
785
+ * The +sg_process_compare_+ family of related functions is intentionally
786
+ * left out in this binding - it can be done with Enumerable#sort_by in Ruby.
787
+ * The following example corresponds to the <tt>qsort()</tt> example in the
788
+ * <tt>sg_get_process_stats(3)</tt> manpage:
789
+ *
790
+ * ps.sort_by {|p| p[:pid] }
791
+ */
792
+ static VALUE
793
+ statgrab_process_stats(VALUE self)
794
+ {
795
+ int entries, i;
796
+ sg_process_stats *stats;
797
+ VALUE arr, info, time_now;
798
+
799
+ stats = sg_get_process_stats(&entries);
800
+ if (stats == NULL)
801
+ statgrab_handle_error();
802
+
803
+ arr = rb_ary_new();
804
+ for (i = 0; i < entries; i++) {
805
+ info = rb_hash_new();
806
+ rb_hash_aset(info, ID2SYM(rb_intern("process_name")),
807
+ rb_str_new2(stats[i].process_name));
808
+
809
+ if (stats[i].proctitle != NULL)
810
+ rb_hash_aset(info, ID2SYM(rb_intern("proctitle")),
811
+ rb_str_new2(stats[i].proctitle));
812
+
813
+ rb_hash_aset(info, ID2SYM(rb_intern("pid")),
814
+ INT2FIX(stats[i].pid));
815
+ rb_hash_aset(info, ID2SYM(rb_intern("parent")),
816
+ INT2FIX(stats[i].parent));
817
+ rb_hash_aset(info, ID2SYM(rb_intern("pgid")),
818
+ INT2FIX(stats[i].pgid));
819
+ rb_hash_aset(info, ID2SYM(rb_intern("uid")),
820
+ INT2FIX(stats[i].uid));
821
+ rb_hash_aset(info, ID2SYM(rb_intern("euid")),
822
+ INT2FIX(stats[i].euid));
823
+ rb_hash_aset(info, ID2SYM(rb_intern("gid")),
824
+ INT2FIX(stats[i].gid));
825
+ rb_hash_aset(info, ID2SYM(rb_intern("egid")),
826
+ INT2FIX(stats[i].egid));
827
+ rb_hash_aset(info, ID2SYM(rb_intern("proc_size")),
828
+ INT2NUM(stats[i].proc_size/1024));
829
+ rb_hash_aset(info, ID2SYM(rb_intern("proc_resident")),
830
+ INT2NUM(stats[i].proc_resident/1024));
831
+ rb_hash_aset(info, ID2SYM(rb_intern("time_spent")),
832
+ INT2NUM(stats[i].time_spent));
833
+ rb_hash_aset(info, ID2SYM(rb_intern("cpu_percent")),
834
+ INT2NUM(stats[i].cpu_percent));
835
+ rb_hash_aset(info, ID2SYM(rb_intern("nice")),
836
+ INT2NUM(stats[i].nice));
837
+
838
+ switch(stats[i].state) {
839
+ case SG_PROCESS_STATE_RUNNING:
840
+ rb_hash_aset(info, ID2SYM(rb_intern("state")),
841
+ ID2SYM(rb_intern("running")));
842
+ break;
843
+ case SG_PROCESS_STATE_SLEEPING:
844
+ rb_hash_aset(info, ID2SYM(rb_intern("state")),
845
+ ID2SYM(rb_intern("sleeping")));
846
+ break;
847
+ case SG_PROCESS_STATE_STOPPED:
848
+ rb_hash_aset(info, ID2SYM(rb_intern("state")),
849
+ ID2SYM(rb_intern("stopped")));
850
+ break;
851
+ case SG_PROCESS_STATE_ZOMBIE:
852
+ rb_hash_aset(info, ID2SYM(rb_intern("state")),
853
+ ID2SYM(rb_intern("zombie")));
854
+ break;
855
+ case SG_PROCESS_STATE_UNKNOWN:
856
+ rb_hash_aset(info, ID2SYM(rb_intern("state")),
857
+ ID2SYM(rb_intern("unknown")));
858
+ break;
859
+ }
860
+
861
+ time_now = rb_funcall(rb_cTime, rb_intern("now"), 0);
862
+ rb_hash_aset(info, ID2SYM(rb_intern("started")),
863
+ rb_funcall(time_now, rb_intern("-"), 1,
864
+ INT2NUM(stats[i].time_spent)));
865
+
866
+ rb_ary_push(arr, info);
867
+ }
868
+
869
+ return arr;
870
+ }
871
+
872
+ /*
873
+ * Brief process count for different states,
874
+ * see <tt>sg_get_process_count(3)</tt> manpage.
875
+ */
876
+ static VALUE
877
+ statgrab_process_count(VALUE self)
878
+ {
879
+ sg_process_count *count;
880
+ VALUE info;
881
+
882
+ count = sg_get_process_count();
883
+ if (count == NULL)
884
+ statgrab_handle_error();
885
+
886
+ info = rb_hash_new();
887
+ rb_hash_aset(info, ID2SYM(rb_intern("total")),
888
+ INT2FIX(count->total));
889
+ rb_hash_aset(info, ID2SYM(rb_intern("running")),
890
+ INT2FIX(count->running));
891
+ rb_hash_aset(info, ID2SYM(rb_intern("sleeping")),
892
+ INT2FIX(count->sleeping));
893
+ rb_hash_aset(info, ID2SYM(rb_intern("stopped")),
894
+ INT2FIX(count->stopped));
895
+ rb_hash_aset(info, ID2SYM(rb_intern("zombie")),
896
+ INT2FIX(count->zombie));
897
+
898
+ return info;
899
+ }
900
+
901
+ /*
902
+ * The current logged in users,
903
+ * see <tt>sg_get_user_stats(3)</tt> manpage.
904
+ *
905
+ * Returns an array of login names.
906
+ */
907
+ static VALUE
908
+ statgrab_user_stats(VALUE self)
909
+ {
910
+ int i;
911
+ char *names, *token, *saveptr;
912
+ sg_user_stats *stats;
913
+ VALUE arr;
914
+
915
+ stats = sg_get_user_stats();
916
+ if (stats == NULL)
917
+ statgrab_handle_error();
918
+
919
+ arr = rb_ary_new();
920
+ for (i = 0, names = stats->name_list; i < stats->num_entries;
921
+ i++, names = NULL) {
922
+ token = strtok_r(names, " ", &saveptr);
923
+ rb_ary_push(arr, rb_str_new2(token));
924
+ }
925
+
926
+ return arr;
927
+ }
928
+
929
+ void
930
+ Init_statgrab()
931
+ {
932
+ /*
933
+ * = Statgrab for Ruby
934
+ *
935
+ * Bindings to the libstatgrab portable system statistics library.
936
+ *
937
+ * == API
938
+ *
939
+ * All instance methods except user_stats return symbol hashes or
940
+ * arrays of symbol hashes corresponding to their C struct
941
+ * counterparts, see libstatgrab manuals for reference. Also, +enum+
942
+ * datatypes are represented as lowercased symbols with namespacing
943
+ * cut off, e.g. +SG_IFACE_DUPLEX_FULL+ becomes <tt>:full</tt>.
944
+ * Errors are handled and raises corresponding exceptions, all
945
+ * inheriting from Statgrab::Exception. Byte values are represented
946
+ * in kilobytes.
947
+ *
948
+ * == Sample program
949
+ *
950
+ * require 'statgrab'
951
+ * begin
952
+ * sg = Statgrab.new
953
+ * puts "CPU Usage: %.1f%%" % sg.cpu_percents[:user]
954
+ * rescue Statgrab::Exception => e
955
+ * puts "Error! %s" % e.message
956
+ * end
957
+ */
958
+ cStatgrab = rb_define_class("Statgrab", rb_cObject);
959
+
960
+ /*
961
+ * Raised when a statgrab function failed and an error was set.
962
+ */
963
+ eStatgrabException = rb_define_class_under(cStatgrab,
964
+ "Exception", rb_eException);
965
+ eStatgrabAsprintfError = rb_define_class_under(cStatgrab,
966
+ "AsprintfError", eStatgrabException);
967
+ eStatgrabDevicesError = rb_define_class_under(cStatgrab,
968
+ "DevicesError", eStatgrabException);
969
+ eStatgrabDevstatGetdevsError = rb_define_class_under(cStatgrab,
970
+ "DevstatGetdevsError", eStatgrabException);
971
+ eStatgrabDevstatSelectdevsError = rb_define_class_under(cStatgrab,
972
+ "DevstatSelectdevsError", eStatgrabException);
973
+ eStatgrabDiskinfoError = rb_define_class_under(cStatgrab,
974
+ "DiskinfoError", eStatgrabException);
975
+ eStatgrabEnoentError = rb_define_class_under(cStatgrab,
976
+ "EnoentError", eStatgrabException);
977
+ eStatgrabGetifaddrsError = rb_define_class_under(cStatgrab,
978
+ "GetifaddrsError", eStatgrabException);
979
+ eStatgrabGetmntinfoError = rb_define_class_under(cStatgrab,
980
+ "GetmntinfoError", eStatgrabException);
981
+ eStatgrabGetpagesizeError = rb_define_class_under(cStatgrab,
982
+ "GetpagesizeError", eStatgrabException);
983
+ eStatgrabHostError = rb_define_class_under(cStatgrab,
984
+ "HostError", eStatgrabException);
985
+ eStatgrabKstatDataLookupError = rb_define_class_under(cStatgrab,
986
+ "KstatDataLookupError", eStatgrabException);
987
+ eStatgrabKstatLookupError = rb_define_class_under(cStatgrab,
988
+ "KstatLookupError", eStatgrabException);
989
+ eStatgrabKstatOpenError = rb_define_class_under(cStatgrab,
990
+ "KstatOpenError", eStatgrabException);
991
+ eStatgrabKstatReadError = rb_define_class_under(cStatgrab,
992
+ "KstatReadError", eStatgrabException);
993
+ eStatgrabKvmGetswapinfoError = rb_define_class_under(cStatgrab,
994
+ "KvmGetswapinfoError", eStatgrabException);
995
+ eStatgrabKvmOpenfilesError = rb_define_class_under(cStatgrab,
996
+ "KvmOpenfilesError", eStatgrabException);
997
+ eStatgrabMallocError = rb_define_class_under(cStatgrab,
998
+ "MallocError", eStatgrabException);
999
+ eStatgrabMemstatusError = rb_define_class_under(cStatgrab,
1000
+ "MemstatusError", eStatgrabException);
1001
+ eStatgrabOpenError = rb_define_class_under(cStatgrab, "OpenError",
1002
+ eStatgrabException);
1003
+ eStatgrabOpendirError = rb_define_class_under(cStatgrab,
1004
+ "OpendirError", eStatgrabException);
1005
+ eStatgrabParseError = rb_define_class_under(cStatgrab,
1006
+ "ParseError", eStatgrabException);
1007
+ eStatgrabPdhaddError = rb_define_class_under(cStatgrab,
1008
+ "PdhaddError", eStatgrabException);
1009
+ eStatgrabPdhcollectError = rb_define_class_under(cStatgrab,
1010
+ "PdhcollectError", eStatgrabException);
1011
+ eStatgrabPdhopenError = rb_define_class_under(cStatgrab,
1012
+ "PdhopenError", eStatgrabException);
1013
+ eStatgrabPdhreadError = rb_define_class_under(cStatgrab,
1014
+ "PdhreadError", eStatgrabException);
1015
+ eStatgrabPermissionError = rb_define_class_under(cStatgrab,
1016
+ "PermissionError", eStatgrabException);
1017
+ eStatgrabPstatError = rb_define_class_under(cStatgrab,
1018
+ "PstatError", eStatgrabException);
1019
+ eStatgrabSetegidError = rb_define_class_under(cStatgrab,
1020
+ "SetegidError", eStatgrabException);
1021
+ eStatgrabSeteuidError = rb_define_class_under(cStatgrab,
1022
+ "SeteuidError", eStatgrabException);
1023
+ eStatgrabSetmntentError = rb_define_class_under(cStatgrab,
1024
+ "SetmntentError", eStatgrabException);
1025
+ eStatgrabSocketError = rb_define_class_under(cStatgrab,
1026
+ "SocketError", eStatgrabException);
1027
+ eStatgrabSwapctlError = rb_define_class_under(cStatgrab,
1028
+ "SwapctlError", eStatgrabException);
1029
+ eStatgrabSysconfError = rb_define_class_under(cStatgrab,
1030
+ "SysconfError", eStatgrabException);
1031
+ eStatgrabSysctlError = rb_define_class_under(cStatgrab,
1032
+ "SysctlError", eStatgrabException);
1033
+ eStatgrabSysctlbynameError = rb_define_class_under(cStatgrab,
1034
+ "SysctlbynameError", eStatgrabException);
1035
+ eStatgrabSysctlnametomibError = rb_define_class_under(cStatgrab,
1036
+ "SysctlnametomibError", eStatgrabException);
1037
+ eStatgrabUnameError = rb_define_class_under(cStatgrab,
1038
+ "UnameError", eStatgrabException);
1039
+ eStatgrabUnsupportedError = rb_define_class_under(cStatgrab,
1040
+ "UnsupportedError", eStatgrabException);
1041
+ eStatgrabXswVerMismatchError = rb_define_class_under(cStatgrab,
1042
+ "XswVerMismatchError", eStatgrabException);
1043
+
1044
+ /*
1045
+ * Methods
1046
+ */
1047
+ rb_define_method(cStatgrab, "initialize", statgrab_initialize, -2);
1048
+
1049
+ rb_define_method(cStatgrab, "cpu_stats", statgrab_cpu_stats, 0);
1050
+ rb_define_alias(cStatgrab, "cpu", "cpu_stats");
1051
+
1052
+ rb_define_method(cStatgrab, "cpu_stats_diff",
1053
+ statgrab_cpu_stats_diff, 0);
1054
+ rb_define_alias(cStatgrab, "cpu_difference", "cpu_stats_diff");
1055
+
1056
+ rb_define_method(cStatgrab, "cpu_percents",
1057
+ statgrab_cpu_percents, 0);
1058
+
1059
+ rb_define_method(cStatgrab, "disk_io_stats",
1060
+ statgrab_disk_io_stats, 0);
1061
+ rb_define_alias(cStatgrab, "disk_io", "disk_io_stats");
1062
+
1063
+ rb_define_method(cStatgrab, "disk_io_stats_diff",
1064
+ statgrab_disk_io_stats_diff, 0);
1065
+ rb_define_alias(cStatgrab, "disk_io_difference",
1066
+ "disk_io_stats_diff");
1067
+
1068
+ rb_define_method(cStatgrab, "fs_stats", statgrab_fs_stats, 0);
1069
+ rb_define_alias(cStatgrab, "fs", "fs_stats");
1070
+ rb_define_alias(cStatgrab, "file_system", "fs_stats");
1071
+
1072
+ rb_define_method(cStatgrab, "host_info", statgrab_host_info, 0);
1073
+ rb_define_alias(cStatgrab, "host", "host_info");
1074
+ rb_define_alias(cStatgrab, "system", "host_info");
1075
+
1076
+ rb_define_method(cStatgrab, "load_stats", statgrab_load_stats, 0);
1077
+ rb_define_alias(cStatgrab, "load", "load_stats");
1078
+ rb_define_alias(cStatgrab, "loadavg", "load_stats");
1079
+ rb_define_alias(cStatgrab, "load_average", "load_stats");
1080
+ rb_define_alias(cStatgrab, "system_load", "load_stats");
1081
+
1082
+ rb_define_method(cStatgrab, "mem_stats", statgrab_mem_stats, 0);
1083
+ rb_define_alias(cStatgrab, "memory", "mem_stats");
1084
+
1085
+ rb_define_method(cStatgrab, "swap_stats", statgrab_swap_stats, 0);
1086
+ rb_define_alias(cStatgrab, "swap", "swap_stats");
1087
+
1088
+ rb_define_method(cStatgrab, "network_io_stats",
1089
+ statgrab_network_io_stats, 0);
1090
+ rb_define_alias(cStatgrab, "network_io", "network_io_stats");
1091
+ rb_define_alias(cStatgrab, "net_io", "network_io_stats");
1092
+ rb_define_alias(cStatgrab, "network", "network_io_stats");
1093
+ rb_define_alias(cStatgrab, "net", "network_io_stats");
1094
+
1095
+ rb_define_method(cStatgrab, "network_io_stats_diff",
1096
+ statgrab_network_io_stats_diff, 0);
1097
+ rb_define_alias(cStatgrab, "network_io_difference",
1098
+ "network_io_stats_diff");
1099
+ rb_define_alias(cStatgrab, "network_difference",
1100
+ "network_io_stats_diff");
1101
+ rb_define_alias(cStatgrab, "net_difference",
1102
+ "network_io_stats_diff");
1103
+
1104
+ rb_define_method(cStatgrab, "network_iface_stats",
1105
+ statgrab_network_iface_stats, 0);
1106
+ rb_define_alias(cStatgrab, "network_iface", "network_iface_stats");
1107
+ rb_define_alias(cStatgrab, "net_iface", "network_iface_stats");
1108
+ rb_define_alias(cStatgrab, "iface", "network_iface_stats");
1109
+
1110
+ rb_define_method(cStatgrab, "page_stats", statgrab_page_stats, 0);
1111
+ rb_define_alias(cStatgrab, "page", "page_stats");
1112
+ rb_define_alias(cStatgrab, "pages", "page_stats");
1113
+
1114
+ rb_define_method(cStatgrab, "page_stats_diff",
1115
+ statgrab_page_stats_diff, 0);
1116
+ rb_define_alias(cStatgrab, "page_difference", "page_stats_diff");
1117
+
1118
+ rb_define_method(cStatgrab, "process_stats",
1119
+ statgrab_process_stats, 0);
1120
+ rb_define_alias(cStatgrab, "process", "process_stats");
1121
+ rb_define_alias(cStatgrab, "processes", "process_stats");
1122
+ rb_define_alias(cStatgrab, "proc", "process_stats");
1123
+ rb_define_alias(cStatgrab, "ps", "process_stats");
1124
+
1125
+ rb_define_method(cStatgrab, "process_count",
1126
+ statgrab_process_count, 0);
1127
+ rb_define_alias(cStatgrab, "proc_count", "process_count");
1128
+
1129
+ rb_define_method(cStatgrab, "user_stats",
1130
+ statgrab_user_stats, 0);
1131
+ rb_define_alias(cStatgrab, "users", "user_stats");
1132
+ }
1133
+
1134
+ /*
1135
+ * vim: ts=8 sw=8 noet
1136
+ */