SplitIce Sever Process Checker

Status
Not open for further replies.

SplitIce

Active Member
2,474
2008
240
0
This script restarts processes that have died/segfaulted. Wont protect against defuncts or anything like that but its a basic start. Also checks that certain processes dont exceed a set memory usage and if they do can execute a php function or shell command.

Run via CLI, as root.

PHP:
<?php
    /* SplitIce Sever Process Checker */
    /* Version: 1.2 */
    /* All distributed versions must keep this header intact */
    /* Need software development work? Contact me: fast, reasonable rates */

    $_CONFIG = array();

    /* Process is running checks */
    $_CONFIG['CHECKS'] = array(
    /* Substring to check for => Command to run if not found */
    'nginx: worker process'=>'/etc/init.d/nginx restart',
    'nginx: master process /usr/sbin/nginx'=>'/etc/init.d/nginx restart',

    '/usr/bin/php5-fpm'=>'/etc/init.d/php5-fpm restart',
    'searchd' => '/usr/local/bin/searchd'
    );

    /* Process memory limits */
    $_CONFIG['MEM'] = array(
    /* Substring to check for => array(max memory,command to run if exceeds) */
    /* Size in KB */
    /* if command is an array then it is treated as a callback (php function) */
    'searchd'=>array(1024*1024,array('searchd_restart'))
    );

    /* Example Callback for Memory checks */
    function searchd_restart($PID){
        /* This example deals with the memory leaks in the sphinx daemon */
        exec('killall searchd');//Nice kill
        exec('killall -2 searchd');//agrivated kill
        Sleep(1);
        exec('killall -3 searchd');//angy kill
        exec('killall -4 searchd');//skitzo kill
        passthru('/usr/local/bin/searchd');//reborn :D
    }

    /* Program Starts */
    function processes(){
        $processes = array();
        foreach(glob('/proc/*/cmdline') as $p){
            $pt = explode('/',$p);
            if(is_numeric($pt[2])) {
                if($CMD = file_get_contents($p)) {
                    $processes[$pt[2]] = trim($CMD);
                }
            }
        }
        return $processes;
    }
    
    function process_mem($PID){
        $data = file('/proc/'.$PID.'/status');
        foreach($data as $v){
            if(!substr_compare($v,'VmRSS:',0,6)){
                $t = trim(substr($v,7));
                $t = trim(substr($t,0,strpos($t,' ')));
                return (int)$t;
            }
        }
        return false;
    }

    $CHECKS = array();
    while(true){
        $processes = processes();
        foreach($_CONFIG['CHECKS'] as $m=>$cmd){
            $check = false;
            foreach($processes as $PID=>$p){
                if(stripos($p,$m)!==false){
                    $check = true;
                    break;
                }
            }
            if(!$check){
                passthru($cmd);
                Sleep(5);//Multi-Teired Check delay
            }
        }
        foreach ($_CONFIG['MEM'] as $process=>$data){
            $check = false;
            foreach($processes as $PID=>$p){
                if(stripos($p,$process)!==false){
                    $check = $PID;
                    break;
                }
            }
            if($check){
                if(process_mem($check)>$data[0]){
                    if(is_array($data[1])){
                        call_user_func($data[1][0],$check);
                    }else{
                        passthru($data[1]);
                    }
                    Sleep(5);//Multi-Teired Check delay
                }
            }
        }
        Sleep(30);
    }
?>
 
2 comments
Status
Not open for further replies.
Back
Top