X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=gosa-core%2Finclude%2Fclass_stats.inc;h=ae5be145d2390369664bff3d7ea88f380e145a12;hb=9773098162a69430e5c427eb458578bb39e15560;hp=b64ebe1e4f0cbc146951b8dae0d73c52f30f4e8d;hpb=90ae103941738c97b225a755900edfbe2155c6fd;p=gosa.git diff --git a/gosa-core/include/class_stats.inc b/gosa-core/include/class_stats.inc index b64ebe1e4..ae5be145d 100644 --- a/gosa-core/include/class_stats.inc +++ b/gosa-core/include/class_stats.inc @@ -2,34 +2,67 @@ class stats { - + static protected $lastCpuLoad = ""; static protected $lastCpuLoadTimestamp = 0; static protected $tableName = "stats"; - static protected $tableFile = "/var/spool/gosa/stats"; static protected $lastHandle = NULL; static protected $statsEnabled = FALSE; - static function getDatabaseHandle() + + /*! \brief This method tries to connect the GOsa-stats database and + * then returns a database handle on success else NULL. + * + * (The GOsa-stats database has to be enabled : statsDatabaseEnabled/statsDatabaseDirectory) + * + * This database will then contain information about the use of GOsa, + * no customer data will be stored. + * + * @return handle Returns a sqlite database handle. + */ + static function getDatabaseHandle($filename = '') { - // Try to return last valid handle. - if(stats::$lastHandle != NULL && is_resource(stats::$lastHandle)){ - return(stats::$lastHandle); - } + // We cannot log while the path to store logs in is empty. + global $config; // Check if Logging is enabled - global $config; if(!is_object($config) || ! $config instanceOf config){ return(NULL); } - + + $path = $config->get_cfg_value('core', 'statsDatabaseDirectory'); + if(empty($path)){ + return(NULL); + } + + // Check if path exists, if not try to create it + if(!is_dir($path) ){ + $res = @mkdir($path); + if(!$res){ + return(NULL); + } + } + + // Append a date suffix to the database file, to prevent huge and unusable database files. + if($filename == ''){ + $filename = date('Y-m-d'); + } + $tableFile = $path.'/'.$filename; + + // Try to return last valid handle. + if(file_exists($tableFile) && + isset(stats::$lastHandle[$filename]) && + stats::$lastHandle[$filename] != NULL && + is_resource(stats::$lastHandle[$filename])){ + return(stats::$lastHandle[$filename]); + } + // Get statsFile property - stats::$tableFile = $config->get_cfg_value('core', 'statsDatabaseFile'); stats::$statsEnabled = $config->boolValueIsTrue('core', 'statsDatabaseEnabled'); if(!stats::$statsEnabled){ - return; + return(NULL); } // Check for SQLite extension @@ -38,22 +71,46 @@ class stats } // Check if we are able to read/write the given database file. - if(!is_writeable(stats::$tableFile)){ + if(!is_writeable($path) && !is_writeable(dirname($tableFile))){ return(NULL); } // Try to create database, if it exists just open it. - $handle = sqlite_popen(stats::$tableFile, 0666, $error); + $handle = sqlite_open($tableFile, 0666, $error); if($handle){ stats::createDatabaseOnDemand($handle); } - stats::$lastHandle = $handle; + stats::$lastHandle[$filename] = $handle; return($handle); } - /*! \brief | - * @param | - * @return | + + /*! \brief Returns a list of all created stat files + * @return Array A list of all currently stored stat files. + */ + static function getLocalStatFiles() + { + $res = array(); + + // Check if we're creating logs right now. + if(stats::getDatabaseHandle()){ + global $config; + + // Walk through all files found in the storage path + $path = $config->get_cfg_value('core', 'statsDatabaseDirectory'); + $dir = opendir($path); + while($file = readdir($dir)){ + if(is_file($path.'/'.$file) && !preg_match('/.old$/', $file)) { + $res[] = $file; + } + } + } + return($res); + } + + + /*! \brief Check whether the qlite extension is available or not. + * @return boolean TRUE on success else FALSE */ static function checkSQLiteExtension() { @@ -61,21 +118,21 @@ class stats } - /*! \brief | - * @param | - * @return | + /*! \brief Drops the current stats table and thus enforces a recreation. + * @param handle The database handle to use. */ static function dropTable($handle) { $TABLE_NAME = stats::$tableName; $query = "DROP TABLE '{$TABLE_NAME}'"; $ret = sqlite_query($query, $handle); + stats::$lastHandle = NULL; + stats::getDatabaseHandle(); } - /*! \brief | - * @param | - * @return | + /*! \brief Returns the currently used amount of memory form the PHP process. + * @return int The amount of bytes used for the PHP process. */ static function get_memory_usage() { @@ -83,14 +140,14 @@ class stats } - /*! \brief | - * @param | - * @return | + /*! \brief Returns the current CPU load. + * The result will be cached and one updated every 5 seconds. + * @return float The current 'cpu_load'. */ static function get_cpu_load() { $cur = time(); - if(empty(stats::$lastCpuLoad) || (($cur - stats::$lastCpuLoadTimestamp) >=2 )){ + if(empty(stats::$lastCpuLoad) || (($cur - stats::$lastCpuLoadTimestamp) >= 5 )){ list($one, $five, $ten) =preg_split("/ /",shell_exec('cat /proc/loadavg')); stats::$lastCpuLoad = $one; stats::$lastCpuLoadTimestamp = $cur; @@ -99,9 +156,9 @@ class stats } - /*! \brief | - * @param | - * @return | + /*! \brief This method checks if the 'stats' table is already present, + * if it is not then it will be created. + * @param handle The sqlite database handle */ static function createDatabaseOnDemand($handle) { @@ -135,9 +192,15 @@ class stats } - /*! \brief | - * @param | - * @return | + /*! \brief Creates a new 'stats' table entry. + * -> Logs a GOsa action/activity in the sqlite stats table. + * @param string type The action type, e.g. ldap/plugin/management + * @param string plugin The plugin name, e.g. userManagement/user/posixAccount + * @param string category The plugin category e.g. users/servers/groups + * @param string action The action done e.g. edit/view/open/move + * @param int amount The amount, e.g. for multiple edit + * @param float duration The elapsed time. + * @param string info Some infos form the action, e.g. the used hashing mehtod for pwd changes. */ static function log($type, $plugin, $category, $action, $amount = 1, $duration = 0, $info ='') { @@ -154,7 +217,7 @@ class stats // -- 'clicks' is a counter wich is set in main.php -> Number of page reloads // -- 'overallRenderTimer' is set in main.php -> timestamp of rendering start. if(!isset($clicks) || empty($clicks)) $clicks = 0; - if(!isset($overallRenderTimer) || empty($overallRenderTimer)){ + if(!isset($overallRenderTimer)){ $renderTime = 0; }else{ $renderTime = microtime(TRUE) - $overallRenderTimer; @@ -170,14 +233,14 @@ class stats $plugin = sqlite_escape_string($plugin); $action = sqlite_escape_string($action); $timestamp = time(); - $mtimestamp = microtime(TRUE); + $mtimestamp = number_format(microtime(TRUE), 4,'.',''); $amount = sqlite_escape_string($amount); - $duration = sqlite_escape_string($duration); - $renderTime = sqlite_escape_string($renderTime); + $duration = sqlite_escape_string(number_format($duration, 4,'.','')); + $renderTime = sqlite_escape_string(number_format($renderTime, 4,'.','')); $info = sqlite_escape_string($info); $clicks = sqlite_escape_string($clicks); $memory_usage = sqlite_escape_string(stats::get_memory_usage()); - $cpu_load = sqlite_escape_string(stats::get_cpu_load()); + $cpu_load = sqlite_escape_string(number_format(stats::get_cpu_load(),4,'.','')); // Clean up category, which usally comes from acl_category and may still contain // some special chars like / @@ -186,366 +249,180 @@ class stats $tmp[] = trim($cat, '\/,; '); } $category = sqlite_escape_string(implode($tmp, ', ')); - + // Create insert statement. $TABLE_NAME = stats::$tableName; $query = " INSERT INTO {$TABLE_NAME} - (ACTID, TYPE, PLUGIN, CATEGORY, ACTION, UUID, MTIMESTAMP, TIMESTAMP, - AMOUNT, DURATION, RENDER_TIME, MEMORY_USAGE, CPU_LOAD, INFO) + (ACTID, TYPE, PLUGIN, CATEGORY, ACTION, UUID, MTIMESTAMP, TIMESTAMP, + AMOUNT, DURATION, RENDER_TIME, MEMORY_USAGE, CPU_LOAD, INFO) VALUES - ('{$clicks}','{$type}','{$plugin}','{$category}','{$action}','{$uuid}', - '{$mtimestamp}','{$timestamp}','{$amount}','{$duration}','{$renderTime}', - '{$memory_usage}','{$cpu_load}','{$info}')"; + ('{$clicks}','{$type}','{$plugin}','{$category}','{$action}','{$uuid}', + '{$mtimestamp}','{$timestamp}','{$amount}','{$duration}','{$renderTime}', + '{$memory_usage}','{$cpu_load}','{$info}')"; sqlite_query($query, $res); } - - - /*! \brief | - * @param | - * @return | - */ - static function show() - { - $res = stats::getDatabaseHandle(); - if(!$res) return; - - $TABLE_NAME = stats::$tableName; - $query = "SELECT * FROM {$TABLE_NAME} ORDER BY MTIMESTAMP"; - $query = "SELECT PLUGIN, ACTION, MAX(DURATION) as 'DURATION' FROM {$TABLE_NAME} WHERE ACTION='modify' GROUP BY PLUGIN,ACTION "; - $query = "SELECT * FROM {$TABLE_NAME} ORDER BY ID DESC LIMIT 30"; - $query = "SELECT * FROM {$TABLE_NAME} WHERE plugin != 'LDAP' ORDER BY ID DESC LIMIT 30"; - $ret = sqlite_query($query, $res); - echo "
"; - - $colSize = 16; - - $title = FALSE; - foreach(sqlite_fetch_all($ret) as $entry){ - if(!$title){ - foreach($entry as $key => $str) { - if(is_numeric($key)) continue; - echo str_pad($key,$colSize,' ')."|"; - } - echo "\n"; - foreach($entry as $key => $str) { - if(is_numeric($key)) continue; - echo str_pad('',$colSize,'-')."-"; - } - echo "\n"; - $title = TRUE; - } - - foreach($entry as $key => $str){ - if(is_numeric($key)) continue; - if($key == "DURATION" || $key == "MTIMESTAMP" || $key == "CPU_LOAD"){ - $str = sprintf("%0.4f", preg_replace("/,/",".",$str)); - echo str_pad($str,$colSize,' ', STR_PAD_LEFT)."|"; - }else{ - echo str_pad($str,$colSize,' ')."|"; - } - } - echo "\n"; - } - echo sqlite_error_string($ret); - - echo "\n------ \n"; - echo "Time spent per plugin-category \n"; - echo "------ \n"; - - $query = " - SELECT SUM(DURATION) AS DUR, CATEGORY - FROM {$TABLE_NAME} - WHERE plugin != 'LDAP' - GROUP BY CATEGORY - ORDER BY DUR DESC LIMIT 10"; - $ret = sqlite_query($query, $res); - - $colSize = 16; - $title = FALSE; - foreach(sqlite_fetch_all($ret) as $entry){ - foreach($entry as $key => $str){ - if(is_numeric($key)) continue; - echo str_pad($str,$colSize,' ')."|"; - } - echo "\n"; - } - - - echo sqlite_error_string($ret); - - echo "\n------ \n"; - echo "Time spent per plugin \n"; - echo "------ \n"; - - $query = " - SELECT SUM(DURATION) AS DUR, PLUGIN, UUID - FROM {$TABLE_NAME} - WHERE plugin != 'LDAP' - GROUP BY PLUGIN, UUID - ORDER BY DUR DESC LIMIT 10"; - $ret = sqlite_query($query, $res); - - $colSize = 16; - $title = FALSE; - foreach(sqlite_fetch_all($ret) as $entry){ - foreach($entry as $key => $str){ - if(is_numeric($key)) continue; - echo str_pad($str,$colSize,' ')."|"; - } - echo "\n"; - } - echo sqlite_error_string($ret); - # * Anzahl von Objekttypen - # * Anzahl der Löschungen pro Objekttyp - # * Anzahl der Erzeugungen pro Objekttyp - # * Anzahl der Bearbeitungen pro Objekttyp - # * Anzahl der Verschiebungen pro Objekttyp - # * Anzahl der Mehrfachbearbeitungen pro Objekttyp - # * Antwortzeiten pro aktion - # * Anzahl der Passwortänderungen - # * Anzahl der unterschiedlichen Anmeldungen - - echo "\n------ \n"; - echo "Actions done per plugin \n"; - echo "------ \n"; - - $query = " - SELECT COUNT(ACTION) as CNT, ACTION, PLUGIN - FROM {$TABLE_NAME} - WHERE TYPE = 'plugin' - AND PLUGIN != 'LDAP' - GROUP BY ACTION,PLUGIN - ORDER BY CNT DESC LIMIT 30"; - $ret = sqlite_query($query, $res); - - $colSize = 16; - $title = FALSE; - foreach(sqlite_fetch_all($ret) as $entry){ - foreach($entry as $key => $str){ - if(is_numeric($key)) continue; - echo str_pad($str,$colSize,' ')."|"; - } - echo "\n"; - } - echo sqlite_error_string($ret); - - echo "\n------ \n"; - echo "'create' actions done per plugin (5 most)\n"; - echo "------ \n"; - - $query = " - SELECT COUNT(ACTION) as CNT, ACTION, PLUGIN - FROM {$TABLE_NAME} - WHERE TYPE = 'plugin' - AND PLUGIN != 'LDAP' - AND ACTION = 'create' - GROUP BY ACTION,PLUGIN - ORDER BY CNT DESC LIMIT 5"; - $ret = sqlite_query($query, $res); - - $colSize = 16; - $title = FALSE; - foreach(sqlite_fetch_all($ret) as $entry){ - foreach($entry as $key => $str){ - if(is_numeric($key)) continue; - echo str_pad($str,$colSize,' ')."|"; + /*! \brief Closes all sqlite handles opened by this class + */ + static function closeHandles() + { + foreach(stats::lastHandle as $handle){ + if($handle && is_resource($handle)){ + sqlite_close($handle); } - echo "\n"; } - echo sqlite_error_string($ret); + } - echo "\n------ \n"; - echo "'move' actions done per plugin (5 most)\n"; - echo "------ \n"; - $query = " - SELECT COUNT(ACTION) as CNT, ACTION, PLUGIN - FROM {$TABLE_NAME} - WHERE TYPE = 'plugin' - AND PLUGIN != 'LDAP' - AND ACTION = 'move' - GROUP BY ACTION,PLUGIN - ORDER BY CNT DESC LIMIT 5"; - $ret = sqlite_query($query, $res); - - $colSize = 16; - $title = FALSE; - foreach(sqlite_fetch_all($ret) as $entry){ - foreach($entry as $key => $str){ - if(is_numeric($key)) continue; - echo str_pad($str,$colSize,' ')."|"; - } - echo "\n"; - } - echo sqlite_error_string($ret); + /*! \brief This method returns all entries of the GOsa-stats table. + * You can limit the result by setting the from/to parameter (timestamp). + * @param int from The timestamp to start the result from. + * @param int to The timestamp to end the request. + * @return array An array containing the requested entries. + */ + static function generateStatisticDump($filename) + { + // Get database connection + $TABLE_NAME = stats::$tableName; + $handle = stats::getDatabaseHandle($filename); + if(!$handle) return; + + $query = + " SELECT ". + " TYPE, PLUGIN, CATEGORY, ACTION, ". + " UUID, DATE(TIMESTAMP, 'unixepoch') as date, ". + " AVG(DURATION), AVG(RENDER_TIME), SUM(AMOUNT), ". + " AVG(MEMORY_USAGE), AVG(CPU_LOAD), INFO ". + " FROM ". + " stats ". + " GROUP BY ". + " TYPE, PLUGIN, CATEGORY, ACTION, UUID, date, INFO ". + " ORDER BY ". + " ID "; + + // Create Filter and start query + $ret = sqlite_array_query($query, $handle, SQLITE_ASSOC); + return($ret); + } - echo "\n------ \n"; - echo "'view' actions done per plugin (5 most)\n"; - echo "------ \n"; - $query = " - SELECT COUNT(ACTION) as CNT, ACTION, PLUGIN - FROM {$TABLE_NAME} - WHERE TYPE = 'plugin' - AND PLUGIN != 'LDAP' - AND ACTION = 'view' - GROUP BY ACTION,PLUGIN - ORDER BY CNT DESC LIMIT 5"; - $ret = sqlite_query($query, $res); - - $colSize = 16; - $title = FALSE; - foreach(sqlite_fetch_all($ret) as $entry){ - foreach($entry as $key => $str){ - if(is_numeric($key)) continue; - echo str_pad($str,$colSize,' ')."|"; - } - echo "\n"; + static function removeStatsFile($filename) + { + // Get statsFile property + global $config; + $path = $config->get_cfg_value('core', 'statsDatabaseDirectory'); + stats::$statsEnabled = $config->boolValueIsTrue('core', 'statsDatabaseEnabled'); + if(!stats::$statsEnabled){ + return(NULL); } - echo sqlite_error_string($ret); - - echo "\n------ \n"; - echo "'open' actions done per plugin (5 most)\n"; - echo "------ \n"; - $query = " - SELECT COUNT(ACTION) as CNT, ACTION, PLUGIN, UUID - FROM {$TABLE_NAME} - WHERE TYPE = 'plugin' - AND PLUGIN != 'LDAP' - AND ACTION = 'open' - GROUP BY ACTION,PLUGIN, UUID - ORDER BY CNT DESC LIMIT 5"; - $ret = sqlite_query($query, $res); - - $colSize = 16; - $title = FALSE; - foreach(sqlite_fetch_all($ret) as $entry){ - foreach($entry as $key => $str){ - if(is_numeric($key)) continue; - echo str_pad($str,$colSize,' ')."|"; - } - echo "\n"; + // We cannot log while the path to store logs in is empty. + if(empty($path)){ + return(NULL); } - echo sqlite_error_string($ret); - - echo "\n------ \n"; - echo "Time per session\n"; - echo "------ \n"; - $query = " - SELECT SUM(DURATION) as DUR, UUID - FROM {$TABLE_NAME} - GROUP BY UUID - ORDER BY DUR DESC LIMIT 10"; - $ret = sqlite_query($query, $res); - - $colSize = 16; - $title = FALSE; - foreach(sqlite_fetch_all($ret) as $entry){ - foreach($entry as $key => $str){ - if(is_numeric($key)) continue; - echo str_pad($str,$colSize,' ')."|"; - } - echo "\n"; + // Check if file exists and then remove it + if(isset(stats::$lastHandle[$filename]) && is_resource(stats::$lastHandle[$filename])){ + sqlite_close(stats::$lastHandle[$filename]); } - echo sqlite_error_string($ret); - - echo "\n------ \n"; - echo "Most used password hashes\n"; - echo "------ \n"; - - $query = " - SELECT DISTINCT(INFO), COUNT(INFO) as CNT - FROM {$TABLE_NAME} - WHERE ACTION = 'change_password' - GROUP BY INFO - ORDER BY INFO DESC LIMIT 10"; - $ret = sqlite_query($query, $res); - - $colSize = 16; - $title = FALSE; - foreach(sqlite_fetch_all($ret) as $entry){ - foreach($entry as $key => $str){ - if(is_numeric($key)) continue; - echo str_pad($str,$colSize,' ')."|"; - } - echo "\n"; + if(file_exists($path.'/'.$filename)){ + #unlink($path.'/'.$filename); + rename($path.'/'.$filename, $path.'/'.$filename.".old"); } - echo sqlite_error_string($ret); - - echo "\n------ \n"; - echo "Actions at all\n"; - echo "------ \n"; + } - $query = " - SELECT DISTINCT(ACTION), COUNT(ACTION) AS CNT - FROM {$TABLE_NAME} - GROUP BY ACTION - ORDER BY CNT DESC LIMIT 10"; - $ret = sqlite_query($query, $res); - - $colSize = 16; - $title = FALSE; - foreach(sqlite_fetch_all($ret) as $entry){ - foreach($entry as $key => $str){ - if(is_numeric($key)) continue; - echo str_pad($str,$colSize,' ')."|"; + + static function getLdapObjectCount($config, $statisticConformResult = FALSE, $date = "") + { + $ldap = $config->get_ldap_link(); + $ldap->cd($config->current['BASE']); + + // A list of objectClasses to search for, indexed by their + // object-category + $ocsToSearchFor = array( + "department" => array("gosaDepartment"), + "devices" => array("gotoDevice"), + "fai" => array("FAIobject"), + "gofaxlist" => array("goFaxRBlock","goFaxSBlock"), + "gofonconference" => array("goFonConference"), + "phone" => array("goFonHardware"), + "gofonmacro" => array("goFonMacro"), + "users" => array("gosaAccount"), + "acls" => array("gosaAcl","gosaRole"), + "application" => array("gosaApplication"), + "ogroups" => array("gosaGroupOfNames"), + "roles" => array("organizationalRole"), + "server" => array("goServer"), + "printer" => array("gotoPrinter"), + "terminal" => array("gotoTerminal"), + "workstation" => array("gotoWorkstation"), + "winworkstation" => array("sambaSamAccount"), + "incoming" => array("goHard"), + "component" => array("ieee802Device"), + "mimetypes" => array("gotoMimeType"), + "groups" => array("posixGroup"), + "sudo" => array("sudoRole")); + + // Build up a filter which contains all objectClass combined by OR. + // We will later sum up the results using PHP. + $filter = ""; + $categoryCounter = array(); + foreach($ocsToSearchFor as $category => $ocs){ + foreach($ocs as $oc){ + $filter.= "(objectClass={$oc})"; } - echo "\n"; + $categoryCounter[$category] = 0; } - echo sqlite_error_string($ret); - - echo "\n------ \n"; - echo "Time spent per action\n"; - echo "------ \n"; - - $query = " - SELECT DISTINCT(ACTION), SUM(DURATION) AS DUR - FROM {$TABLE_NAME} - GROUP BY ACTION - ORDER BY DUR DESC LIMIT 10"; - $ret = sqlite_query($query, $res); - - $colSize = 16; - $title = FALSE; - foreach(sqlite_fetch_all($ret) as $entry){ - foreach($entry as $key => $str){ - if(is_numeric($key)) continue; - echo str_pad($str,$colSize,' ')."|"; + $filter = "(|{$filter})"; + + // Initiate the ldap query + $res = $ldap->search($filter, array('objectClass')); + if($ldap->success()) { + + // Count number of results per category + while($entry = $ldap->fetch()){ + foreach($ocsToSearchFor as $category => $ocs){ + if(count(array_intersect($ocs, $entry['objectClass']))){ + $categoryCounter[$category] ++; + break; + } + } } - echo "\n"; } - echo sqlite_error_string($ret); - - echo "\n------ \n"; - echo "Average time per action\n"; - echo "------ \n"; - - $query = " - SELECT DISTINCT(ACTION), AVG(DURATION) AS DUR - FROM {$TABLE_NAME} - GROUP BY ACTION - ORDER BY DUR DESC LIMIT 10"; - $ret = sqlite_query($query, $res); - - $colSize = 16; - $title = FALSE; - foreach(sqlite_fetch_all($ret) as $entry){ - foreach($entry as $key => $str){ - if(is_numeric($key)) continue; - echo str_pad($str,$colSize,' ')."|"; + arsort($categoryCounter); + + // Do we have to return the result as SQL INSERT statement? + if($statisticConformResult){ + $uuid = $config->getGOsaUUID(); + $type = 'objectCount'; + $plugin = ''; + $action = ''; + $date = (empty($date))?date('Y-m-d'):$date; + $memory_usage = sqlite_escape_string(stats::get_memory_usage()); + $cpu_load = sqlite_escape_string(number_format(stats::get_cpu_load(),4,'.','')); + $sql = array(); + foreach($categoryCounter as $category => $amount){ + $sql[] = array( + "type" => $type, + "plugin" => $plugin, + "category" => $category, + "action" => $action, + "uuid" => $uuid, + "date" => $date, + "duration" => 0, + "render_time" => 0, + "amount" => $amount, + "mem_usage" => $memory_usage, + "load" => $cpu_load, + "info" => ''); } - echo "\n"; + return($sql); + }else{ + return($categoryCounter); } - echo sqlite_error_string($ret); - - echo ""; } } - ?>