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 $graphID_1 = 0;
14 var $graphID_2 = 0;
15 var $graphID_3 = 0;
16 var $graphID_4 = 0;
17 var $graphID_5 = 0;
19 var $graph1DatePicker1 = 0;
20 var $graph1DatePicker2 = 0;
22 var $unsbmittedFiles = array();
24 function __construct($config)
25 {
26 plugin::plugin($config, NULL);
28 // Init start and stop times for graph 1
29 $this->graph1DatePicker1 = date('d.m.Y', time() - 7 * 24 * 60 *60);
30 $this->graph1DatePicker2 = date('d.m.Y', time());
32 // First try to retrieve values via RPC
33 $this->rpcConfigured = FALSE;
34 if ($this->config->get_cfg_value("core","gosaRpcServer") != ""){
35 $this->rpcConfigured = TRUE;
36 $this->rpcHandle = $this->config->getRpcHandle(
37 "http://10.3.64.59:4000",
38 "65717fe6-9e3e-11df-b010-5452005f1250",
39 "WyukwauWoid2",
40 TRUE);
41 }
43 // Get list of unsubmitted files.
44 $this->unsbmittedFiles = $this->getUnsubmittedStatistics();
46 // Collect category translations
47 $this->catTranslations = array();
48 foreach($this->config->configRegistry->getListOfPlugins() as $plugin => $data){
49 if(isset($data['plCategory'])){
50 foreach($data['plCategory'] as $id => $name){
51 if(!is_numeric($id)){
52 $this->catTranslations[$id] = $name['description'];
53 }
54 }
55 }
56 }
57 }
59 /*! \brief Returns a list local stored statistic files
60 @param Array A list of filenames and dates.
61 */
62 function getLocalStatisticsFiles()
63 {
65 $res = stats::getLocalStatFiles();
66 $tmp = array();
67 if(count($res)){
68 foreach($res as $file){
69 $date = strtotime($file);
70 if($date){
71 $tmp[$file] = $date;
72 }
73 }
74 }
75 return($tmp);
76 }
79 /*! \brief Returns a list of not transmitted stat files (except files for the current day)
80 * @return Array A list of unsubmitted statistic files.
81 */
82 function getUnsubmittedStatistics()
83 {
84 $available = $this->getLocalStatisticsFiles();
85 $alreadyTransmitted = $this->getStatisticsDatesFromServer();
87 $unsubmitted = array();
88 foreach($available as $key => $day){
89 if(!isset($alreadyTransmitted[$key])) $unsubmitted [$key] = $day;
90 }
92 // Exclude statistic collection from today, they are still active and cannot be submitted.
93 $curDate = date('Y-m-d');
94 if(isset($unsubmitted)) unset($unsubmitted[$curDate]);
96 return($unsubmitted);
97 }
100 /*! \brief Request a list of dates for which the server can return statistics.
101 @param Array A list of dates $ret=[iso-str] = timestamp
102 */
103 function getStatisticsDatesFromServer()
104 {
105 $res = $this->rpcHandle->getInstanceStatDates();
106 $dates = array();
107 if(!$this->rpcHandle->success()){
108 msg_dialog::display(_("Error"),msgPool::rpcError($this->rpcHandle->get_error()),ERROR_DIALOG);
109 }else{
110 foreach($res as $date){
111 $dates[$date] = strtotime($date);
112 }
113 }
114 return($dates);
115 }
118 function execute()
119 {
120 if(isset($_POST['graph1DatePicker1'])) $this->graph1DatePicker1 = get_post('graph1DatePicker1');
121 if(isset($_POST['graph1DatePicker2'])) $this->graph1DatePicker2 = get_post('graph1DatePicker2');
123 $smarty = get_smarty();
124 $smarty->assign('graph1DatePicker1', $this->graph1DatePicker1);
125 $smarty->assign('graph1DatePicker2', $this->graph1DatePicker2);
127 // Do not render anything if we are not prepared to send and receive data via rpc.
128 $smarty->assign("rpcConfigured", $this->rpcConfigured);
129 $smarty->assign("validRpcHandle", TRUE);
130 if(!$this->rpcConfigured || !$this->rpcHandle){
131 $smarty->assign("validRpcHandle", FALSE);
132 return($smarty->fetch(get_template_path('statistics.tpl', TRUE)));
133 }
135 // Send stats
136 if(isset($_POST['transmitStatistics'])){
137 $this->unsbmittedFiles = $this->getUnsubmittedStatistics();
138 foreach($this->unsbmittedFiles as $filename => $date){
139 $tmp = stats::dumpTables($filename);
140 $dump = array();
141 foreach($tmp as $entry){
142 $dump[] = array_values($entry);
143 }
144 $res = $this->rpcHandle->updateInstanceStatus($dump);
145 if(!$this->rpcHandle->success()){
146 msg_dialog::display(_("Error"),msgPool::rpcError($this->rpcHandle->get_error()),ERROR_DIALOG);
147 }else{
148 stats::removeStatsFile($filename);
149 echo "Inserted ".$res." entries for date ".date('d.m.Y', $date)."<br>";
150 }
151 }
152 $this->unsbmittedFiles = $this->getUnsubmittedStatistics();
153 }
155 // Transmit daily statistics to GOsa-Server
156 if(isset($_POST['receiveStatistics'])){
158 // First try to retrieve values via RPC
159 if ($this->config->get_cfg_value("core","gosaRpcServer") != ""){
161 $start = strtotime($this->graph1DatePicker1);
162 $stop = strtotime($this->graph1DatePicker2);
164 // Request statistics now
165 $res = $this->rpcHandle->getInstanceStats($start,$stop);
166 if(!$this->rpcHandle->success()){
167 msg_dialog::display(_("Error"),msgPool::rpcError($this->rpcHandle->get_error()),ERROR_DIALOG);
168 }
170 if($res && $this->rpcHandle->success()){
172 // Include pChart
173 new pChartInclude();
175 // Get most used categories, but only eight at once.
176 if(count($res['actionsPerCategory'])){
177 $this->generateCategoryPieGraph($res['actionsPerCategory']);
178 }
181 // --------
182 // Generate combined line and car chart of plugin usage, ldap execution time and errors
183 // --------
185 // Generate new and unique graph id
186 $this->graphID_2 = preg_replace("/[^0-9]/","",microtime(TRUE));
188 // Prepare transmitted data, sort it by date and collect
189 // transmitted timestamps to be able to print the x-Axis labels.
190 $dataArray = array();
191 $dates = array();
192 $DataSet2 = new pData;
193 $max = 1;
194 $seriesNumber = 0;
195 foreach($res['actionsPerInterval'] as $category => $entriesPerDate){
197 // Collect data per category and store used timestamps
198 foreach($entriesPerDate as $dateStr => $count){
199 $date = strtotime($dateStr);
200 $dates[$date]=$date;
202 // Do not append empty data
203 if(empty($category)) continue;
204 if($count) $count = ($count);
205 $dataArray[$category][$date] = $count;
206 }
208 // Do not append empty data
209 if(empty($category)) continue;
211 // Sort results.
212 ksort($dataArray[$category]);
214 // Add results to our data set.
215 $DataSet2->AddPoint($dataArray[$category], $category);
216 $DataSet2->SetSerieName($this->getCategoryTranslation($category), $category);
217 $DataSet2->AddSerie($category);
218 $seriesNumber++;
220 // Detect maximum value, to adjust the Y-Axis
221 $tmpMax = max($dataArray[$category]);
222 if($tmpMax > $max) $max = $tmpMax;
223 }
224 ksort($dates);
226 // Prepare date strings for X-Axis, only print a given number of
227 // of labels to keep the axis readable.
228 $Xam = 5; // Number of labels
229 $cnt = 0;
230 $dateSeries = array();
231 foreach($dates as $stamp){
232 if((count($dates) <= $Xam) || ($cnt % (floor(count($dates) / $Xam )) == 0)){
233 $dateSeries[$stamp] = date('d.m.Y',$stamp);
234 }else{
235 $dateSeries[$stamp] = ' ';
236 }
237 $cnt ++;
238 }
240 $DataSet2->AddPoint($dateSeries, 'date');
241 $DataSet2->SetAbsciseLabelSerie('date');
243 $Test2 = new pChart(800,230);
244 $Test2->setFixedScale(0.0001,($max*1.1));
245 $Test2->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",10);
246 $Test2->setGraphArea(50,30,585,200);
247 $Test2->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);
248 $Test2->drawRoundedRectangle(5,5,695,225,5,230,230,230);
249 $Test2->drawGraphArea(255,255,255,TRUE);
250 $Test2->drawGrid(4,TRUE,200,200,200,50);
251 $Test2->drawTreshold(0,143,55,72,TRUE,TRUE);
252 $Test2->drawTitle(50,22,"Plugin usage over time",50,50,50,585);
254 if(count($dates)){
255 $Test2->drawScale($DataSet2->GetData(),$DataSet2->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2);
256 }
258 // Draw the cubic curve graph
259 if(count($dataArray)){
260 $Test2->drawFilledLineGraph($DataSet2->GetData(),$DataSet2->GetDataDescription(),50,TRUE);
261 }
263 // Add error series
264 $errors = array();
265 foreach($res['errorsPerInterval'] as $dateStr => $count){
266 $date = strtotime($dateStr);
267 if($count !=0) $count = ($count);
268 $errors[$date] = $count;
269 }
271 ksort($errors);
273 $DataSet2->AddPoint($errors, 'Errors');
274 $DataSet2->SetSerieName(_('Error'), 'Errors');
275 $DataSet2->AddSerie('Errors');
276 $seriesNumber ++;
278 $Test2->setColorPalette($seriesNumber-1,255,0,0);
280 // Draw legend
281 $Test2->drawLegend(650,30,$DataSet2->GetDataDescription(),255,255,255);
283 // Remove plugin usage from data series, just keep error series.
284 foreach($dataArray as $categoryName => $list){
285 $DataSet2->RemoveSerie($categoryName);
286 }
288 // Draw right scale (Errors per day)
289 $Test2->setFixedScale(0.0001,(max($errors) +1) *1.1);
290 $Test2->drawRightScale($DataSet2->GetData(),$DataSet2->GetDataDescription(),SCALE_NORMAL,120,150,150,TRUE,0,2);
291 $Test2->drawBarGraph($DataSet2->GetData(),$DataSet2->GetDataDescription());
293 $file = '/tmp/graph_'.$this->graphID_2;
294 $Test2->Render($file);
295 session::set('statistics::graphFile'.$this->graphID_2,$file);
299 // Prepare Data
300 $graphData = array();
301 foreach($res['usagePerInterval'] as $dateStr => $data){
302 $date = strtotime($dateStr);
303 foreach($data as $name => $val){
304 $graphData[$name][$date] = $val;
305 }
306 }
308 // Sort Data
309 foreach($graphData as $key => $data)
310 ksort($graphData[$key]);
312 // Generate new and unique graph id
313 $this->graphID_3 = preg_replace("/[^0-9]/","",microtime(TRUE));
315 // Prepare transmitted data, sort it by date and collect
316 // transmitted timestamps to be able to print the x-Axis labels.
317 $DataSet3 = new pData;
319 $max = max($graphData['max_mem']);
321 $DataSet3->AddPoint(array_values($graphData['max_mem']), 'max_mem');
322 $DataSet3->AddPoint(array_values($graphData['avg_mem']), 'avg_mem');
323 $DataSet3->AddPoint(array_values($graphData['min_mem']), 'min_mem');
325 $DataSet3->SetSerieName('Min Memory', 'min_mem');
326 $DataSet3->SetSerieName('Max Memory', 'max_mem');
327 $DataSet3->SetSerieName('Average Memory', 'avg_mem');
329 $DataSet3->AddAllSeries();
330 $DataSet3->AddPoint($dateSeries, 'date');
331 $DataSet3->SetAbsciseLabelSerie('date');
333 $Test3 = new pChart(800,230);
334 $Test3->setFixedScale(0.0001,($max*1.1));
335 $Test3->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",10);
336 $Test3->setGraphArea(50,30,585,200);
337 $Test3->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);
338 $Test3->drawRoundedRectangle(5,5,695,225,5,230,230,230);
339 $Test3->drawGraphArea(255,255,255,TRUE);
340 $Test3->drawGrid(4,TRUE,200,200,200,50);
341 $Test3->drawTreshold(0,143,55,72,TRUE,TRUE);
342 $Test3->drawTitle(50,22,"Memory usage",50,50,50,585);
344 $Test3->drawScale($DataSet3->GetData(),$DataSet3->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2, FALSE);
345 $Test3->drawFilledCubicCurve($DataSet3->GetData(),$DataSet3->GetDataDescription(),.1,50);
347 $file = '/tmp/graph_'.$this->graphID_3;
348 $Test3->Render($file);
349 session::set('statistics::graphFile'.$this->graphID_3,$file);
357 // Generate new and unique graph id
358 $this->graphID_4 = preg_replace("/[^0-9]/","",microtime(TRUE));
360 // Prepare transmitted data, sort it by date and collect
361 // transmitted timestamps to be able to print the x-Axis labels.
362 $DataSet4 = new pData;
364 $max = max($graphData['max_dur']);
366 $DataSet4->AddPoint(array_values($graphData['max_dur']), 'max_dur');
367 $DataSet4->AddPoint(array_values($graphData['avg_dur']), 'avg_dur');
368 $DataSet4->AddPoint(array_values($graphData['min_dur']), 'min_dur');
370 $DataSet4->SetSerieName('Min dur', 'min_dur');
371 $DataSet4->SetSerieName('Max dur', 'max_dur');
372 $DataSet4->SetSerieName('Average dur', 'avg_dur');
374 $DataSet4->AddAllSeries();
375 $DataSet4->AddPoint($dateSeries, 'date');
376 $DataSet4->SetAbsciseLabelSerie('date');
378 $Test4 = new pChart(800,230);
379 $Test4->setFixedScale(0.0001,($max*1.1));
380 $Test4->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",10);
381 $Test4->setGraphArea(50,30,585,200);
382 $Test4->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);
383 $Test4->drawRoundedRectangle(5,5,695,225,5,230,230,230);
384 $Test4->drawGraphArea(255,255,255,TRUE);
385 $Test4->drawGrid(4,TRUE,200,200,200,50);
386 $Test4->drawTreshold(0,143,55,72,TRUE,TRUE);
387 $Test4->drawTitle(50,22,"Render time",50,50,50,585);
389 $Test4->drawScale($DataSet4->GetData(),$DataSet4->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2, FALSE);
390 $Test4->drawFilledCubicCurve($DataSet4->GetData(),$DataSet4->GetDataDescription(),.1,50);
392 $file= '/tmp/graph_'.$this->graphID_4;
393 $Test4->Render($file);
394 session::set('statistics::graphFile'.$this->graphID_4,$file);
403 // Generate new and unique graph id
404 $this->graphID_5 = preg_replace("/[^0-9]/","",microtime(TRUE));
406 // Prepare transmitted data, sort it by date and collect
407 // transmitted timestamps to be able to print the x-Axis labels.
408 $DataSet5 = new pData;
410 $max = max($graphData['max_load']);
412 $DataSet5->AddPoint(array_values($graphData['max_load']), 'max_load');
413 $DataSet5->AddPoint(array_values($graphData['avg_load']), 'avg_load');
414 $DataSet5->AddPoint(array_values($graphData['min_load']), 'min_load');
416 $DataSet5->SetSerieName('Min Load', 'min_load');
417 $DataSet5->SetSerieName('Max Load', 'max_load');
418 $DataSet5->SetSerieName('Average Load', 'avg_load');
420 $DataSet5->AddAllSeries();
421 $DataSet5->AddPoint($dateSeries, 'date');
422 $DataSet5->SetAbsciseLabelSerie('date');
424 $Test5 = new pChart(800,230);
425 $Test5->setFixedScale(0.0001,($max*1.1));
426 $Test5->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",10);
427 $Test5->setGraphArea(50,30,585,200);
428 $Test5->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);
429 $Test5->drawRoundedRectangle(5,5,695,225,5,230,230,230);
430 $Test5->drawGraphArea(255,255,255,TRUE);
431 $Test5->drawGrid(4,TRUE,200,200,200,50);
432 $Test5->drawTreshold(0,143,55,72,TRUE,TRUE);
433 $Test5->drawTitle(50,22,"CPU load",50,50,50,585);
435 $Test5->drawScale($DataSet5->GetData(),$DataSet5->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2, FALSE);
436 $Test5->drawFilledCubicCurve($DataSet5->GetData(),$DataSet5->GetDataDescription(),.1,50);
438 $file = '/tmp/graph_'.$this->graphID_5;
439 $Test5->Render($file);
440 session::set('statistics::graphFile'.$this->graphID_5,$file);
442 }
443 }
444 }
446 $smarty->assign('graphID_1', $this->graphID_1);
447 $smarty->assign('graphID_2', $this->graphID_2);
448 $smarty->assign('graphID_3', $this->graphID_3);
449 $smarty->assign('graphID_4', $this->graphID_4);
450 $smarty->assign('graphID_5', $this->graphID_5);
451 $smarty->assign('unsbmittedFiles', count($this->unsbmittedFiles));
452 $smarty->assign('unsbmittedFilesMsg',
453 sprintf(
454 _("You have currently %s unsubmitted statistic collection, do you want to transmit them now?"),
455 count($this->unsbmittedFiles)));
456 return($smarty->fetch(get_template_path('statistics.tpl', TRUE)));
457 }
460 function check()
461 {
462 $messages = plugin::check();
463 return($messages);
464 }
467 function save_object()
468 {
469 plugin::save_object();
470 }
473 /*! \brief This method tries to translate category names.
474 * @param The category name to translate
475 * @return String The translated category names.
476 */
477 function getCategoryTranslation($name)
478 {
479 $ret ="";
481 // Extract category names from the given string.
482 $list = preg_split("/, /", $name);
484 // We do not have a category for systems directly, so we've to map all system types to 'System'.
485 // If we do not map to _(Systems) the graph legend will be half screen width.
486 if(count(array_intersect(array('server','terminal','workstation', 'opsi', 'component'), $list))){
487 return(_("Systems"));
488 }
490 // Walk through category names and try to find a translation.
491 foreach($list as $cat){
492 $cat = trim($cat);
493 if(isset($this->catTranslations[$cat])){
494 $ret .= _($this->catTranslations[$cat]).", ";
495 }else{
496 $ret .= $cat.", ";
497 }
498 }
499 return(rtrim($ret, ', '));
500 }
503 function generateCategoryPieGraph($data)
504 {
505 // Sort data by usage count and slice array to get
506 // the eight most used categories
507 arsort($data);
508 $mostUsedCategories = array_slice($data,0,8);
509 $values = array_values($mostUsedCategories);
510 $keys = array_keys($mostUsedCategories);
512 // Try to find a translation for the given category names
513 foreach($keys as $id => $cat){
514 $keys[$id] = $this->getCategoryTranslation($cat);
515 }
517 // Dataset definition
518 $DataSet = new pData;
519 $DataSet->AddPoint($values,"Serie1");
520 $DataSet->AddAllSeries();
521 $DataSet->AddPoint($keys,"Serie2");
522 $DataSet->SetAbsciseLabelSerie("Serie2");
524 // Initialise the graph
525 $Test = new pChart(500,200);
526 $Test->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",10);
527 $Test->drawPieGraph($DataSet->GetData(),$DataSet->GetDataDescription(),150,90,110,PIE_PERCENTAGE,TRUE,50,20,5);
528 $Test->drawPieLegend(310,15,$DataSet->GetData(),$DataSet->GetDataDescription(),200,255,200);
530 // Store graph data
531 $this->graphID_1 = preg_replace("/[^0-9]/","",microtime(TRUE));
532 $file = '/tmp/graph_'.$this->graphID_1;
533 $Test->Render($file);
534 session::set('statistics::graphFile'.$this->graphID_1,$file);
535 }
536 }
537 ?>