1 <?php
3 class stats
4 {
6 static protected $last_cpu_load = "";
7 static protected $last_cpu_load_stamp = 0;
10 static function checkDatabase()
11 {
12 $TABLE_NAME = 'stats';
14 // Check for modules
15 // php5-sqlite
18 // Try to create database, if it exists just open it.
19 $res = sqlite_open('/var/spool/gosa/stats', 0666, $error);
20 if(!$res){
21 return($res);
22 }
24 // Delete Table
25 $query = "DROP TABLE '{$TABLE_NAME}'";
26 # $ret = sqlite_query($query, $res);
28 // List Tables an check if there is already everything we need.
29 $query = "SELECT name FROM sqlite_master WHERE type='table' and name='{$TABLE_NAME}'";
30 $ret = sqlite_query($query, $res);
31 if(!count(sqlite_fetch_all($ret))){
33 // Check for table existance
34 $query = "
35 CREATE TABLE {$TABLE_NAME} (
36 ID INTEGER PRIMARY KEY,
37 ACTID INTEGER,
38 TYPE TEXT,
39 PLUGIN TEXT,
40 CATEGORY TEXT,
41 ACTION TEXT,
42 UUID TEXT,
43 TIMESTAMP INTEGER,
44 MTIMESTAMP REAL,
45 DURATION REAL,
46 RENDER_TIME REAL,
47 AMOUNT INTEGER,
48 MEMORY_USAGE INTEGER,
49 CPU_LOAD FLOAT,
50 INFO BLOB
51 )";
52 $ret = sqlite_query($query, $res);
53 }
55 return($res);
56 }
60 static function log($type, $plugin, $category, $action, $amount = 1, $duration = 0, $info ='')
61 {
62 global $config;
63 global $clicks;
64 global $overallRenderTimer;
67 if(!isset($clicks) || empty($clicks)) $clicks = 0;
68 if(!isset($overallRenderTimer) || empty($overallRenderTimer)){
69 $renderTime = 0;
70 }else{
71 $renderTime = microtime(TRUE) - $overallRenderTimer;
73 // Now set the overallRenderTimer to the current timestamp - else
74 // we will not be able to sum up the render time in a single SQL statement.
75 $overallRenderTimer = microtime(TRUE);
77 }
79 if(is_object($config) && $config instanceOf config){
80 $uuid = $config->getGOsaUUID();
81 }else{
82 $uuid = "";
83 }
85 $type = sqlite_escape_string($type);
86 $plugin = sqlite_escape_string($plugin);
87 $action = sqlite_escape_string($action);
88 $timestamp = time();
89 $mtimestamp = microtime(TRUE);
90 $amount = sqlite_escape_string($amount);
91 $duration = sqlite_escape_string($duration);
92 $renderTime = sqlite_escape_string($renderTime);
93 $info = sqlite_escape_string($info);
94 $clicks = sqlite_escape_string($clicks);
95 $memory_usage = sqlite_escape_string(stats::get_memory_usage());
96 $cpu_load = sqlite_escape_string(stats::get_cpu_load());
98 $tmp = array();
99 foreach($category as $cat){
100 $tmp[] = trim($cat, '\/,; ');
101 }
102 $category = sqlite_escape_string(implode($tmp, ', '));
104 $res = stats::checkDatabase();
105 $TABLE_NAME = 'stats';
106 $query = "
107 INSERT INTO {$TABLE_NAME}
108 (ACTID, TYPE, PLUGIN, CATEGORY, ACTION, UUID, MTIMESTAMP, TIMESTAMP,
109 AMOUNT, DURATION, RENDER_TIME, MEMORY_USAGE, CPU_LOAD, INFO)
110 VALUES
111 ('{$clicks}','{$type}','{$plugin}','{$category}','{$action}','{$uuid}',
112 '{$mtimestamp}','{$timestamp}','{$amount}','{$duration}','{$renderTime}',
113 '{$memory_usage}','{$cpu_load}','{$info}')";
114 sqlite_query($query, $res);
115 }
117 static function get_memory_usage()
118 {
119 return(memory_get_usage());
120 }
122 static function get_cpu_load()
123 {
124 $cur = time();
125 if(empty(stats::$last_cpu_load) || (($cur - stats::$last_cpu_load_stamp) >=2 )){
126 list($one, $five, $ten) =preg_split("/ /",shell_exec('cat /proc/loadavg'));
127 stats::$last_cpu_load = $one;
128 stats::$last_cpu_load_stamp = $cur;
129 }
130 return(stats::$last_cpu_load);
131 }
133 static function show()
134 {
135 $res = stats::checkDatabase();
136 $TABLE_NAME = 'stats';
137 $query = "SELECT * FROM {$TABLE_NAME} ORDER BY MTIMESTAMP";
138 $query = "SELECT PLUGIN, ACTION, MAX(DURATION) as 'DURATION' FROM {$TABLE_NAME} WHERE ACTION='modify' GROUP BY PLUGIN,ACTION ";
139 $query = "SELECT * FROM {$TABLE_NAME} ORDER BY ID DESC LIMIT 30";
140 $query = "SELECT * FROM {$TABLE_NAME} WHERE plugin != 'LDAP' ORDER BY ID DESC LIMIT 30";
141 $ret = sqlite_query($query, $res);
142 echo "<pre>";
144 $colSize = 16;
146 $title = FALSE;
147 foreach(sqlite_fetch_all($ret) as $entry){
148 if(!$title){
149 foreach($entry as $key => $str) {
150 if(is_numeric($key)) continue;
151 echo str_pad($key,$colSize,' ')."|";
152 }
153 echo "\n";
154 foreach($entry as $key => $str) {
155 if(is_numeric($key)) continue;
156 echo str_pad('',$colSize,'-')."-";
157 }
158 echo "\n";
159 $title = TRUE;
160 }
162 foreach($entry as $key => $str){
163 if(is_numeric($key)) continue;
164 if($key == "DURATION" || $key == "MTIMESTAMP" || $key == "CPU_LOAD"){
165 $str = sprintf("%0.4f", preg_replace("/,/",".",$str));
166 echo str_pad($str,$colSize,' ', STR_PAD_LEFT)."|";
167 }else{
168 echo str_pad($str,$colSize,' ')."|";
169 }
170 }
171 echo "\n";
172 }
173 echo sqlite_error_string($ret);
175 echo "\n------ \n";
176 echo "Time spent per plugin-category \n";
177 echo "------ \n";
179 $query = "
180 SELECT SUM(DURATION) AS DUR, CATEGORY
181 FROM {$TABLE_NAME}
182 WHERE plugin != 'LDAP'
183 GROUP BY CATEGORY
184 ORDER BY DUR DESC LIMIT 10";
185 $ret = sqlite_query($query, $res);
187 $colSize = 16;
188 $title = FALSE;
189 foreach(sqlite_fetch_all($ret) as $entry){
190 foreach($entry as $key => $str){
191 if(is_numeric($key)) continue;
192 echo str_pad($str,$colSize,' ')."|";
193 }
194 echo "\n";
195 }
198 echo sqlite_error_string($ret);
200 echo "\n------ \n";
201 echo "Time spent per plugin \n";
202 echo "------ \n";
204 $query = "
205 SELECT SUM(DURATION) AS DUR, PLUGIN, UUID
206 FROM {$TABLE_NAME}
207 WHERE plugin != 'LDAP'
208 GROUP BY PLUGIN, UUID
209 ORDER BY DUR DESC LIMIT 10";
210 $ret = sqlite_query($query, $res);
212 $colSize = 16;
213 $title = FALSE;
214 foreach(sqlite_fetch_all($ret) as $entry){
215 foreach($entry as $key => $str){
216 if(is_numeric($key)) continue;
217 echo str_pad($str,$colSize,' ')."|";
218 }
219 echo "\n";
220 }
221 echo sqlite_error_string($ret);
223 # * Anzahl von Objekttypen
224 # * Anzahl der Löschungen pro Objekttyp
225 # * Anzahl der Erzeugungen pro Objekttyp
226 # * Anzahl der Bearbeitungen pro Objekttyp
227 # * Anzahl der Verschiebungen pro Objekttyp
228 # * Anzahl der Mehrfachbearbeitungen pro Objekttyp
229 # * Antwortzeiten pro aktion
230 # * Anzahl der Passwortänderungen
231 # * Anzahl der unterschiedlichen Anmeldungen
234 echo "\n------ \n";
235 echo "Actions done per plugin \n";
236 echo "------ \n";
238 $query = "
239 SELECT COUNT(ACTION) as CNT, ACTION, PLUGIN
240 FROM {$TABLE_NAME}
241 WHERE TYPE = 'plugin'
242 AND PLUGIN != 'LDAP'
243 GROUP BY ACTION,PLUGIN
244 ORDER BY CNT DESC LIMIT 30";
245 $ret = sqlite_query($query, $res);
247 $colSize = 16;
248 $title = FALSE;
249 foreach(sqlite_fetch_all($ret) as $entry){
250 foreach($entry as $key => $str){
251 if(is_numeric($key)) continue;
252 echo str_pad($str,$colSize,' ')."|";
253 }
254 echo "\n";
255 }
256 echo sqlite_error_string($ret);
258 echo "\n------ \n";
259 echo "'create' actions done per plugin (5 most)\n";
260 echo "------ \n";
262 $query = "
263 SELECT COUNT(ACTION) as CNT, ACTION, PLUGIN
264 FROM {$TABLE_NAME}
265 WHERE TYPE = 'plugin'
266 AND PLUGIN != 'LDAP'
267 AND ACTION = 'create'
268 GROUP BY ACTION,PLUGIN
269 ORDER BY CNT DESC LIMIT 5";
270 $ret = sqlite_query($query, $res);
272 $colSize = 16;
273 $title = FALSE;
274 foreach(sqlite_fetch_all($ret) as $entry){
275 foreach($entry as $key => $str){
276 if(is_numeric($key)) continue;
277 echo str_pad($str,$colSize,' ')."|";
278 }
279 echo "\n";
280 }
281 echo sqlite_error_string($ret);
283 echo "\n------ \n";
284 echo "'move' actions done per plugin (5 most)\n";
285 echo "------ \n";
287 $query = "
288 SELECT COUNT(ACTION) as CNT, ACTION, PLUGIN
289 FROM {$TABLE_NAME}
290 WHERE TYPE = 'plugin'
291 AND PLUGIN != 'LDAP'
292 AND ACTION = 'move'
293 GROUP BY ACTION,PLUGIN
294 ORDER BY CNT DESC LIMIT 5";
295 $ret = sqlite_query($query, $res);
297 $colSize = 16;
298 $title = FALSE;
299 foreach(sqlite_fetch_all($ret) as $entry){
300 foreach($entry as $key => $str){
301 if(is_numeric($key)) continue;
302 echo str_pad($str,$colSize,' ')."|";
303 }
304 echo "\n";
305 }
306 echo sqlite_error_string($ret);
308 echo "\n------ \n";
309 echo "'view' actions done per plugin (5 most)\n";
310 echo "------ \n";
312 $query = "
313 SELECT COUNT(ACTION) as CNT, ACTION, PLUGIN
314 FROM {$TABLE_NAME}
315 WHERE TYPE = 'plugin'
316 AND PLUGIN != 'LDAP'
317 AND ACTION = 'view'
318 GROUP BY ACTION,PLUGIN
319 ORDER BY CNT DESC LIMIT 5";
320 $ret = sqlite_query($query, $res);
322 $colSize = 16;
323 $title = FALSE;
324 foreach(sqlite_fetch_all($ret) as $entry){
325 foreach($entry as $key => $str){
326 if(is_numeric($key)) continue;
327 echo str_pad($str,$colSize,' ')."|";
328 }
329 echo "\n";
330 }
331 echo sqlite_error_string($ret);
333 echo "\n------ \n";
334 echo "'open' actions done per plugin (5 most)\n";
335 echo "------ \n";
337 $query = "
338 SELECT COUNT(ACTION) as CNT, ACTION, PLUGIN, UUID
339 FROM {$TABLE_NAME}
340 WHERE TYPE = 'plugin'
341 AND PLUGIN != 'LDAP'
342 AND ACTION = 'open'
343 GROUP BY ACTION,PLUGIN, UUID
344 ORDER BY CNT DESC LIMIT 5";
345 $ret = sqlite_query($query, $res);
347 $colSize = 16;
348 $title = FALSE;
349 foreach(sqlite_fetch_all($ret) as $entry){
350 foreach($entry as $key => $str){
351 if(is_numeric($key)) continue;
352 echo str_pad($str,$colSize,' ')."|";
353 }
354 echo "\n";
355 }
356 echo sqlite_error_string($ret);
358 echo "\n------ \n";
359 echo "Time per session\n";
360 echo "------ \n";
362 $query = "
363 SELECT SUM(DURATION) as DUR, UUID
364 FROM {$TABLE_NAME}
365 GROUP BY UUID
366 ORDER BY DUR DESC LIMIT 10";
367 $ret = sqlite_query($query, $res);
369 $colSize = 16;
370 $title = FALSE;
371 foreach(sqlite_fetch_all($ret) as $entry){
372 foreach($entry as $key => $str){
373 if(is_numeric($key)) continue;
374 echo str_pad($str,$colSize,' ')."|";
375 }
376 echo "\n";
377 }
378 echo sqlite_error_string($ret);
380 echo "\n------ \n";
381 echo "Most used password hashes\n";
382 echo "------ \n";
384 $query = "
385 SELECT DISTINCT(INFO), COUNT(INFO) as CNT
386 FROM {$TABLE_NAME}
387 WHERE ACTION = 'change_password'
388 GROUP BY INFO
389 ORDER BY INFO DESC LIMIT 10";
390 $ret = sqlite_query($query, $res);
392 $colSize = 16;
393 $title = FALSE;
394 foreach(sqlite_fetch_all($ret) as $entry){
395 foreach($entry as $key => $str){
396 if(is_numeric($key)) continue;
397 echo str_pad($str,$colSize,' ')."|";
398 }
399 echo "\n";
400 }
401 echo sqlite_error_string($ret);
403 echo "\n------ \n";
404 echo "Actions at all\n";
405 echo "------ \n";
407 $query = "
408 SELECT DISTINCT(ACTION), COUNT(ACTION) AS CNT
409 FROM {$TABLE_NAME}
410 GROUP BY ACTION
411 ORDER BY CNT DESC LIMIT 10";
412 $ret = sqlite_query($query, $res);
414 $colSize = 16;
415 $title = FALSE;
416 foreach(sqlite_fetch_all($ret) as $entry){
417 foreach($entry as $key => $str){
418 if(is_numeric($key)) continue;
419 echo str_pad($str,$colSize,' ')."|";
420 }
421 echo "\n";
422 }
423 echo sqlite_error_string($ret);
425 echo "\n------ \n";
426 echo "Time spent per action\n";
427 echo "------ \n";
429 $query = "
430 SELECT DISTINCT(ACTION), SUM(DURATION) AS DUR
431 FROM {$TABLE_NAME}
432 GROUP BY ACTION
433 ORDER BY DUR DESC LIMIT 10";
434 $ret = sqlite_query($query, $res);
436 $colSize = 16;
437 $title = FALSE;
438 foreach(sqlite_fetch_all($ret) as $entry){
439 foreach($entry as $key => $str){
440 if(is_numeric($key)) continue;
441 echo str_pad($str,$colSize,' ')."|";
442 }
443 echo "\n";
444 }
445 echo sqlite_error_string($ret);
447 echo "\n------ \n";
448 echo "Average time per action\n";
449 echo "------ \n";
451 $query = "
452 SELECT DISTINCT(ACTION), AVG(DURATION) AS DUR
453 FROM {$TABLE_NAME}
454 GROUP BY ACTION
455 ORDER BY DUR DESC LIMIT 10";
456 $ret = sqlite_query($query, $res);
458 $colSize = 16;
459 $title = FALSE;
460 foreach(sqlite_fetch_all($ret) as $entry){
461 foreach($entry as $key => $str){
462 if(is_numeric($key)) continue;
463 echo str_pad($str,$colSize,' ')."|";
464 }
465 echo "\n";
466 }
467 echo sqlite_error_string($ret);
469 echo "</pre>";
470 }
471 }
474 ?>