Search code examples
phpmysqlserver-load

sys_getloadavg for MySQL


Is there something like this, but for MySQL?

<?php
if (function_exists('sys_getloadavg')) {
    $load = sys_getloadavg();

    if ($load[0] > 60) {
        header('HTTP/1.1 503 Too busy, try again later');
        die('Server too busy. Please try again later.');
    }
}

Solution

  • There's no standard function for that for sure.

    So your options are:

    1) if on linux: read from `/proc/loadavg', e.g.:

    create table loadavg(l1 int, l2 int, l3 int, entities varchar(100), last_pid int);
    [...]
    truncate loadavg;
    load data infile '/proc/loadavg' into table loadavg fields terminated by ' ';
    select l1 from loadavg;
    

    2) write a User Defined Function in C to retrieve the value

    I'm pretty sure I've seen a UDF for this in the past, but my Google skills fail me in finding it again right now ...