|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright (c) 2011-present Mediasift Ltd |
|
5
|
|
|
* All rights reserved. |
|
6
|
|
|
* |
|
7
|
|
|
* Redistribution and use in source and binary forms, with or without |
|
8
|
|
|
* modification, are permitted provided that the following conditions |
|
9
|
|
|
* are met: |
|
10
|
|
|
* |
|
11
|
|
|
* * Redistributions of source code must retain the above copyright |
|
12
|
|
|
* notice, this list of conditions and the following disclaimer. |
|
13
|
|
|
* |
|
14
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
|
15
|
|
|
* notice, this list of conditions and the following disclaimer in |
|
16
|
|
|
* the documentation and/or other materials provided with the |
|
17
|
|
|
* distribution. |
|
18
|
|
|
* |
|
19
|
|
|
* * Neither the names of the copyright holders nor the names of his |
|
20
|
|
|
* contributors may be used to endorse or promote products derived |
|
21
|
|
|
* from this software without specific prior written permission. |
|
22
|
|
|
* |
|
23
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
24
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
25
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
|
26
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
|
27
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
|
28
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
29
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
30
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|
31
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
32
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
|
33
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
34
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
|
35
|
|
|
* |
|
36
|
|
|
* @category Libraries |
|
37
|
|
|
* @package Storyplayer/Modules/Screen |
|
38
|
|
|
* @author Stuart Herbert <[email protected]> |
|
39
|
|
|
* @copyright 2011-present Mediasift Ltd www.datasift.com |
|
40
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
41
|
|
|
* @link http://datasift.github.io/storyplayer |
|
42
|
|
|
*/ |
|
43
|
|
|
|
|
44
|
|
|
namespace Storyplayer\SPv2\Modules\Screen; |
|
45
|
|
|
|
|
46
|
|
|
use DataSift\Storyplayer\HostLib; |
|
47
|
|
|
use DataSift\Storyplayer\OsLib; |
|
48
|
|
|
|
|
49
|
|
|
use DataSift\Stone\DataLib\DataPrinter; |
|
50
|
|
|
use DataSift\Stone\ObjectLib\BaseObject; |
|
51
|
|
|
|
|
52
|
|
|
use GanbaroDigital\TextTools\Filters\FilterColumns; |
|
53
|
|
|
use GanbaroDigital\TextTools\Filters\FilterForMatchingRegex; |
|
54
|
|
|
use GanbaroDigital\TextTools\Filters\FilterForMatchingString; |
|
55
|
|
|
|
|
56
|
|
|
use Storyplayer\SPv2\Modules\Exceptions; |
|
57
|
|
|
use Storyplayer\SPv2\Modules\Host; |
|
58
|
|
|
use Storyplayer\SPv2\Modules\Host\HostAwareModule; |
|
59
|
|
|
use Storyplayer\SPv2\Modules\Log; |
|
60
|
|
|
use Storyplayer\SPv2\Modules\Screen; |
|
61
|
|
|
use Storyplayer\SPv2\Modules\Shell; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* get information about a given host |
|
65
|
|
|
* |
|
66
|
|
|
* @category Libraries |
|
67
|
|
|
* @package Storyplayer/Modules/Screen |
|
68
|
|
|
* @author Stuart Herbert <[email protected]> |
|
69
|
|
|
* @copyright 2011-present Mediasift Ltd www.datasift.com |
|
70
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
71
|
|
|
* @link http://datasift.github.io/storyplayer |
|
72
|
|
|
*/ |
|
73
|
|
|
class FromScreen extends HostAwareModule |
|
74
|
|
|
{ |
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $sessionName |
|
77
|
|
|
* @return bool |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getScreenIsRunning($sessionName) |
|
80
|
|
|
{ |
|
81
|
|
|
// what are we doing? |
|
82
|
|
|
$log = Log::usingLog()->startAction("check if screen session '{$sessionName}' is still running"); |
|
83
|
|
|
|
|
84
|
|
|
// get the details |
|
85
|
|
|
$sessionData = Screen::fromHost($this->args[0])->getScreenSessionDetails($sessionName); |
|
86
|
|
|
|
|
87
|
|
|
// all done |
|
88
|
|
|
if ($sessionData) { |
|
89
|
|
|
$log->endAction("still running"); |
|
90
|
|
|
return true; |
|
91
|
|
|
} |
|
92
|
|
|
else { |
|
93
|
|
|
$log->endAction("not running"); |
|
94
|
|
|
return false; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param string $sessionName |
|
100
|
|
|
* @return BaseObject|null |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getScreenSessionDetails($sessionName) |
|
103
|
|
|
{ |
|
104
|
|
|
// what are we doing? |
|
105
|
|
|
$log = Log::usingLog()->startAction("get details about screen session '{$sessionName}' on host '{$this->args[0]}' from Storyplayer"); |
|
106
|
|
|
|
|
107
|
|
|
// are there any details? |
|
108
|
|
|
$cmd = "screen -ls"; |
|
109
|
|
|
$result = Shell::onHost($this->args[0])->runCommandAndIgnoreErrors($cmd); |
|
110
|
|
|
|
|
111
|
|
|
// NOTE: |
|
112
|
|
|
// |
|
113
|
|
|
// screen is not a well-behaved UNIX program, and its exit code |
|
114
|
|
|
// can be non-zero when everything is good |
|
115
|
|
|
if (empty($result->output)) { |
|
116
|
|
|
$msg = "unable to get list of screen sessions"; |
|
|
|
|
|
|
117
|
|
|
return null; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
// do we have the session we are looking for? |
|
121
|
|
|
$lines = explode("\n", $result->output); |
|
122
|
|
|
$lines = FilterForMatchingRegex::against($lines, "/[0-9]+\\.{$sessionName}\t/"); |
|
123
|
|
|
$lines = FilterColumns::from($lines, "0", '.'); |
|
124
|
|
|
|
|
125
|
|
|
if (empty($lines)) { |
|
126
|
|
|
$msg = "screen session '{$sessionName}' is not running"; |
|
127
|
|
|
$log->endAction($msg); |
|
128
|
|
|
return null; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
// there might be |
|
132
|
|
|
$processDetails = new BaseObject; |
|
133
|
|
|
$processDetails->hostId = $this->args[0]; |
|
134
|
|
|
$processDetails->name = $sessionName; |
|
135
|
|
|
$processDetails->type = 'screen'; |
|
136
|
|
|
$processDetails->pid = trim(rtrim($lines[0])); |
|
137
|
|
|
|
|
138
|
|
|
// all done |
|
139
|
|
|
$log->endAction("session is running as PID '{$processDetails->pid}'"); |
|
140
|
|
|
return $processDetails; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @return array<object> |
|
145
|
|
|
*/ |
|
146
|
|
|
public function getAllScreenSessions() |
|
147
|
|
|
{ |
|
148
|
|
|
// what are we doing? |
|
149
|
|
|
$log = Log::usingLog()->startAction("get details about all screen sessions on host '{$this->args[0]}'"); |
|
150
|
|
|
|
|
151
|
|
|
// are there any details? |
|
152
|
|
|
$cmd = "screen -ls"; |
|
153
|
|
|
$result = Shell::onHost($this->args[0])->runCommandAndIgnoreErrors($cmd); |
|
154
|
|
|
|
|
155
|
|
|
// NOTE: |
|
156
|
|
|
// |
|
157
|
|
|
// screen is not a well-behaved UNIX program, and its exit code |
|
158
|
|
|
// can be non-zero when everything is good |
|
159
|
|
|
if (empty($result->output)) { |
|
160
|
|
|
$msg = "unable to get list of screen sessions"; |
|
161
|
|
|
$log->endAction($msg); |
|
162
|
|
|
return []; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
// reduce the output down to a list of sessions |
|
166
|
|
|
$lines = explode("\n", $result->output); |
|
167
|
|
|
$lines = FilterForMatchingRegex::against($lines, "/[0-9]+.+\t/"); |
|
168
|
|
|
|
|
169
|
|
|
if (empty($lines)) { |
|
170
|
|
|
$msg = "no screen processes running"; |
|
171
|
|
|
$log->endAction($msg); |
|
172
|
|
|
return []; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
$retval = []; |
|
176
|
|
|
foreach ($lines as $line) { |
|
177
|
|
|
$parts = explode('.', $line); |
|
178
|
|
|
|
|
179
|
|
|
$processDetails = new BaseObject; |
|
180
|
|
|
$processDetails->hostId = $this->args[0]; |
|
181
|
|
|
$processDetails->type = 'screen'; |
|
182
|
|
|
$processDetails->pid = trim($parts[0]); |
|
183
|
|
|
$processDetails->name = rtrim($parts[1]); |
|
184
|
|
|
|
|
185
|
|
|
$retval[] = $processDetails; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
// all done |
|
189
|
|
|
$log->endAction("found " . count($retval) . " screen process(es)"); |
|
190
|
|
|
|
|
191
|
|
|
// all done |
|
192
|
|
|
return $retval; |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.