Commit c0d8b29cf6c38842d83cf2b2a7ede48471fce24d

Authored by Conrad Vermeulen
1 parent 536f9aa8

KTS-1753

" Implement Disk Usage Plugin"
Implemented.

Committed By: Conrad Vermeulen
Reviewed By: Kevin Fourie

git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@7357 c91229c3-7414-0410-bfa2-8a42b809f60b
plugins/housekeeper/DiskUsageDashlet.inc.php 0 โ†’ 100755
  1 +<?php
  2 +
  3 +/**
  4 + *
  5 + * Copyright (c) 2007 Jam Warehouse http://www.jamwarehouse.com
  6 + *
  7 + * This program is free software; you can redistribute it and/or modify
  8 + * it under the terms of the GNU General Public License as published by
  9 + * the Free Software Foundation; using version 2 of the License.
  10 + *
  11 + * This program is distributed in the hope that it will be useful,
  12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + * GNU General Public License for more details.
  15 + *
  16 + * You should have received a copy of the GNU General Public License
  17 + * along with this program; if not, write to the Free Software
  18 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19 + *
  20 + * -------------------------------------------------------------------------
  21 + *
  22 + * You can contact the copyright owner regarding licensing via the contact
  23 + * details that can be found on the KnowledgeTree web site:
  24 + *
  25 + * http://www.knowledgetree.com/
  26 + */
  27 +
  28 +class DiskUsageDashlet extends KTBaseDashlet
  29 +{
  30 + private $dfCmd;
  31 + private $usage;
  32 + private $warningPercent;
  33 + private $urgentPercent;
  34 +
  35 + function DiskUsageDashlet()
  36 + {
  37 + $this->sTitle = _kt('Disk Usage');
  38 + $this->sClass = "ktInfo";
  39 + }
  40 +
  41 + function is_active($oUser)
  42 + {
  43 + $dfCmd = KTUtil::findCommand('externalBinary/df','df');
  44 + if ($dfCmd === false)
  45 + {
  46 + return false;
  47 + }
  48 + $this->dfCmd = $dfCmd;
  49 +
  50 + $config = KTConfig::getSingleton();
  51 + $this->warningPercent = $config->get('DiskUsage/warningThreshold', 15);
  52 + $this->urgentPercent = $config->get('DiskUsage/urgentThreshold', 5);
  53 +
  54 + $this->getUsage();
  55 +
  56 + return Permission::userIsSystemAdministrator();
  57 + }
  58 +
  59 + function getUsage($refresh=false)
  60 + {
  61 + $check = true;
  62 + // check if we have a cached result
  63 + if (isset($_SESSION['DiskUsage']))
  64 + {
  65 + // we will only do the check every 5 minutes
  66 + if (time() - $_SESSION['DiskUsage']['time'] < 5 * 60)
  67 + {
  68 + $check = false;
  69 + $this->usage = $_SESSION['DiskUsage']['usage'];
  70 + }
  71 + }
  72 +
  73 + // we will only check if the result is not cached, or after 5 minutes
  74 + if ($check)
  75 + {
  76 + $result = ktutil::pexec($this->dfCmd);
  77 +
  78 + $result = $result['out'];
  79 + unset($result[0]);
  80 +
  81 + $usage=array();
  82 + foreach($result as $line)
  83 + {
  84 + preg_match('/(.*)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\%\s+(.*)/', $line, $matches);
  85 + list($line, $filesystem, $size, $used, $avail, $usedp, $mount) = $matches;
  86 +
  87 + if ($usedp >= 100 - $this->urgentPercent)
  88 + {
  89 + $colour = 'red';
  90 + }
  91 + elseif ($usedp >= 100 - $this->warningPercent)
  92 + {
  93 + $colour = 'orange';
  94 + }
  95 +
  96 + $usage[] = array(
  97 + 'filesystem'=>$filesystem,
  98 + 'size'=>KTUtil::filesizeToString($size),
  99 + 'used'=>KTUtil::filesizeToString($used),
  100 + 'available'=>KTUtil::filesizeToString($avail),
  101 + 'usage'=>$usedp . '%',
  102 + 'mounted'=>$mount,
  103 + 'colour'=>$colour
  104 + );
  105 + }
  106 +
  107 + $this->usage = $usage;
  108 +
  109 + $_SESSION['DiskUsage']['time'] = time();
  110 + $_SESSION['DiskUsage']['usage'] = $this->usage;
  111 + }
  112 + }
  113 +
  114 + function render()
  115 + {
  116 + $oTemplating =& KTTemplating::getSingleton();
  117 + $oTemplate = $oTemplating->loadTemplate('DiskUsage');
  118 +
  119 + $oRegistry =& KTPluginRegistry::getSingleton();
  120 + $oPlugin =& $oRegistry->getPlugin('ktcore.housekeeper.plugin');
  121 +
  122 + $dispatcherURL = $oPlugin->getURLPath('HouseKeeperDispatcher.php');
  123 +
  124 +
  125 + $aTemplateData = array(
  126 + 'context' => $this,
  127 + 'usages'=>$this->usage,
  128 + 'warnPercent'=>$this->warningPercent,
  129 + 'urgentPercent'=>$this->urgentPercent,
  130 + 'dispatcherURL'=>$dispatcherURL
  131 + );
  132 +
  133 + return $oTemplate->render($aTemplateData);
  134 + }
  135 +}
  136 +
  137 +
  138 +?>
