Ankündigung

Einklappen
Keine Ankündigung bisher.

CPU-Auslastung auslesen unter Linux

Einklappen

Neue Werbung 2019

Einklappen
X
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • CPU-Auslastung auslesen unter Linux

    Hallo Leute,

    ich bin gerade neu hier und wollte gleich mal meine Lösung zur CPU-Auslastung auslesen unter Linux präsentieren.

    Ich heute den ganzen Nachmittag damit verbracht ne Lösung zu finden die ohne Zusatzpakete wie zb. phpsysinfo auskommt da mein Web-Server auf einem CentOS 4 ( ich weiß ist schon alt aber der letzte mit i586 unterstützung ) läuft.

    Wenn man mehr als einen CPU-Kern hat kann man $cores anpassen.


    Nun zum Code:
    PHP-Code:
    <?php
    $cores 
    1;

    $output1 null;
    $output2 null;

    // First output of /proc/stat
    exec("cat /proc/stat"$output1);

    // Set the time interval
    sleep(1);

    // Second output of /proc/stat
    exec("cat /proc/stat"$output2);

    // CPU total load
    $cpu_load_total 0;

    for ( 
    $i=$i $cores $i++ )
    {
      
    // Get informations from first row of /proc/stat
      
    $cpu_stat_1 explode(" "$output1$i ]);
      
    $cpu_stat_2 explode(" "$output2$i ]);

      
    // Init arrays
      
    $info1 = array( "user"   => $cpu_stat_1[1], 
                      
    "nice"   => $cpu_stat_1[2],
                      
    "system" => $cpu_stat_1[3], 
                      
    "idle"   => $cpu_stat_1[4]
                    );
                    
      
    $info2 = array( "user"   => $cpu_stat_2[1], 
                      
    "nice"   => $cpu_stat_2[2],
                      
    "system" => $cpu_stat_2[3], 
                      
    "idle"   => $cpu_stat_2[4]
                    );

      
    // Informations that should involve the calculation
      
    $idlesum $info2["idle"]-$info1["idle"] +
                 
    $info2["system"]-$info1["system"];

      
    // Sum the outputvalues
      
    $sum1 array_sum$info1 );
      
    $sum2 array_sum$info2 );

      
    // Calculate the cpu-load
      
    $load = ( - ( $idlesum / ( $sum2 $sum1 ) ) ) *100;
      
      
    $cpu_load_total += $load;
    }

    // Devide the total cpu load because we sum it in the loop
    $cpu_load_total /= $cores;

    // Round it to 2 decimals
    echo round$cpu_load_total).'% CPU-Load<br>';
    ?>
    Hier ein Praxisbeispiel:
    http://www.rherzog.com/cpu.php

    Natürlich lässt sich das ganze auch in eine Funktion packen.
    Man muss dann lediglich die letzte Zeile "echo" entfernen und mit "return $cpu_load_total;" ersetzen.

    Bei der eigenen Webseite bei einem Webhoster wirds wohl nicht klappen da diese den Zugriff auf /proc/stat unterbinden werden.

    Dieser Link kann auch helfen:
    http://de.wikipedia.org/wiki/Load#Pr...erte_bei_Linux

    Kritik zum Code erwünscht

    Grüße Ralf

  • #2
    PHP-Code:
    exec("cat /proc/stat"$output1); 
    Die Dateien kannst du auch einfach einlesen:
    PHP-Code:
    $output1 file('/proc/stat'); 
    Grüße.

    Kommentar


    • #3
      Da gibs auch x fertige Klassen für, goggledoo: 'php processor load'

      Kommentar

      Lädt...
      X