ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/extras/python/statgrab.pyx
Revision: 1.1
Committed: Fri Feb 6 14:10:08 2004 UTC (20 years, 10 months ago) by tdb
Branch: MAIN
Log Message:
Add libstatgrab python wrapper. This won't be installed with libstatgrab,
or in fact have anything done to it - it's just there if people want it.
If it matures, I'll consider making it a package in it's own right.

File Contents

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