... ...
plugins/housekeeper/FolderUsageDashlet.inc.php 0 โ†’ 100755
  1 +<?php
  2 +
  3 +/**
  4 + *
  5 + * Copyright (c) 2007 Jam Warehouse http://www.jamwarehouse.com
  6 + *
  7 + * This program is free software; you can redistribute it and/or modify
  8 + * it under the terms of the GNU General Public License as published by
  9 + * the Free Software Foundation; using version 2 of the License.
  10 + *
  11 + * This program is distributed in the hope that it will be useful,
  12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + * GNU General Public License for more details.
  15 + *
  16 + * You should have received a copy of the GNU General Public License
  17 + * along with this program; if not, write to the Free Software
  18 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19 + *
  20 + * -------------------------------------------------------------------------
  21 + *
  22 + * You can contact the copyright owner regarding licensing via the contact
  23 + * details that can be found on the KnowledgeTree web site:
  24 + *
  25 + * http://www.knowledgetree.com/
  26 + */
  27 +
  28 +class FolderUsageDashlet extends KTBaseDashlet
  29 +{
  30 + private $usage;
  31 +
  32 + function FolderUsageDashlet()
  33 + {
  34 + $this->sTitle = _kt('System Folder Usage');
  35 + $this->sClass = "ktInfo";
  36 + }
  37 +
  38 + function is_active($oUser)
  39 + {
  40 + return Permission::userIsSystemAdministrator();
  41 + }
  42 +
  43 + function scanPath($path,$pattern)
  44 + {
  45 + $files=0;
  46 + $filesize=0;
  47 +
  48 + if ($dh = opendir($path))
  49 + {
  50 + while (($file = readdir($dh)) !== false)
  51 + {
  52 + if (substr($file,0,1) == '.')
  53 + {
  54 + continue;
  55 + }
  56 +
  57 + $full = $path . '/' . $file;
  58 +
  59 + if (!is_readable($full) || !is_writable($full))
  60 + {
  61 + continue;
  62 + }
  63 +
  64 + if (is_dir($full))
  65 + {
  66 + $result = $this->scanPath($full,$pattern);
  67 + $files += $result['files'];
  68 + $filesize += $result['filesize'];
  69 + continue;
  70 + }
  71 + if ($pattern != '')
  72 + {
  73 + if (preg_match('/' . $pattern . '/', $file) === false)
  74 + {
  75 + continue;
  76 + }
  77 + }
  78 +
  79 + $files++;
  80 + $filesize += filesize($full);
  81 + }
  82 + closedir($dh);
  83 + }
  84 + return array('files'=>$files,'filesize'=>$filesize,'dir'=>$path);
  85 + }
  86 +
  87 + function getUsage()
  88 + {
  89 + $check = true;
  90 + // check if we have a cached result
  91 + if (isset($_SESSION['SystemFolderUsage']))
  92 + {
  93 + // we will only do the check every 5 minutes
  94 + if (time() - $_SESSION['SystemFolderUsage']['time'] < 5 * 60)
  95 + {
  96 + $check = false;
  97 + $this->usage = $_SESSION['SystemFolderUsage']['usage'];
  98 + }
  99 + }
  100 +
  101 + // we will only check if the result is not cached, or after 5 minutes
  102 + if ($check)
  103 + {
  104 + $usage = array();
  105 +
  106 + $oRegistry =& KTPluginRegistry::getSingleton();
  107 + $oPlugin =& $oRegistry->getPlugin('ktcore.housekeeper.plugin');
  108 +
  109 + $folders = $oPlugin->getDirectories();
  110 +
  111 + foreach($folders as $folder)
  112 + {
  113 + $directory = $folder['folder'];
  114 + $pattern = $folder['pattern'];
  115 + $canClean = $folder['canClean'];
  116 + $name = $folder['name'];
  117 +
  118 + $temp = $this->scanPath($directory,$pattern);
  119 +
  120 + $usage[] = array(
  121 + 'description'=>$name,
  122 + 'folder'=>$directory,
  123 + 'files'=>number_format($temp['files'],0,'.',','),
  124 + 'filesize'=>KTUtil::filesizeToString($temp['filesize']),
  125 + 'action'=>$i,
  126 + 'canClean'=>$canClean
  127 + );
  128 + $this->usage = $usage;
  129 + }
  130 +
  131 + $_SESSION['SystemFolderUsage']['time'] = time();
  132 + $_SESSION['SystemFolderUsage']['usage'] = $this->usage;
  133 + }
  134 + }
  135 +
  136 + function render()
  137 + {
  138 + $oTemplating =& KTTemplating::getSingleton();
  139 + $oTemplate = $oTemplating->loadTemplate('FolderUsage');
  140 +
  141 + $oRegistry =& KTPluginRegistry::getSingleton();
  142 + $oPlugin =& $oRegistry->getPlugin('ktcore.housekeeper.plugin');
  143 +
  144 + $dispatcherURL = $oPlugin->getURLPath('HouseKeeperDispatcher.php');
  145 +
  146 + $this->getUsage();
  147 +
  148 + $aTemplateData = array(
  149 + 'context' => $this,
  150 + 'usages'=>$this->usage,
  151 + 'dispatcherURL'=>$dispatcherURL
  152 + );
  153 +
  154 + return $oTemplate->render($aTemplateData);
  155 + }
  156 +}
  157 +
  158 +
  159 +?>
