-
Notifications
You must be signed in to change notification settings - Fork 4
/
statgrab.pyx
590 lines (525 loc) · 18.3 KB
/
statgrab.pyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
#
# pystatgrab
# https://libstatgrab.org/pystatgrab/
# Copyright (C) 2004-2019 Tim Bishop
# Copyright (C) 2005-2013 Adam Sampson
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
# $Id$
#
from libc.stdlib cimport malloc, free
from sys import version_info
cimport libstatgrab as sg
# Helper code
class StatgrabError(Exception):
"""Exception representing a libstatgrab error."""
def __init__(self):
cdef sg.sg_error_details details
sg.sg_get_error_details(&details)
self.error = details.error
self.errno_value = details.errno_value
self.error_arg = details.error_arg
cdef char *msg = NULL
sg.sg_strperror(&msg, &details)
super(StatgrabError, self).__init__("statgrab error: " + msg)
free(msg)
cdef int _not_error(sg.sg_error code) except -1:
"""Raise StatgrabError if code is not SG_ERROR_NONE."""
if code != sg.SG_ERROR_NONE:
raise StatgrabError()
return 0
cdef int _not_null(const void *value) except -1:
"""Raise StatgrabError if value is NULL."""
if value == NULL:
raise StatgrabError()
return 0
cdef int _vector_not_null(const void *value, size_t entries) except -1:
"""Check the return value from a vector-returning function.
Raise StatgrabError if:
- entries > 0 and value is NULL, or
- entries == 0 and sg_get_error indicates an error has occurred."""
# FIXME: this is an API oddity; libstatgrab issue #17 discusses it
if entries > 0 and value == NULL:
raise StatgrabError()
elif entries == 0 and sg.sg_get_error() != sg.SG_ERROR_NONE:
raise StatgrabError()
return 0
class Result(dict):
def __init__(self, attrs):
super(Result, self).__init__(attrs)
# For compatibility with older pystatgrab.
self.attrs = attrs
def __getattr__(self, item):
try:
return self.__getitem__(item)
except KeyError:
raise AttributeError(item)
def _str_to_bytes(s):
if version_info[0] >= 3 and isinstance(s, str):
return bytes(s, 'utf-8')
else:
return s
def _bytes_to_str(b):
if version_info[0] >= 3 and isinstance(b, bytes):
return str(b, 'utf-8')
else:
return b
# libstatgrab constants exported
ERROR_NONE = sg.SG_ERROR_NONE
ERROR_INVALID_ARGUMENT = sg.SG_ERROR_INVALID_ARGUMENT
ERROR_ASPRINTF = sg.SG_ERROR_ASPRINTF
ERROR_SPRINTF = sg.SG_ERROR_SPRINTF
ERROR_DEVICES = sg.SG_ERROR_DEVICES
ERROR_DEVSTAT_GETDEVS = sg.SG_ERROR_DEVSTAT_GETDEVS
ERROR_DEVSTAT_SELECTDEVS = sg.SG_ERROR_DEVSTAT_SELECTDEVS
ERROR_DISKINFO = sg.SG_ERROR_DISKINFO
ERROR_ENOENT = sg.SG_ERROR_ENOENT
ERROR_GETIFADDRS = sg.SG_ERROR_GETIFADDRS
ERROR_GETMNTINFO = sg.SG_ERROR_GETMNTINFO
ERROR_GETPAGESIZE = sg.SG_ERROR_GETPAGESIZE
ERROR_HOST = sg.SG_ERROR_HOST
ERROR_KSTAT_DATA_LOOKUP = sg.SG_ERROR_KSTAT_DATA_LOOKUP
ERROR_KSTAT_LOOKUP = sg.SG_ERROR_KSTAT_LOOKUP
ERROR_KSTAT_OPEN = sg.SG_ERROR_KSTAT_OPEN
ERROR_KSTAT_READ = sg.SG_ERROR_KSTAT_READ
ERROR_KVM_GETSWAPINFO = sg.SG_ERROR_KVM_GETSWAPINFO
ERROR_KVM_OPENFILES = sg.SG_ERROR_KVM_OPENFILES
ERROR_MALLOC = sg.SG_ERROR_MALLOC
ERROR_MEMSTATUS = sg.SG_ERROR_MEMSTATUS
ERROR_OPEN = sg.SG_ERROR_OPEN
ERROR_OPENDIR = sg.SG_ERROR_OPENDIR
ERROR_READDIR = sg.SG_ERROR_READDIR
ERROR_PARSE = sg.SG_ERROR_PARSE
ERROR_PDHADD = sg.SG_ERROR_PDHADD
ERROR_PDHCOLLECT = sg.SG_ERROR_PDHCOLLECT
ERROR_PDHOPEN = sg.SG_ERROR_PDHOPEN
ERROR_PDHREAD = sg.SG_ERROR_PDHREAD
ERROR_PERMISSION = sg.SG_ERROR_PERMISSION
ERROR_PSTAT = sg.SG_ERROR_PSTAT
ERROR_SETEGID = sg.SG_ERROR_SETEGID
ERROR_SETEUID = sg.SG_ERROR_SETEUID
ERROR_SETMNTENT = sg.SG_ERROR_SETMNTENT
ERROR_SOCKET = sg.SG_ERROR_SOCKET
ERROR_SWAPCTL = sg.SG_ERROR_SWAPCTL
ERROR_SYSCONF = sg.SG_ERROR_SYSCONF
ERROR_SYSCTL = sg.SG_ERROR_SYSCTL
ERROR_SYSCTLBYNAME = sg.SG_ERROR_SYSCTLBYNAME
ERROR_SYSCTLNAMETOMIB = sg.SG_ERROR_SYSCTLNAMETOMIB
ERROR_SYSINFO = sg.SG_ERROR_SYSINFO
ERROR_MACHCALL = sg.SG_ERROR_MACHCALL
ERROR_IOKIT = sg.SG_ERROR_IOKIT
ERROR_UNAME = sg.SG_ERROR_UNAME
ERROR_UNSUPPORTED = sg.SG_ERROR_UNSUPPORTED
ERROR_XSW_VER_MISMATCH = sg.SG_ERROR_XSW_VER_MISMATCH
ERROR_GETMSG = sg.SG_ERROR_GETMSG
ERROR_PUTMSG = sg.SG_ERROR_PUTMSG
ERROR_INITIALISATION = sg.SG_ERROR_INITIALISATION
ERROR_MUTEX_LOCK = sg.SG_ERROR_MUTEX_LOCK
ERROR_MUTEX_UNLOCK = sg.SG_ERROR_MUTEX_UNLOCK
entire_cpu_percent = sg.sg_entire_cpu_percent
last_diff_cpu_percent = sg.sg_last_diff_cpu_percent
new_diff_cpu_percent = sg.sg_new_diff_cpu_percent
IFACE_DUPLEX_FULL = sg.SG_IFACE_DUPLEX_FULL
IFACE_DUPLEX_HALF = sg.SG_IFACE_DUPLEX_HALF
IFACE_DUPLEX_UNKNOWN = sg.SG_IFACE_DUPLEX_UNKNOWN
IFACE_DOWN = sg.SG_IFACE_DOWN
IFACE_UP = sg.SG_IFACE_UP
PROCESS_STATE_RUNNING = sg.SG_PROCESS_STATE_RUNNING
PROCESS_STATE_SLEEPING = sg.SG_PROCESS_STATE_SLEEPING
PROCESS_STATE_STOPPED = sg.SG_PROCESS_STATE_STOPPED
PROCESS_STATE_ZOMBIE = sg.SG_PROCESS_STATE_ZOMBIE
PROCESS_STATE_UNKNOWN = sg.SG_PROCESS_STATE_UNKNOWN
entire_process_count = sg.sg_entire_process_count
last_process_count = sg.sg_last_process_count
# libstatgrab functions exported
# FIXME: docstrings
def init(ignore_init_errors=False):
_not_error(sg.sg_init(ignore_init_errors))
def snapshot():
_not_error(sg.sg_snapshot())
def shutdown():
_not_error(sg.sg_shutdown())
def drop_privileges():
_not_error(sg.sg_drop_privileges())
def get_host_info():
cdef const sg.sg_host_info *s = sg.sg_get_host_info(NULL)
_not_null(s)
return Result({
'os_name': s.os_name,
'os_release': s.os_release,
'os_version': s.os_version,
'platform': s.platform,
'hostname': s.hostname,
'bitwidth': s.bitwidth,
'host_state': s.host_state,
'ncpus': s.ncpus,
'maxcpus': s.maxcpus,
'uptime': s.uptime,
'systime': s.systime,
})
cdef _make_cpu_stats(const sg.sg_cpu_stats *s):
return Result({
'user': s.user,
'kernel': s.kernel,
'idle': s.idle,
'iowait': s.iowait,
'swap': s.swap,
'nice': s.nice,
'total': s.total,
'context_switches': s.context_switches,
'voluntary_context_switches': s.voluntary_context_switches,
'involuntary_context_switches': s.involuntary_context_switches,
'syscalls': s.syscalls,
'interrupts': s.interrupts,
'soft_interrupts': s.soft_interrupts,
'systime': s.systime,
})
def get_cpu_stats():
cdef const sg.sg_cpu_stats *s = sg.sg_get_cpu_stats(NULL)
_not_null(s)
return _make_cpu_stats(s)
def get_cpu_stats_diff():
cdef const sg.sg_cpu_stats *s = sg.sg_get_cpu_stats_diff(NULL)
_not_null(s)
return _make_cpu_stats(s)
def get_cpu_percents(cps=new_diff_cpu_percent):
cdef const sg.sg_cpu_percents *s = sg.sg_get_cpu_percents(NULL)
_not_null(s)
return Result({
'user': s.user,
'kernel': s.kernel,
'idle': s.idle,
'iowait': s.iowait,
'swap': s.swap,
'nice': s.nice,
'time_taken': s.time_taken,
})
def get_mem_stats():
cdef const sg.sg_mem_stats *s = sg.sg_get_mem_stats(NULL)
_not_null(s)
return Result({
'total': s.total,
'free': s.free,
'used': s.used,
'cache': s.cache,
'systime': s.systime,
})
def get_load_stats():
cdef const sg.sg_load_stats *s = sg.sg_get_load_stats(NULL)
_not_null(s)
return Result({
'min1': s.min1,
'min5': s.min5,
'min15': s.min15,
'systime': s.systime,
})
cdef _make_user_stats(const sg.sg_user_stats *s):
return Result({
'login_name': s.login_name,
# Note special handling for record_id.
'record_id': s.record_id[:s.record_id_size],
'device': s.device,
'hostname': s.hostname,
'pid': s.pid,
'login_time': s.login_time,
'systime': s.systime,
})
def get_user_stats():
cdef size_t n
cdef const sg.sg_user_stats *s = sg.sg_get_user_stats(&n)
_vector_not_null(s, n)
return [_make_user_stats(&s[i]) for i in range(n)]
def get_swap_stats():
cdef const sg.sg_swap_stats *s = sg.sg_get_swap_stats(NULL)
_not_null(s)
return Result({
'total': s.total,
'used': s.used,
'free': s.free,
'systime': s.systime,
})
def get_valid_filesystems():
cdef size_t n
cdef const char **s = sg.sg_get_valid_filesystems(&n)
_vector_not_null(s, n)
return [_bytes_to_str(s[i]) for i in range(n)]
def set_valid_filesystems(valid_fs):
cdef int num_fs = len(valid_fs)
cdef const char **vs
vs = <const char **>malloc((num_fs + 1) * sizeof(const char *))
if vs == NULL:
raise MemoryError("malloc failed")
valid_fs = [_str_to_bytes(x) for x in valid_fs]
for i in range(num_fs):
vs[i] = valid_fs[i]
vs[num_fs] = NULL
_not_error(sg.sg_set_valid_filesystems(vs))
free(vs)
cdef _make_fs_stats(const sg.sg_fs_stats *s):
return Result({
'device_name': s.device_name,
'device_canonical': s.device_canonical,
'fs_type': s.fs_type,
'mnt_point': s.mnt_point,
'device_type': s.device_type,
'size': s.size,
'used': s.used,
'free': s.free,
'avail': s.avail,
'total_inodes': s.total_inodes,
'used_inodes': s.used_inodes,
'free_inodes': s.free_inodes,
'avail_inodes': s.avail_inodes,
'io_size': s.io_size,
'block_size': s.block_size,
'total_blocks': s.total_blocks,
'free_blocks': s.free_blocks,
'used_blocks': s.used_blocks,
'avail_blocks': s.avail_blocks,
'systime': s.systime,
})
def get_fs_stats():
cdef size_t n
cdef const sg.sg_fs_stats *s = sg.sg_get_fs_stats(&n)
_vector_not_null(s, n)
return [_make_fs_stats(&s[i]) for i in range(n)]
def get_fs_stats_diff():
cdef size_t n
cdef const sg.sg_fs_stats *s = sg.sg_get_fs_stats_diff(&n)
_vector_not_null(s, n)
return [_make_fs_stats(&s[i]) for i in range(n)]
cdef _make_disk_io_stats(const sg.sg_disk_io_stats *s):
return Result({
'disk_name': s.disk_name,
'read_bytes': s.read_bytes,
'write_bytes': s.write_bytes,
'systime': s.systime,
})
def get_disk_io_stats():
cdef size_t n
cdef const sg.sg_disk_io_stats *s = sg.sg_get_disk_io_stats(&n)
_vector_not_null(s, n)
return [_make_disk_io_stats(&s[i]) for i in range(n)]
def get_disk_io_stats_diff():
cdef size_t n
cdef const sg.sg_disk_io_stats *s = sg.sg_get_disk_io_stats_diff(&n)
_vector_not_null(s, n)
return [_make_disk_io_stats(&s[i]) for i in range(n)]
cdef _make_network_io_stats(const sg.sg_network_io_stats *s):
return Result({
'interface_name': s.interface_name,
'tx': s.tx,
'rx': s.rx,
'ipackets': s.ipackets,
'opackets': s.opackets,
'ierrors': s.ierrors,
'oerrors': s.oerrors,
'collisions': s.collisions,
'systime': s.systime,
})
def get_network_io_stats():
cdef size_t n
cdef const sg.sg_network_io_stats *s = sg.sg_get_network_io_stats(&n)
_vector_not_null(s, n)
return [_make_network_io_stats(&s[i]) for i in range(n)]
def get_network_io_stats_diff():
cdef size_t n
cdef const sg.sg_network_io_stats *s = sg.sg_get_network_io_stats_diff(&n)
_vector_not_null(s, n)
return [_make_network_io_stats(&s[i]) for i in range(n)]
cdef _make_network_iface_stats(const sg.sg_network_iface_stats *s):
return Result({
'interface_name': s.interface_name,
'speed': s.speed,
'factor': s.factor,
'duplex': s.duplex,
'up': s.up,
'systime': s.systime,
})
def get_network_iface_stats():
cdef size_t n
cdef const sg.sg_network_iface_stats *s = sg.sg_get_network_iface_stats(&n)
_vector_not_null(s, n)
return [_make_network_iface_stats(&s[i]) for i in range(n)]
cdef _make_page_stats(const sg.sg_page_stats *s):
return Result({
'pages_pagein': s.pages_pagein,
'pages_pageout': s.pages_pageout,
'systime': s.systime,
})
def get_page_stats():
cdef const sg.sg_page_stats *s = sg.sg_get_page_stats(NULL)
_not_null(s)
return _make_page_stats(s)
def get_page_stats_diff():
cdef const sg.sg_page_stats *s = sg.sg_get_page_stats_diff(NULL)
_not_null(s)
return _make_page_stats(s)
cdef _make_process_stats(const sg.sg_process_stats *s):
return Result({
'process_name': s.process_name,
'proctitle': s.proctitle,
'pid': s.pid,
'parent': s.parent,
'pgid': s.pgid,
'sessid': s.sessid,
'uid': s.uid,
'euid': s.euid,
'gid': s.gid,
'egid': s.egid,
'context_switches': s.context_switches,
'voluntary_context_switches': s.voluntary_context_switches,
'involuntary_context_switches': s.involuntary_context_switches,
'proc_size': s.proc_size,
'proc_resident': s.proc_resident,
'start_time': s.start_time,
'time_spent': s.time_spent,
'cpu_percent': s.cpu_percent,
'nice': s.nice,
'state': s.state,
'systime': s.systime,
})
def get_process_stats():
cdef size_t n
cdef const sg.sg_process_stats *s = sg.sg_get_process_stats(&n)
_vector_not_null(s, n)
return [_make_process_stats(&s[i]) for i in range(n)]
def get_process_count(pcs=entire_process_count):
cdef const sg.sg_process_count *s = sg.sg_get_process_count_of(pcs)
_not_null(s)
return Result({
'total': s.total,
'running': s.running,
'sleeping': s.sleeping,
'stopped': s.stopped,
'zombie': s.zombie,
'unknown': s.unknown,
'systime': s.systime,
})
# Backwards compatibility with pystatgrab <= 0.5
SG_ERROR_NONE = ERROR_NONE
SG_ERROR_INVALID_ARGUMENT = ERROR_INVALID_ARGUMENT
SG_ERROR_ASPRINTF = ERROR_ASPRINTF
SG_ERROR_SPRINTF = ERROR_SPRINTF
SG_ERROR_DEVICES = ERROR_DEVICES
SG_ERROR_DEVSTAT_GETDEVS = ERROR_DEVSTAT_GETDEVS
SG_ERROR_DEVSTAT_SELECTDEVS = ERROR_DEVSTAT_SELECTDEVS
SG_ERROR_DISKINFO = ERROR_DISKINFO
SG_ERROR_ENOENT = ERROR_ENOENT
SG_ERROR_GETIFADDRS = ERROR_GETIFADDRS
SG_ERROR_GETMNTINFO = ERROR_GETMNTINFO
SG_ERROR_GETPAGESIZE = ERROR_GETPAGESIZE
SG_ERROR_HOST = ERROR_HOST
SG_ERROR_KSTAT_DATA_LOOKUP = ERROR_KSTAT_DATA_LOOKUP
SG_ERROR_KSTAT_LOOKUP = ERROR_KSTAT_LOOKUP
SG_ERROR_KSTAT_OPEN = ERROR_KSTAT_OPEN
SG_ERROR_KSTAT_READ = ERROR_KSTAT_READ
SG_ERROR_KVM_GETSWAPINFO = ERROR_KVM_GETSWAPINFO
SG_ERROR_KVM_OPENFILES = ERROR_KVM_OPENFILES
SG_ERROR_MALLOC = ERROR_MALLOC
SG_ERROR_MEMSTATUS = ERROR_MEMSTATUS
SG_ERROR_OPEN = ERROR_OPEN
SG_ERROR_OPENDIR = ERROR_OPENDIR
SG_ERROR_READDIR = ERROR_READDIR
SG_ERROR_PARSE = ERROR_PARSE
SG_ERROR_PDHADD = ERROR_PDHADD
SG_ERROR_PDHCOLLECT = ERROR_PDHCOLLECT
SG_ERROR_PDHOPEN = ERROR_PDHOPEN
SG_ERROR_PDHREAD = ERROR_PDHREAD
SG_ERROR_PERMISSION = ERROR_PERMISSION
SG_ERROR_PSTAT = ERROR_PSTAT
SG_ERROR_SETEGID = ERROR_SETEGID
SG_ERROR_SETEUID = ERROR_SETEUID
SG_ERROR_SETMNTENT = ERROR_SETMNTENT
SG_ERROR_SOCKET = ERROR_SOCKET
SG_ERROR_SWAPCTL = ERROR_SWAPCTL
SG_ERROR_SYSCONF = ERROR_SYSCONF
SG_ERROR_SYSCTL = ERROR_SYSCTL
SG_ERROR_SYSCTLBYNAME = ERROR_SYSCTLBYNAME
SG_ERROR_SYSCTLNAMETOMIB = ERROR_SYSCTLNAMETOMIB
SG_ERROR_SYSINFO = ERROR_SYSINFO
SG_ERROR_MACHCALL = ERROR_MACHCALL
SG_ERROR_IOKIT = ERROR_IOKIT
SG_ERROR_UNAME = ERROR_UNAME
SG_ERROR_UNSUPPORTED = ERROR_UNSUPPORTED
SG_ERROR_XSW_VER_MISMATCH = ERROR_XSW_VER_MISMATCH
SG_ERROR_GETMSG = ERROR_GETMSG
SG_ERROR_PUTMSG = ERROR_PUTMSG
SG_ERROR_INITIALISATION = ERROR_INITIALISATION
SG_ERROR_MUTEX_LOCK = ERROR_MUTEX_LOCK
SG_ERROR_MUTEX_UNLOCK = ERROR_MUTEX_UNLOCK
SG_IFACE_DUPLEX_FULL = IFACE_DUPLEX_FULL
SG_IFACE_DUPLEX_HALF = IFACE_DUPLEX_HALF
SG_IFACE_DUPLEX_UNKNOWN = IFACE_DUPLEX_UNKNOWN
SG_IFACE_DOWN = IFACE_DOWN
SG_IFACE_UP = IFACE_UP
SG_PROCESS_STATE_RUNNING = PROCESS_STATE_RUNNING
SG_PROCESS_STATE_SLEEPING = PROCESS_STATE_SLEEPING
SG_PROCESS_STATE_STOPPED = PROCESS_STATE_STOPPED
SG_PROCESS_STATE_ZOMBIE = PROCESS_STATE_ZOMBIE
SG_PROCESS_STATE_UNKNOWN = PROCESS_STATE_UNKNOWN
# Some functions returned True/False for success/failure.
def _wrap_success(func, *args):
try:
func(*args)
return True
except StatgrabError:
return False
def sg_init():
return _wrap_success(init)
def sg_snapshot():
return _wrap_success(snapshot)
def sg_shutdown():
return _wrap_success(shutdown)
def sg_drop_privileges():
return _wrap_success(drop_privileges)
# The error-handling functions just wrapped those from libstatgrab.
def sg_get_error():
return sg.sg_get_error()
def sg_get_error_arg():
return sg.sg_get_error_arg()
def sg_get_error_errno():
return sg.sg_get_error_errno()
def sg_str_error(code):
return _bytes_to_str(sg.sg_str_error(code))
# Some functions work the same way, just with a different name.
sg_get_host_info = get_host_info
sg_get_cpu_stats = get_cpu_stats
sg_get_cpu_stats_diff = get_cpu_stats_diff
sg_get_cpu_percents = get_cpu_percents
sg_get_mem_stats = get_mem_stats
sg_get_load_stats = get_load_stats
sg_get_swap_stats = get_swap_stats
sg_get_fs_stats = get_fs_stats
sg_get_disk_io_stats = get_disk_io_stats
sg_get_disk_io_stats_diff = get_disk_io_stats_diff
sg_get_network_io_stats = get_network_io_stats
sg_get_network_io_stats_diff = get_network_io_stats_diff
sg_get_network_iface_stats = get_network_iface_stats
sg_get_page_stats = get_page_stats
sg_get_page_stats_diff = get_page_stats_diff
sg_get_process_stats = get_process_stats
sg_get_process_count = get_process_count
# User stats used to be a single result rather than a list.
def sg_get_user_stats():
cdef size_t n
cdef const sg.sg_user_stats *s = sg.sg_get_user_stats(&n)
_vector_not_null(s, n)
return Result({
'name_list': " ".join([_bytes_to_str(s[i].login_name) for i in range(n)]),
'num_entries': n,
})