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 $smarty = get_smarty();
128 $smarty->assign('graph1DatePicker1', $this->graph1DatePicker1);
129 $smarty->assign('graph1DatePicker2', $this->graph1DatePicker2);
131 // Do not render anything if we are not prepared to send and receive data via rpc.
132 $smarty->assign("rpcConfigured", $this->rpcConfigured);
133 $smarty->assign("validRpcHandle", TRUE);
134 if(!$this->rpcConfigured || !$this->rpcHandle){
135 $smarty->assign("validRpcHandle", FALSE);
136 return($smarty->fetch(get_template_path('statistics.tpl', TRUE)));
137 }
139 // Send stats
140 if(isset($_POST['transmitStatistics'])){
141 $this->unsbmittedFiles = $this->getUnsubmittedStatistics();
142 foreach($this->unsbmittedFiles as $filename => $date){
143 $tmp = stats::dumpTables($filename);
144 $dump = array();
145 foreach($tmp as $entry){
146 $dump[] = array_values($entry);
147 }
148 $res = $this->rpcHandle->updateInstanceStatus($dump);
149 if(!$this->rpcHandle->success()){
150 msg_dialog::display(_("Error"),msgPool::rpcError($this->rpcHandle->get_error()),ERROR_DIALOG);
151 }else{
152 stats::removeStatsFile($filename);
153 echo "Inserted ".$res." entries for date ".date('d.m.Y', $date)."<br>";
154 }
155 }
156 $this->unsbmittedFiles = $this->getUnsubmittedStatistics();
157 }
159 // Transmit daily statistics to GOsa-Server
160 if(isset($_POST['receiveStatistics']) && $this->rpcConfigured){
161 $start = strtotime($this->graph1DatePicker1);
162 $stop = strtotime($this->graph1DatePicker2);
163 $res = $this->rpcHandle->getInstanceStats($start,$stop);
164 if(!$this->rpcHandle->success()){
165 msg_dialog::display(_("Error"),msgPool::rpcError($this->rpcHandle->get_error()),ERROR_DIALOG);
166 }elseif($res){
167 $this->statisticData = $this->prepareGraphData($res);
168 $this->reloadGraphs();
169 }
170 }
172 $smarty->assign('graphID_1', $this->graphID_1);
173 $smarty->assign('graphID_2', $this->graphID_2);
174 $smarty->assign('graphID_3', $this->graphID_3);
175 $smarty->assign('graphID_4', $this->graphID_4);
176 $smarty->assign('graphID_5', $this->graphID_5);
177 $smarty->assign('unsbmittedFiles', count($this->unsbmittedFiles));
178 $smarty->assign('unsbmittedFilesMsg', sprintf(
179 _("You have currently %s unsubmitted statistic collection, do you want to transmit them now?"),
180 count($this->unsbmittedFiles)));
181 return($smarty->fetch(get_template_path('statistics.tpl', TRUE)));
182 }
185 /*! \brief Prepares the graph data we've received from the rpc-service.
186 * This method will construct a usable data-array with converted
187 * date strings.
188 */
189 function prepareGraphData($res)
190 {
192 /* Build up array which represents the amount of errors per
193 * interval.
194 */
195 $gData = array();
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 if(isset($_POST['graph1DatePicker1'])) $this->graph1DatePicker1 = get_post('graph1DatePicker1');
264 if(isset($_POST['graph1DatePicker2'])) $this->graph1DatePicker2 = get_post('graph1DatePicker2');
265 }
268 /*! \brief This method tries to translate category names.
269 * @param The category name to translate
270 * @return String The translated category names.
271 */
272 function getCategoryTranslation($name)
273 {
274 $ret ="";
276 // Extract category names from the given string.
277 $list = preg_split("/, /", $name);
279 // We do not have a category for systems directly, so we've to map all system types to 'System'.
280 // If we do not map to _(Systems) the graph legend will be half screen width.
281 if(count(array_intersect(array('server','terminal','workstation', 'opsi', 'component'), $list))){
282 return(_("Systems"));
283 }
285 // Walk through category names and try to find a translation.
286 foreach($list as $cat){
287 $cat = trim($cat);
288 if(isset($this->catTranslations[$cat])){
289 $cat = _($this->catTranslations[$cat]);
290 }elseif(!empty($cat)){
291 $cat = _($cat);
292 }
293 $ret .= $cat.", ";
294 }
295 return(rtrim($ret, ', '));
296 }
299 /*! \brief Reload the graph images.
300 */
301 function reloadGraphs()
302 {
303 new pChartInclude();
304 $gData = $this->statisticData;
305 if(count($gData['actionsPerCategory'])){
306 $this->generateCategoryPieGraph($gData['actionsPerCategory']);
307 }
308 $this->generateActionsGraph($gData);
309 }
312 /*! \brief Generates the line-graph which displays the plugin usage over time.
313 */
314 function generateActionsGraph($gData)
315 {
316 $lineMax = 100;
317 $errorMax = (max($gData['errorsPerInterval']) < 100)? 100:max($gData['errorsPerInterval']);
318 $dataSet = new pData;
319 foreach($gData['actionsPerInterval'] as $category => $entriesPerDate){
320 if(empty($category)) continue;
322 // Add results to our data set.
323 $dataSet->AddPoint($entriesPerDate, $category);
324 $dataSet->SetSerieName($this->getCategoryTranslation($category), $category);
325 $dataSet->AddSerie($category);
327 // Detect maximum value, to adjust the Y-Axis
328 $tmpMax = max($entriesPerDate);
329 if($tmpMax > $lineMax) $lineMax = $tmpMax;
330 }
332 // Add timeline
333 $dataSet->AddPoint($gData['dates'], 'date');
334 $dataSet->SetAbsciseLabelSerie('date');
336 $chart = new pChart(800,230);
337 $chart->setFixedScale(0.000,$lineMax);
338 $chart->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",10);
339 $chart->setGraphArea(50,30,585,200);
340 $chart->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);
341 $chart->drawRoundedRectangle(5,5,695,225,5,230,230,230);
342 $chart->drawGraphArea(255,255,255,TRUE);
343 $chart->drawGrid(4,TRUE,200,200,200,50);
344 $chart->drawTreshold(0,143,55,72,TRUE,TRUE);
345 $chart->drawTitle(50,22,"Plugin usage over time",50,50,50,585);
346 $chart->drawScale($dataSet->GetData(),$dataSet->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2, TRUE);
347 $chart->drawFilledLineGraph($dataSet->GetData(),$dataSet->GetDataDescription(),50,TRUE);
348 $chart->setColorPalette(count($gData['actionsPerInterval']),255,0,0);
350 // Draw legend
351 $dataSet->AddPoint($gData['errorsPerInterval'], 'Errors');
352 $dataSet->SetSerieName(_('Error'), 'Errors');
353 $dataSet->AddSerie('Errors');
354 $chart->drawLegend(650,30,$dataSet->GetDataDescription(),255,255,255);
356 // Remove all graph series and add the error-series, then draw the new graph.
357 foreach($gData['actionsPerInterval'] as $category => $data){
358 $dataSet->RemoveSerie($category);
359 }
360 $chart->setFixedScale(0,$errorMax);
361 $chart->drawRightScale($dataSet->GetData(),$dataSet->GetDataDescription(),SCALE_NORMAL,120,150,150,TRUE,0,2, TRUE);
362 $chart->drawBarGraph($dataSet->GetData(),$dataSet->GetDataDescription());
364 // Generate new and unique graph id
365 $this->graphID_2 = preg_replace("/[^0-9]/","",microtime(TRUE));
366 $file = '/tmp/graph_'.$this->graphID_2;
367 $chart->Render($file);
368 session::set('statistics::graphFile'.$this->graphID_2,$file);
370 return;
371 }
373 # // Prepare Data
374 # $graphData = array();
375 # foreach($gData['usagePerInterval'] as $dateStr => $data){
376 # $date = strtotime($dateStr);
377 # foreach($data as $name => $val){
378 # $graphData[$name][$date] = $val;
379 # }
380 # }
381 #
382 # // Sort Data
383 # foreach($graphData as $key => $data)
384 # ksort($graphData[$key]);
385 #
386 # // Generate new and unique graph id
387 # $this->graphID_3 = preg_replace("/[^0-9]/","",microtime(TRUE));
388 #
389 # // Prepare transmitted data, sort it by date and collect
390 # // transmitted timestamps to be able to print the x-Axis labels.
391 # $dataSet = new pData;
392 #
393 # $max = max($graphData['max_mem']);
394 #
395 # $dataSet->AddPoint(array_values($graphData['max_mem']), 'max_mem');
396 # $dataSet->AddPoint(array_values($graphData['avg_mem']), 'avg_mem');
397 # $dataSet->AddPoint(array_values($graphData['min_mem']), 'min_mem');
398 #
399 # $dataSet->SetSerieName('Min Memory', 'min_mem');
400 # $dataSet->SetSerieName('Max Memory', 'max_mem');
401 # $dataSet->SetSerieName('Average Memory', 'avg_mem');
402 #
403 # $dataSet->AddAllSeries();
404 # $dataSet->AddPoint($dateSeries, 'date');
405 # $dataSet->SetAbsciseLabelSerie('date');
406 #
407 # $chart = new pChart(800,230);
408 # $chart->setFixedScale(0.0001,($max*1.1));
409 # $chart->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",10);
410 # $chart->setGraphArea(50,30,585,200);
411 # $chart->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);
412 # $chart->drawRoundedRectangle(5,5,695,225,5,230,230,230);
413 # $chart->drawGraphArea(255,255,255,TRUE);
414 # $chart->drawGrid(4,TRUE,200,200,200,50);
415 # $chart->drawTreshold(0,143,55,72,TRUE,TRUE);
416 # $chart->drawTitle(50,22,"Memory usage",50,50,50,585);
417 #
418 # $chart->drawScale($dataSet->GetData(),$dataSet->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2, FALSE);
419 # $chart->drawFilledCubicCurve($dataSet->GetData(),$dataSet->GetDataDescription(),.1,50);
420 #
421 # $file = '/tmp/graph_'.$this->graphID_3;
422 # $chart->Render($file);
423 # session::set('statistics::graphFile'.$this->graphID_3,$file);
424 #
425 #
426 #
427 #
428 #
429 #
430 #
431 # // Generate new and unique graph id
432 # $this->graphID_4 = preg_replace("/[^0-9]/","",microtime(TRUE));
433 #
434 # // Prepare transmitted data, sort it by date and collect
435 # // transmitted timestamps to be able to print the x-Axis labels.
436 # $dataSet = new pData;
437 #
438 # $max = max($graphData['max_dur']);
439 #
440 # $dataSet->AddPoint(array_values($graphData['max_dur']), 'max_dur');
441 # $dataSet->AddPoint(array_values($graphData['avg_dur']), 'avg_dur');
442 # $dataSet->AddPoint(array_values($graphData['min_dur']), 'min_dur');
443 #
444 # $dataSet->SetSerieName('Min dur', 'min_dur');
445 # $dataSet->SetSerieName('Max dur', 'max_dur');
446 # $dataSet->SetSerieName('Average dur', 'avg_dur');
447 #
448 # $dataSet->AddAllSeries();
449 # $dataSet->AddPoint($dateSeries, 'date');
450 # $dataSet->SetAbsciseLabelSerie('date');
451 #
452 # $chart = new pChart(800,230);
453 # $chart->setFixedScale(0.0001,($max*1.1));
454 # $chart->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",10);
455 # $chart->setGraphArea(50,30,585,200);
456 # $chart->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);
457 # $chart->drawRoundedRectangle(5,5,695,225,5,230,230,230);
458 # $chart->drawGraphArea(255,255,255,TRUE);
459 # $chart->drawGrid(4,TRUE,200,200,200,50);
460 # $chart->drawTreshold(0,143,55,72,TRUE,TRUE);
461 # $chart->drawTitle(50,22,"Render time",50,50,50,585);
462 #
463 # $chart->drawScale($dataSet->GetData(),$dataSet->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2, FALSE);
464 # $chart->drawFilledCubicCurve($dataSet->GetData(),$dataSet->GetDataDescription(),.1,50);
465 #
466 # $file= '/tmp/graph_'.$this->graphID_4;
467 # $chart->Render($file);
468 # session::set('statistics::graphFile'.$this->graphID_4,$file);
469 #
470 #
471 #
472 #
473 #
474 #
475 #
476 #
477 # // Generate new and unique graph id
478 # $this->graphID_5 = preg_replace("/[^0-9]/","",microtime(TRUE));
479 #
480 # // Prepare transmitted data, sort it by date and collect
481 # // transmitted timestamps to be able to print the x-Axis labels.
482 # $dataSet = new pData;
483 #
484 # $max = max($graphData['max_load']);
485 #
486 # $dataSet->AddPoint(array_values($graphData['max_load']), 'max_load');
487 # $dataSet->AddPoint(array_values($graphData['avg_load']), 'avg_load');
488 # $dataSet->AddPoint(array_values($graphData['min_load']), 'min_load');
489 #
490 # $dataSet->SetSerieName('Min Load', 'min_load');
491 # $dataSet->SetSerieName('Max Load', 'max_load');
492 # $dataSet->SetSerieName('Average Load', 'avg_load');
493 #
494 # $dataSet->AddAllSeries();
495 # $dataSet->AddPoint($dateSeries, 'date');
496 # $dataSet->SetAbsciseLabelSerie('date');
497 #
498 # $chart = new pChart(800,230);
499 # $chart->setFixedScale(0.0001,($max*1.1));
500 # $chart->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",10);
501 # $chart->setGraphArea(50,30,585,200);
502 # $chart->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);
503 # $chart->drawRoundedRectangle(5,5,695,225,5,230,230,230);
504 # $chart->drawGraphArea(255,255,255,TRUE);
505 # $chart->drawGrid(4,TRUE,200,200,200,50);
506 # $chart->drawTreshold(0,143,55,72,TRUE,TRUE);
507 # $chart->drawTitle(50,22,"CPU load",50,50,50,585);
508 #
509 # $chart->drawScale($dataSet->GetData(),$dataSet->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2, FALSE);
510 # $chart->drawFilledCubicCurve($dataSet->GetData(),$dataSet->GetDataDescription(),.1,50);
511 #
512 # $file = '/tmp/graph_'.$this->graphID_5;
513 # $chart->Render($file);
514 # session::set('statistics::graphFile'.$this->graphID_5,$file);
515 #
518 /*! \brief Generate the pie-chart which displays the overall-plugin-usage
519 */
520 function generateCategoryPieGraph($data)
521 {
522 // Sort data by usage count and slice array to get
523 // the eight most used categories
524 arsort($data);
525 $mostUsedCategories = array_slice($data,0,7);
527 // Get the rest of categories and combine them 'others'
528 $theRest = array_slice($data,7);
529 $mostUsedCategories['remaining'] = array_sum($theRest);
531 // Try to find a translation for the given category names
532 $values = array_values($mostUsedCategories);
533 $keys = array_keys($mostUsedCategories);
534 foreach($keys as $id => $cat){
535 $keys[$id] = $this->getCategoryTranslation($cat);
536 }
538 // Dataset definition
539 $dataSet = new pData;
540 $dataSet->AddPoint($values,"Serie1");
541 $dataSet->AddAllSeries();
542 $dataSet->AddPoint($keys,"Serie2");
543 $dataSet->SetAbsciseLabelSerie("Serie2");
545 // Set graph area
546 $x = 400;
547 $y = 200;
549 // Initialise the graph
550 $chart = new pChart($x,$y);
551 $chart->setFontProperties($this->font,10);
552 $chart->drawPieGraph($dataSet->GetData(),$dataSet->GetDataDescription(),($x/3),($y/2)-10,($y/2),PIE_PERCENTAGE,TRUE,50,20,3);
553 $chart->drawPieLegend(($x/3*2),15,$dataSet->GetData(),$dataSet->GetDataDescription(),
554 $this->legendR,$this->legendG,$this->legendB);
556 // Store graph data
557 $this->graphID_1 = preg_replace("/[^0-9]/","",microtime(TRUE));
558 $file = '/tmp/graph_'.$this->graphID_1;
559 $chart->Render($file);
560 session::set('statistics::graphFile'.$this->graphID_1,$file);
561 }
562 }
563 ?>