... ...
plugins/housekeeper/HouseKeeperDispatcher.php 0 โ†’ 100644
  1 +<?php
  2 +
  3 +session_start();
  4 +
  5 +require_once("../../config/dmsDefaults.php");
  6 +require_once(KT_LIB_DIR . "/templating/templating.inc.php");
  7 +require_once(KT_LIB_DIR . "/dispatcher.inc.php");
  8 +
  9 +class HouseKeeperDispatcher extends KTStandardDispatcher
  10 +{
  11 + function cleanDirectory($path, $pattern)
  12 + {
  13 + if (!is_readable($path))
  14 + {
  15 + return;
  16 + }
  17 + if ($dh = opendir($path))
  18 + {
  19 + while (($file = readdir($dh)) !== false)
  20 + {
  21 + if (substr($file,0,1) == '.')
  22 + {
  23 + continue;
  24 + }
  25 +
  26 + $full = $path . '/' . $file;
  27 + if (is_dir($full))
  28 + {
  29 + $this->cleanDirectory($full,$pattern);
  30 + if (is_writable($full))
  31 + {
  32 + @rmdir($full);
  33 + }
  34 + continue;
  35 + }
  36 +
  37 + if (!empty($pattern) && !preg_match('/' . $pattern . '/', $file))
  38 + {
  39 + continue;
  40 + }
  41 +
  42 + if (is_writable($full))
  43 + {
  44 + @unlink($full);
  45 + }
  46 +
  47 + }
  48 + closedir($dh);
  49 + }
  50 +
  51 + }
  52 +
  53 + function do_cleanup()
  54 + {
  55 + $folder = KTUtil::arrayGet($_REQUEST, 'folder');
  56 + if (is_null($folder))
  57 + {
  58 + exit(redirect(generateControllerLink('dashboard')));
  59 + }
  60 +
  61 + $oRegistry =& KTPluginRegistry::getSingleton();
  62 + $oPlugin =& $oRegistry->getPlugin('ktcore.housekeeper.plugin');
  63 +
  64 + // we must avoid doing anything to the documents folder at all costs!
  65 + $folder = $oPlugin->getDirectory($folder);
  66 + if (is_null($folder) || !$folder['canClean'])
  67 + {
  68 + exit(redirect(generateControllerLink('dashboard')));
  69 + }
  70 +
  71 + $this->cleanDirectory($folder['folder'], $folder['pattern']);
  72 +
  73 + $this->do_refreshFolderUsage();
  74 + }
  75 +
  76 + function do_refreshDiskUsage()
  77 + {
  78 + session_unregister('DiskUsage');
  79 + exit(redirect(generateControllerLink('dashboard')));
  80 + }
  81 +
  82 + function do_refreshFolderUsage()
  83 + {
  84 + session_unregister('SystemFolderUsage');
  85 + exit(redirect(generateControllerLink('dashboard')));
  86 + }
  87 +}
  88 +$oDispatcher = new HouseKeeperDispatcher();
  89 +$oDispatcher->dispatch();
  90 +
  91 +?>
