Created
October 2, 2012 14:53
-
-
Save Halleck45/3819780 to your computer and use it in GitHub Desktop.
Déclenche un événement quand mémoire > x ou mémoire < y
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php -q | |
<?php | |
ini_set('display_errors', 1); | |
function get_memory() { | |
foreach (file('/proc/meminfo') as $ri) { | |
$m[strtok($ri, ':')] = strtok(''); | |
} | |
return 100 - round(($m['MemFree'] + $m['Buffers'] + $m['Cached']) / $m['MemTotal'] * 100); | |
} | |
$pids = array(); | |
// | |
// Options du programme | |
$options = getopt('', array('min:', 'max:', 'overflow:', 'low:', 'delay:')); | |
if (sizeof($options) != 5) { | |
die('Usage :' | |
. PHP_EOL . './' . basename(__FILE__) | |
. PHP_EOL . "\t" . '--min=20 (percent)' | |
. PHP_EOL . "\t" . '--max=80 (percent)' | |
. PHP_EOL . "\t" . '--overflow=/usr/bin/whenTooMuch' | |
. PHP_EOL . "\t" . '--low=/usr/bin/whenTooFew' | |
. PHP_EOL . "\t" . '--delay=5 (seconds)' | |
. PHP_EOL | |
); | |
} | |
// | |
// Options | |
$min = $options['min']; | |
$max = $options['max']; | |
$programOverflow = $options['overflow']; | |
$programLow = $options['low']; | |
$delay = $options['delay']; | |
$arguments = array(""); | |
$start = null; | |
$lastEvent = null; | |
// | |
// Fork | |
$pid = pcntl_fork(); | |
if ($pid) { | |
exit(); | |
} | |
$pid = null; | |
$pending = false; | |
// | |
// Signals | |
declare(ticks = 1); | |
pcntl_signal(SIGTERM, "sig_handler"); | |
pcntl_signal(SIGHUP, "sig_handler"); | |
pcntl_signal(SIGINT, "sig_handler"); | |
pcntl_signal(SIGUSR1, "sig_handler"); | |
function sig_handler($signo) { | |
global $pids; | |
echo 'I received ' . $signo . PHP_EOL; | |
if ($signo == SIGTERM || $signo == SIGHUP || $signo == SIGINT) { | |
// | |
// Transférer aux enfants | |
foreach ($pids as $p) { | |
posix_kill($p, $signo); | |
} | |
// | |
// Tuer les enfants | |
foreach ($pids as $p) { | |
pcntl_waitpid($p, $status); | |
} | |
echo sprintf('All kids of %s are killed', getmypid()) . PHP_EOL; | |
exit; | |
} else if ($signo == SIGUSR1) { | |
// pas encore | |
} | |
} | |
while (true) { | |
// | |
// déclencheur | |
$memory = get_memory(); | |
if ( | |
($memory >= $max ) | |
|| ($memory <= $min ) | |
) { | |
$event = ($memory >= $max ) ? 'tooMuch' : 'tooFew'; | |
// | |
// Redémarre le délai quand la mémoire fait des bonds | |
if ($event == 'tooMuch' && $lastEvent != 'tooMuch') { | |
$start = null; | |
} elseif ($event == 'tooFiew' && $lastEvent != 'tooFiew') { | |
$start = null; | |
} | |
$programToLaunch = $event == 'tooMuch' ? $programOverflow : $programLow; | |
$lastEvent = $event == 'tooMuch' ? 'tooMuch' : 'tooFiew'; | |
// | |
// Processus | |
if (is_null($start)) { | |
$start = mktime(); | |
} else { | |
// | |
// Délai dépasse | |
if (mktime() - $start > $delay) { | |
// | |
// On le lance qu'un seul script à la fois | |
if (!$pending) { | |
$pid = pcntl_fork(); | |
if ($pid === 0) { | |
// child | |
pcntl_exec($programToLaunch, $arguments); | |
exit; | |
} else { | |
// | |
// Stocker les enfants | |
$pids[] = $pid; | |
$pending = true; | |
pcntl_waitpid($pid, $status, WUNTRACED); | |
if (pcntl_wifexited($status)) { | |
$pending = false; | |
} else if (pcntl_wifstopped($status)) { | |
$pending = false; | |
} else if (pcntl_wifsignaled($status)) { | |
$pending = false; | |
} | |
$start = null; | |
} | |
} else { | |
//echo 'DEJA EN COURS' . PHP_EOL; | |
} | |
} | |
} | |
} else { | |
$start = null; | |
} | |
// Sleep to avoid freezes | |
sleep(1); | |
gc_collect_cycles(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment