Prozessorlast bestimmen

Bash, Shell, PHP, Python, Perl, CGI
r00ty
Posts: 747
Joined: 2003-03-17 15:32

Prozessorlast bestimmen

Post by r00ty »

Hallo !
Gibts denn hier eine elegante Möglichkeit die momentane Prozessorlast und Idle Anteil zu bestimmen ?
Da ich es in einem Script brauche ist top auch im Batch-Mode nicht wirklich dafür geeignet.

/proc/loadavr gibt leider nicht den momentanen Wert.

eine Summation über ps auxf tut ist aber ungenau und auch nicht elegant.

Code: Select all

#! /bin/sh
for i in `ps auxf | awk '{ MEL = MEL + $3; print MEL }'`; do
        cpu=$i;
done
echo "Actual CPU load %: "$cpu;
gibts da noch was ? ein einfacher Shell- oder Perl-Befehl ? oder was in c ?
djholliday
Posts: 26
Joined: 2004-11-23 16:18
Location: Nürnberg

Re: Prozessorlast bestimmen

Post by djholliday »

Versuchs doch mal mit /proc/stat
man 5 proc

(...)
cpu 3357 0 4313 1362393
The number of jiffies (1/100ths of a second) that the
system spent in user mode, user mode with low priority
(nice), system mode, and the idle task, respectively.
The last value should be 100 times the second entry in
the uptime pseudo-file.
(...)
r00ty
Posts: 747
Joined: 2003-03-17 15:32

Re: Prozessorlast bestimmen

Post by r00ty »

der Tipp war gut, jetzt hab ich dieses Script hier gefunden:

Code: Select all

<?php

/**
* array getStat(string $_statPath)
*
* reads the /proc/stat file and parses out the pertinant information
*
* This function is meant to work in conjunction with getCpuUsage.  $_statPath should contain the
* path to stat (typicly /proc/stat)
*
* Returns an array of timings on success, false on failure
**/

function getStat($_statPath)
{
    if (trim($_statPath) == '')
    {
        $_statPath = '/proc/stat';
    }

    if (!is_readable($_statPath))
    {
        return false;
    }

    $stat = file($_statPath);

    if (substr($stat[0], 0, 3) == 'cpu')
    {
        $parts = explode(" ", preg_replace("!cpu +!", "", $stat[0]));
    }
    else
    {
        return false;
    }

    $return = array();
    $return['user'] = $parts[0];
    $return['nice'] = $parts[1];
    $return['system'] = $parts[2];
    $return['idle'] = $parts[3];
    return $return;
}

/**
* array getCpuUsage([string $_startpath])
*
* Returns an array of percentages representing the various CPU usage states
*
* The optional $_statPath variable should only be set if the stat file does not exist
* in /proc/stat
*
* Returns an array of percentages on success, terminates the script on failure
**/

function getCpuUsage($_statPath = '/proc/stat')
{
    $time1 = getStat($_statPath) or die("getCpuUsage(): couldn't access STAT path or STAT file invalidn");
    sleep(1);
    $time2 = getStat($_statPath) or die("getCpuUsage(): couldn't access STAT path or STAT file invalidn");

    $delta = array();

    foreach ($time1 as $k=>$v)
    {
        $delta[$k] = $time2[$k] - $v;
    }

    $deltaTotal = array_sum($delta);

    $percentages = array();

    foreach ($delta as $k=>$v)
    {
        $percentages[$k] = round($v / $deltaTotal * 100, 2);
    }
    return $percentages;
}

// output the structure of the resulting array
var_dump(getCpuUsage());

?>
das werd ich jetzt nur noch auf Konsolen-Ebene direkt runterbrechen