0 92 \ No newline at end of file
... ...
plugins/housekeeper/HouseKeeperPlugin.php 0 โ†’ 100755
  1 +<?
  2 +
  3 +class HouseKeeperPlugin extends KTPlugin
  4 + {
  5 + var $autoRegister = true;
  6 + var $sNamespace = 'ktcore.housekeeper.plugin';
  7 +
  8 + var $folders = array();
  9 +
  10 + function HouseKeeperPlugin($sFilename = null)
  11 + {
  12 + parent::KTPlugin($sFilename);
  13 +
  14 + $this->sFriendlyName = _kt('Housekeeper');
  15 +
  16 + $config = KTConfig::getSingleton();
  17 + $tempDir = $config->get('urls/tmpDirectory');
  18 + $cacheDir = $config->get('cache/cacheDirectory');
  19 + $logDir = $config->get('urls/logDirectory');
  20 + $docsDir = $config->get('urls/documentRoot');
  21 + $luceneDir = $config->get('indexer/luceneDirectory');
  22 +
  23 + $systemDir = OS_UNIX?'/tmp':'c:/windows/temp';
  24 +
  25 + $this->folders = array(
  26 + array(
  27 + 'name'=>_kt('Smarty Cache'),
  28 + 'folder'=>$tempDir,
  29 + 'pattern'=>'^%%.*',
  30 + 'canClean'=>true
  31 + ),
  32 + array(
  33 + 'name'=>_kt('KnowledgeTree Cache'),
  34 + 'folder'=>$cacheDir,
  35 + 'pattern'=>'',
  36 + 'canClean'=>true
  37 + ),
  38 + array(
  39 + 'name'=>_kt('KnowledgeTree Logs'),
  40 + 'folder'=>$logDir,
  41 + 'pattern'=>'.+\.txt$',
  42 + 'canClean'=>true
  43 + ),
  44 + array(
  45 + 'name'=>_kt('System Temporary Folder'),
  46 + 'folder'=>$systemDir,
  47 + 'pattern'=>'(sess_.+)?(.+\.log$)?',
  48 + 'canClean'=>true
  49 + ),
  50 + array(
  51 + 'name'=>_kt('KnowledgeTree Documents'),
  52 + 'folder'=>$docsDir,
  53 + 'pattern'=>'',
  54 + 'canClean'=>false
  55 + ),
  56 + array(
  57 + 'name'=>_kt('KnowledgeTree Lucene Indexes'),
  58 + 'folder'=>$luceneDir,
  59 + 'pattern'=>'',
  60 + 'canClean'=>false
  61 + ),
  62 + );
  63 + }
  64 +
  65 + function getDirectories()
  66 + {
  67 + return $this->folders;
  68 + }
  69 +
  70 + function getDirectory($folder)
  71 + {
  72 + foreach($this->folders as $dir)
  73 + {
  74 + if ($dir['folder'] == $folder)
  75 + {
  76 + return $dir;
  77 + }
  78 + }
  79 + return null;
  80 + }
  81 +
  82 + function setup()
  83 + {
  84 + $this->registerDashlet('DiskUsageDashlet', 'ktcore.diskusage.dashlet', 'DiskUsageDashlet.inc.php');
  85 + $this->registerDashlet('FolderUsageDashlet', 'ktcore.folderusage.dashlet', 'FolderUsageDashlet.inc.php');
  86 +
  87 + $oTemplating =& KTTemplating::getSingleton();
  88 + $oTemplating->addLocation('housekeeper', '/plugins/housekeeper/templates');
  89 + }
  90 +
  91 +}
  92 +
  93 +$oPluginRegistry =& KTPluginRegistry::getSingleton();
  94 +$oPluginRegistry->registerPlugin('HouseKeeperPlugin', 'ktcore.housekeeper.plugin', __FILE__);
  95 +
  96 +?>
