1 |
tdb |
1.2 |
# TODO: deal with failures better (return nothing, or raise error?) |
2 |
tdb |
1.1 |
|
3 |
|
|
ctypedef long time_t |
4 |
|
|
|
5 |
|
|
cdef extern from "statgrab.h": |
6 |
|
|
ctypedef struct cpu_states_t: |
7 |
|
|
long long user |
8 |
|
|
long long kernel |
9 |
|
|
long long idle |
10 |
|
|
long long iowait |
11 |
|
|
long long swap |
12 |
|
|
long long nice |
13 |
|
|
long long total |
14 |
|
|
time_t systime |
15 |
|
|
|
16 |
|
|
ctypedef struct cpu_percent_t: |
17 |
|
|
float user |
18 |
|
|
float kernel |
19 |
|
|
float idle |
20 |
|
|
float iowait |
21 |
|
|
float swap |
22 |
|
|
float nice |
23 |
|
|
time_t time_taken |
24 |
|
|
|
25 |
|
|
ctypedef struct mem_stat_t: |
26 |
|
|
long long total |
27 |
|
|
long long free |
28 |
|
|
long long used |
29 |
|
|
long long cache |
30 |
|
|
|
31 |
|
|
ctypedef struct load_stat_t: |
32 |
|
|
double min1 |
33 |
|
|
double min5 |
34 |
|
|
double min15 |
35 |
|
|
|
36 |
|
|
ctypedef struct user_stat_t: |
37 |
|
|
char *name_list |
38 |
|
|
int num_entries |
39 |
|
|
|
40 |
|
|
ctypedef struct swap_stat_t: |
41 |
|
|
long long total |
42 |
|
|
long long used |
43 |
|
|
long long free |
44 |
|
|
|
45 |
|
|
ctypedef struct general_stat_t: |
46 |
|
|
char *os_name |
47 |
|
|
char *os_release |
48 |
|
|
char *os_version |
49 |
|
|
char *platform |
50 |
|
|
char *hostname |
51 |
|
|
time_t uptime |
52 |
|
|
|
53 |
|
|
ctypedef struct disk_stat_t: |
54 |
|
|
char *device_name |
55 |
|
|
char *fs_type |
56 |
|
|
char *mnt_point |
57 |
|
|
long long size |
58 |
|
|
long long used |
59 |
|
|
long long avail |
60 |
|
|
long long total_inodes |
61 |
|
|
long long used_inodes |
62 |
|
|
long long free_inodes |
63 |
|
|
|
64 |
|
|
ctypedef struct diskio_stat_t: |
65 |
|
|
char *disk_name |
66 |
|
|
long long read_bytes |
67 |
|
|
long long write_bytes |
68 |
|
|
time_t systime |
69 |
|
|
|
70 |
|
|
ctypedef struct process_stat_t: |
71 |
|
|
int total |
72 |
|
|
int running |
73 |
|
|
int sleeping |
74 |
|
|
int stopped |
75 |
|
|
int zombie |
76 |
|
|
|
77 |
|
|
ctypedef struct network_stat_t: |
78 |
|
|
char *interface_name |
79 |
|
|
long long tx |
80 |
|
|
long long rx |
81 |
|
|
time_t systime |
82 |
|
|
|
83 |
|
|
ctypedef struct page_stat_t: |
84 |
|
|
long long pages_pagein |
85 |
|
|
long long pages_pageout |
86 |
|
|
time_t systime |
87 |
|
|
|
88 |
|
|
cdef extern cpu_states_t *get_cpu_totals() |
89 |
|
|
cdef extern cpu_states_t *get_cpu_diff() |
90 |
|
|
cdef extern cpu_percent_t *cpu_percent_usage() |
91 |
|
|
cdef extern mem_stat_t *get_memory_stats() |
92 |
|
|
cdef extern load_stat_t *get_load_stats() |
93 |
|
|
cdef extern user_stat_t *get_user_stats() |
94 |
|
|
cdef extern swap_stat_t *get_swap_stats() |
95 |
|
|
cdef extern general_stat_t *get_general_stats() |
96 |
|
|
cdef extern disk_stat_t *get_disk_stats(int *entries) |
97 |
|
|
cdef extern diskio_stat_t *get_diskio_stats(int *entries) |
98 |
|
|
cdef extern diskio_stat_t *get_diskio_stats_diff(int *entries) |
99 |
|
|
cdef extern process_stat_t *get_process_stats() |
100 |
|
|
cdef extern network_stat_t *get_network_stats(int *entries) |
101 |
|
|
cdef extern network_stat_t *get_network_stats_diff(int *entries) |
102 |
|
|
cdef extern page_stat_t *get_page_stats() |
103 |
|
|
cdef extern page_stat_t *get_page_stats_diff() |
104 |
|
|
cdef extern int statgrab_init() |
105 |
|
|
cdef extern int statgrab_drop_privileges() |
106 |
|
|
|
107 |
|
|
|
108 |
tdb |
1.2 |
class Result: |
109 |
|
|
def __init__(self, attrs): |
110 |
|
|
self.attrs = attrs |
111 |
|
|
for attr in attrs: |
112 |
|
|
setattr(self, attr, attrs[attr]) |
113 |
|
|
def __getitem__(self, item): |
114 |
|
|
return getattr(self, item) |
115 |
|
|
def __repr__(self): |
116 |
|
|
return str(self.attrs) |
117 |
|
|
|
118 |
|
|
|
119 |
tdb |
1.1 |
def py_get_cpu_totals(): |
120 |
|
|
cdef cpu_states_t *s |
121 |
|
|
s = get_cpu_totals() |
122 |
tdb |
1.2 |
return Result( |
123 |
|
|
{'user': s.user, |
124 |
|
|
'kernel': s.kernel, |
125 |
|
|
'idle': s.idle, |
126 |
|
|
'iowait': s.iowait, |
127 |
|
|
'swap': s.swap, |
128 |
|
|
'nice': s.nice, |
129 |
|
|
'total': s.total, |
130 |
|
|
'systime': s.systime, |
131 |
|
|
} |
132 |
|
|
) |
133 |
tdb |
1.1 |
|
134 |
|
|
def py_get_cpu_diff(): |
135 |
|
|
cdef cpu_states_t *s |
136 |
|
|
s = get_cpu_diff() |
137 |
tdb |
1.2 |
return Result( |
138 |
|
|
{'user': s.user, |
139 |
|
|
'kernel': s.kernel, |
140 |
|
|
'idle': s.idle, |
141 |
|
|
'iowait': s.iowait, |
142 |
|
|
'swap': s.swap, |
143 |
|
|
'nice': s.nice, |
144 |
|
|
'total': s.total, |
145 |
|
|
'systime': s.systime, |
146 |
|
|
} |
147 |
|
|
) |
148 |
tdb |
1.1 |
|
149 |
|
|
def py_cpu_percent_usage(): |
150 |
|
|
cdef cpu_percent_t *s |
151 |
|
|
s = cpu_percent_usage() |
152 |
tdb |
1.2 |
return Result( |
153 |
|
|
{'user': s.user, |
154 |
|
|
'kernel': s.kernel, |
155 |
|
|
'idle': s.idle, |
156 |
|
|
'iowait': s.iowait, |
157 |
|
|
'swap': s.swap, |
158 |
|
|
'nice': s.nice, |
159 |
|
|
'time_taken': s.time_taken, |
160 |
|
|
} |
161 |
|
|
) |
162 |
tdb |
1.1 |
|
163 |
|
|
def py_get_memory_stats(): |
164 |
|
|
cdef mem_stat_t *s |
165 |
|
|
s = get_memory_stats() |
166 |
tdb |
1.2 |
return Result( |
167 |
|
|
{'total': s.total, |
168 |
|
|
'used': s.used, |
169 |
|
|
'free': s.free, |
170 |
|
|
'cache': s.cache, |
171 |
|
|
} |
172 |
|
|
) |
173 |
tdb |
1.1 |
|
174 |
|
|
def py_get_load_stats(): |
175 |
|
|
cdef load_stat_t *s |
176 |
|
|
s = get_load_stats() |
177 |
tdb |
1.2 |
return Result( |
178 |
|
|
{'min1': s.min1, |
179 |
|
|
'min5': s.min5, |
180 |
|
|
'min15': s.min15, |
181 |
|
|
} |
182 |
|
|
) |
183 |
tdb |
1.1 |
|
184 |
|
|
def py_get_user_stats(): |
185 |
|
|
cdef user_stat_t *s |
186 |
|
|
s = get_user_stats() |
187 |
tdb |
1.2 |
return Result( |
188 |
|
|
{'name_list': s.name_list, |
189 |
|
|
'num_entries': s.num_entries, |
190 |
|
|
} |
191 |
|
|
) |
192 |
tdb |
1.1 |
|
193 |
|
|
def py_get_swap_stats(): |
194 |
|
|
cdef swap_stat_t *s |
195 |
|
|
s = get_swap_stats() |
196 |
tdb |
1.2 |
return Result( |
197 |
|
|
{'total': s.total, |
198 |
|
|
'used': s.used, |
199 |
|
|
'free': s.free, |
200 |
|
|
} |
201 |
|
|
) |
202 |
tdb |
1.1 |
|
203 |
|
|
def py_get_general_stats(): |
204 |
|
|
cdef general_stat_t *s |
205 |
|
|
s = get_general_stats() |
206 |
tdb |
1.2 |
return Result( |
207 |
|
|
{'os_name': s.os_name, |
208 |
|
|
'os_release': s.os_release, |
209 |
|
|
'os_version': s.os_version, |
210 |
|
|
'platform': s.platform, |
211 |
|
|
'hostname': s.hostname, |
212 |
|
|
'uptime': s.uptime, |
213 |
|
|
} |
214 |
|
|
) |
215 |
tdb |
1.1 |
|
216 |
|
|
def py_get_disk_stats(): |
217 |
|
|
cdef disk_stat_t *s |
218 |
|
|
cdef int entries |
219 |
|
|
s = get_disk_stats(&entries) |
220 |
|
|
list = [entries] |
221 |
|
|
for i from 0 <= i < entries: |
222 |
tdb |
1.2 |
list.append(Result( |
223 |
|
|
{'device_name': s.device_name, |
224 |
|
|
'fs_type': s.fs_type, |
225 |
|
|
'mnt_point': s.mnt_point, |
226 |
|
|
'size': s.size, |
227 |
|
|
'used': s.used, |
228 |
|
|
'avail': s.avail, |
229 |
|
|
'total_inodes': s.total_inodes, |
230 |
|
|
'used_inodes': s.used_inodes, |
231 |
|
|
'free_inodes': s.free_inodes, |
232 |
|
|
} |
233 |
|
|
)) |
234 |
tdb |
1.1 |
s = s + 1 |
235 |
|
|
return list |
236 |
|
|
|
237 |
|
|
def py_get_diskio_stats(): |
238 |
|
|
cdef diskio_stat_t *s |
239 |
|
|
cdef int entries |
240 |
|
|
s = get_diskio_stats(&entries) |
241 |
|
|
list = [entries] |
242 |
|
|
for i from 0 <= i < entries: |
243 |
tdb |
1.2 |
list.append(Result( |
244 |
|
|
{'disk_name': s.disk_name, |
245 |
|
|
'read_bytes': s.read_bytes, |
246 |
|
|
'write_bytes': s.write_bytes, |
247 |
|
|
'systime': s.systime, |
248 |
|
|
} |
249 |
|
|
)) |
250 |
tdb |
1.1 |
s = s + 1 |
251 |
|
|
return list |
252 |
|
|
|
253 |
|
|
def py_get_diskio_stats_diff(): |
254 |
|
|
cdef diskio_stat_t *s |
255 |
|
|
cdef int entries |
256 |
|
|
s = get_diskio_stats_diff(&entries) |
257 |
|
|
list = [entries] |
258 |
|
|
for i from 0 <= i < entries: |
259 |
tdb |
1.2 |
list.append(Result( |
260 |
|
|
{'disk_name': s.disk_name, |
261 |
|
|
'read_bytes': s.read_bytes, |
262 |
|
|
'write_bytes': s.write_bytes, |
263 |
|
|
'systime': s.systime, |
264 |
|
|
} |
265 |
|
|
)) |
266 |
tdb |
1.1 |
s = s + 1 |
267 |
|
|
return list |
268 |
|
|
|
269 |
|
|
def py_get_process_stats(): |
270 |
|
|
cdef process_stat_t *s |
271 |
|
|
s = get_process_stats() |
272 |
tdb |
1.2 |
return Result( |
273 |
|
|
{'total': s.total, |
274 |
|
|
'running': s.running, |
275 |
|
|
'sleeping': s.sleeping, |
276 |
|
|
'stopped': s.stopped, |
277 |
|
|
'zombie': s.zombie, |
278 |
|
|
} |
279 |
|
|
) |
280 |
tdb |
1.1 |
|
281 |
|
|
def py_get_network_stats(): |
282 |
|
|
cdef network_stat_t *s |
283 |
|
|
cdef int entries |
284 |
|
|
s = get_network_stats(&entries) |
285 |
|
|
list = [entries] |
286 |
|
|
for i from 0 <= i < entries: |
287 |
tdb |
1.2 |
list.append(Result( |
288 |
|
|
{'interface_name': s.interface_name, |
289 |
|
|
'tx': s.tx, |
290 |
|
|
'rx': s.rx, |
291 |
|
|
'systime': s.systime, |
292 |
|
|
} |
293 |
|
|
)) |
294 |
tdb |
1.1 |
s = s + 1 |
295 |
|
|
return list |
296 |
|
|
|
297 |
|
|
def py_get_network_stats_diff(): |
298 |
|
|
cdef network_stat_t *s |
299 |
|
|
cdef int entries |
300 |
|
|
s = get_network_stats_diff(&entries) |
301 |
|
|
list = [entries] |
302 |
|
|
for i from 0 <= i < entries: |
303 |
tdb |
1.2 |
list.append(Result( |
304 |
|
|
{'interface_name': s.interface_name, |
305 |
|
|
'tx': s.tx, |
306 |
|
|
'rx': s.rx, |
307 |
|
|
'systime': s.systime, |
308 |
|
|
} |
309 |
|
|
)) |
310 |
tdb |
1.1 |
s = s + 1 |
311 |
|
|
return list |
312 |
|
|
|
313 |
|
|
def py_get_page_stats(): |
314 |
|
|
cdef page_stat_t *s |
315 |
|
|
s = get_page_stats() |
316 |
tdb |
1.2 |
return Result( |
317 |
|
|
{'pages_pagein': s.pages_pagein, |
318 |
|
|
'pages_pageout': s.pages_pageout, |
319 |
|
|
} |
320 |
|
|
) |
321 |
tdb |
1.1 |
|
322 |
|
|
def py_get_page_stats_diff(): |
323 |
|
|
cdef page_stat_t *s |
324 |
|
|
s = get_page_stats_diff() |
325 |
tdb |
1.2 |
return Result( |
326 |
|
|
{'pages_pagein': s.pages_pagein, |
327 |
|
|
'pages_pageout': s.pages_pageout, |
328 |
|
|
} |
329 |
|
|
) |
330 |
tdb |
1.1 |
|
331 |
|
|
def py_statgrab_init(): |
332 |
|
|
return statgrab_init() |
333 |
|
|
|
334 |
|
|
def py_statgrab_drop_privileges(): |
335 |
|
|
return statgrab_drop_privileges() |