public function getProgramIsRunning($programName) { // what are we doing? $log = usingLog()->startAction("is program '{$programName}' running under supervisor on host '{$this->args[0]}'?"); // get the host details $hostDetails = $this->getHostDetails(); //run the supervisorctl command $result = usingHost($hostDetails->hostId)->runCommandAndIgnoreErrors("sudo supervisorctl status"); // |egrep '^$programName' | awk '{print \\$2}'"); // did the command succeed? if ($result->didCommandFail()) { $msg = "command failed with return code '{$result->returnCode}' and output '{$result->output}'"; $log->endAction($msg); throw new E5xx_ActionFailed(__METHOD__); } // reduce the output down $lines = explode("\n", $result->output); $lines = FilterForMatchingRegex::against($lines, "/^{$programName} /"); $lines = FilterColumns::from($lines, "1", ' '); if (empty($lines)) { $log->endAction("supervisor does not know about '{$programName}'"); return false; } // what happened? if ($lines[0] == 'RUNNING') { $log->endAction('current status is RUNNING'); return true; } // if we get here, then the program is not RUNNING, and we // treat that as a failure $log->endAction('current status is ' . $lines[0]); return false; }
/** * * @param HostDetails $hostDetails * @param string $pid * @return boolean */ public function getPidIsRunning($hostDetails, $pid) { // what are we doing? $log = usingLog()->startAction("is process PID '{$pid}' running on UNIX '{$hostDetails->hostId}'?"); // SSH in and have a look $command = "ps -ef | grep '{$pid}'"; $result = $this->runCommand($hostDetails, $command); // what did we find? if ($result->didCommandFail() || empty($result->output)) { $log->endAction("cannot get process list"); return false; } // reduce down the output we have $pids = explode("\n", $result->output); $pids = FilterColumns::from($pids, "1", ' '); $pids = FilterForMatchingRegex::against($pids, "/^{$pid}\$/"); // success? if (empty($pids)) { $log->endAction("not running"); return false; } // success $log->endAction("is running"); return true; }
/** * @covers ::againstString * @covers ::matchLine * @dataProvider provideStringsToMatch */ public function testCanStaticallyFilterStrings($data, $searchString, $expectedResult) { // ---------------------------------------------------------------- // setup your test // ---------------------------------------------------------------- // perform the change $actualResult = FilterForMatchingRegex::againstString($data, $searchString); // ---------------------------------------------------------------- // test the results $this->assertEquals($expectedResult, $actualResult); }
/** * @return array<object> */ public function getAllScreenSessions() { // what are we doing? $log = usingLog()->startAction("get details about all screen sessions on host '{$this->args[0]}'"); // are there any details? $cmd = "screen -ls"; $result = usingHost($this->args[0])->runCommandAndIgnoreErrors($cmd); // NOTE: // // screen is not a well-behaved UNIX program, and its exit code // can be non-zero when everything is good if (empty($result->output)) { $msg = "unable to get list of screen sessions"; $log->endAction($msg); return []; } // reduce the output down to a list of sessions $lines = explode("\n", $result->output); $lines = FilterForMatchingRegex::against($lines, "/[0-9]+.+\t/"); if (empty($lines)) { $msg = "no screen processes running"; $log->endAction($msg); return []; } $retval = []; foreach ($lines as $line) { $parts = explode('.', $line); $processDetails = new BaseObject(); $processDetails->hostId = $this->args[0]; $processDetails->type = 'screen'; $processDetails->pid = trim($parts[0]); $processDetails->name = rtrim($parts[1]); $retval[] = $processDetails; } // all done $log->endAction("found " . count($retval) . " screen process(es)"); // all done return $retval; }
/** * * @param HostDetails $hostDetails * @param string $packageName * @return BaseObject */ public function getInstalledPackageDetails($hostDetails, $packageName) { // what are we doing? $log = usingLog()->startAction("get details for package '{$packageName}' installed in host '{$hostDetails->hostId}'"); // get the details $command = 'dpkg-query -W --showformat=\'\\${Package} \\${Version}\\t\\${Status}\\n\' ' . $packageName; $result = $this->runCommand($hostDetails, $command); // any luck? if ($result->didCommandFail()) { $log->endAction("could not get details ... package not installed?"); return new BaseObject(); } // study the output $lines = explode("\n", $result->output); $lines = FilterForMatchingRegex::against($lines, "|^{$packageName} |"); $lines = FilterForMatchingString::against($lines, "install ok installed"); if (empty($lines)) { $log->endAction("package not installed?"); return new BaseObject(); } // we have some information to return $return = new BaseObject(); $return->name = FilterColumns::fromString($lines[0], "0", " "); $return->version = FilterColumns::fromString($lines[0], "1", "\t"); $return->repo = "unknown"; // all done $log->endAction(); return $return; }