0 97 \ No newline at end of file
... ...
plugins/housekeeper/templates/DiskUsage.smarty 0 โ†’ 100755
  1 +<table border=0 cellspacing="0" cellpadding="0" width="100%">
  2 + <tr>
  3 + <td width="250"><B>{i18n}Disk Mount{/i18n}</td>
  4 + <td align="center"><B>{i18n}Size{/i18n}</td>
  5 + <td align="center"><B>{i18n}Used{/i18n}</td>
  6 + <td align="center"><B>{i18n}Available{/i18n}</td>
  7 + <td align="center"><B>{i18n}Usage{/i18n}</td>
  8 +
  9 + </tr>
  10 + <tr><td colspan=5><hr></tr>
  11 +{assign var=orange value=0}
  12 +{assign var=red value=0}
  13 +{section name=usage loop=$usages}
  14 +
  15 +{if $usages[usage].colour == 'orange'}
  16 + {assign var=orange value=1}
  17 +{/if}
  18 +{if $usages[usage].colour == 'red'}
  19 + {assign var=red value=1}
  20 +{/if}
  21 + <tr bgcolor="{$usages[usage].colour}">
  22 + <td>{$usages[usage].mounted}</td>
  23 + <td align=right>{$usages[usage].size}&nbsp;&nbsp;</td>
  24 + <td align=right>{$usages[usage].used}&nbsp;&nbsp;</td>
  25 + <td align=right>{$usages[usage].available}&nbsp;&nbsp;</td>
  26 + <td align=right>{$usages[usage].usage}&nbsp;&nbsp;</td>
  27 +
  28 + </tr>
  29 +{/section}
  30 + <tr><td colspan=5><hr></tr>
  31 +</table>
  32 +<table width="100%">
  33 +<tr>
  34 +{if $orange==1 || $red==1}
  35 +<td ><nobr><b>{i18n}Free Space{/i18n}</td>
  36 +{/if}
  37 +{if $orange==1}
  38 +<td bgcolor=orange><nobr> &lt; {$warnPercent} %</td>
  39 +{/if}
  40 +{if $red==1}
  41 +<td bgcolor=red><nobr> &lt; {$urgentPercent} %</td>
  42 +{/if}
  43 +<td width="100%" align="right"><a href="{$dispatcherURL}?action=refreshDiskUsage">{i18n}refresh{/i18n}</a></td>
  44 +</table>
0 45 \ No newline at end of file
... ...
plugins/housekeeper/templates/FolderUsage.smarty 0 โ†’ 100755
  1 +{literal}
  2 +<script>
  3 +function cleanupFolder(path)
  4 +{
  5 + if (confirm('{/literal}{i18n}Are you sure you want to clear{/i18n}{literal} ' + path + '?'))
  6 + {
  7 + document.location = "{/literal}{$dispatcherURL}{literal}?action=cleanup&folder=" + path;
  8 + }
  9 +}
  10 +</script>
  11 +{/literal}
  12 +
  13 +<table border=0 cellspacing="1" cellpadding="1" width="100%">
  14 + <tr>
  15 + <td ><B>{i18n}Description{/i18n}</td>
  16 + <td width="80" align="center"><B># {i18n}Files{/i18n}</td>
  17 + <td width="130" align="center"><B>{i18n}Space Used{/i18n}</td>
  18 + <td width="80" align="center"><B>{i18n}Action{/i18n}</td>
  19 + </tr>
  20 + <tr><td colspan=4><hr></tr>
  21 +{section name=usage loop=$usages}
  22 +{if $usages[usage].folder!= ''}
  23 + <tr>
  24 + <td><i>{$usages[usage].description}</i><br>({$usages[usage].folder}) </td>
  25 + <td align=right>{$usages[usage].files}&nbsp;&nbsp;</td>
  26 + <td align=right>{$usages[usage].filesize}&nbsp;&nbsp;</td>
  27 + <td align="center">{if $usages[usage].canClean}<a href="javascript:cleanupFolder('{$usages[usage].folder}')">{i18n}cleanup{/i18n}</a>{else}{i18n}N/A{/i18n}{/if}&nbsp;&nbsp;</td>
  28 + </tr>
  29 +{/if}
  30 +{/section}
  31 + <tr><td colspan=4><hr></tr>
  32 +</table>
  33 +<table width="100%">
  34 +<tr width="100%">
  35 +<td align="right"><a href="{$dispatcherURL}?action=refreshFolderUsage">{i18n}refresh{/i18n}</a></td>
  36 +</table>
... ...