| Total Complexity | 159 |
| Total Lines | 740 |
| Duplicated Lines | 0 % |
| Changes | 14 | ||
| Bugs | 1 | Features | 0 |
Complex classes like GSyncTop often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use GSyncTop, and based on these observations, apply Extract Interface, too.
| 1 | #!/usr/bin/env php |
||
| 74 | class GSyncTop { |
||
| 75 | // show options |
||
| 76 | public const SHOW_DEFAULT = 0; |
||
| 77 | public const SHOW_ACTIVE_ONLY = 1; |
||
| 78 | public const SHOW_UNKNOWN_ONLY = 2; |
||
| 79 | public const SHOW_TERM_DEFAULT_TIME = 5; // 5 secs |
||
| 80 | |||
| 81 | private $topCollector; |
||
| 82 | private $starttime; |
||
| 83 | private $currenttime; |
||
| 84 | private $action; |
||
| 85 | private $filter; |
||
| 86 | private $status; |
||
| 87 | private $statusexpire; |
||
| 88 | private $helpexpire; |
||
| 89 | private $doingTail; |
||
| 90 | private $wide; |
||
| 91 | private $wasEnabled; |
||
| 92 | private $terminate; |
||
| 93 | private $scrSize; |
||
| 94 | private $pingInterval; |
||
| 95 | private $showPush; |
||
| 96 | private $showOption; |
||
| 97 | private $showTermSec; |
||
| 98 | |||
| 99 | private $linesActive = []; |
||
| 100 | private $linesOpen = []; |
||
| 101 | private $linesUnknown = []; |
||
| 102 | private $linesTerm = []; |
||
| 103 | private $pushConn = 0; |
||
| 104 | private $activeConn = []; |
||
| 105 | private $activeHosts = []; |
||
| 106 | private $activeUsers = []; |
||
| 107 | private $activeDevices = []; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Constructor. |
||
| 111 | */ |
||
| 112 | public function __construct() { |
||
| 113 | $this->starttime = time(); |
||
| 114 | $this->currenttime = time(); |
||
| 115 | $this->action = ""; |
||
| 116 | $this->filter = false; |
||
| 117 | $this->status = false; |
||
| 118 | $this->statusexpire = 0; |
||
| 119 | $this->helpexpire = 0; |
||
| 120 | $this->doingTail = false; |
||
| 121 | $this->wide = false; |
||
| 122 | $this->terminate = false; |
||
| 123 | $this->showPush = true; |
||
| 124 | $this->showOption = self::SHOW_DEFAULT; |
||
| 125 | $this->showTermSec = self::SHOW_TERM_DEFAULT_TIME; |
||
| 126 | $this->scrSize = ['width' => 80, 'height' => 24]; |
||
| 127 | $this->pingInterval = (defined('PING_INTERVAL') && PING_INTERVAL > 0) ? PING_INTERVAL : 12; |
||
| 128 | |||
| 129 | // get a TopCollector |
||
| 130 | $this->topCollector = new TopCollector(); |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Requests data from the running grommunio-sync processes. |
||
| 135 | */ |
||
| 136 | private function initialize() { |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Main loop of grommunio-sync-top |
||
| 149 | * Runs until termination is requested. |
||
| 150 | */ |
||
| 151 | public function run() { |
||
| 152 | $this->initialize(); |
||
| 153 | |||
| 154 | do { |
||
| 155 | $this->currenttime = time(); |
||
| 156 | |||
| 157 | // see if shared memory is active |
||
| 158 | if (!$this->IsAvailable()) { |
||
| 159 | $this->terminate = true; |
||
| 160 | } |
||
| 161 | |||
| 162 | // active processes should continue sending data |
||
| 163 | $this->topCollector->CollectData(); |
||
| 164 | |||
| 165 | // get and process data from processes |
||
| 166 | $this->topCollector->ClearLatest(); |
||
| 167 | $topdata = $this->topCollector->ReadLatest(); |
||
| 168 | $this->processData($topdata); |
||
| 169 | |||
| 170 | // check if screen size changed |
||
| 171 | $s = $this->scrGetSize(); |
||
| 172 | if ($this->scrSize['width'] != $s['width']) { |
||
| 173 | if ($s['width'] > 180) { |
||
| 174 | $this->wide = true; |
||
| 175 | } |
||
| 176 | else { |
||
| 177 | $this->wide = false; |
||
| 178 | } |
||
| 179 | } |
||
| 180 | $this->scrSize = $s; |
||
| 181 | |||
| 182 | // print overview |
||
| 183 | $this->scrOverview(); |
||
| 184 | |||
| 185 | // wait for user input |
||
| 186 | $this->readLineProcess(); |
||
| 187 | } |
||
| 188 | while ($this->terminate !== true); |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Indicates if TopCollector is available collecting data. |
||
| 193 | * |
||
| 194 | * @return bool |
||
| 195 | */ |
||
| 196 | public function IsAvailable() { |
||
| 197 | if (defined('TOPCOLLECTOR_DISABLED') && constant('TOPCOLLECTOR_DISABLED') === true) { |
||
| 198 | return false; |
||
| 199 | } |
||
| 200 | |||
| 201 | return $this->topCollector->IsActive(); |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Processes data written by the running processes. |
||
| 206 | * |
||
| 207 | * @param array $data |
||
| 208 | */ |
||
| 209 | private function processData($data) { |
||
| 210 | $this->linesActive = []; |
||
| 211 | $this->linesOpen = []; |
||
| 212 | $this->linesUnknown = []; |
||
| 213 | $this->linesTerm = []; |
||
| 214 | $this->pushConn = 0; |
||
| 215 | $this->activeConn = []; |
||
| 216 | $this->activeHosts = []; |
||
| 217 | $this->activeUsers = []; |
||
| 218 | $this->activeDevices = []; |
||
| 219 | |||
| 220 | if (!is_array($data)) { |
||
|
|
|||
| 221 | return; |
||
| 222 | } |
||
| 223 | |||
| 224 | foreach ($data as $devid => $users) { |
||
| 225 | foreach ($users as $user => $pids) { |
||
| 226 | foreach ($pids as $pid => $line) { |
||
| 227 | if (!is_array($line)) { |
||
| 228 | continue; |
||
| 229 | } |
||
| 230 | |||
| 231 | $line['command'] = Utils::GetCommandFromCode($line['command']); |
||
| 232 | |||
| 233 | if ($line["ended"] == 0) { |
||
| 234 | $this->activeDevices[$devid] = 1; |
||
| 235 | $this->activeUsers[$user] = 1; |
||
| 236 | $this->activeConn[$pid] = 1; |
||
| 237 | $this->activeHosts[$line['ip']] = 1; |
||
| 238 | |||
| 239 | $line["time"] = $this->currenttime - $line['start']; |
||
| 240 | if ($line['push'] === true) { |
||
| 241 | ++$this->pushConn; |
||
| 242 | } |
||
| 243 | |||
| 244 | // ignore push connections |
||
| 245 | if ($line['push'] === true && !$this->showPush) { |
||
| 246 | continue; |
||
| 247 | } |
||
| 248 | |||
| 249 | if ($this->filter !== false) { |
||
| 250 | $f = $this->filter; |
||
| 251 | if (!($line["pid"] == $f || $line["ip"] == $f || strtolower($line['command']) == strtolower($f) || preg_match("/.*?{$f}.*?/i", (string) $line['user']) || |
||
| 252 | preg_match("/.*?{$f}.*?/i", (string) $line['devagent']) || preg_match("/.*?{$f}.*?/i", (string) $line['devid']) || preg_match("/.*?{$f}.*?/i", (string) $line['addinfo']))) { |
||
| 253 | continue; |
||
| 254 | } |
||
| 255 | } |
||
| 256 | |||
| 257 | $lastUpdate = $this->currenttime - $line["update"]; |
||
| 258 | if ($this->currenttime - $line["update"] < 2) { |
||
| 259 | $this->linesActive[$line["update"] . $line["pid"]] = $line; |
||
| 260 | } |
||
| 261 | elseif (($line['push'] === true && $lastUpdate > ($this->pingInterval + 2)) || ($line['push'] !== true && $lastUpdate > 4)) { |
||
| 262 | $this->linesUnknown[$line["update"] . $line["pid"]] = $line; |
||
| 263 | } |
||
| 264 | else { |
||
| 265 | $this->linesOpen[$line["update"] . $line["pid"]] = $line; |
||
| 266 | } |
||
| 267 | } |
||
| 268 | else { |
||
| 269 | // do not show terminated + expired connections |
||
| 270 | if ($this->currenttime > $line['ended'] + $this->showTermSec) { |
||
| 271 | continue; |
||
| 272 | } |
||
| 273 | |||
| 274 | if ($this->filter !== false) { |
||
| 275 | $f = $this->filter; |
||
| 276 | if ( |
||
| 277 | !( |
||
| 278 | $line['pid'] == $f || |
||
| 279 | $line['ip'] == $f || |
||
| 280 | strtolower($line['command']) == strtolower($f) || |
||
| 281 | preg_match("/.*?{$f}.*?/i", (string) $line['user']) || |
||
| 282 | preg_match("/.*?{$f}.*?/i", (string) $line['devagent']) || |
||
| 283 | preg_match("/.*?{$f}.*?/i", (string) $line['devid']) || |
||
| 284 | preg_match("/.*?{$f}.*?/i", (string) $line['addinfo']) |
||
| 285 | )) { |
||
| 286 | continue; |
||
| 287 | } |
||
| 288 | } |
||
| 289 | |||
| 290 | $line['time'] = $line['ended'] - $line['start']; |
||
| 291 | $this->linesTerm[$line['update'] . $line['pid']] = $line; |
||
| 292 | } |
||
| 293 | } |
||
| 294 | } |
||
| 295 | } |
||
| 296 | |||
| 297 | // sort by execution time |
||
| 298 | krsort($this->linesActive); |
||
| 299 | krsort($this->linesOpen); |
||
| 300 | krsort($this->linesUnknown); |
||
| 301 | krsort($this->linesTerm); |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Prints data to the terminal. |
||
| 306 | */ |
||
| 307 | private function scrOverview() { |
||
| 308 | $linesAvail = $this->scrSize['height'] - 7; |
||
| 309 | $lc = 1; |
||
| 310 | echo "\e[?25l\e[1;1H"; |
||
| 311 | $this->scrPrintAt($lc, 0, sprintf( |
||
| 312 | "grommunio-sync-top live stats (%s, Gromox %s) %s\n", |
||
| 313 | $this->getVersion(), |
||
| 314 | phpversion("mapi"), |
||
| 315 | date("Y-m-d H:i:s") |
||
| 316 | )); |
||
| 317 | ++$lc; |
||
| 318 | |||
| 319 | $this->scrPrintAt($lc, 0, sprintf( |
||
| 320 | "Conn: \e[1m%4d\e[0m open, \e[1m%4d\e[0m push; " . |
||
| 321 | "\e[1m%4d\e[0m users, \e[1m%4d\e[0m devices, \e[1m%4d\e[0m hosts\n", |
||
| 322 | count($this->activeConn), |
||
| 323 | $this->pushConn, |
||
| 324 | count($this->activeUsers), |
||
| 325 | count($this->activeDevices), |
||
| 326 | count($this->activeHosts) |
||
| 327 | )); |
||
| 328 | ++$lc; |
||
| 329 | |||
| 330 | // remove old status |
||
| 331 | if ($this->statusexpire < $this->currenttime) { |
||
| 332 | $this->status = false; |
||
| 333 | } |
||
| 334 | |||
| 335 | // show request information and help command |
||
| 336 | if ($this->starttime + 6 > $this->currenttime) { |
||
| 337 | $this->status = sprintf("Requesting information (takes up to %dsecs)", $this->pingInterval) . str_repeat(".", $this->currenttime - $this->starttime) . " type \033[01;31mh\033[00;31m or \033[01;31mhelp\033[00;31m for usage instructions"; |
||
| 338 | $this->statusexpire = $this->currenttime + 1; |
||
| 339 | } |
||
| 340 | |||
| 341 | $str = ""; |
||
| 342 | if (!$this->showPush) { |
||
| 343 | $str .= "\033[00;32mPush: \033[01;32mNo\033[0m "; |
||
| 344 | } |
||
| 345 | |||
| 346 | if ($this->showOption == self::SHOW_ACTIVE_ONLY) { |
||
| 347 | $str .= "\033[01;32mActive only\033[0m "; |
||
| 348 | } |
||
| 349 | |||
| 350 | if ($this->showOption == self::SHOW_UNKNOWN_ONLY) { |
||
| 351 | $str .= "\033[01;32mUnknown only\033[0m "; |
||
| 352 | } |
||
| 353 | |||
| 354 | if ($this->showTermSec != self::SHOW_TERM_DEFAULT_TIME) { |
||
| 355 | $str .= "\033[01;32mTerminated: " . $this->showTermSec . "s\033[0m "; |
||
| 356 | } |
||
| 357 | |||
| 358 | if ($this->filter !== false || ($this->status !== false && $this->statusexpire > $this->currenttime)) { |
||
| 359 | // print filter in green |
||
| 360 | if ($this->filter !== false) { |
||
| 361 | $str .= "\033[00;32mFilter: \033[01;32m{$this->filter}\033[0m "; |
||
| 362 | } |
||
| 363 | // print status in red |
||
| 364 | if ($this->status !== false) { |
||
| 365 | $str .= "\033[00;31m{$this->status}\033[0m"; |
||
| 366 | } |
||
| 367 | } |
||
| 368 | $this->scrPrintAt($lc, 0, "Action: \033[01m" . $this->action . "\033[0m\n"); |
||
| 369 | ++$lc; |
||
| 370 | $this->scrPrintAt($lc, 0, $str . "\n"); |
||
| 371 | ++$lc; |
||
| 372 | $header = $this->getLine(['pid' => 'PID', 'ip' => 'ADDRESS', 'user' => 'USER', 'command' => 'COMMAND', 'time' => 'TIME', 'devagent' => 'AGENT', 'devid' => 'DEVID', 'addinfo' => 'Additional Information', 'asversion' => 'EAS']); |
||
| 373 | $this->scrPrintAt($lc, 0, "\033[7m" . $header . str_repeat(" ", $this->scrSize['width'] - ($this->scrSize['width'] > strlen($header) ? strlen($header) : 0)) . "\033[0m\n"); |
||
| 374 | ++$lc; |
||
| 375 | |||
| 376 | if ($linesAvail < 1) { |
||
| 377 | echo "Terminal too small, no room to display any useful information."; |
||
| 378 | ++$lc; |
||
| 379 | |||
| 380 | return; |
||
| 381 | } |
||
| 382 | |||
| 383 | // print help text if requested |
||
| 384 | $help = false; |
||
| 385 | if ($this->helpexpire > $this->currenttime) { |
||
| 386 | $help = $this->scrHelp(); |
||
| 387 | $linesAvail -= (count($help) + 1); |
||
| 388 | } |
||
| 389 | |||
| 390 | $toPrintActive = $linesAvail; |
||
| 391 | $toPrintOpen = $linesAvail; |
||
| 392 | $toPrintUnknown = $linesAvail; |
||
| 393 | $toPrintTerm = $linesAvail; |
||
| 394 | |||
| 395 | // default view: show all unknown, no terminated and half active+open |
||
| 396 | if (count($this->linesActive) + count($this->linesOpen) + count($this->linesUnknown) > $linesAvail) { |
||
| 397 | $toPrintUnknown = count($this->linesUnknown); |
||
| 398 | $toPrintActive = count($this->linesActive); |
||
| 399 | $toPrintOpen = $linesAvail - $toPrintUnknown - $toPrintActive; |
||
| 400 | $toPrintTerm = 0; |
||
| 401 | } |
||
| 402 | |||
| 403 | if ($this->showOption == self::SHOW_ACTIVE_ONLY) { |
||
| 404 | $toPrintActive = $linesAvail; |
||
| 405 | $toPrintOpen = 0; |
||
| 406 | $toPrintUnknown = 0; |
||
| 407 | $toPrintTerm = 0; |
||
| 408 | } |
||
| 409 | |||
| 410 | if ($this->showOption == self::SHOW_UNKNOWN_ONLY) { |
||
| 411 | $toPrintActive = 0; |
||
| 412 | $toPrintOpen = 0; |
||
| 413 | $toPrintUnknown = $linesAvail; |
||
| 414 | $toPrintTerm = 0; |
||
| 415 | } |
||
| 416 | |||
| 417 | $linesprinted = 0; |
||
| 418 | foreach ($this->linesActive as $time => $l) { |
||
| 419 | if ($linesprinted >= $toPrintActive) { |
||
| 420 | break; |
||
| 421 | } |
||
| 422 | |||
| 423 | $this->scrPrintAt($lc, 0, "\033[01m" . $this->getLine($l) . "\033[0m\n"); |
||
| 424 | ++$lc; |
||
| 425 | ++$linesprinted; |
||
| 426 | } |
||
| 427 | |||
| 428 | $linesprinted = 0; |
||
| 429 | foreach ($this->linesOpen as $time => $l) { |
||
| 430 | if ($linesprinted >= $toPrintOpen) { |
||
| 431 | break; |
||
| 432 | } |
||
| 433 | |||
| 434 | $this->scrPrintAt($lc, 0, $this->getLine($l) . "\n"); |
||
| 435 | ++$lc; |
||
| 436 | ++$linesprinted; |
||
| 437 | } |
||
| 438 | |||
| 439 | $linesprinted = 0; |
||
| 440 | foreach ($this->linesUnknown as $time => $l) { |
||
| 441 | if ($linesprinted >= $toPrintUnknown) { |
||
| 442 | break; |
||
| 443 | } |
||
| 444 | $time = intval($time); |
||
| 445 | $color = "0;31m"; |
||
| 446 | if (!isset($l['start'])) { |
||
| 447 | $l['start'] = $time; |
||
| 448 | } |
||
| 449 | if ((!isset($l['push']) || $l['push'] == false) && $time - $l["start"] > 30) { |
||
| 450 | $color = "1;31m"; |
||
| 451 | } |
||
| 452 | $this->scrPrintAt($lc, 0, "\033[0" . $color . $this->getLine($l) . "\033[0m\n"); |
||
| 453 | ++$lc; |
||
| 454 | ++$linesprinted; |
||
| 455 | } |
||
| 456 | |||
| 457 | if ($toPrintTerm > 0) { |
||
| 458 | $toPrintTerm = $linesAvail - $lc + 5; |
||
| 459 | } |
||
| 460 | |||
| 461 | $linesprinted = 0; |
||
| 462 | foreach ($this->linesTerm as $time => $l) { |
||
| 463 | if ($linesprinted >= $toPrintTerm) { |
||
| 464 | break; |
||
| 465 | } |
||
| 466 | |||
| 467 | $this->scrPrintAt($lc, 0, "\033[01;30m" . $this->getLine($l) . "\033[0m\n"); |
||
| 468 | ++$lc; |
||
| 469 | ++$linesprinted; |
||
| 470 | } |
||
| 471 | |||
| 472 | // add the lines used when displaying the help text |
||
| 473 | if ($help !== false) { |
||
| 474 | while ($lc < $linesAvail + 7) { |
||
| 475 | $this->scrPrintAt($lc, 0, "\033[K\n"); |
||
| 476 | ++$lc; |
||
| 477 | } |
||
| 478 | if ($linesAvail < 1) { |
||
| 479 | $this->scrPrintAt($lc, 0, "Can't display help text, terminal has not enough lines. Use -h or --help to see help information. \n"); |
||
| 480 | ++$lc; |
||
| 481 | } |
||
| 482 | else { |
||
| 483 | foreach ($help as $h) { |
||
| 484 | $this->scrPrintAt($lc, 0, $h . "\n"); |
||
| 485 | ++$lc; |
||
| 486 | } |
||
| 487 | } |
||
| 488 | } |
||
| 489 | $this->scrPrintAt($lc, 0, "\033[K\n"); |
||
| 490 | ++$lc; |
||
| 491 | $this->scrPrintAt($lc, 0, "Colorscheme: \033[01mActive \033[0mOpen \033[01;31mUnknown \033[01;30mTerminated\033[0m"); |
||
| 492 | /* Clear rest of area */ |
||
| 493 | echo "\e[J"; |
||
| 494 | /* Reposition cursor to Action: line */ |
||
| 495 | printf("\e[3;%dH\e[?25h", 9 + strlen($this->action)); |
||
| 496 | } |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Waits for a keystroke and processes the requested command. |
||
| 500 | */ |
||
| 501 | private function readLineProcess() { |
||
| 502 | $ans = explode("^^", (string) `bash -c "read -n 1 -t 1 ANS ; echo \\\$?^^\\\$ANS;"`); |
||
| 503 | |||
| 504 | if ($ans[0] < 128) { |
||
| 505 | if (isset($ans[1]) && bin2hex(trim($ans[1])) == "7f") { |
||
| 506 | $this->action = substr($this->action, 0, -1); |
||
| 507 | } |
||
| 508 | |||
| 509 | if (isset($ans[1]) && $ans[1] != "") { |
||
| 510 | $this->action .= trim((string) preg_replace("/[^A-Za-z0-9:]/", "", $ans[1])); |
||
| 511 | } |
||
| 512 | |||
| 513 | if (bin2hex($ans[0]) == "30" && bin2hex($ans[1]) == "0a") { |
||
| 514 | $cmds = explode(':', $this->action); |
||
| 515 | if ($cmds[0] == "quit" || $cmds[0] == "q" || (isset($cmds[1]) && $cmds[0] == "" && $cmds[1] == "q")) { |
||
| 516 | $this->topCollector->CollectData(true); |
||
| 517 | $this->topCollector->ClearLatest(true); |
||
| 518 | |||
| 519 | $this->terminate = true; |
||
| 520 | } |
||
| 521 | elseif ($cmds[0] == "clear") { |
||
| 522 | $this->topCollector->ClearLatest(true); |
||
| 523 | $this->topCollector->CollectData(true); |
||
| 524 | $this->topCollector->ReInitIPC(); |
||
| 525 | } |
||
| 526 | elseif ($cmds[0] == "filter" || $cmds[0] == "f") { |
||
| 527 | if (!isset($cmds[1]) || $cmds[1] == "") { |
||
| 528 | $this->filter = false; |
||
| 529 | $this->status = "No filter"; |
||
| 530 | $this->statusexpire = $this->currenttime + 5; |
||
| 531 | } |
||
| 532 | else { |
||
| 533 | $this->filter = $cmds[1]; |
||
| 534 | $this->status = false; |
||
| 535 | } |
||
| 536 | } |
||
| 537 | elseif ($cmds[0] == "option" || $cmds[0] == "o") { |
||
| 538 | if (!isset($cmds[1]) || $cmds[1] == "") { |
||
| 539 | $this->status = "Option value needs to be specified. See 'help' or 'h' for instructions"; |
||
| 540 | $this->statusexpire = $this->currenttime + 5; |
||
| 541 | } |
||
| 542 | elseif ($cmds[1] == "p" || $cmds[1] == "push" || $cmds[1] == "ping") { |
||
| 543 | $this->showPush = !$this->showPush; |
||
| 544 | } |
||
| 545 | elseif ($cmds[1] == "a" || $cmds[1] == "active") { |
||
| 546 | $this->showOption = self::SHOW_ACTIVE_ONLY; |
||
| 547 | } |
||
| 548 | elseif ($cmds[1] == "u" || $cmds[1] == "unknown") { |
||
| 549 | $this->showOption = self::SHOW_UNKNOWN_ONLY; |
||
| 550 | } |
||
| 551 | elseif ($cmds[1] == "d" || $cmds[1] == "default") { |
||
| 552 | $this->showOption = self::SHOW_DEFAULT; |
||
| 553 | $this->showTermSec = self::SHOW_TERM_DEFAULT_TIME; |
||
| 554 | $this->showPush = true; |
||
| 555 | } |
||
| 556 | elseif (is_numeric($cmds[1])) { |
||
| 557 | $this->showTermSec = $cmds[1]; |
||
| 558 | } |
||
| 559 | else { |
||
| 560 | $this->status = sprintf("Option '%s' unknown", $cmds[1]); |
||
| 561 | $this->statusexpire = $this->currenttime + 5; |
||
| 562 | } |
||
| 563 | } |
||
| 564 | elseif ($cmds[0] == "reset" || $cmds[0] == "r") { |
||
| 565 | $this->filter = false; |
||
| 566 | $this->wide = false; |
||
| 567 | $this->helpexpire = 0; |
||
| 568 | $this->status = "reset"; |
||
| 569 | $this->statusexpire = $this->currenttime + 2; |
||
| 570 | } |
||
| 571 | // enable/disable wide view |
||
| 572 | elseif ($cmds[0] == "wide" || $cmds[0] == "w") { |
||
| 573 | $this->wide = !$this->wide; |
||
| 574 | $this->status = ($this->wide) ? "w i d e view" : "normal view"; |
||
| 575 | $this->statusexpire = $this->currenttime + 2; |
||
| 576 | } |
||
| 577 | elseif ($cmds[0] == "help" || $cmds[0] == "h") { |
||
| 578 | $this->helpexpire = $this->currenttime + 20; |
||
| 579 | } |
||
| 580 | // grep the log file |
||
| 581 | elseif (($cmds[0] == "log" || $cmds[0] == "l") && isset($cmds[1])) { |
||
| 582 | if (!file_exists(LOGFILE)) { |
||
| 583 | $this->status = "Logfile can not be found: " . LOGFILE; |
||
| 584 | } |
||
| 585 | else { |
||
| 586 | system('bash -c "fgrep -a ' . escapeshellarg($cmds[1]) . ' ' . LOGFILE . ' | less +G" > `tty`'); |
||
| 587 | $this->status = "Returning from log, updating data"; |
||
| 588 | } |
||
| 589 | $this->statusexpire = time() + 5; // it might be much "later" now |
||
| 590 | } |
||
| 591 | // tail the log file |
||
| 592 | elseif ($cmds[0] == "tail" || $cmds[0] == "t") { |
||
| 593 | if (!file_exists(LOGFILE)) { |
||
| 594 | $this->status = "Logfile can not be found: " . LOGFILE; |
||
| 595 | } |
||
| 596 | else { |
||
| 597 | $this->doingTail = true; |
||
| 598 | $this->scrClear(); |
||
| 599 | $this->scrPrintAt(1, 0, $this->scrAsBold("Press CTRL+C to return to grommunio-sync-top\n\n")); |
||
| 600 | $secondary = ""; |
||
| 601 | if (isset($cmds[1])) { |
||
| 602 | $secondary = " -n 200 | grep " . escapeshellarg($cmds[1]); |
||
| 603 | } |
||
| 604 | system('bash -c "tail -f ' . LOGFILE . $secondary . '" > `tty`'); |
||
| 605 | $this->doingTail = false; |
||
| 606 | $this->status = "Returning from tail, updating data"; |
||
| 607 | } |
||
| 608 | $this->statusexpire = time() + 5; // it might be much "later" now |
||
| 609 | } |
||
| 610 | // tail the error log file |
||
| 611 | elseif ($cmds[0] == "error" || $cmds[0] == "e") { |
||
| 612 | if (!file_exists(LOGERRORFILE)) { |
||
| 613 | $this->status = "Error logfile can not be found: " . LOGERRORFILE; |
||
| 614 | } |
||
| 615 | else { |
||
| 616 | $this->doingTail = true; |
||
| 617 | $this->scrClear(); |
||
| 618 | $this->scrPrintAt(1, 0, $this->scrAsBold("Press CTRL+C to return to grommunio-sync-top\n\n")); |
||
| 619 | $secondary = ""; |
||
| 620 | if (isset($cmds[1])) { |
||
| 621 | $secondary = " -n 200 | grep " . escapeshellarg($cmds[1]); |
||
| 622 | } |
||
| 623 | system('bash -c "tail -f ' . LOGERRORFILE . $secondary . '" > `tty`'); |
||
| 624 | $this->doingTail = false; |
||
| 625 | $this->status = "Returning from tail, updating data"; |
||
| 626 | } |
||
| 627 | $this->statusexpire = time() + 5; // it might be much "later" now |
||
| 628 | } |
||
| 629 | elseif ($cmds[0] != "") { |
||
| 630 | $this->status = sprintf("Command '%s' unknown", $cmds[0]); |
||
| 631 | $this->statusexpire = $this->currenttime + 8; |
||
| 632 | } |
||
| 633 | $this->action = ""; |
||
| 634 | } |
||
| 635 | } |
||
| 636 | } |
||
| 637 | |||
| 638 | /** |
||
| 639 | * Signal handler function. |
||
| 640 | * |
||
| 641 | * @param int $signo signal number |
||
| 642 | */ |
||
| 643 | public function SignalHandler($signo) { |
||
| 644 | // don't terminate if the signal was sent by terminating tail |
||
| 645 | if (!$this->doingTail) { |
||
| 646 | $this->topCollector->CollectData(true); |
||
| 647 | $this->topCollector->ClearLatest(true); |
||
| 648 | $this->terminate = true; |
||
| 649 | } |
||
| 650 | } |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Returns usage instructions. |
||
| 654 | * |
||
| 655 | * @return string |
||
| 656 | */ |
||
| 657 | public function UsageInstructions() { |
||
| 658 | $help = "Usage:\n\tgrommunio-sync-top\n\n" . |
||
| 659 | " grommunio-sync-top is a live top-like overview of what grommunio-sync is doing. It does not have specific command line options.\n\n" . |
||
| 660 | " When grommunio-sync-top is running you can specify certain actions and options which can be executed (listed below).\n" . |
||
| 661 | " This help information can also be shown inside grommunio-sync-top by hitting 'help' or 'h'.\n\n"; |
||
| 662 | $scrhelp = $this->scrHelp(); |
||
| 663 | unset($scrhelp[0]); |
||
| 664 | |||
| 665 | $help .= implode("\n", $scrhelp); |
||
| 666 | $help .= "\n\n"; |
||
| 667 | |||
| 668 | return $help; |
||
| 669 | } |
||
| 670 | |||
| 671 | /** |
||
| 672 | * Prints a 'help' text at the end of the page. |
||
| 673 | * |
||
| 674 | * @return array with help lines |
||
| 675 | */ |
||
| 676 | private function scrHelp() { |
||
| 677 | $h = []; |
||
| 678 | $secs = $this->helpexpire - $this->currenttime; |
||
| 679 | $h[] = "Actions supported by grommunio-sync-top (help page still displayed for " . $secs . "secs)"; |
||
| 680 | $h[] = " " . $this->scrAsBold("Action") . " " . $this->scrAsBold("Comment"); |
||
| 681 | $h[] = " " . $this->scrAsBold("h") . " or " . $this->scrAsBold("help") . " Displays this information."; |
||
| 682 | $h[] = " " . $this->scrAsBold("q") . ", " . $this->scrAsBold("quit") . " or " . $this->scrAsBold(":q") . " Exits grommunio-sync-top."; |
||
| 683 | $h[] = " " . $this->scrAsBold("w") . " or " . $this->scrAsBold("wide") . " Tries not to truncate data. Automatically done if more than 180 columns available."; |
||
| 684 | $h[] = " " . $this->scrAsBold("f:VAL") . " or " . $this->scrAsBold("filter:VAL") . " Only display connections which contain VAL. This value is case-insensitive."; |
||
| 685 | $h[] = " " . $this->scrAsBold("f:") . " or " . $this->scrAsBold("filter:") . " Without a search word: resets the filter."; |
||
| 686 | $h[] = " " . $this->scrAsBold("l:STR") . " or " . $this->scrAsBold("log:STR") . " Issues 'less +G' on the logfile, after grepping on the optional STR."; |
||
| 687 | $h[] = " " . $this->scrAsBold("t:STR") . " or " . $this->scrAsBold("tail:STR") . " Issues 'tail -f' on the logfile, grepping for optional STR."; |
||
| 688 | $h[] = " " . $this->scrAsBold("e:STR") . " or " . $this->scrAsBold("error:STR") . " Issues 'tail -f' on the error logfile, grepping for optional STR."; |
||
| 689 | $h[] = " " . $this->scrAsBold("r") . " or " . $this->scrAsBold("reset") . " Resets 'wide' or 'filter'."; |
||
| 690 | $h[] = " " . $this->scrAsBold("o:") . " or " . $this->scrAsBold("option:") . " Sets display options. Valid options specified below"; |
||
| 691 | $h[] = " " . $this->scrAsBold(" p") . " or " . $this->scrAsBold("push") . " Lists/not lists active and open push connections."; |
||
| 692 | $h[] = " " . $this->scrAsBold(" a") . " or " . $this->scrAsBold("action") . " Lists only active connections."; |
||
| 693 | $h[] = " " . $this->scrAsBold(" u") . " or " . $this->scrAsBold("unknown") . " Lists only unknown connections."; |
||
| 694 | $h[] = " " . $this->scrAsBold(" 10") . " or " . $this->scrAsBold("20") . " Lists terminated connections for 10 or 20 seconds. Any other number can be used."; |
||
| 695 | $h[] = " " . $this->scrAsBold(" d") . " or " . $this->scrAsBold("default") . " Uses default options"; |
||
| 696 | |||
| 697 | return $h; |
||
| 698 | } |
||
| 699 | |||
| 700 | /** |
||
| 701 | * Encapsulates string with different color escape characters. |
||
| 702 | * |
||
| 703 | * @param string $text |
||
| 704 | * |
||
| 705 | * @return string same text as bold |
||
| 706 | */ |
||
| 707 | private function scrAsBold($text) { |
||
| 708 | return "\033[01m" . $text . "\033[0m"; |
||
| 709 | } |
||
| 710 | |||
| 711 | /** |
||
| 712 | * Prints one line of precessed data. |
||
| 713 | * |
||
| 714 | * @param array $l line information |
||
| 715 | * |
||
| 716 | * @return string |
||
| 717 | */ |
||
| 718 | private function getLine($l) { |
||
| 719 | if (!isset($l['pid']) || !isset($l['ip']) || !isset($l['user']) || !isset($l['command']) || !isset($l['time']) || !isset($l['devagent']) || !isset($l['devid']) || !isset($l['addinfo']) || !isset($l['asversion'])) { |
||
| 720 | return ""; |
||
| 721 | } |
||
| 722 | if ($this->wide === true) { |
||
| 723 | return sprintf("%s%s%s%s%s%s%s%s%s", $this->ptStr($l['pid'], 7), $this->ptStr($l['ip'], 40), $this->ptStr($l['user'], 24), $this->ptStr($l['command'], 16), $this->ptStr($this->sec2min($l['time']), 8), $this->ptStr($l['devagent'], 28), $this->ptStr($l['asversion'], 5), $this->ptStr($l['devid'], 33, true), $l['addinfo']); |
||
| 724 | } |
||
| 725 | |||
| 726 | return sprintf("%s%s%s%s%s%s%s%s%s", $this->ptStr($l['pid'], 7), $this->ptStr($l['ip'], 16), $this->ptStr($l['user'], 8), $this->ptStr($l['command'], 8), $this->ptStr($this->sec2min($l['time']), 6), $this->ptStr($l['devagent'], 20), $this->ptStr($l['asversion'], 5), $this->ptStr($l['devid'], 12, true), $l['addinfo']); |
||
| 727 | } |
||
| 728 | |||
| 729 | /** |
||
| 730 | * Pads and trims string. |
||
| 731 | * |
||
| 732 | * @param string $str to be trimmed/padded |
||
| 733 | * @param int $size characters to be considered |
||
| 734 | * @param bool $cutmiddle (optional) indicates where to long information should |
||
| 735 | * be trimmed of, false means at the end |
||
| 736 | * |
||
| 737 | * @return string |
||
| 738 | */ |
||
| 739 | private function ptStr($str, $size, $cutmiddle = false) { |
||
| 740 | if (strlen($str) < $size) { |
||
| 741 | return str_pad($str, $size); |
||
| 742 | } |
||
| 743 | if ($cutmiddle === true) { |
||
| 744 | $cut = ($size - 2) / 2; |
||
| 745 | |||
| 746 | return $this->ptStr(substr($str, 0, $cut) . ".." . substr($str, (-1) * ($cut - 1)), $size); |
||
| 747 | } |
||
| 748 | |||
| 749 | return substr($str, 0, $size - 3) . ".. "; |
||
| 750 | } |
||
| 751 | |||
| 752 | /** |
||
| 753 | * Tries to discover the size of the current terminal. |
||
| 754 | * |
||
| 755 | * @return array 'width' and 'height' as keys |
||
| 756 | */ |
||
| 757 | private function scrGetSize() { |
||
| 758 | $tty = strtolower(exec('stty -a | fgrep columns')); |
||
| 759 | if (preg_match_all("/rows.([0-9]+);.columns.([0-9]+);/", $tty, $output) || |
||
| 760 | preg_match_all("/([0-9]+).rows;.([0-9]+).columns;/", $tty, $output)) { |
||
| 761 | return ['width' => $output[2][0], 'height' => $output[1][0]]; |
||
| 762 | } |
||
| 763 | |||
| 764 | return ['width' => 80, 'height' => 24]; |
||
| 765 | } |
||
| 766 | |||
| 767 | /** |
||
| 768 | * Returns the version of the current grommunio-sync installation. |
||
| 769 | * |
||
| 770 | * @return string |
||
| 771 | */ |
||
| 772 | private function getVersion() { |
||
| 773 | return GROMMUNIOSYNC_VERSION; |
||
| 774 | } |
||
| 775 | |||
| 776 | /** |
||
| 777 | * Converts seconds in MM:SS. |
||
| 778 | * |
||
| 779 | * @param int $s seconds |
||
| 780 | * |
||
| 781 | * @return string |
||
| 782 | */ |
||
| 783 | private function sec2min($s) { |
||
| 784 | if (!is_int($s)) { |
||
| 785 | return $s; |
||
| 786 | } |
||
| 787 | |||
| 788 | return sprintf("%02.2d:%02.2d", floor($s / 60), $s % 60); |
||
| 789 | } |
||
| 790 | |||
| 791 | /** |
||
| 792 | * Resets the default colors of the terminal. |
||
| 793 | */ |
||
| 794 | private function scrDefaultColors() { |
||
| 795 | echo "\033[0m"; |
||
| 796 | } |
||
| 797 | |||
| 798 | /** |
||
| 799 | * Clears screen of the terminal. |
||
| 800 | */ |
||
| 801 | public function scrClear() { |
||
| 803 | } |
||
| 804 | |||
| 805 | /** |
||
| 806 | * Prints a text at a specific screen/terminal coordinates. |
||
| 807 | * |
||
| 808 | * @param int $row row number |
||
| 809 | * @param int $col column number |
||
| 810 | * @param string $text to be printed |
||
| 811 | */ |
||
| 812 | private function scrPrintAt($row, $col, $text = "") { |
||
| 814 | } |
||
| 815 | } |
||
| 816 |