1 <?php
3 class statistics extends plugin
4 {
5 var $plHeadline = 'Statistics';
6 var $plDescription = 'GOsa usage statistics';
7 var $plShortIcon = 'statistics.png';
8 var $plIcon = 'plugin.png';
10 var $rpcHandle = NULL;
11 var $rpcConfigured = FALSE;
13 var $statisticData = array();
15 var $graphID_1 = 0;
16 var $graphID_2 = 0;
17 var $graphID_3 = 0;
18 var $graphID_4 = 0;
19 var $graphID_5 = 0;
21 var $legendR = 235;
22 var $legendG = 235;
23 var $legendB = 235;
25 var $font = "./themes/default/fonts/LiberationSans-Regular.ttf";
27 var $graph1DatePicker1 = 0;
28 var $graph1DatePicker2 = 0;
30 var $unsbmittedFiles = array();
32 function __construct($config)
33 {
34 plugin::plugin($config, NULL);
36 // Init start and stop times for graph 1
37 $this->graph1DatePicker1 = date('d.m.Y', time() - 14 * 24 * 60 *60);
38 $this->graph1DatePicker2 = date('d.m.Y', time());
40 // First try to retrieve values via RPC
41 $this->rpcConfigured = FALSE;
42 if ($this->config->get_cfg_value("core","gosaRpcServer") != ""){
43 $this->rpcConfigured = TRUE;
44 $this->rpcHandle = $this->config->getRpcHandle(
45 "http://10.3.64.59:4000",
46 "65717fe6-9e3e-11df-b010-5452005f1250",
47 "WyukwauWoid2",
48 TRUE);
49 }
51 // Get list of unsubmitted files.
52 $this->unsbmittedFiles = $this->getUnsubmittedStatistics();
54 // Collect category translations
55 $this->catTranslations = array();
56 foreach($this->config->configRegistry->getListOfPlugins() as $plugin => $data){
57 if(isset($data['plCategory'])){
58 foreach($data['plCategory'] as $id => $name){
59 if(!is_numeric($id)){
60 $this->catTranslations[$id] = $name['description'];
61 }
62 }
63 }
64 }
65 }
67 /*! \brief Returns a list local stored statistic files
68 @param Array A list of filenames and dates.
69 */
70 function getLocalStatisticsFiles()
71 {
72 $res = stats::getLocalStatFiles();
73 $tmp = array();
74 if(count($res)){
75 foreach($res as $file){
76 $date = strtotime($file);
77 if($date){
78 $tmp[$file] = $date;
79 }
80 }
81 }
82 return($tmp);
83 }
86 /*! \brief Returns a list of not transmitted stat files (except files for the current day)
87 * @return Array A list of unsubmitted statistic files.
88 */
89 function getUnsubmittedStatistics()
90 {
91 $available = $this->getLocalStatisticsFiles();
92 $alreadyTransmitted = $this->getStatisticsDatesFromServer();
94 $unsubmitted = array();
95 foreach($available as $key => $day){
96 if(!isset($alreadyTransmitted[$key])) $unsubmitted [$key] = $day;
97 }
99 // Exclude statistic collection from today, they are still active and cannot be submitted.
100 $curDate = date('Y-m-d');
101 if(isset($unsubmitted)) unset($unsubmitted[$curDate]);
103 return($unsubmitted);
104 }
107 /*! \brief Request a list of dates for which the server can return statistics.
108 @param Array A list of dates $ret=[iso-str] = timestamp
109 */
110 function getStatisticsDatesFromServer()
111 {
112 $res = $this->rpcHandle->getInstanceStatDates();
113 $dates = array();
114 if(!$this->rpcHandle->success()){
115 msg_dialog::display(_("Error"),msgPool::rpcError($this->rpcHandle->get_error()),ERROR_DIALOG);
116 }else{
117 foreach($res as $date){
118 $dates[$date] = strtotime($date);
119 }
120 }
121 return($dates);
122 }
125 function execute()
126 {
127 if(isset($_POST['graph1DatePicker1'])) $this->graph1DatePicker1 = get_post('graph1DatePicker1');
128 if(isset($_POST['graph1DatePicker2'])) $this->graph1DatePicker2 = get_post('graph1DatePicker2');
130 $smarty = get_smarty();
131 $smarty->assign('graph1DatePicker1', $this->graph1DatePicker1);
132 $smarty->assign('graph1DatePicker2', $this->graph1DatePicker2);
134 // Do not render anything if we are not prepared to send and receive data via rpc.
135 $smarty->assign("rpcConfigured", $this->rpcConfigured);
136 $smarty->assign("validRpcHandle", TRUE);
137 if(!$this->rpcConfigured || !$this->rpcHandle){
138 $smarty->assign("validRpcHandle", FALSE);
139 return($smarty->fetch(get_template_path('statistics.tpl', TRUE)));
140 }
142 // Send stats
143 if(isset($_POST['transmitStatistics'])){
144 $this->unsbmittedFiles = $this->getUnsubmittedStatistics();
145 foreach($this->unsbmittedFiles as $filename => $date){
146 $tmp = stats::dumpTables($filename);
147 $dump = array();
148 foreach($tmp as $entry){
149 $dump[] = array_values($entry);
150 }
151 $res = $this->rpcHandle->updateInstanceStatus($dump);
152 if(!$this->rpcHandle->success()){
153 msg_dialog::display(_("Error"),msgPool::rpcError($this->rpcHandle->get_error()),ERROR_DIALOG);
154 }else{
155 stats::removeStatsFile($filename);
156 echo "Inserted ".$res." entries for date ".date('d.m.Y', $date)."<br>";
157 }
158 }
159 $this->unsbmittedFiles = $this->getUnsubmittedStatistics();
160 }
162 // Transmit daily statistics to GOsa-Server
163 if(isset($_POST['receiveStatistics']) && $this->rpcConfigured){
164 $start = strtotime($this->graph1DatePicker1);
165 $stop = strtotime($this->graph1DatePicker2);
166 $res = $this->rpcHandle->getInstanceStats($start,$stop);
167 if(!$this->rpcHandle->success()){
168 msg_dialog::display(_("Error"),msgPool::rpcError($this->rpcHandle->get_error()),ERROR_DIALOG);
169 }elseif($res){
170 $this->statisticData = $this->prepareGraphData($res);
171 $this->reloadGraphs();
172 }
173 }
175 $smarty->assign('graphID_1', $this->graphID_1);
176 $smarty->assign('graphID_2', $this->graphID_2);
177 $smarty->assign('graphID_3', $this->graphID_3);
178 $smarty->assign('graphID_4', $this->graphID_4);
179 $smarty->assign('graphID_5', $this->graphID_5);
180 $smarty->assign('unsbmittedFiles', count($this->unsbmittedFiles));
181 $smarty->assign('unsbmittedFilesMsg',
182 sprintf(
183 _("You have currently %s unsubmitted statistic collection, do you want to transmit them now?"),
184 count($this->unsbmittedFiles)));
185 return($smarty->fetch(get_template_path('statistics.tpl', TRUE)));
186 }
189 function prepareGraphData($res)
190 {
191 $gData = array();
193 /* Build up array which represents the amount of errors per
194 * interval.
195 */
196 foreach($res['errorsPerInterval'] as $dateStr => $data){
197 $date = strtotime($dateStr);
198 $gData['errorsPerInterval'][$date] = $data;
199 }
200 ksort($gData['errorsPerInterval']);
203 /* Build up timeline
204 */
205 $Xam = 5;
206 $cnt = 0;
207 $numCnt = $res['errorsPerInterval'];
208 foreach($gData['errorsPerInterval'] as $date => $data){
209 if((count($numCnt) <= $Xam) ||
210 ($cnt % (floor(count($numCnt) / $Xam )) == 0)){
211 $gData['dates'][$date] = date('d.m.Y', $date);
212 }else{
213 $gData['dates'][$date] = ' ';
214 }
215 $cnt ++;
216 }
217 ksort($gData['dates']);
220 /* Build up 'actions per category' array, this will later
221 * be represented using a pie chart.
222 */
223 $gData['actionsPerCategory'] = $res['actionsPerCategory'];
224 arsort($gData['actionsPerCategory']);
227 /* Build up system-info array per interval.
228 */
229 foreach($res['usagePerInterval'] as $dateStr => $data){
230 $date = strtotime($dateStr);
231 foreach($data as $type => $count){
232 $gData['usagePerInterval'][$type][$date] = $count;
233 }
234 }
235 foreach($gData['usagePerInterval'] as $key => $data)
236 ksort($gData['usagePerInterval'][$key]);
239 /* Prepare actions-per-interval array.
240 */
241 foreach($res['actionsPerInterval'] as $category => $data){
242 if(empty($category)) continue;
243 foreach($data as $dateStr => $count){
244 $date = strtotime($dateStr);
245 $gData['actionsPerInterval'][$category][$date]=$count;
246 }
247 ksort($gData['actionsPerInterval'][$category]);
248 }
249 return($gData);
250 }
253 function check()
254 {
255 $messages = plugin::check();
256 return($messages);
257 }
260 function save_object()
261 {
262 plugin::save_object();
263 }
266 /*! \brief This method tries to translate category names.
267 * @param The category name to translate
268 * @return String The translated category names.
269 */
270 function getCategoryTranslation($name)
271 {
272 $ret ="";
274 // Extract category names from the given string.
275 $list = preg_split("/, /", $name);
277 // We do not have a category for systems directly, so we've to map all system types to 'System'.
278 // If we do not map to _(Systems) the graph legend will be half screen width.
279 if(count(array_intersect(array('server','terminal','workstation', 'opsi', 'component'), $list))){
280 return(_("Systems"));
281 }
283 // Walk through category names and try to find a translation.
284 foreach($list as $cat){
285 $cat = trim($cat);
286 if(isset($this->catTranslations[$cat])){
287 $cat = _($this->catTranslations[$cat]);
288 }elseif(!empty($cat)){
289 $cat = _($cat);
290 }
291 $ret .= $cat.", ";
292 }
293 return(rtrim($ret, ', '));
294 }
297 function reloadGraphs()
298 {
299 new pChartInclude();
300 $gData = $this->statisticData;
301 if(count($gData['actionsPerCategory'])){
302 $this->generateCategoryPieGraph($gData['actionsPerCategory']);
303 }
304 $this->generateActionsGraph($gData);
305 }
308 function generateActionsGraph($gData)
309 {
310 $lineMax = 100;
311 $errorMax = (max($gData['errorsPerInterval']) < 100)? 100:max($gData['errorsPerInterval']);
312 $dataSet = new pData;
313 foreach($gData['actionsPerInterval'] as $category => $entriesPerDate){
314 if(empty($category)) continue;
316 // Add results to our data set.
317 $dataSet->AddPoint($entriesPerDate, $category);
318 $dataSet->SetSerieName($this->getCategoryTranslation($category), $category);
319 $dataSet->AddSerie($category);
321 // Detect maximum value, to adjust the Y-Axis
322 $tmpMax = max($entriesPerDate);
323 if($tmpMax > $lineMax) $lineMax = $tmpMax;
324 }
326 // Add timeline
327 $dataSet->AddPoint($gData['dates'], 'date');
328 $dataSet->SetAbsciseLabelSerie('date');
330 $chart = new pChart(800,230);
331 $chart->setFixedScale(0.000,$lineMax);
332 $chart->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",10);
333 $chart->setGraphArea(50,30,585,200);
334 $chart->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);
335 $chart->drawRoundedRectangle(5,5,695,225,5,230,230,230);
336 $chart->drawGraphArea(255,255,255,TRUE);
337 $chart->drawGrid(4,TRUE,200,200,200,50);
338 $chart->drawTreshold(0,143,55,72,TRUE,TRUE);
339 $chart->drawTitle(50,22,"Plugin usage over time",50,50,50,585);
340 $chart->drawScale($dataSet->GetData(),$dataSet->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2, TRUE);
341 $chart->drawFilledLineGraph($dataSet->GetData(),$dataSet->GetDataDescription(),50,TRUE);
342 $chart->setColorPalette(count($gData['actionsPerInterval']),255,0,0);
344 // Draw legend
345 $dataSet->AddPoint($gData['errorsPerInterval'], 'Errors');
346 $dataSet->SetSerieName(_('Error'), 'Errors');
347 $dataSet->AddSerie('Errors');
348 $chart->drawLegend(650,30,$dataSet->GetDataDescription(),255,255,255);
350 // Remove all graph series and add the error-series, then draw the new graph.
351 foreach($gData['actionsPerInterval'] as $category => $data){
352 $dataSet->RemoveSerie($category);
353 }
354 $chart->setFixedScale(0,$errorMax);
355 $chart->drawRightScale($dataSet->GetData(),$dataSet->GetDataDescription(),SCALE_NORMAL,120,150,150,TRUE,0,2, TRUE);
356 $chart->drawBarGraph($dataSet->GetData(),$dataSet->GetDataDescription());
358 // Generate new and unique graph id
359 $this->graphID_2 = preg_replace("/[^0-9]/","",microtime(TRUE));
360 $file = '/tmp/graph_'.$this->graphID_2;
361 $chart->Render($file);
362 session::set('statistics::graphFile'.$this->graphID_2,$file);
364 return;
365 }
367 # // Prepare Data
368 # $graphData = array();
369 # foreach($gData['usagePerInterval'] as $dateStr => $data){
370 # $date = strtotime($dateStr);
371 # foreach($data as $name => $val){
372 # $graphData[$name][$date] = $val;
373 # }
374 # }
375 #
376 # // Sort Data
377 # foreach($graphData as $key => $data)
378 # ksort($graphData[$key]);
379 #
380 # // Generate new and unique graph id
381 # $this->graphID_3 = preg_replace("/[^0-9]/","",microtime(TRUE));
382 #
383 # // Prepare transmitted data, sort it by date and collect
384 # // transmitted timestamps to be able to print the x-Axis labels.
385 # $dataSet = new pData;
386 #
387 # $max = max($graphData['max_mem']);
388 #
389 # $dataSet->AddPoint(array_values($graphData['max_mem']), 'max_mem');
390 # $dataSet->AddPoint(array_values($graphData['avg_mem']), 'avg_mem');
391 # $dataSet->AddPoint(array_values($graphData['min_mem']), 'min_mem');
392 #
393 # $dataSet->SetSerieName('Min Memory', 'min_mem');
394 # $dataSet->SetSerieName('Max Memory', 'max_mem');
395 # $dataSet->SetSerieName('Average Memory', 'avg_mem');
396 #
397 # $dataSet->AddAllSeries();
398 # $dataSet->AddPoint($dateSeries, 'date');
399 # $dataSet->SetAbsciseLabelSerie('date');
400 #
401 # $chart = new pChart(800,230);
402 # $chart->setFixedScale(0.0001,($max*1.1));
403 # $chart->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",10);
404 # $chart->setGraphArea(50,30,585,200);
405 # $chart->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);
406 # $chart->drawRoundedRectangle(5,5,695,225,5,230,230,230);
407 # $chart->drawGraphArea(255,255,255,TRUE);
408 # $chart->drawGrid(4,TRUE,200,200,200,50);
409 # $chart->drawTreshold(0,143,55,72,TRUE,TRUE);
410 # $chart->drawTitle(50,22,"Memory usage",50,50,50,585);
411 #
412 # $chart->drawScale($dataSet->GetData(),$dataSet->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2, FALSE);
413 # $chart->drawFilledCubicCurve($dataSet->GetData(),$dataSet->GetDataDescription(),.1,50);
414 #
415 # $file = '/tmp/graph_'.$this->graphID_3;
416 # $chart->Render($file);
417 # session::set('statistics::graphFile'.$this->graphID_3,$file);
418 #
419 #
420 #
421 #
422 #
423 #
424 #
425 # // Generate new and unique graph id
426 # $this->graphID_4 = preg_replace("/[^0-9]/","",microtime(TRUE));
427 #
428 # // Prepare transmitted data, sort it by date and collect
429 # // transmitted timestamps to be able to print the x-Axis labels.
430 # $dataSet = new pData;
431 #
432 # $max = max($graphData['max_dur']);
433 #
434 # $dataSet->AddPoint(array_values($graphData['max_dur']), 'max_dur');
435 # $dataSet->AddPoint(array_values($graphData['avg_dur']), 'avg_dur');
436 # $dataSet->AddPoint(array_values($graphData['min_dur']), 'min_dur');
437 #
438 # $dataSet->SetSerieName('Min dur', 'min_dur');
439 # $dataSet->SetSerieName('Max dur', 'max_dur');
440 # $dataSet->SetSerieName('Average dur', 'avg_dur');
441 #
442 # $dataSet->AddAllSeries();
443 # $dataSet->AddPoint($dateSeries, 'date');
444 # $dataSet->SetAbsciseLabelSerie('date');
445 #
446 # $chart = new pChart(800,230);
447 # $chart->setFixedScale(0.0001,($max*1.1));
448 # $chart->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",10);
449 # $chart->setGraphArea(50,30,585,200);
450 # $chart->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);
451 # $chart->drawRoundedRectangle(5,5,695,225,5,230,230,230);
452 # $chart->drawGraphArea(255,255,255,TRUE);
453 # $chart->drawGrid(4,TRUE,200,200,200,50);
454 # $chart->drawTreshold(0,143,55,72,TRUE,TRUE);
455 # $chart->drawTitle(50,22,"Render time",50,50,50,585);
456 #
457 # $chart->drawScale($dataSet->GetData(),$dataSet->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2, FALSE);
458 # $chart->drawFilledCubicCurve($dataSet->GetData(),$dataSet->GetDataDescription(),.1,50);
459 #
460 # $file= '/tmp/graph_'.$this->graphID_4;
461 # $chart->Render($file);
462 # session::set('statistics::graphFile'.$this->graphID_4,$file);
463 #
464 #
465 #
466 #
467 #
468 #
469 #
470 #
471 # // Generate new and unique graph id
472 # $this->graphID_5 = preg_replace("/[^0-9]/","",microtime(TRUE));
473 #
474 # // Prepare transmitted data, sort it by date and collect
475 # // transmitted timestamps to be able to print the x-Axis labels.
476 # $dataSet = new pData;
477 #
478 # $max = max($graphData['max_load']);
479 #
480 # $dataSet->AddPoint(array_values($graphData['max_load']), 'max_load');
481 # $dataSet->AddPoint(array_values($graphData['avg_load']), 'avg_load');
482 # $dataSet->AddPoint(array_values($graphData['min_load']), 'min_load');
483 #
484 # $dataSet->SetSerieName('Min Load', 'min_load');
485 # $dataSet->SetSerieName('Max Load', 'max_load');
486 # $dataSet->SetSerieName('Average Load', 'avg_load');
487 #
488 # $dataSet->AddAllSeries();
489 # $dataSet->AddPoint($dateSeries, 'date');
490 # $dataSet->SetAbsciseLabelSerie('date');
491 #
492 # $chart = new pChart(800,230);
493 # $chart->setFixedScale(0.0001,($max*1.1));
494 # $chart->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",10);
495 # $chart->setGraphArea(50,30,585,200);
496 # $chart->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);
497 # $chart->drawRoundedRectangle(5,5,695,225,5,230,230,230);
498 # $chart->drawGraphArea(255,255,255,TRUE);
499 # $chart->drawGrid(4,TRUE,200,200,200,50);
500 # $chart->drawTreshold(0,143,55,72,TRUE,TRUE);
501 # $chart->drawTitle(50,22,"CPU load",50,50,50,585);
502 #
503 # $chart->drawScale($dataSet->GetData(),$dataSet->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2, FALSE);
504 # $chart->drawFilledCubicCurve($dataSet->GetData(),$dataSet->GetDataDescription(),.1,50);
505 #
506 # $file = '/tmp/graph_'.$this->graphID_5;
507 # $chart->Render($file);
508 # session::set('statistics::graphFile'.$this->graphID_5,$file);
509 #
512 function generateCategoryPieGraph($data)
513 {
514 // Sort data by usage count and slice array to get
515 // the eight most used categories
516 arsort($data);
517 $mostUsedCategories = array_slice($data,0,7);
519 // Get the rest of categories and combine them 'others'
520 $theRest = array_slice($data,7);
521 $mostUsedCategories['remaining'] = array_sum($theRest);
523 // Try to find a translation for the given category names
524 $values = array_values($mostUsedCategories);
525 $keys = array_keys($mostUsedCategories);
526 foreach($keys as $id => $cat){
527 $keys[$id] = $this->getCategoryTranslation($cat);
528 }
530 // Dataset definition
531 $dataSet = new pData;
532 $dataSet->AddPoint($values,"Serie1");
533 $dataSet->AddAllSeries();
534 $dataSet->AddPoint($keys,"Serie2");
535 $dataSet->SetAbsciseLabelSerie("Serie2");
537 // Set graph area
538 $x = 400;
539 $y = 200;
541 // Initialise the graph
542 $chart = new pChart($x,$y);
543 $chart->setFontProperties($this->font,10);
544 $chart->drawPieGraph($dataSet->GetData(),$dataSet->GetDataDescription(),($x/3),($y/2)-10,($y/2),PIE_PERCENTAGE,TRUE,50,20,3);
545 $chart->drawPieLegend(($x/3*2),15,$dataSet->GetData(),$dataSet->GetDataDescription(),
546 $this->legendR,$this->legendG,$this->legendB);
548 // Store graph data
549 $this->graphID_1 = preg_replace("/[^0-9]/","",microtime(TRUE));
550 $file = '/tmp/graph_'.$this->graphID_1;
551 $chart->Render($file);
552 session::set('statistics::graphFile'.$this->graphID_1,$file);
553 }
554 }
555 ?>