statgrab 0.1

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.
Files changed (5) hide show
  1. data/LICENSE +202 -0
  2. data/README.mkdn +68 -0
  3. data/extconf.rb +3 -0
  4. data/statgrab.c +1120 -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.
@@ -0,0 +1,68 @@
1
+ Statgrab for Ruby
2
+ =================
3
+
4
+ Ruby bindings to the libstatgrab portable system statistics library.
5
+
6
+ - [Releases on Rubyforge][1]
7
+ - [Ticket tracking on Lighthouse][2]
8
+ - [API documentation][3]
9
+
10
+ Git repositories:
11
+
12
+ - [Github][4]
13
+ - [Gitorious][5]
14
+ - [Rubyforge][6]
15
+
16
+ [1]: http://rubyforge.org/projects/statgrab/
17
+ [2]: http://amazing.lighthouseapp.com/projects/11473-statgrab-for-ruby/
18
+ [3]: http://statgrab.rubyforge.org/doc/
19
+ [4]: http://github.com/dag/ruby-statgrab/tree/master
20
+ [5]: http://gitorious.org/projects/ruby-statgrab
21
+ [6]: http://rubyforge.org/scm/?group_id=6160
22
+
23
+
24
+ Building and installing
25
+ -----------------------
26
+
27
+ First, make sure libstatgrab is installed on your system.
28
+
29
+ Follow these procedures to build and install manually:
30
+
31
+ ruby extconf.rb
32
+ make
33
+ su
34
+ make install
35
+
36
+ Or if you prefer, install remotely from Rubyforge via Rubygems:
37
+
38
+ su
39
+ gem install statgrab
40
+
41
+
42
+ Sample program
43
+ --------------
44
+
45
+ require 'statgrab'
46
+ begin
47
+ sg = Statgrab.new
48
+ puts "CPU Usage: %.1f%%" % sg.cpu_percents[:user]
49
+ rescue Statgrab::Exception => e
50
+ puts "Error! %s" % e.message
51
+ end
52
+
53
+
54
+ Licensing
55
+ ---------
56
+
57
+ Copyright 2008 Dag Odenhall <dag.odenhall@gmail.com>
58
+
59
+ Licensed under the Apache License, Version 2.0 (the "License").
60
+ You may obtain a copy of the License at
61
+
62
+ <http://www.apache.org/licenses/LICENSE-2.0>
63
+
64
+ Unless required by applicable law or agreed to in writing, software
65
+ distributed under the License is distributed on an "AS IS" BASIS,
66
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
67
+ See the License for the specific language governing permissions and
68
+ limitations under the License.
@@ -0,0 +1,3 @@
1
+ require 'mkmf'
2
+ have_library("statgrab")
3
+ create_makefile("statgrab")
@@ -0,0 +1,1120 @@
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(void)
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
+ if ((stats = sg_get_cpu_stats()) == NULL)
248
+ statgrab_handle_error();
249
+
250
+ info = rb_hash_new();
251
+ rb_hash_aset(info, ID2SYM(rb_intern("user")), INT2NUM(stats->user));
252
+ rb_hash_aset(info, ID2SYM(rb_intern("kernel")),
253
+ INT2NUM(stats->kernel));
254
+ rb_hash_aset(info, ID2SYM(rb_intern("idle")), INT2NUM(stats->idle));
255
+ rb_hash_aset(info, ID2SYM(rb_intern("iowait")),
256
+ INT2NUM(stats->iowait));
257
+ rb_hash_aset(info, ID2SYM(rb_intern("swap")), INT2NUM(stats->swap));
258
+ rb_hash_aset(info, ID2SYM(rb_intern("nice")), INT2NUM(stats->nice));
259
+ rb_hash_aset(info, ID2SYM(rb_intern("systime")),
260
+ INT2NUM(stats->systime));
261
+
262
+ time_at = rb_funcall(rb_cTime, rb_intern("at"), 1,
263
+ INT2NUM(stats->systime));
264
+ rb_hash_aset(info, ID2SYM(rb_intern("time")), time_at);
265
+
266
+ return info;
267
+ }
268
+
269
+ /*
270
+ * CPU usage as ticks spent since last call,
271
+ * see <tt>sg_get_cpu_stats_diff(3)</tt> manpage.
272
+ */
273
+ static VALUE
274
+ statgrab_cpu_stats_diff(VALUE self)
275
+ {
276
+ sg_cpu_stats *stats;
277
+ VALUE info, time_at;
278
+
279
+ if ((stats = sg_get_cpu_stats_diff()) == NULL)
280
+ statgrab_handle_error();
281
+
282
+ info = rb_hash_new();
283
+ rb_hash_aset(info, ID2SYM(rb_intern("user")), INT2NUM(stats->user));
284
+ rb_hash_aset(info, ID2SYM(rb_intern("kernel")),
285
+ INT2NUM(stats->kernel));
286
+ rb_hash_aset(info, ID2SYM(rb_intern("idle")), INT2NUM(stats->idle));
287
+ rb_hash_aset(info, ID2SYM(rb_intern("iowait")),
288
+ INT2NUM(stats->iowait));
289
+ rb_hash_aset(info, ID2SYM(rb_intern("swap")), INT2NUM(stats->swap));
290
+ rb_hash_aset(info, ID2SYM(rb_intern("nice")), INT2NUM(stats->nice));
291
+ rb_hash_aset(info, ID2SYM(rb_intern("systime")),
292
+ INT2NUM(stats->systime));
293
+
294
+ time_at = rb_funcall(rb_cTime, rb_intern("at"), 1,
295
+ INT2NUM(stats->systime));
296
+ rb_hash_aset(info, ID2SYM(rb_intern("time")), time_at);
297
+
298
+ return info;
299
+ }
300
+
301
+ /*
302
+ * CPU usage as ticks difference in percents,
303
+ * see <tt>sg_get_cpu_percents(3)</tt> manpage.
304
+ */
305
+ static VALUE
306
+ statgrab_cpu_percents(VALUE self)
307
+ {
308
+ sg_cpu_percents *percents;
309
+ VALUE info, time_now;
310
+
311
+ if ((percents = sg_get_cpu_percents()) == NULL)
312
+ statgrab_handle_error();
313
+
314
+ info = rb_hash_new();
315
+ rb_hash_aset(info, ID2SYM(rb_intern("user")),
316
+ rb_float_new(percents->user));
317
+ rb_hash_aset(info, ID2SYM(rb_intern("kernel")),
318
+ rb_float_new(percents->kernel));
319
+ rb_hash_aset(info, ID2SYM(rb_intern("idle")),
320
+ rb_float_new(percents->idle));
321
+ rb_hash_aset(info, ID2SYM(rb_intern("iowait")),
322
+ rb_float_new(percents->iowait));
323
+ rb_hash_aset(info, ID2SYM(rb_intern("swap")),
324
+ rb_float_new(percents->swap));
325
+ rb_hash_aset(info, ID2SYM(rb_intern("nice")),
326
+ rb_float_new(percents->nice));
327
+ rb_hash_aset(info, ID2SYM(rb_intern("time_taken")),
328
+ INT2NUM(percents->time_taken));
329
+
330
+ time_now = rb_funcall(rb_cTime, rb_intern("now"), 0);
331
+ rb_hash_aset(info, ID2SYM(rb_intern("last_call")),
332
+ rb_funcall(time_now, rb_intern("-"), 1,
333
+ INT2NUM(percents->time_taken)));
334
+
335
+ return info;
336
+ }
337
+
338
+ /*
339
+ * Disk I/O statistics, see <tt>sg_get_disk_io_stats(3)</tt> manpage.
340
+ */
341
+ static VALUE
342
+ statgrab_disk_io_stats(VALUE self)
343
+ {
344
+ int entries, i;
345
+ sg_disk_io_stats *stats;
346
+ VALUE arr, info, time_at;
347
+
348
+ if ((stats = sg_get_disk_io_stats(&entries)) == NULL)
349
+ statgrab_handle_error();
350
+
351
+ arr = rb_ary_new();
352
+ for (i = 0; i < entries; i++) {
353
+ info = rb_hash_new();
354
+ rb_hash_aset(info, ID2SYM(rb_intern("disk_name")),
355
+ rb_str_new2(stats[i].disk_name));
356
+ rb_hash_aset(info, ID2SYM(rb_intern("read_bytes")),
357
+ INT2NUM(stats[i].read_bytes/1024));
358
+ rb_hash_aset(info, ID2SYM(rb_intern("write_bytes")),
359
+ INT2NUM(stats[i].write_bytes/1024));
360
+ rb_hash_aset(info, ID2SYM(rb_intern("systime")),
361
+ INT2NUM(stats[i].systime));
362
+
363
+ time_at = rb_funcall(rb_cTime, rb_intern("at"), 1,
364
+ INT2NUM(stats[i].systime));
365
+ rb_hash_aset(info, ID2SYM(rb_intern("time")), time_at);
366
+
367
+ rb_ary_push(arr, info);
368
+ }
369
+
370
+ return arr;
371
+ }
372
+
373
+ /*
374
+ * Disk I/O statistics difference since last call,
375
+ * see <tt>sg_get_disk_io_stats_diff(3)</tt> manpage.
376
+ */
377
+ static VALUE
378
+ statgrab_disk_io_stats_diff(VALUE self)
379
+ {
380
+ int entries, i;
381
+ sg_disk_io_stats *stats;
382
+ VALUE arr, info, time_now;
383
+
384
+ if ((stats = sg_get_disk_io_stats_diff(&entries)) == NULL)
385
+ statgrab_handle_error();
386
+
387
+ arr = rb_ary_new();
388
+ for(i = 0; i < entries; i++) {
389
+ info = rb_hash_new();
390
+ rb_hash_aset(info, ID2SYM(rb_intern("disk_name")),
391
+ rb_str_new2(stats[i].disk_name));
392
+ rb_hash_aset(info, ID2SYM(rb_intern("read_bytes")),
393
+ INT2NUM(stats[i].read_bytes/1024));
394
+ rb_hash_aset(info, ID2SYM(rb_intern("write_bytes")),
395
+ INT2NUM(stats[i].write_bytes/1024));
396
+ rb_hash_aset(info, ID2SYM(rb_intern("systime")),
397
+ INT2NUM(stats[i].systime));
398
+
399
+ time_now = rb_funcall(rb_cTime, rb_intern("now"), 0);
400
+ rb_hash_aset(info, ID2SYM(rb_intern("last_call")),
401
+ rb_funcall(time_now, rb_intern("-"), 1,
402
+ INT2NUM(stats[i].systime)));
403
+
404
+ rb_ary_push(arr, info);
405
+ }
406
+
407
+ return arr;
408
+ }
409
+
410
+ /*
411
+ * Filesystem statistics, see <tt>sg_get_fs_stats(3)</tt> manpage.
412
+ */
413
+ static VALUE
414
+ statgrab_fs_stats(VALUE self)
415
+ {
416
+ int entries, i;
417
+ sg_fs_stats *stats;
418
+ VALUE arr, info;
419
+
420
+ if ((stats = sg_get_fs_stats(&entries)) == NULL)
421
+ statgrab_handle_error();
422
+
423
+ arr = rb_ary_new();
424
+ for (i = 0; i < entries; i++) {
425
+ info = rb_hash_new();
426
+ rb_hash_aset(info, ID2SYM(rb_intern("device_name")),
427
+ rb_str_new2(stats[i].device_name));
428
+ rb_hash_aset(info, ID2SYM(rb_intern("fs_type")),
429
+ rb_str_new2(stats[i].fs_type));
430
+ rb_hash_aset(info, ID2SYM(rb_intern("mnt_point")),
431
+ rb_str_new2(stats[i].mnt_point));
432
+ rb_hash_aset(info, ID2SYM(rb_intern("size")),
433
+ INT2NUM(stats[i].size/1024));
434
+ rb_hash_aset(info, ID2SYM(rb_intern("used")),
435
+ INT2NUM(stats[i].used/1024));
436
+ rb_hash_aset(info, ID2SYM(rb_intern("avail")),
437
+ INT2NUM(stats[i].avail/1024));
438
+ rb_hash_aset(info, ID2SYM(rb_intern("total_inodes")),
439
+ INT2NUM(stats[i].total_inodes));
440
+ rb_hash_aset(info, ID2SYM(rb_intern("used_inodes")),
441
+ INT2NUM(stats[i].used_inodes));
442
+ rb_hash_aset(info, ID2SYM(rb_intern("free_inodes")),
443
+ INT2NUM(stats[i].free_inodes));
444
+ rb_hash_aset(info, ID2SYM(rb_intern("avail_blocks")),
445
+ INT2NUM(stats[i].avail_blocks));
446
+ rb_hash_aset(info, ID2SYM(rb_intern("io_size")),
447
+ INT2NUM(stats[i].io_size));
448
+ rb_hash_aset(info, ID2SYM(rb_intern("block_size")),
449
+ INT2NUM(stats[i].block_size));
450
+ rb_hash_aset(info, ID2SYM(rb_intern("total_blocks")),
451
+ INT2NUM(stats[i].total_blocks));
452
+ rb_hash_aset(info, ID2SYM(rb_intern("free_blocks")),
453
+ INT2NUM(stats[i].free_blocks));
454
+ rb_hash_aset(info, ID2SYM(rb_intern("used_blocks")),
455
+ INT2NUM(stats[i].used_blocks));
456
+ rb_hash_aset(info, ID2SYM(rb_intern("avail_blocks")),
457
+ INT2NUM(stats[i].avail_blocks));
458
+
459
+ rb_ary_push(arr, info);
460
+ }
461
+
462
+ return arr;
463
+ }
464
+
465
+ /*
466
+ * General operating system statistics,
467
+ * see <tt>sg_get_host_info(3)</tt> manpage.
468
+ */
469
+ static VALUE
470
+ statgrab_host_info(VALUE self)
471
+ {
472
+ sg_host_info *host;
473
+ VALUE info, time_now;
474
+
475
+ if ((host = sg_get_host_info()) == NULL)
476
+ statgrab_handle_error();
477
+
478
+ info = rb_hash_new();
479
+ rb_hash_aset(info, ID2SYM(rb_intern("os_name")),
480
+ rb_str_new2(host->os_name));
481
+ rb_hash_aset(info, ID2SYM(rb_intern("os_release")),
482
+ rb_str_new2(host->os_release));
483
+ rb_hash_aset(info, ID2SYM(rb_intern("os_version")),
484
+ rb_str_new2(host->os_version));
485
+ rb_hash_aset(info, ID2SYM(rb_intern("platform")),
486
+ rb_str_new2(host->platform));
487
+ rb_hash_aset(info, ID2SYM(rb_intern("hostname")),
488
+ rb_str_new2(host->hostname));
489
+ rb_hash_aset(info, ID2SYM(rb_intern("uptime")),
490
+ INT2NUM(host->uptime));
491
+
492
+ time_now = rb_funcall(rb_cTime, rb_intern("now"), 0);
493
+ rb_hash_aset(info, ID2SYM(rb_intern("booted")),
494
+ rb_funcall(time_now, rb_intern("-"), 1,
495
+ INT2NUM(host->uptime)));
496
+
497
+ return info;
498
+ }
499
+
500
+ /*
501
+ * System load, see <tt>sg_get_load_stats(3)</tt> manpage.
502
+ */
503
+ static VALUE
504
+ statgrab_load_stats(VALUE self)
505
+ {
506
+ sg_load_stats *load;
507
+ VALUE info;
508
+
509
+ if ((load = sg_get_load_stats()) == NULL)
510
+ statgrab_handle_error();
511
+
512
+ info = rb_hash_new();
513
+ rb_hash_aset(info, ID2SYM(rb_intern("min1")),
514
+ rb_float_new(load->min1));
515
+ rb_hash_aset(info, ID2SYM(rb_intern("min5")),
516
+ rb_float_new(load->min5));
517
+ rb_hash_aset(info, ID2SYM(rb_intern("min15")),
518
+ rb_float_new(load->min15));
519
+
520
+ return info;
521
+ }
522
+
523
+ /*
524
+ * Memory statistics, see <tt>sg_get_mem_stats(3)</tt> manpage.
525
+ */
526
+ static VALUE
527
+ statgrab_mem_stats(VALUE self)
528
+ {
529
+ sg_mem_stats *stats;
530
+ VALUE info;
531
+
532
+ if ((stats = sg_get_mem_stats()) == NULL)
533
+ statgrab_handle_error();
534
+
535
+ info = rb_hash_new();
536
+ rb_hash_aset(info, ID2SYM(rb_intern("total")),
537
+ INT2NUM(stats->total/1024));
538
+ rb_hash_aset(info, ID2SYM(rb_intern("free")),
539
+ INT2NUM(stats->free/1024));
540
+ rb_hash_aset(info, ID2SYM(rb_intern("used")),
541
+ INT2NUM(stats->used/1024));
542
+ rb_hash_aset(info, ID2SYM(rb_intern("cache")),
543
+ INT2NUM(stats->cache/1024));
544
+
545
+ return info;
546
+ }
547
+
548
+ /*
549
+ * Swap statistics, see <tt>sg_get_swap_stats(3)</tt> manpage.
550
+ */
551
+ static VALUE
552
+ statgrab_swap_stats(VALUE self)
553
+ {
554
+ sg_swap_stats *stats;
555
+ VALUE info;
556
+
557
+ if ((stats = sg_get_swap_stats()) == NULL)
558
+ statgrab_handle_error();
559
+
560
+ info = rb_hash_new();
561
+ rb_hash_aset(info, ID2SYM(rb_intern("total")),
562
+ INT2NUM(stats->total/1024));
563
+ rb_hash_aset(info, ID2SYM(rb_intern("used")),
564
+ INT2NUM(stats->used/1024));
565
+ rb_hash_aset(info, ID2SYM(rb_intern("free")),
566
+ INT2NUM(stats->free/1024));
567
+
568
+ return info;
569
+ }
570
+
571
+ /*
572
+ * Network statistics, see <tt>sg_get_network_io_stats(3)</tt> manpage.
573
+ */
574
+ static VALUE
575
+ statgrab_network_io_stats(VALUE self)
576
+ {
577
+ int entries, i;
578
+ sg_network_io_stats *stats;
579
+ VALUE arr, info, time_at;
580
+
581
+ if ((stats = sg_get_network_io_stats(&entries)) == NULL)
582
+ statgrab_handle_error();
583
+
584
+ arr = rb_ary_new();
585
+ for (i = 0; i < entries; i++) {
586
+ info = rb_hash_new();
587
+ rb_hash_aset(info, ID2SYM(rb_intern("interface_name")),
588
+ rb_str_new2(stats[i].interface_name));
589
+ rb_hash_aset(info, ID2SYM(rb_intern("tx")),
590
+ INT2NUM(stats[i].tx/1024));
591
+ rb_hash_aset(info, ID2SYM(rb_intern("rx")),
592
+ INT2NUM(stats[i].rx/1024));
593
+ rb_hash_aset(info, ID2SYM(rb_intern("ipackets")),
594
+ INT2NUM(stats[i].ipackets));
595
+ rb_hash_aset(info, ID2SYM(rb_intern("opackets")),
596
+ INT2NUM(stats[i].opackets));
597
+ rb_hash_aset(info, ID2SYM(rb_intern("ierrors")),
598
+ INT2NUM(stats[i].ierrors));
599
+ rb_hash_aset(info, ID2SYM(rb_intern("oerrors")),
600
+ INT2NUM(stats[i].oerrors));
601
+ rb_hash_aset(info, ID2SYM(rb_intern("collisions")),
602
+ INT2NUM(stats[i].collisions));
603
+ rb_hash_aset(info, ID2SYM(rb_intern("systime")),
604
+ INT2NUM(stats[i].systime));
605
+
606
+ time_at = rb_funcall(rb_cTime, rb_intern("at"), 1,
607
+ INT2NUM(stats[i].systime));
608
+ rb_hash_aset(info, ID2SYM(rb_intern("time")), time_at);
609
+
610
+ rb_ary_push(arr, info);
611
+ }
612
+
613
+ return arr;
614
+ }
615
+
616
+ /*
617
+ * Network statistics difference since last call,
618
+ * see <tt>sg_get_network_io_stats_diff(3)</tt> manpage.
619
+ */
620
+ static VALUE
621
+ statgrab_network_io_stats_diff(VALUE self)
622
+ {
623
+ int entries, i;
624
+ sg_network_io_stats *stats;
625
+ VALUE arr, info, time_now;
626
+
627
+ if ((stats = sg_get_network_io_stats_diff(&entries)) == NULL)
628
+ statgrab_handle_error();
629
+
630
+ arr = rb_ary_new();
631
+ for (i = 0; i < entries; i++) {
632
+ info = rb_hash_new();
633
+ rb_hash_aset(info, ID2SYM(rb_intern("interface_name")),
634
+ rb_str_new2(stats[i].interface_name));
635
+ rb_hash_aset(info, ID2SYM(rb_intern("tx")),
636
+ INT2NUM(stats[i].tx/1024));
637
+ rb_hash_aset(info, ID2SYM(rb_intern("rx")),
638
+ INT2NUM(stats[i].rx/1024));
639
+ rb_hash_aset(info, ID2SYM(rb_intern("ipackets")),
640
+ INT2NUM(stats[i].ipackets));
641
+ rb_hash_aset(info, ID2SYM(rb_intern("opackets")),
642
+ INT2NUM(stats[i].opackets));
643
+ rb_hash_aset(info, ID2SYM(rb_intern("ierrors")),
644
+ INT2NUM(stats[i].ierrors));
645
+ rb_hash_aset(info, ID2SYM(rb_intern("oerrors")),
646
+ INT2NUM(stats[i].oerrors));
647
+ rb_hash_aset(info, ID2SYM(rb_intern("collisions")),
648
+ INT2NUM(stats[i].collisions));
649
+ rb_hash_aset(info, ID2SYM(rb_intern("systime")),
650
+ INT2NUM(stats[i].systime));
651
+
652
+ time_now = rb_funcall(rb_cTime, rb_intern("now"), 0);
653
+ rb_hash_aset(info, ID2SYM(rb_intern("last_call")),
654
+ rb_funcall(time_now, rb_intern("-"), 1,
655
+ INT2NUM(stats[i].systime)));
656
+
657
+ rb_ary_push(arr, info);
658
+ }
659
+
660
+ return arr;
661
+ }
662
+
663
+ /*
664
+ * Network interface statistics,
665
+ * see <tt>sg_get_network_iface_stats(3)</tt> manpage.
666
+ */
667
+ static VALUE
668
+ statgrab_network_iface_stats(VALUE self)
669
+ {
670
+ int entries, i;
671
+ sg_network_iface_stats *stats;
672
+ VALUE arr, info;
673
+
674
+ if ((stats = sg_get_network_iface_stats(&entries)) == NULL)
675
+ statgrab_handle_error();
676
+
677
+ arr = rb_ary_new();
678
+ for (i = 0; i < entries; i++) {
679
+ info = rb_hash_new();
680
+ rb_hash_aset(info, ID2SYM(rb_intern("interface_name")),
681
+ rb_str_new2(stats[i].interface_name));
682
+ rb_hash_aset(info, ID2SYM(rb_intern("speed")),
683
+ INT2FIX(stats[i].speed));
684
+
685
+ switch(stats[i].duplex) {
686
+ case SG_IFACE_DUPLEX_FULL:
687
+ rb_hash_aset(info, ID2SYM(rb_intern("duplex")),
688
+ ID2SYM(rb_intern("full")));
689
+ break;
690
+ case SG_IFACE_DUPLEX_HALF:
691
+ rb_hash_aset(info, ID2SYM(rb_intern("duplex")),
692
+ ID2SYM(rb_intern("half")));
693
+ break;
694
+ case SG_IFACE_DUPLEX_UNKNOWN:
695
+ rb_hash_aset(info, ID2SYM(rb_intern("duplex")),
696
+ ID2SYM(rb_intern("unknown")));
697
+ break;
698
+ }
699
+
700
+ if (stats[i].up)
701
+ rb_hash_aset(info, ID2SYM(rb_intern("up")), Qtrue);
702
+ else
703
+ rb_hash_aset(info, ID2SYM(rb_intern("up")), Qfalse);
704
+
705
+ rb_ary_push(arr, info);
706
+ }
707
+
708
+ return arr;
709
+ }
710
+
711
+ /*
712
+ * Paging statistics, see <tt>sg_get_page_stats(3)</tt> manpage.
713
+ */
714
+ static VALUE
715
+ statgrab_page_stats(VALUE self)
716
+ {
717
+ sg_page_stats *stats;
718
+ VALUE info, time_at;
719
+
720
+ if ((stats = sg_get_page_stats()) == NULL)
721
+ statgrab_handle_error();
722
+
723
+ info = rb_hash_new();
724
+ rb_hash_aset(info, ID2SYM(rb_intern("pages_pagein")),
725
+ INT2NUM(stats->pages_pagein));
726
+ rb_hash_aset(info, ID2SYM(rb_intern("pages_pageout")),
727
+ INT2NUM(stats->pages_pageout));
728
+ rb_hash_aset(info, ID2SYM(rb_intern("systime")),
729
+ INT2NUM(stats->systime));
730
+
731
+ time_at = rb_funcall(rb_cTime, rb_intern("at"), 1,
732
+ INT2NUM(stats->systime));
733
+ rb_hash_aset(info, ID2SYM(rb_intern("time")), time_at);
734
+
735
+ return info;
736
+ }
737
+
738
+ /*
739
+ * Paging statistics difference since last call,
740
+ * see <tt>sg_get_page_stats(3)</tt> manpage.
741
+ */
742
+ static VALUE
743
+ statgrab_page_stats_diff(VALUE self)
744
+ {
745
+ sg_page_stats *stats;
746
+ VALUE info, time_now;
747
+
748
+ if ((stats = sg_get_page_stats_diff()) == NULL)
749
+ statgrab_handle_error();
750
+
751
+ info = rb_hash_new();
752
+ rb_hash_aset(info, ID2SYM(rb_intern("pages_pagein")),
753
+ INT2NUM(stats->pages_pagein));
754
+ rb_hash_aset(info, ID2SYM(rb_intern("pages_pageout")),
755
+ INT2NUM(stats->pages_pageout));
756
+ rb_hash_aset(info, ID2SYM(rb_intern("systime")),
757
+ INT2NUM(stats->systime));
758
+
759
+ time_now = rb_funcall(rb_cTime, rb_intern("now"), 0);
760
+ rb_hash_aset(info, ID2SYM(rb_intern("last_call")),
761
+ rb_funcall(time_now, rb_intern("-"), 1,
762
+ INT2NUM(stats->systime)));
763
+
764
+ return info;
765
+ }
766
+
767
+ /*
768
+ * Process statistics, see <tt>sg_get_process_stats(3)</tt> manpage.
769
+ *
770
+ * The +sg_process_compare_+ family of related functions is intentionally
771
+ * left out in this binding - it can be done with Enumerable#sort_by in Ruby.
772
+ * The following example corresponds to the <tt>qsort()</tt> example in the
773
+ * <tt>sg_get_process_stats(3)</tt> manpage:
774
+ *
775
+ * ps.sort_by {|p| p[:pid] }
776
+ */
777
+ static VALUE
778
+ statgrab_process_stats(VALUE self)
779
+ {
780
+ int entries, i;
781
+ sg_process_stats *stats;
782
+ VALUE arr, info, time_now;
783
+
784
+ if ((stats = sg_get_process_stats(&entries)) == NULL)
785
+ statgrab_handle_error();
786
+
787
+ arr = rb_ary_new();
788
+ for (i = 0; i < entries; i++) {
789
+ info = rb_hash_new();
790
+ rb_hash_aset(info, ID2SYM(rb_intern("process_name")),
791
+ rb_str_new2(stats[i].process_name));
792
+
793
+ if (stats[i].proctitle != NULL)
794
+ rb_hash_aset(info, ID2SYM(rb_intern("proctitle")),
795
+ rb_str_new2(stats[i].proctitle));
796
+
797
+ rb_hash_aset(info, ID2SYM(rb_intern("pid")),
798
+ INT2FIX(stats[i].pid));
799
+ rb_hash_aset(info, ID2SYM(rb_intern("parent")),
800
+ INT2FIX(stats[i].parent));
801
+ rb_hash_aset(info, ID2SYM(rb_intern("pgid")),
802
+ INT2FIX(stats[i].pgid));
803
+ rb_hash_aset(info, ID2SYM(rb_intern("uid")),
804
+ INT2FIX(stats[i].uid));
805
+ rb_hash_aset(info, ID2SYM(rb_intern("euid")),
806
+ INT2FIX(stats[i].euid));
807
+ rb_hash_aset(info, ID2SYM(rb_intern("gid")),
808
+ INT2FIX(stats[i].gid));
809
+ rb_hash_aset(info, ID2SYM(rb_intern("egid")),
810
+ INT2FIX(stats[i].egid));
811
+ rb_hash_aset(info, ID2SYM(rb_intern("proc_size")),
812
+ INT2NUM(stats[i].proc_size/1024));
813
+ rb_hash_aset(info, ID2SYM(rb_intern("proc_resident")),
814
+ INT2NUM(stats[i].proc_resident/1024));
815
+ rb_hash_aset(info, ID2SYM(rb_intern("time_spent")),
816
+ INT2NUM(stats[i].time_spent));
817
+ rb_hash_aset(info, ID2SYM(rb_intern("cpu_percent")),
818
+ INT2NUM(stats[i].cpu_percent));
819
+ rb_hash_aset(info, ID2SYM(rb_intern("nice")),
820
+ INT2NUM(stats[i].nice));
821
+
822
+ switch(stats[i].state) {
823
+ case SG_PROCESS_STATE_RUNNING:
824
+ rb_hash_aset(info, ID2SYM(rb_intern("state")),
825
+ ID2SYM(rb_intern("running")));
826
+ break;
827
+ case SG_PROCESS_STATE_SLEEPING:
828
+ rb_hash_aset(info, ID2SYM(rb_intern("state")),
829
+ ID2SYM(rb_intern("sleeping")));
830
+ break;
831
+ case SG_PROCESS_STATE_STOPPED:
832
+ rb_hash_aset(info, ID2SYM(rb_intern("state")),
833
+ ID2SYM(rb_intern("stopped")));
834
+ break;
835
+ case SG_PROCESS_STATE_ZOMBIE:
836
+ rb_hash_aset(info, ID2SYM(rb_intern("state")),
837
+ ID2SYM(rb_intern("zombie")));
838
+ break;
839
+ case SG_PROCESS_STATE_UNKNOWN:
840
+ rb_hash_aset(info, ID2SYM(rb_intern("state")),
841
+ ID2SYM(rb_intern("unknown")));
842
+ break;
843
+ }
844
+
845
+ time_now = rb_funcall(rb_cTime, rb_intern("now"), 0);
846
+ rb_hash_aset(info, ID2SYM(rb_intern("started")),
847
+ rb_funcall(time_now, rb_intern("-"), 1,
848
+ INT2NUM(stats[i].time_spent)));
849
+
850
+ rb_ary_push(arr, info);
851
+ }
852
+
853
+ return arr;
854
+ }
855
+
856
+ /*
857
+ * Brief process count for different states,
858
+ * see <tt>sg_get_process_count(3)</tt> manpage.
859
+ */
860
+ static VALUE
861
+ statgrab_process_count(VALUE self)
862
+ {
863
+ sg_process_count *count;
864
+ VALUE info;
865
+
866
+ if ((count = sg_get_process_count()) == NULL)
867
+ statgrab_handle_error();
868
+
869
+ info = rb_hash_new();
870
+ rb_hash_aset(info, ID2SYM(rb_intern("total")),
871
+ INT2FIX(count->total));
872
+ rb_hash_aset(info, ID2SYM(rb_intern("running")),
873
+ INT2FIX(count->running));
874
+ rb_hash_aset(info, ID2SYM(rb_intern("sleeping")),
875
+ INT2FIX(count->sleeping));
876
+ rb_hash_aset(info, ID2SYM(rb_intern("stopped")),
877
+ INT2FIX(count->stopped));
878
+ rb_hash_aset(info, ID2SYM(rb_intern("zombie")),
879
+ INT2FIX(count->zombie));
880
+
881
+ return info;
882
+ }
883
+
884
+ /*
885
+ * The current logged in users,
886
+ * see <tt>sg_get_user_stats(3)</tt> manpage.
887
+ *
888
+ * Returns an array of login names.
889
+ */
890
+ static VALUE
891
+ statgrab_user_stats(VALUE self)
892
+ {
893
+ int i;
894
+ char *names, *token, *saveptr;
895
+ sg_user_stats *stats;
896
+ VALUE arr;
897
+
898
+ if ((stats = sg_get_user_stats()) == NULL)
899
+ statgrab_handle_error();
900
+
901
+ arr = rb_ary_new();
902
+ for (i = 0, names = stats->name_list; i < stats->num_entries;
903
+ i++, names = NULL) {
904
+ token = strtok_r(names, " ", &saveptr);
905
+ rb_ary_push(arr, rb_str_new2(token));
906
+ }
907
+
908
+ return arr;
909
+ }
910
+
911
+ void
912
+ Init_statgrab(void)
913
+ {
914
+ /*
915
+ * = Statgrab for Ruby
916
+ *
917
+ * Bindings to the libstatgrab portable system statistics library.
918
+ *
919
+ * == API
920
+ *
921
+ * All instance methods except user_stats return symbol hashes or
922
+ * arrays of symbol hashes corresponding to their C struct
923
+ * counterparts, see libstatgrab manuals for reference. Also, +enum+
924
+ * datatypes are represented as lowercased symbols with namespacing
925
+ * cut off, e.g. +SG_IFACE_DUPLEX_FULL+ becomes <tt>:full</tt>.
926
+ * Errors are handled and raises corresponding exceptions, all
927
+ * inheriting from Statgrab::Exception. Byte values are represented
928
+ * in kilobytes.
929
+ *
930
+ * == Sample program
931
+ *
932
+ * require 'statgrab'
933
+ * begin
934
+ * sg = Statgrab.new
935
+ * puts "CPU Usage: %.1f%%" % sg.cpu_percents[:user]
936
+ * rescue Statgrab::Exception => e
937
+ * puts "Error! %s" % e.message
938
+ * end
939
+ */
940
+ cStatgrab = rb_define_class("Statgrab", rb_cObject);
941
+
942
+ rb_define_const(cStatgrab, "VERSION", rb_str_new2("0.1"));
943
+
944
+ /*
945
+ * Raised when a statgrab function failed and an error was set.
946
+ */
947
+ eStatgrabException = rb_define_class_under(cStatgrab,
948
+ "Exception", rb_eException);
949
+ eStatgrabAsprintfError = rb_define_class_under(cStatgrab,
950
+ "AsprintfError", eStatgrabException);
951
+ eStatgrabDevicesError = rb_define_class_under(cStatgrab,
952
+ "DevicesError", eStatgrabException);
953
+ eStatgrabDevstatGetdevsError = rb_define_class_under(cStatgrab,
954
+ "DevstatGetdevsError", eStatgrabException);
955
+ eStatgrabDevstatSelectdevsError = rb_define_class_under(cStatgrab,
956
+ "DevstatSelectdevsError", eStatgrabException);
957
+ eStatgrabDiskinfoError = rb_define_class_under(cStatgrab,
958
+ "DiskinfoError", eStatgrabException);
959
+ eStatgrabEnoentError = rb_define_class_under(cStatgrab,
960
+ "EnoentError", eStatgrabException);
961
+ eStatgrabGetifaddrsError = rb_define_class_under(cStatgrab,
962
+ "GetifaddrsError", eStatgrabException);
963
+ eStatgrabGetmntinfoError = rb_define_class_under(cStatgrab,
964
+ "GetmntinfoError", eStatgrabException);
965
+ eStatgrabGetpagesizeError = rb_define_class_under(cStatgrab,
966
+ "GetpagesizeError", eStatgrabException);
967
+ eStatgrabHostError = rb_define_class_under(cStatgrab,
968
+ "HostError", eStatgrabException);
969
+ eStatgrabKstatDataLookupError = rb_define_class_under(cStatgrab,
970
+ "KstatDataLookupError", eStatgrabException);
971
+ eStatgrabKstatLookupError = rb_define_class_under(cStatgrab,
972
+ "KstatLookupError", eStatgrabException);
973
+ eStatgrabKstatOpenError = rb_define_class_under(cStatgrab,
974
+ "KstatOpenError", eStatgrabException);
975
+ eStatgrabKstatReadError = rb_define_class_under(cStatgrab,
976
+ "KstatReadError", eStatgrabException);
977
+ eStatgrabKvmGetswapinfoError = rb_define_class_under(cStatgrab,
978
+ "KvmGetswapinfoError", eStatgrabException);
979
+ eStatgrabKvmOpenfilesError = rb_define_class_under(cStatgrab,
980
+ "KvmOpenfilesError", eStatgrabException);
981
+ eStatgrabMallocError = rb_define_class_under(cStatgrab,
982
+ "MallocError", eStatgrabException);
983
+ eStatgrabMemstatusError = rb_define_class_under(cStatgrab,
984
+ "MemstatusError", eStatgrabException);
985
+ eStatgrabOpenError = rb_define_class_under(cStatgrab, "OpenError",
986
+ eStatgrabException);
987
+ eStatgrabOpendirError = rb_define_class_under(cStatgrab,
988
+ "OpendirError", eStatgrabException);
989
+ eStatgrabParseError = rb_define_class_under(cStatgrab,
990
+ "ParseError", eStatgrabException);
991
+ eStatgrabPdhaddError = rb_define_class_under(cStatgrab,
992
+ "PdhaddError", eStatgrabException);
993
+ eStatgrabPdhcollectError = rb_define_class_under(cStatgrab,
994
+ "PdhcollectError", eStatgrabException);
995
+ eStatgrabPdhopenError = rb_define_class_under(cStatgrab,
996
+ "PdhopenError", eStatgrabException);
997
+ eStatgrabPdhreadError = rb_define_class_under(cStatgrab,
998
+ "PdhreadError", eStatgrabException);
999
+ eStatgrabPermissionError = rb_define_class_under(cStatgrab,
1000
+ "PermissionError", eStatgrabException);
1001
+ eStatgrabPstatError = rb_define_class_under(cStatgrab,
1002
+ "PstatError", eStatgrabException);
1003
+ eStatgrabSetegidError = rb_define_class_under(cStatgrab,
1004
+ "SetegidError", eStatgrabException);
1005
+ eStatgrabSeteuidError = rb_define_class_under(cStatgrab,
1006
+ "SeteuidError", eStatgrabException);
1007
+ eStatgrabSetmntentError = rb_define_class_under(cStatgrab,
1008
+ "SetmntentError", eStatgrabException);
1009
+ eStatgrabSocketError = rb_define_class_under(cStatgrab,
1010
+ "SocketError", eStatgrabException);
1011
+ eStatgrabSwapctlError = rb_define_class_under(cStatgrab,
1012
+ "SwapctlError", eStatgrabException);
1013
+ eStatgrabSysconfError = rb_define_class_under(cStatgrab,
1014
+ "SysconfError", eStatgrabException);
1015
+ eStatgrabSysctlError = rb_define_class_under(cStatgrab,
1016
+ "SysctlError", eStatgrabException);
1017
+ eStatgrabSysctlbynameError = rb_define_class_under(cStatgrab,
1018
+ "SysctlbynameError", eStatgrabException);
1019
+ eStatgrabSysctlnametomibError = rb_define_class_under(cStatgrab,
1020
+ "SysctlnametomibError", eStatgrabException);
1021
+ eStatgrabUnameError = rb_define_class_under(cStatgrab,
1022
+ "UnameError", eStatgrabException);
1023
+ eStatgrabUnsupportedError = rb_define_class_under(cStatgrab,
1024
+ "UnsupportedError", eStatgrabException);
1025
+ eStatgrabXswVerMismatchError = rb_define_class_under(cStatgrab,
1026
+ "XswVerMismatchError", eStatgrabException);
1027
+
1028
+ /*
1029
+ * Methods
1030
+ */
1031
+ rb_define_method(cStatgrab, "initialize", statgrab_initialize, -2);
1032
+
1033
+ rb_define_method(cStatgrab, "cpu_stats", statgrab_cpu_stats, 0);
1034
+ rb_define_alias(cStatgrab, "cpu", "cpu_stats");
1035
+
1036
+ rb_define_method(cStatgrab, "cpu_stats_diff",
1037
+ statgrab_cpu_stats_diff, 0);
1038
+ rb_define_alias(cStatgrab, "cpu_difference", "cpu_stats_diff");
1039
+
1040
+ rb_define_method(cStatgrab, "cpu_percents",
1041
+ statgrab_cpu_percents, 0);
1042
+
1043
+ rb_define_method(cStatgrab, "disk_io_stats",
1044
+ statgrab_disk_io_stats, 0);
1045
+ rb_define_alias(cStatgrab, "disk_io", "disk_io_stats");
1046
+
1047
+ rb_define_method(cStatgrab, "disk_io_stats_diff",
1048
+ statgrab_disk_io_stats_diff, 0);
1049
+ rb_define_alias(cStatgrab, "disk_io_difference",
1050
+ "disk_io_stats_diff");
1051
+
1052
+ rb_define_method(cStatgrab, "fs_stats", statgrab_fs_stats, 0);
1053
+ rb_define_alias(cStatgrab, "fs", "fs_stats");
1054
+ rb_define_alias(cStatgrab, "file_system", "fs_stats");
1055
+
1056
+ rb_define_method(cStatgrab, "host_info", statgrab_host_info, 0);
1057
+ rb_define_alias(cStatgrab, "host", "host_info");
1058
+ rb_define_alias(cStatgrab, "system", "host_info");
1059
+
1060
+ rb_define_method(cStatgrab, "load_stats", statgrab_load_stats, 0);
1061
+ rb_define_alias(cStatgrab, "load", "load_stats");
1062
+ rb_define_alias(cStatgrab, "loadavg", "load_stats");
1063
+ rb_define_alias(cStatgrab, "load_average", "load_stats");
1064
+ rb_define_alias(cStatgrab, "system_load", "load_stats");
1065
+
1066
+ rb_define_method(cStatgrab, "mem_stats", statgrab_mem_stats, 0);
1067
+ rb_define_alias(cStatgrab, "memory", "mem_stats");
1068
+
1069
+ rb_define_method(cStatgrab, "swap_stats", statgrab_swap_stats, 0);
1070
+ rb_define_alias(cStatgrab, "swap", "swap_stats");
1071
+
1072
+ rb_define_method(cStatgrab, "network_io_stats",
1073
+ statgrab_network_io_stats, 0);
1074
+ rb_define_alias(cStatgrab, "network_io", "network_io_stats");
1075
+ rb_define_alias(cStatgrab, "net_io", "network_io_stats");
1076
+ rb_define_alias(cStatgrab, "network", "network_io_stats");
1077
+ rb_define_alias(cStatgrab, "net", "network_io_stats");
1078
+
1079
+ rb_define_method(cStatgrab, "network_io_stats_diff",
1080
+ statgrab_network_io_stats_diff, 0);
1081
+ rb_define_alias(cStatgrab, "network_io_difference",
1082
+ "network_io_stats_diff");
1083
+ rb_define_alias(cStatgrab, "network_difference",
1084
+ "network_io_stats_diff");
1085
+ rb_define_alias(cStatgrab, "net_difference",
1086
+ "network_io_stats_diff");
1087
+
1088
+ rb_define_method(cStatgrab, "network_iface_stats",
1089
+ statgrab_network_iface_stats, 0);
1090
+ rb_define_alias(cStatgrab, "network_iface", "network_iface_stats");
1091
+ rb_define_alias(cStatgrab, "net_iface", "network_iface_stats");
1092
+ rb_define_alias(cStatgrab, "iface", "network_iface_stats");
1093
+
1094
+ rb_define_method(cStatgrab, "page_stats", statgrab_page_stats, 0);
1095
+ rb_define_alias(cStatgrab, "page", "page_stats");
1096
+ rb_define_alias(cStatgrab, "pages", "page_stats");
1097
+
1098
+ rb_define_method(cStatgrab, "page_stats_diff",
1099
+ statgrab_page_stats_diff, 0);
1100
+ rb_define_alias(cStatgrab, "page_difference", "page_stats_diff");
1101
+
1102
+ rb_define_method(cStatgrab, "process_stats",
1103
+ statgrab_process_stats, 0);
1104
+ rb_define_alias(cStatgrab, "process", "process_stats");
1105
+ rb_define_alias(cStatgrab, "processes", "process_stats");
1106
+ rb_define_alias(cStatgrab, "proc", "process_stats");
1107
+ rb_define_alias(cStatgrab, "ps", "process_stats");
1108
+
1109
+ rb_define_method(cStatgrab, "process_count",
1110
+ statgrab_process_count, 0);
1111
+ rb_define_alias(cStatgrab, "proc_count", "process_count");
1112
+
1113
+ rb_define_method(cStatgrab, "user_stats",
1114
+ statgrab_user_stats, 0);
1115
+ rb_define_alias(cStatgrab, "users", "user_stats");
1116
+ }
1117
+
1118
+ /*
1119
+ * vim: ts=8 sw=8 noet
1120
+ */