P
P
PO6OT2015-09-23 13:05:49
PHP
PO6OT, 2015-09-23 13:05:49

How to monitor load and limits on shared from PHP?

I decided to create a daemon on a shared hosting, refuted the phrase "PHP is designed to die", but sometimes the error 508 Resource Limit Exceeded occurs.
The daemon already knows how to bypass this error, but I would like to get rid of it completely, since eventually the limit will be reached and the hosting provider will disable the account.
To avoid error 508, I assume that the daemon controls the load on the server and if the load is close to the limit, sleep, waiting for the completion of child processes.
I don’t know how to monitor the load and limits on shared from PHP, I dug through the Internet, I found almost nothing.
Hosting - hostinger.ru
Live daemon - godaemon.tk Daemon
sources:
index.php:

<?php
if(file_exists('./daemon.php') & isset($_GET['stop'])){
    rename('./daemon.php', './daemon.php~');
    sleep(3);
    header('Location: http://'.$_SERVER[HTTP_HOST]);
}elseif(isset($_GET['restart'])){
    include('./lib.inc');
    @rename('./daemon.php', './daemon.php~');
    sleep(3);
    rename('./daemon.php~', './daemon.php');
    ping('daemon.php');
    sleep(3);
    header('Location: http://'.$_SERVER[HTTP_HOST]);
}
    echo '<a href="/?restart" class="button">Start/Restart</a> ';
if(file_exists('./daemon.php'))
    echo '<a href="/?stop" class="button">Stop</a>';
?><br>

<div class="display">-</div>
<script type="text/javascript"><!--
var xd;
setInterval(function(){
xd&&xd.abort();
xd = new XMLHttpRequest();
xd.onreadystatechange = function(){
if(xd.readyState == 4){
document.querySelector(".display").textContent = xd.responseText;
}
}
xd.open('GET', '/display.txt', true);
xd.send();

}, 3000);
//--></script>

daemon.php:
<?php
if(date('his')-file_get_contents('./date')<2)
    exit;
    file_put_contents('./date', date('his'));
    include('./lib.inc');
    $f=fopen('./lock', 'w+');
    flock($f, LOCK_EX);
    ping('core.php');
    sleep(3);
    ping('daemon.php');
    usleep(200);
    ping('daemon.php');
    usleep(200);
    flock($f, LOCK_UN);

core.php:
<?php
    ini_set('display_errors', 'on');
    error_reporting(E_ALL);
    ini_set('html_errors', 'off');
    $xreservedbasedir=__DIR__;
    $xreservedtmpbuf=str_repeat('x', 1024 * 3);
function ob_write($buffer){
    unset($GLOBALS['xreservedtmpbuf']);
    file_put_contents($GLOBALS['xreservedbasedir'].'/display.txt', $buffer, LOCK_EX);
}
    ob_start('ob_write');
    include('./script.php');

script.php:
<?php
    echo date("Y.m.d h:i:s"); //любой код, который исполняет демон

lib.inc:
<?php
function ping($action){
    $h=$_SERVER[HTTP_HOST];
    $http=fsockopen($h, 80, $e1, $e2, 1);
if($http){
    fwrite($http,
    'GET /'.$action.' HTTP/1.1'."\r\n".
    'Accept: */*'."\r\n".
    'Host: '.$h."\r\n\r\n"
    );
    fclose($http);
}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MetaDone, 2015-09-23
@woonem

hostinger.ru - run from there. a long time ago I hung a test site there, of course, attendance is zero. So they sent me packs of letters about exceeding the resource limit.
And if on the topic - on the shared you sit next to the rest of the bunch of clients who also load the server.
look at this
code.blitzaffe.com/pages/phpclasses/files/cpu_load...
and standard functions can get memory consumption

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question