// ********** settings **********************************************************
$go_exe = 'D:\OS2Apps\go.exe'; // path to go.exe
$allowed_to_kill = array('127.0.0.1', '192.168.16.124', '217.10.34.24'); // hosts that are allowed to kill processes
// ********** global vars *******************************************************
$proc = array();
$numproc = 0;
$numthr = 0;
$uptime = '';
$header = '
go.exe web interface
%s
';
$styles = '';
$footer = '
';
// ========== start =============================================================
if ((strpos($_SERVER['HTTP_USER_AGENT'], 'Mozilla/4') !== false) and !strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
$header = sprintf($header, ''); // f.cking Netscape!
} else {
$header = sprintf($header, $styles);
}
preg_match('/(^[^?]+)(\?|$)/', $_SERVER['REQUEST_URI'], $m); // CGI workaround
$self = $m[1];
$k = intval($_GET['k']);
$ka = $_GET['ka'];
// ********** request to kill ***************************************************
if ($k or strlen($ka)) {
if ($_GET['sure'] == 'y') {
if (!in_array($_SERVER['REMOTE_ADDR'], $allowed_to_kill)) {
echo $header;
echo "Go kill yourself, bro!\n
You are not allowed to kill processes on this box.";
echo "
<< back\n";
echo $footer;
die();
}
$cmd = '';
if ($pid = intval($_GET['k'])) { $cmd = "$go_exe -k $pid"; }
if ($name = $_GET['ka']) { $cmd = "$go_exe -ka $name"; }
if (strlen($cmd)) { exec($cmd); }
my_redirect($self);
die();
}
echo $header;
$go = `$go_exe`;
parse_procs($go, $proc, $numproc, $numthr, $uptime);
if ($k) {
$name = '';
foreach ($proc as $a) {
if (intval($a['pid']) == $k) {
$name = trim($a['name']);
break;
}
}
if (strlen($name)) {
echo "Are you sure you want to kill process with PID $k ($name)?\n";
echo '
yes '."\n";
echo ' no '."\n";
} else {
echo "Process with PID $k not found!\n";
echo "
<< back\n";
}
} else {
$count= 0;
foreach ($proc as $a) {
if (trim($a['name']) == $ka) { $count++; }
}
if ($count) {
echo "Are you sure you want to kill all $ka processes (total: $count)?\n";
echo '
yes '."\n";
echo ' no '."\n";
} else {
echo "No $ka process(es) found!\n";
echo "
<< back\n";
}
}
echo $footer;
die();
}
// ********** thread list *******************************************************
if ($pid = intval($_GET['tl'])) {
echo $header;
$cmd = $go_exe." -tl $pid";
echo "\n";
passthru($cmd);
echo "
\n";
echo "
<< back\n";
echo $footer;
die();
}
// ********** show `go` output **************************************************
$sort = $_GET['sort'];
$sortby = array('PID' => 'sort_by_pid', 'PPID' => 'sort_by_ppid', 'Sess' => 'sort_by_sess', 'Thr' => 'sort_by_thr', 'Prio' => 'sort_by_prio', 'CPU' => 'sort_by_cpu', 'Name' => 'sort_by_name');
echo $header;
$go = `$go_exe`;
parse_procs($go, $proc, $numproc, $numthr, $uptime);
$n = sizeof($proc);
if (strlen($sort)) {
if (strlen($sortby[$sort])) {
usort($proc, $sortby[$sort]);
if (($sort == 'CPU') or ($sort == 'Name')) {
for ($i=0;$i<$n;$i++) { $proc[$i]['name'] = trim($proc[$i]['name']); }
}
}
}
?>
There are =$numproc?> processes with =$numthr?> threads.
System uptime is =$uptime?>.
Process list
if (strlen($sortby[$sort])) {
echo " (sorted by $sort) (view unsorted)";
}
?>:
PID |
PPID |
Sess |
Thr |
Prio |
CPU time |
Name |
==== | ==== | ======= | === | ==== | ========== | ============ |
$tmpl = ''.
'%s | '. // pid
'%s | '. // ppid
'%s | '. // sess1
'%s | '. // sess2
'%s | '. // thr
'%s | '. // prio
'%s | '. // cpu
'%s | '. // name
'
'."\n";
for ($i=0;$i<$n;$i++) {
printf($tmpl,
$self, $proc[$i]['pid'], $proc[$i]['pid'], $proc[$i]['pid'],
$proc[$i]['ppid'],
$proc[$i]['sess1'],
$proc[$i]['sess2'],
$self, $proc[$i]['pid'], $proc[$i]['thr'],
$proc[$i]['prio'],
$proc[$i]['cpu'],
$self, urlencode(trim($proc[$i]['name'])), trim($proc[$i]['name']), str_replace(' ', ' ', $proc[$i]['name']));
}
?>
==== | ==== | ======= | === | ==== | ========== | ============ |
echo $footer;
// ========== end ===============================================================
// ********** funcs *************************************************************
// parse `go.exe` output to $proc array
function parse_procs(&$go, &$proc, &$numproc, &$numthr, &$uptime) {
preg_match('/ (\d+) Processes with (\d+)/', $go, $m);
list($match, $numproc, $numthr) = $m;
preg_match('/uptime is (\d+)d (\d+)h (\d+)m (\d+)s/', $go, $m);
list($match, $day, $hour, $min, $sec) = $m;
if (intval($day)) {
$uptime = sprintf('%d days %02d:%02d:%02d', $day, $hour, $min, $sec);
} else {
$uptime = sprintf('%02d:%02d:%02d', $hour, $min, $sec);
}
preg_match_all('/(\d+)\s+(\d+)\s([0-9A-F]+) (\w+)\s+(\d+)\s+([0-9A-F]+)\s+([.:0-9]+) (\s*[\S]+)/m', $go, $matches);
// 0: match
// 1: P-ID
// 2: PPID
// 3: Sess 1
// 4: Sess 2
// 5: Thr
// 6: Prio
// 7: CPU Time
// 8: Name
list($match, $pid, $ppid, $sess1, $sess2, $thr, $prio, $cpu, $name) = $matches;
$n = sizeof($match);
for ($i=0;$i<$n;$i++) {
$proc[$i]['pid'] = $pid[$i];
$proc[$i]['ppid'] = $ppid[$i];
$proc[$i]['sess1'] = $sess1[$i];
$proc[$i]['sess2'] = $sess2[$i];
$proc[$i]['thr'] = $thr[$i];
$proc[$i]['prio'] = $prio[$i];
$proc[$i]['cpu'] = $cpu[$i];
$proc[$i]['name'] = $name[$i];
}
}
// sorting functions
function sort_by_pid($a, $b) {
return (intval($a['pid']) < intval($b['pid'])) ? -1 : 1;
}
function sort_by_ppid($a, $b) {
if (intval($a['ppid']) == intval($b['ppid'])) {
return (intval($a['pid']) < intval($b['pid'])) ? -1 : 1;
}
return (intval($a['ppid']) < intval($b['ppid'])) ? -1 : 1;
}
function sort_by_sess($a, $b) {
if ($a['sess1'] == $b['sess1']) {
if ($a['sess2'] == $b['sess2']) {
return (intval($a['pid']) < intval($b['pid'])) ? -1 : 1;
}
return ($a['sess2'] < $b['sess2']) ? -1 : 1;
}
return ($a['sess1'] < $b['sess1']) ? -1 : 1;
}
function sort_by_thr($a, $b) {
if (intval($a['thr']) == intval($b['thr'])) {
return (intval($a['pid']) < intval($b['pid'])) ? -1 : 1;
}
return (intval($a['thr']) > intval($b['thr'])) ? -1 : 1;
}
function sort_by_prio($a, $b) {
if ($a['prio'] == $b['prio']) {
return (intval($a['pid']) < intval($b['pid'])) ? -1 : 1;
}
return ($a['prio'] < $b['prio']) ? -1 : 1;
}
function sort_by_cpu($a, $b) {
preg_match('/(\d+):(\d{2}):(\d{2})\.(\d{2})/', $a['cpu'], $m);
list($match, $h, $m, $s, $th) = $m; $a1 = 60*60*$h + 60*$m + $s + $th/100;
preg_match('/(\d+):(\d+):(\d+)\.(\d+)/', $b['cpu'], $m);
list($match, $h, $m, $s, $th) = $m; $b1 = 60*60*$h + 60*$m + $s + $th/100;
if ($a1 == $b1) {
return (intval($a['pid']) < intval($b['pid'])) ? -1 : 1;
}
return ($a1 > $b1) ? -1 : 1;
}
function sort_by_name($a, $b) {
if (trim($a['name']) == trim($b['name'])) {
return (intval($a['pid']) < intval($b['pid'])) ? -1 : 1;
}
return (trim($a['name']) < trim($b['name'])) ? -1 : 1;
}
// redirection
function my_redirect($url, $code = 302) {
if ($code == 303) {
preg_match('/^HTTP\/(\d)\.(\d)/', $_SERVER['SERVER_PROTOCOL'], $m);
if (intval($m[1])*10+intval($m[2]) < 11) { $code = 302; }
}
if ($code == 303) {
Header($_SERVER['SERVER_PROTOCOL'].' 303 See Other');
} else {
Header($_SERVER['SERVER_PROTOCOL'].' 302 Found');
}
if (substr($url, 0, 1) == '/') {
$url = 'http://'.$_SERVER['HTTP_HOST'].$url;
} elseif (!strpos($url, '://')) {
preg_match('/(.+?)[^\/]+$/', $_SERVER['REQUEST_URI'], $m);
$url = 'http://'.$_SERVER['HTTP_HOST'].$m[1].$url;
}
Header('Location: '.$url);
?>
Oops!
??? ??????? ?? ???????? HTTP redirect (status code: =$code?>).
???????? ?? ???? ??????.
Your browser does not understand HTTP redirect (status code: =$code?>).
Follow this link.
}
// Dmitry Ban, ban at ram.ru
?>