AmfphpMonitorService.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * This file is part of amfPHP
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the license that is bundled
  8. * with this package in the file license.txt.
  9. */
  10. /**
  11. * monitoring service. controls logging, and provides method to fetch data.
  12. *
  13. * @amfphpHide
  14. * @package Amfphp_Plugins_Monitor
  15. * @author Ariel Sommeria-klein
  16. */
  17. class AmfphpMonitorService {
  18. /**
  19. * path to log.
  20. * @see AmfphpMonitor maxLogFileSize
  21. * @var type string
  22. */
  23. public static $logPath;
  24. /**
  25. * restrict access to amfphp_admin.
  26. * @var boolean
  27. */
  28. public static $restrictAccess;
  29. /**
  30. * max log file size
  31. * @see AmfphpMonitor maxLogFileSize
  32. * @var int
  33. */
  34. public static $maxLogFileSize;
  35. /**
  36. * get method roles
  37. * @param string $methodName
  38. * @return array
  39. */
  40. public function _getMethodRoles($methodName) {
  41. if (self::$restrictAccess) {
  42. return array('amfphp_admin');
  43. }
  44. }
  45. /**
  46. * parse logged data and return it.
  47. * the format is
  48. * obj -> sortedDatas ( array ( uri => times (array (name => array of times))
  49. * obj -> timeNames
  50. *
  51. * note: timeNames are needed for rendering on the client side, to make sure that each series has the same times.
  52. * This could be done on the client side, but is faster to do here.
  53. *
  54. * @todo calculate averages per service instead of just returning the data raw.
  55. * @param boolean $flush get rid of the logged data
  56. * @return array
  57. */
  58. public function getData($flush){
  59. if(!is_writable(self::$logPath) || !is_readable(self::$logPath)){
  60. throw new Amfphp_Core_Exception('AmfphpMonitor does not have permission to read and write to log file: ' . self::$logPath);
  61. }
  62. if(!file_exists(self::$logPath)){
  63. return null;
  64. }
  65. $fileContent = file_get_contents(self::$logPath);
  66. //ignore "php exit " line
  67. $loggedData = substr($fileContent, 16);
  68. if($flush){
  69. $this->flush();
  70. }
  71. $exploded = explode("\n", $loggedData);
  72. //data is sorted by target uri
  73. $sortedData = array();
  74. //use associative array to avoid duplicating time names, then return keys.
  75. $timeNamesAssoc = array();
  76. foreach($exploded as $serializedRecord){
  77. $record = unserialize($serializedRecord);
  78. if(!$record){
  79. continue;
  80. }
  81. $uri = $record->uri;
  82. if(!isset($sortedData[$uri])){
  83. $sortedData[$uri] = array();
  84. }
  85. $uriData = &$sortedData[$uri];
  86. //sort times
  87. foreach($record->times as $timeName => $timeValue){
  88. if(!isset ($uriData[$timeName])){
  89. $uriData[$timeName] = array();
  90. }
  91. $uriData[$timeName][] = $timeValue;
  92. $timeNamesAssoc[$timeName] = '';
  93. }
  94. }
  95. $ret = new stdClass();
  96. $ret->sortedData = $sortedData;
  97. $ret->timeNames = array_keys($timeNamesAssoc);
  98. if(filesize(self::$logPath) > self::$maxLogFileSize){
  99. $ret->serverComment = 'The log file is full, so it is possible that your latest service calls are not represented. Either flush the log using the "flush" button or increase the log size(maxLogFileSize)';
  100. }
  101. return $ret;
  102. }
  103. /**
  104. * flush monitor log
  105. */
  106. public function flush(){
  107. if(!is_writable(self::$logPath) || !is_readable(self::$logPath)){
  108. throw new Amfphp_Core_Exception('AmfphpMonitor does not have permission to read and write to log file: ' . self::$logPath);
  109. }
  110. if(file_put_contents(self::$logPath, "<?php exit();?>\n") === false){
  111. throw new Amfphp_Core_Exception('AmfphpMonitor write file failed ' . self::$logPath);
  112. }
  113. }
  114. }
  115. ?>