1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
<?php
/***********************************************
* File : topcollector.php
* Project : Z-Push
* Descr : available everywhere to collect
* data which could be displayed in z-push-top
* the 'persistent' flag should be used with care, so
* there is not too much information
*
* Created : 20.10.2011
*
* Copyright 2007 - 2011 Zarafa Deutschland GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation with the following additional
* term according to sec. 7:
*
* According to sec. 7 of the GNU Affero General Public License, version 3,
* the terms of the AGPL are supplemented with the following terms:
*
* "Zarafa" is a registered trademark of Zarafa B.V.
* "Z-Push" is a registered trademark of Zarafa Deutschland GmbH
* The licensing of the Program under the AGPL does not imply a trademark license.
* Therefore any rights, title and interest in our trademarks remain entirely with us.
*
* However, if you propagate an unmodified version of the Program you are
* allowed to use the term "Z-Push" to indicate that you distribute the Program.
* Furthermore you may use our trademarks where it is necessary to indicate
* the intended purpose of a product or service provided you use it in accordance
* with honest practices in industrial or commercial matters.
* If you want to propagate modified versions of the Program under the name "Z-Push",
* you may only do so if you have a written permission by Zarafa Deutschland GmbH
* (to acquire a permission please contact Zarafa at trademark@zarafa.com).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Consult LICENSE file for details
************************************************/
class TopCollector extends InterProcessData {
const ENABLEDAT = 2;
const TOPDATA = 3;
protected $preserved;
protected $latest;
/**
* Constructor
*
* @access public
*/
public function TopCollector() {
// initialize super parameters
$this->allocate = 2097152; // 2 MB
$this->type = 20;
parent::__construct();
// initialize params
$this->InitializeParams();
$this->preserved = array();
// static vars come from the parent class
$this->latest = array( "pid" => self::$pid,
"ip" => Request::GetRemoteAddr(),
"user" => self::$user,
"start" => self::$start,
"devtype" => Request::GetDeviceType(),
"devid" => self::$devid,
"devagent" => Request::GetUserAgent(),
"command" => Request::GetCommandCode(),
"ended" => 0,
"push" => false,
);
$this->AnnounceInformation("initializing");
}
/**
* Destructor
* indicates that the process is shutting down
*
* @access public
*/
public function __destruct() {
$this->AnnounceInformation("terminated", false, true);
}
/**
* Advices all other processes that they should start/stop
* collecting data. The data saved is a timestamp. It has to be
* reactivated every couple of seconds
*
* @param boolean $stop (opt) default false (do collect)
*
* @access public
* @return boolean indicating if it was set to collect before
*/
public function CollectData($stop = false) {
$wasEnabled = false;
// exclusive block
if ($this->blockMutex()) {
$wasEnabled = ($this->hasData(self::ENABLEDAT)) ? $this->getData(self::ENABLEDAT) : false;
$time = time();
if ($stop === true) $time = 0;
if (! $this->setData($time, self::ENABLEDAT))
return false;
$this->releaseMutex();
}
// end exclusive block
return $wasEnabled;
}
/**
* Announces a string to the TopCollector
*
* @param string $info
* @param boolean $preserve info should be displayed when process terminates
* @param boolean $terminating indicates if the process is terminating
*
* @access public
* @return boolean
*/
public function AnnounceInformation($addinfo, $preserve = false, $terminating = false) {
$this->latest["addinfo"] = $addinfo;
$this->latest["update"] = time();
if ($terminating) {
$this->latest["ended"] = time();
foreach ($this->preserved as $p)
$this->latest["addinfo"] .= " : ".$p;
}
if ($preserve)
$this->preserved[] = $addinfo;
// exclusive block
if ($this->blockMutex()) {
if ($this->isEnabled()) {
$topdata = ($this->hasData(self::TOPDATA)) ? $this->getData(self::TOPDATA): array();
$this->checkArrayStructure($topdata);
// update
$topdata[self::$devid][self::$user][self::$pid] = $this->latest;
$ok = $this->setData($topdata, self::TOPDATA);
}
$this->releaseMutex();
}
// end exclusive block
if ($this->isEnabled() === true && !$ok) {
ZLog::Write(LOGLEVEL_WARN, "TopCollector::AnnounceInformation(): could not write to shared memory. Z-Push top will not display this data.");
return false;
}
return true;
}
/**
* Returns all available top data
*
* @access public
* @return array
*/
public function ReadLatest() {
$topdata = array();
// exclusive block
if ($this->blockMutex()) {
$topdata = ($this->hasData(self::TOPDATA)) ? $this->getData(self::TOPDATA) : array();
$this->releaseMutex();
}
// end exclusive block
return $topdata;
}
/**
* Cleans up data collected so far
*
* @param boolean $all (optional) if set all data independently from the age is removed
*
* @access public
* @return boolean status
*/
public function ClearLatest($all = false) {
// it's ok when doing this every 10 sec
if ($all == false && time() % 10 != 0 )
return true;
$stat = false;
// exclusive block
if ($this->blockMutex()) {
if ($all == true) {
$topdata = array();
}
else {
$topdata = ($this->hasData(self::TOPDATA)) ? $this->getData(self::TOPDATA) : array();
$toClear = array();
foreach ($topdata as $devid=>$users) {
foreach ($users as $user=>$pids) {
foreach ($pids as $pid=>$line) {
// remove everything which terminated for 20 secs or is not updated for more than 120 secs
if (($line["ended"] != 0 && time() - $line["ended"] > 20) ||
time() - $line["update"] > 120) {
$toClear[] = array($devid, $user, $pid);
}
}
}
}
foreach ($toClear as $tc)
unset($topdata[$tc[0]][$tc[1]][$tc[2]]);
}
$stat = $this->setData($topdata, self::TOPDATA);
$this->releaseMutex();
}
// end exclusive block
return $stat;
}
/**
* Sets a different UserAgent for this connection
*
* @param string $agent
*
* @access public
* @return boolean
*/
public function SetUserAgent($agent) {
$this->latest["devagent"] = $agent;
}
/**
* Marks this process as push connection
*
* @param string $agent
*
* @access public
* @return boolean
*/
public function SetAsPushConnection() {
$this->latest["push"] = true;
}
/**
* Indicates if top data should be saved or not
* Returns true for 10 seconds after the latest CollectData()
* SHOULD only be called with locked mutex!
*
* @access private
* @return boolean
*/
private function isEnabled() {
$isEnabled = ($this->hasData(self::ENABLEDAT)) ? $this->getData(self::ENABLEDAT) : false;
return ($isEnabled !== false && ($isEnabled +300) > time());
}
/**
* Builds an array structure for the top data
*
* @param array $topdata reference to the topdata array
*
* @access private
* @return
*/
private function checkArrayStructure(&$topdata) {
if (!isset($topdata) || !is_array($topdata))
$topdata = array();
if (!isset($topdata[self::$devid]))
$topdata[self::$devid] = array();
if (!isset($topdata[self::$devid][self::$user]))
$topdata[self::$devid][self::$user] = array();
if (!isset($topdata[self::$devid][self::$user][self::$pid]))
$topdata[self::$devid][self::$user][self::$pid] = array();
}
}
?>
|