hyperic-sigar 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/COPYING +339 -0
- data/EXCEPTIONS +104 -0
- data/README +2 -0
- data/Rakefile +87 -0
- data/bindings/SigarWrapper.pm +2934 -0
- data/bindings/ruby/examples/cpu_info.rb +16 -0
- data/bindings/ruby/examples/df.rb +32 -0
- data/bindings/ruby/examples/free.rb +19 -0
- data/bindings/ruby/examples/ifconfig.rb +67 -0
- data/bindings/ruby/examples/netstat.rb +54 -0
- data/bindings/ruby/examples/pargs.rb +18 -0
- data/bindings/ruby/examples/penv.rb +14 -0
- data/bindings/ruby/examples/route.rb +31 -0
- data/bindings/ruby/examples/who.rb +13 -0
- data/bindings/ruby/extconf.rb +110 -0
- data/bindings/ruby/rbsigar.c +628 -0
- data/include/sigar.h +901 -0
- data/include/sigar_fileinfo.h +141 -0
- data/include/sigar_format.h +65 -0
- data/include/sigar_getline.h +18 -0
- data/include/sigar_log.h +82 -0
- data/include/sigar_private.h +365 -0
- data/include/sigar_ptql.h +55 -0
- data/include/sigar_util.h +192 -0
- data/src/os/aix/aix_sigar.c +1927 -0
- data/src/os/aix/sigar_os.h +71 -0
- data/src/os/darwin/darwin_sigar.c +3450 -0
- data/src/os/darwin/sigar_os.h +82 -0
- data/src/os/hpux/dlpi.c +284 -0
- data/src/os/hpux/hpux_sigar.c +1205 -0
- data/src/os/hpux/sigar_os.h +51 -0
- data/src/os/linux/linux_sigar.c +2595 -0
- data/src/os/linux/sigar_os.h +84 -0
- data/src/os/netware/netware_sigar.c +719 -0
- data/src/os/netware/sigar_os.h +26 -0
- data/src/os/osf1/osf1_sigar.c +593 -0
- data/src/os/osf1/sigar_os.h +42 -0
- data/src/os/solaris/get_mib2.c +321 -0
- data/src/os/solaris/get_mib2.h +127 -0
- data/src/os/solaris/hmekstat.h +77 -0
- data/src/os/solaris/kstats.c +182 -0
- data/src/os/solaris/procfs.c +99 -0
- data/src/os/solaris/sigar_os.h +225 -0
- data/src/os/solaris/solaris_sigar.c +2561 -0
- data/src/os/stub/sigar_os.h +8 -0
- data/src/os/stub/stub_sigar.c +303 -0
- data/src/os/win32/peb.c +213 -0
- data/src/os/win32/sigar_os.h +623 -0
- data/src/os/win32/sigar_pdh.h +49 -0
- data/src/os/win32/win32_sigar.c +3718 -0
- data/src/sigar.c +2292 -0
- data/src/sigar_cache.c +181 -0
- data/src/sigar_fileinfo.c +792 -0
- data/src/sigar_format.c +649 -0
- data/src/sigar_getline.c +1849 -0
- data/src/sigar_ptql.c +1966 -0
- data/src/sigar_signal.c +218 -0
- data/src/sigar_util.c +1061 -0
- data/version.properties +11 -0
- metadata +112 -0
data/src/sigar_format.c
ADDED
@@ -0,0 +1,649 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (C) [2004, 2005, 2006], Hyperic, Inc.
|
3
|
+
* This file is part of SIGAR.
|
4
|
+
*
|
5
|
+
* SIGAR is free software; you can redistribute it and/or modify
|
6
|
+
* it under the terms version 2 of the GNU General Public License as
|
7
|
+
* published by the Free Software Foundation. This program is distributed
|
8
|
+
* in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
|
9
|
+
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
10
|
+
* PARTICULAR PURPOSE. See the GNU General Public License for more
|
11
|
+
* details.
|
12
|
+
*
|
13
|
+
* You should have received a copy of the GNU General Public License
|
14
|
+
* along with this program; if not, write to the Free Software
|
15
|
+
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
16
|
+
* USA.
|
17
|
+
*/
|
18
|
+
|
19
|
+
/* Utility functions to provide string formatting of SIGAR data */
|
20
|
+
|
21
|
+
#include "sigar.h"
|
22
|
+
#include "sigar_private.h"
|
23
|
+
#include "sigar_util.h"
|
24
|
+
#include "sigar_os.h"
|
25
|
+
#include "sigar_format.h"
|
26
|
+
|
27
|
+
#include <errno.h>
|
28
|
+
#include <stdio.h>
|
29
|
+
|
30
|
+
#ifndef WIN32
|
31
|
+
#include <netinet/in.h>
|
32
|
+
#include <arpa/inet.h>
|
33
|
+
#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(_AIX)
|
34
|
+
#include <sys/socket.h>
|
35
|
+
#endif
|
36
|
+
#include <pwd.h>
|
37
|
+
#include <grp.h>
|
38
|
+
|
39
|
+
/* sysconf(_SC_GET{PW,GR}_R_SIZE_MAX) */
|
40
|
+
#define R_SIZE_MAX 1024
|
41
|
+
|
42
|
+
int sigar_user_name_get(sigar_t *sigar, int uid, char *buf, int buflen)
|
43
|
+
{
|
44
|
+
struct passwd *pw = NULL;
|
45
|
+
/* XXX cache lookup */
|
46
|
+
|
47
|
+
# ifdef HAVE_GETPWUID_R
|
48
|
+
struct passwd pwbuf;
|
49
|
+
char buffer[R_SIZE_MAX];
|
50
|
+
|
51
|
+
if (getpwuid_r(uid, &pwbuf, buffer, sizeof(buffer), &pw) != 0) {
|
52
|
+
return errno;
|
53
|
+
}
|
54
|
+
if (!pw) {
|
55
|
+
return ENOENT;
|
56
|
+
}
|
57
|
+
# else
|
58
|
+
if ((pw = getpwuid(uid)) == NULL) {
|
59
|
+
return errno;
|
60
|
+
}
|
61
|
+
# endif
|
62
|
+
|
63
|
+
strncpy(buf, pw->pw_name, buflen);
|
64
|
+
buf[buflen-1] = '\0';
|
65
|
+
|
66
|
+
return SIGAR_OK;
|
67
|
+
}
|
68
|
+
|
69
|
+
int sigar_group_name_get(sigar_t *sigar, int gid, char *buf, int buflen)
|
70
|
+
{
|
71
|
+
struct group *gr;
|
72
|
+
/* XXX cache lookup */
|
73
|
+
|
74
|
+
# ifdef HAVE_GETGRGID_R
|
75
|
+
struct group grbuf;
|
76
|
+
char buffer[R_SIZE_MAX];
|
77
|
+
|
78
|
+
if (getgrgid_r(gid, &grbuf, buffer, sizeof(buffer), &gr) != 0) {
|
79
|
+
return errno;
|
80
|
+
}
|
81
|
+
# else
|
82
|
+
if ((gr = getgrgid(gid)) == NULL) {
|
83
|
+
return errno;
|
84
|
+
}
|
85
|
+
# endif
|
86
|
+
|
87
|
+
if (gr && gr->gr_name) {
|
88
|
+
strncpy(buf, gr->gr_name, buflen);
|
89
|
+
}
|
90
|
+
else {
|
91
|
+
/* seen on linux.. apache httpd.conf has:
|
92
|
+
* Group #-1
|
93
|
+
* results in uid == -1 and gr == NULL.
|
94
|
+
* wtf getgrgid_r doesnt fail instead?
|
95
|
+
*/
|
96
|
+
sprintf(buf, "%d", gid);
|
97
|
+
}
|
98
|
+
buf[buflen-1] = '\0';
|
99
|
+
|
100
|
+
return SIGAR_OK;
|
101
|
+
}
|
102
|
+
|
103
|
+
int sigar_user_id_get(sigar_t *sigar, const char *name, int *uid)
|
104
|
+
{
|
105
|
+
/* XXX cache lookup */
|
106
|
+
struct passwd *pw;
|
107
|
+
|
108
|
+
# ifdef HAVE_GETPWNAM_R
|
109
|
+
struct passwd pwbuf;
|
110
|
+
char buf[R_SIZE_MAX];
|
111
|
+
|
112
|
+
if (getpwnam_r(name, &pwbuf, buf, sizeof(buf), &pw) != 0) {
|
113
|
+
return errno;
|
114
|
+
}
|
115
|
+
# else
|
116
|
+
if (!(pw = getpwnam(name))) {
|
117
|
+
return errno;
|
118
|
+
}
|
119
|
+
# endif
|
120
|
+
|
121
|
+
*uid = (int)pw->pw_uid;
|
122
|
+
return SIGAR_OK;
|
123
|
+
}
|
124
|
+
|
125
|
+
#endif /* WIN32 */
|
126
|
+
|
127
|
+
static char *sigar_error_string(int err)
|
128
|
+
{
|
129
|
+
switch (err) {
|
130
|
+
case SIGAR_ENOTIMPL:
|
131
|
+
return "This function has not been implemented on this platform";
|
132
|
+
default:
|
133
|
+
return "Error string not specified yet";
|
134
|
+
}
|
135
|
+
}
|
136
|
+
|
137
|
+
SIGAR_DECLARE(char *) sigar_strerror(sigar_t *sigar, int err)
|
138
|
+
{
|
139
|
+
char *buf;
|
140
|
+
|
141
|
+
if (err < 0) {
|
142
|
+
return sigar->errbuf;
|
143
|
+
}
|
144
|
+
|
145
|
+
if (err > SIGAR_OS_START_ERROR) {
|
146
|
+
if ((buf = sigar_os_error_string(sigar, err)) != NULL) {
|
147
|
+
return buf;
|
148
|
+
}
|
149
|
+
return "Unknown OS Error"; /* should never happen */
|
150
|
+
}
|
151
|
+
|
152
|
+
if (err > SIGAR_START_ERROR) {
|
153
|
+
return sigar_error_string(err);
|
154
|
+
}
|
155
|
+
|
156
|
+
return sigar_strerror_get(err, sigar->errbuf, sizeof(sigar->errbuf));
|
157
|
+
}
|
158
|
+
|
159
|
+
char *sigar_strerror_get(int err, char *errbuf, int buflen)
|
160
|
+
{
|
161
|
+
char *buf = NULL;
|
162
|
+
#ifdef WIN32
|
163
|
+
DWORD len;
|
164
|
+
|
165
|
+
len = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
|
166
|
+
FORMAT_MESSAGE_IGNORE_INSERTS,
|
167
|
+
NULL,
|
168
|
+
err,
|
169
|
+
0, /* default language */
|
170
|
+
(LPTSTR)errbuf,
|
171
|
+
(DWORD)buflen,
|
172
|
+
NULL);
|
173
|
+
#else
|
174
|
+
|
175
|
+
#if defined(HAVE_STRERROR_R) && defined(HAVE_STRERROR_R_GLIBC)
|
176
|
+
/*
|
177
|
+
* strerror_r man page says:
|
178
|
+
* "The GNU version may, but need not, use the user supplied buffer"
|
179
|
+
*/
|
180
|
+
buf = strerror_r(err, errbuf, buflen);
|
181
|
+
#elif defined(HAVE_STRERROR_R)
|
182
|
+
if (strerror_r(err, errbuf, buflen) < 0) {
|
183
|
+
buf = "Unknown Error";
|
184
|
+
}
|
185
|
+
#else
|
186
|
+
/* strerror() is thread safe on solaris and hpux */
|
187
|
+
buf = strerror(err);
|
188
|
+
#endif
|
189
|
+
|
190
|
+
if (buf != NULL) {
|
191
|
+
SIGAR_STRNCPY(errbuf, buf, buflen);
|
192
|
+
}
|
193
|
+
|
194
|
+
#endif
|
195
|
+
return errbuf;
|
196
|
+
}
|
197
|
+
|
198
|
+
void sigar_strerror_set(sigar_t *sigar, char *msg)
|
199
|
+
{
|
200
|
+
SIGAR_SSTRCPY(sigar->errbuf, msg);
|
201
|
+
}
|
202
|
+
|
203
|
+
#ifdef WIN32
|
204
|
+
#define vsnprintf _vsnprintf
|
205
|
+
#endif
|
206
|
+
|
207
|
+
void sigar_strerror_printf(sigar_t *sigar, const char *format, ...)
|
208
|
+
{
|
209
|
+
va_list args;
|
210
|
+
|
211
|
+
va_start(args, format);
|
212
|
+
vsnprintf(sigar->errbuf, sizeof(sigar->errbuf), format, args);
|
213
|
+
va_end(args);
|
214
|
+
}
|
215
|
+
|
216
|
+
/* copy apr_strfsize */
|
217
|
+
SIGAR_DECLARE(char *) sigar_format_size(sigar_uint64_t size, char *buf)
|
218
|
+
{
|
219
|
+
const char ord[] = "KMGTPE";
|
220
|
+
const char *o = ord;
|
221
|
+
int remain;
|
222
|
+
|
223
|
+
if (size == SIGAR_FIELD_NOTIMPL) {
|
224
|
+
buf[0] = '-';
|
225
|
+
buf[1] = '\0';
|
226
|
+
return buf;
|
227
|
+
}
|
228
|
+
|
229
|
+
if (size < 973) {
|
230
|
+
sprintf(buf, "%3d ", (int) size);
|
231
|
+
return buf;
|
232
|
+
}
|
233
|
+
|
234
|
+
do {
|
235
|
+
remain = (int)(size & 1023);
|
236
|
+
size >>= 10;
|
237
|
+
|
238
|
+
if (size >= 973) {
|
239
|
+
++o;
|
240
|
+
continue;
|
241
|
+
}
|
242
|
+
|
243
|
+
if (size < 9 || (size == 9 && remain < 973)) {
|
244
|
+
if ((remain = ((remain * 5) + 256) / 512) >= 10) {
|
245
|
+
++size;
|
246
|
+
remain = 0;
|
247
|
+
}
|
248
|
+
sprintf(buf, "%d.%d%c", (int) size, remain, *o);
|
249
|
+
return buf;
|
250
|
+
}
|
251
|
+
|
252
|
+
if (remain >= 512) {
|
253
|
+
++size;
|
254
|
+
}
|
255
|
+
|
256
|
+
sprintf(buf, "%3d%c", (int) size, *o);
|
257
|
+
|
258
|
+
return buf;
|
259
|
+
} while (1);
|
260
|
+
}
|
261
|
+
|
262
|
+
|
263
|
+
SIGAR_DECLARE(int) sigar_uptime_string(sigar_t *sigar,
|
264
|
+
sigar_uptime_t *uptime,
|
265
|
+
char *buffer,
|
266
|
+
int buflen)
|
267
|
+
{
|
268
|
+
char *ptr = buffer;
|
269
|
+
int time = (int)uptime->uptime;
|
270
|
+
int minutes, hours, days, offset = 0;
|
271
|
+
|
272
|
+
/* XXX: get rid of sprintf and/or check for overflow */
|
273
|
+
days = time / (60*60*24);
|
274
|
+
|
275
|
+
if (days) {
|
276
|
+
offset += sprintf(ptr + offset, "%d day%s, ",
|
277
|
+
days, (days > 1) ? "s" : "");
|
278
|
+
}
|
279
|
+
|
280
|
+
minutes = time / 60;
|
281
|
+
hours = minutes / 60;
|
282
|
+
hours = hours % 24;
|
283
|
+
minutes = minutes % 60;
|
284
|
+
|
285
|
+
if (hours) {
|
286
|
+
offset += sprintf(ptr + offset, "%2d:%02d",
|
287
|
+
hours, minutes);
|
288
|
+
}
|
289
|
+
else {
|
290
|
+
offset += sprintf(ptr + offset, "%d min", minutes);
|
291
|
+
}
|
292
|
+
|
293
|
+
return SIGAR_OK;
|
294
|
+
}
|
295
|
+
|
296
|
+
/* threadsafe alternative to inet_ntoa (inet_ntop4 from apr) */
|
297
|
+
int sigar_inet_ntoa(sigar_t *sigar,
|
298
|
+
sigar_uint32_t address,
|
299
|
+
char *addr_str)
|
300
|
+
{
|
301
|
+
char *next=addr_str;
|
302
|
+
int n=0;
|
303
|
+
const unsigned char *src =
|
304
|
+
(const unsigned char *)&address;
|
305
|
+
|
306
|
+
do {
|
307
|
+
unsigned char u = *src++;
|
308
|
+
if (u > 99) {
|
309
|
+
*next++ = '0' + u/100;
|
310
|
+
u %= 100;
|
311
|
+
*next++ = '0' + u/10;
|
312
|
+
u %= 10;
|
313
|
+
}
|
314
|
+
else if (u > 9) {
|
315
|
+
*next++ = '0' + u/10;
|
316
|
+
u %= 10;
|
317
|
+
}
|
318
|
+
*next++ = '0' + u;
|
319
|
+
*next++ = '.';
|
320
|
+
n++;
|
321
|
+
} while (n < 4);
|
322
|
+
|
323
|
+
*--next = 0;
|
324
|
+
|
325
|
+
return SIGAR_OK;
|
326
|
+
}
|
327
|
+
|
328
|
+
static int sigar_ether_ntoa(char *buff, unsigned char *ptr)
|
329
|
+
{
|
330
|
+
sprintf(buff, "%02X:%02X:%02X:%02X:%02X:%02X",
|
331
|
+
(ptr[0] & 0xff), (ptr[1] & 0xff), (ptr[2] & 0xff),
|
332
|
+
(ptr[3] & 0xff), (ptr[4] & 0xff), (ptr[5] & 0xff));
|
333
|
+
return SIGAR_OK;
|
334
|
+
}
|
335
|
+
|
336
|
+
SIGAR_DECLARE(int) sigar_net_address_equals(sigar_net_address_t *addr1,
|
337
|
+
sigar_net_address_t *addr2)
|
338
|
+
|
339
|
+
{
|
340
|
+
if (addr1->family != addr2->family) {
|
341
|
+
return EINVAL;
|
342
|
+
}
|
343
|
+
|
344
|
+
switch (addr1->family) {
|
345
|
+
case SIGAR_AF_INET:
|
346
|
+
return memcmp(&addr1->addr.in, &addr2->addr.in, sizeof(addr1->addr.in));
|
347
|
+
case SIGAR_AF_INET6:
|
348
|
+
return memcmp(&addr1->addr.in6, &addr2->addr.in6, sizeof(addr1->addr.in6));
|
349
|
+
case SIGAR_AF_LINK:
|
350
|
+
return memcmp(&addr1->addr.mac, &addr2->addr.mac, sizeof(addr1->addr.mac));
|
351
|
+
default:
|
352
|
+
return EINVAL;
|
353
|
+
}
|
354
|
+
}
|
355
|
+
|
356
|
+
#if !defined(WIN32) && !defined(NETWARE) && !defined(__hpux)
|
357
|
+
#define sigar_inet_ntop inet_ntop
|
358
|
+
#define sigar_inet_ntop_errno errno
|
359
|
+
#else
|
360
|
+
#define sigar_inet_ntop(af, src, dst, size) NULL
|
361
|
+
#define sigar_inet_ntop_errno EINVAL
|
362
|
+
#endif
|
363
|
+
|
364
|
+
SIGAR_DECLARE(int) sigar_net_address_to_string(sigar_t *sigar,
|
365
|
+
sigar_net_address_t *address,
|
366
|
+
char *addr_str)
|
367
|
+
{
|
368
|
+
switch (address->family) {
|
369
|
+
case SIGAR_AF_INET6:
|
370
|
+
if (sigar_inet_ntop(AF_INET6, (const void *)&address->addr.in6,
|
371
|
+
addr_str, SIGAR_INET6_ADDRSTRLEN))
|
372
|
+
{
|
373
|
+
return SIGAR_OK;
|
374
|
+
}
|
375
|
+
else {
|
376
|
+
return sigar_inet_ntop_errno;
|
377
|
+
}
|
378
|
+
case SIGAR_AF_INET:
|
379
|
+
return sigar_inet_ntoa(sigar, address->addr.in, addr_str);
|
380
|
+
case SIGAR_AF_UNSPEC:
|
381
|
+
return sigar_inet_ntoa(sigar, 0, addr_str); /*XXX*/
|
382
|
+
case SIGAR_AF_LINK:
|
383
|
+
return sigar_ether_ntoa(addr_str, &address->addr.mac[0]);
|
384
|
+
default:
|
385
|
+
return EINVAL;
|
386
|
+
}
|
387
|
+
}
|
388
|
+
|
389
|
+
SIGAR_DECLARE(sigar_uint32_t) sigar_net_address_hash(sigar_net_address_t *address)
|
390
|
+
{
|
391
|
+
sigar_uint32_t hash = 0;
|
392
|
+
unsigned char *data;
|
393
|
+
int i=0, size, elts;
|
394
|
+
|
395
|
+
switch (address->family) {
|
396
|
+
case SIGAR_AF_UNSPEC:
|
397
|
+
case SIGAR_AF_INET:
|
398
|
+
return address->addr.in;
|
399
|
+
case SIGAR_AF_INET6:
|
400
|
+
data = (unsigned char *)&address->addr.in6;
|
401
|
+
size = sizeof(address->addr.in6);
|
402
|
+
elts = 4;
|
403
|
+
break;
|
404
|
+
case SIGAR_AF_LINK:
|
405
|
+
data = (unsigned char *)&address->addr.mac;
|
406
|
+
size = sizeof(address->addr.mac);
|
407
|
+
elts = 2;
|
408
|
+
break;
|
409
|
+
default:
|
410
|
+
return -1;
|
411
|
+
}
|
412
|
+
|
413
|
+
while (i<size) {
|
414
|
+
int j=0;
|
415
|
+
int component=0;
|
416
|
+
while (j<elts && i<size) {
|
417
|
+
component = (component << 8) + data[i];
|
418
|
+
j++;
|
419
|
+
i++;
|
420
|
+
}
|
421
|
+
hash += component;
|
422
|
+
}
|
423
|
+
|
424
|
+
return hash;
|
425
|
+
}
|
426
|
+
|
427
|
+
SIGAR_DECLARE(const char *)sigar_net_connection_type_get(int type)
|
428
|
+
{
|
429
|
+
switch (type) {
|
430
|
+
case SIGAR_NETCONN_TCP:
|
431
|
+
return "tcp";
|
432
|
+
case SIGAR_NETCONN_UDP:
|
433
|
+
return "udp";
|
434
|
+
case SIGAR_NETCONN_RAW:
|
435
|
+
return "raw";
|
436
|
+
case SIGAR_NETCONN_UNIX:
|
437
|
+
return "unix";
|
438
|
+
default:
|
439
|
+
return "unknown";
|
440
|
+
}
|
441
|
+
}
|
442
|
+
|
443
|
+
SIGAR_DECLARE(const char *)sigar_net_connection_state_get(int state)
|
444
|
+
{
|
445
|
+
switch (state) {
|
446
|
+
case SIGAR_TCP_ESTABLISHED:
|
447
|
+
return "ESTABLISHED";
|
448
|
+
case SIGAR_TCP_SYN_SENT:
|
449
|
+
return "SYN_SENT";
|
450
|
+
case SIGAR_TCP_SYN_RECV:
|
451
|
+
return "SYN_RECV";
|
452
|
+
case SIGAR_TCP_FIN_WAIT1:
|
453
|
+
return "FIN_WAIT1";
|
454
|
+
case SIGAR_TCP_FIN_WAIT2:
|
455
|
+
return "FIN_WAIT2";
|
456
|
+
case SIGAR_TCP_TIME_WAIT:
|
457
|
+
return "TIME_WAIT";
|
458
|
+
case SIGAR_TCP_CLOSE:
|
459
|
+
return "CLOSE";
|
460
|
+
case SIGAR_TCP_CLOSE_WAIT:
|
461
|
+
return "CLOSE_WAIT";
|
462
|
+
case SIGAR_TCP_LAST_ACK:
|
463
|
+
return "LAST_ACK";
|
464
|
+
case SIGAR_TCP_LISTEN:
|
465
|
+
return "LISTEN";
|
466
|
+
case SIGAR_TCP_CLOSING:
|
467
|
+
return "CLOSING";
|
468
|
+
case SIGAR_TCP_IDLE:
|
469
|
+
return "IDLE";
|
470
|
+
case SIGAR_TCP_BOUND:
|
471
|
+
return "BOUND";
|
472
|
+
case SIGAR_TCP_UNKNOWN:
|
473
|
+
default:
|
474
|
+
return "UNKNOWN";
|
475
|
+
}
|
476
|
+
}
|
477
|
+
|
478
|
+
SIGAR_DECLARE(char *) sigar_net_interface_flags_to_string(sigar_uint64_t flags, char *buf)
|
479
|
+
{
|
480
|
+
*buf = '\0';
|
481
|
+
|
482
|
+
if (flags == 0) {
|
483
|
+
strcat(buf, "[NO FLAGS] ");
|
484
|
+
}
|
485
|
+
if (flags & SIGAR_IFF_UP) {
|
486
|
+
strcat(buf, "UP ");
|
487
|
+
}
|
488
|
+
if (flags & SIGAR_IFF_BROADCAST) {
|
489
|
+
strcat(buf, "BROADCAST ");
|
490
|
+
}
|
491
|
+
if (flags & SIGAR_IFF_DEBUG) {
|
492
|
+
strcat(buf, "DEBUG ");
|
493
|
+
}
|
494
|
+
if (flags & SIGAR_IFF_LOOPBACK) {
|
495
|
+
strcat(buf, "LOOPBACK ");
|
496
|
+
}
|
497
|
+
if (flags & SIGAR_IFF_POINTOPOINT) {
|
498
|
+
strcat(buf, "POINTOPOINT ");
|
499
|
+
}
|
500
|
+
if (flags & SIGAR_IFF_NOTRAILERS) {
|
501
|
+
strcat(buf, "NOTRAILERS ");
|
502
|
+
}
|
503
|
+
if (flags & SIGAR_IFF_RUNNING) {
|
504
|
+
strcat(buf, "RUNNING ");
|
505
|
+
}
|
506
|
+
if (flags & SIGAR_IFF_NOARP) {
|
507
|
+
strcat(buf, "NOARP ");
|
508
|
+
}
|
509
|
+
if (flags & SIGAR_IFF_PROMISC) {
|
510
|
+
strcat(buf, "PROMISC ");
|
511
|
+
}
|
512
|
+
if (flags & SIGAR_IFF_ALLMULTI) {
|
513
|
+
strcat(buf, "ALLMULTI ");
|
514
|
+
}
|
515
|
+
if (flags & SIGAR_IFF_MULTICAST) {
|
516
|
+
strcat(buf, "MULTICAST ");
|
517
|
+
}
|
518
|
+
|
519
|
+
return buf;
|
520
|
+
}
|
521
|
+
|
522
|
+
#ifdef WIN32
|
523
|
+
#define NET_SERVICES_FILE "C:\\windows\\system32\\drivers\\etc\\services"
|
524
|
+
#else
|
525
|
+
#define NET_SERVICES_FILE "/etc/services"
|
526
|
+
#endif
|
527
|
+
|
528
|
+
static int net_services_parse(sigar_cache_t *names, char *type)
|
529
|
+
{
|
530
|
+
FILE *fp;
|
531
|
+
char buffer[8192], *ptr;
|
532
|
+
char *file;
|
533
|
+
|
534
|
+
|
535
|
+
if (!(file = getenv("SIGAR_NET_SERVICES_FILE"))) {
|
536
|
+
file = NET_SERVICES_FILE;
|
537
|
+
}
|
538
|
+
|
539
|
+
if (!(fp = fopen(file, "r"))) {
|
540
|
+
return errno;
|
541
|
+
}
|
542
|
+
|
543
|
+
while ((ptr = fgets(buffer, sizeof(buffer), fp))) {
|
544
|
+
int port;
|
545
|
+
char name[256], proto[56];
|
546
|
+
sigar_cache_entry_t *entry;
|
547
|
+
|
548
|
+
while (sigar_isspace(*ptr)) {
|
549
|
+
++ptr;
|
550
|
+
}
|
551
|
+
if ((*ptr == '#') || (*ptr == '\0')) {
|
552
|
+
continue;
|
553
|
+
}
|
554
|
+
|
555
|
+
if (sscanf(ptr, "%s%d/%s", name, &port, proto) != 3) {
|
556
|
+
continue;
|
557
|
+
}
|
558
|
+
if (!strEQ(type, proto)) {
|
559
|
+
continue;
|
560
|
+
}
|
561
|
+
|
562
|
+
entry = sigar_cache_get(names, port);
|
563
|
+
if (!entry->value) {
|
564
|
+
entry->value = strdup(name);
|
565
|
+
}
|
566
|
+
}
|
567
|
+
|
568
|
+
fclose(fp);
|
569
|
+
return SIGAR_OK;
|
570
|
+
}
|
571
|
+
|
572
|
+
SIGAR_DECLARE(char *)sigar_net_services_name_get(sigar_t *sigar,
|
573
|
+
int protocol, unsigned long port)
|
574
|
+
{
|
575
|
+
sigar_cache_entry_t *entry;
|
576
|
+
sigar_cache_t **names;
|
577
|
+
char *pname;
|
578
|
+
|
579
|
+
switch (protocol) {
|
580
|
+
case SIGAR_NETCONN_TCP:
|
581
|
+
names = &sigar->net_services_tcp;
|
582
|
+
pname = "tcp";
|
583
|
+
break;
|
584
|
+
case SIGAR_NETCONN_UDP:
|
585
|
+
names = &sigar->net_services_udp;
|
586
|
+
pname = "udp";
|
587
|
+
break;
|
588
|
+
default:
|
589
|
+
return NULL;
|
590
|
+
}
|
591
|
+
|
592
|
+
if (*names == NULL) {
|
593
|
+
*names = sigar_cache_new(1024);
|
594
|
+
net_services_parse(*names, pname);
|
595
|
+
}
|
596
|
+
|
597
|
+
if ((entry = sigar_cache_find(*names, port))) {
|
598
|
+
return (char *)entry->value;
|
599
|
+
}
|
600
|
+
else {
|
601
|
+
return NULL;
|
602
|
+
}
|
603
|
+
}
|
604
|
+
|
605
|
+
SIGAR_DECLARE(int) sigar_cpu_perc_calculate(sigar_cpu_t *prev,
|
606
|
+
sigar_cpu_t *curr,
|
607
|
+
sigar_cpu_perc_t *perc)
|
608
|
+
{
|
609
|
+
double diff_user, diff_sys, diff_nice, diff_idle;
|
610
|
+
double diff_wait, diff_irq, diff_soft_irq, diff_stolen;
|
611
|
+
double diff_total;
|
612
|
+
|
613
|
+
diff_user = curr->user - prev->user;
|
614
|
+
diff_sys = curr->sys - prev->sys;
|
615
|
+
diff_nice = curr->nice - prev->nice;
|
616
|
+
diff_idle = curr->idle - prev->idle;
|
617
|
+
diff_wait = curr->wait - prev->wait;
|
618
|
+
diff_irq = curr->irq - prev->irq;
|
619
|
+
diff_soft_irq = curr->soft_irq - prev->soft_irq;
|
620
|
+
diff_stolen = curr->stolen - prev->stolen;
|
621
|
+
|
622
|
+
diff_user = diff_user < 0 ? 0 : diff_user;
|
623
|
+
diff_sys = diff_sys < 0 ? 0 : diff_sys;
|
624
|
+
diff_nice = diff_nice < 0 ? 0 : diff_nice;
|
625
|
+
diff_idle = diff_idle < 0 ? 0 : diff_idle;
|
626
|
+
diff_wait = diff_wait < 0 ? 0 : diff_wait;
|
627
|
+
diff_irq = diff_irq < 0 ? 0 : diff_irq;
|
628
|
+
diff_soft_irq = diff_soft_irq < 0 ? 0 : diff_soft_irq;
|
629
|
+
diff_stolen = diff_stolen < 0 ? 0 : diff_stolen;
|
630
|
+
|
631
|
+
diff_total =
|
632
|
+
diff_user + diff_sys + diff_nice + diff_idle +
|
633
|
+
diff_wait + diff_irq + diff_soft_irq +
|
634
|
+
diff_stolen;
|
635
|
+
|
636
|
+
perc->user = diff_user / diff_total;
|
637
|
+
perc->sys = diff_sys / diff_total;
|
638
|
+
perc->nice = diff_nice / diff_total;
|
639
|
+
perc->idle = diff_idle / diff_total;
|
640
|
+
perc->wait = diff_wait / diff_total;
|
641
|
+
perc->irq = diff_irq / diff_total;
|
642
|
+
perc->soft_irq = diff_soft_irq / diff_total;
|
643
|
+
perc->stolen = diff_stolen / diff_total;
|
644
|
+
|
645
|
+
perc->combined =
|
646
|
+
perc->user + perc->sys + perc->nice + perc->wait;
|
647
|
+
|
648
|
+
return SIGAR_OK;
|
649
|
+
}
|