SystemLast

Aus Programmers Guide

Wechseln zu: Navigation, Suche
<?php
/**
 *    vmstat_extract()
 *
 *    @param    param_vmstat_output    Ausgabe die von vmstat erzeugt wurde.
 *
 *    Diese Funktion verarbeitet die Ausgabe eines vmstat-Aufrufs
 *    (siehe [url]http://www.rt.com/man/vmstat.8.html[/url]) und liefert ein assoziatives
 *    Array mit allen Informationen. Die Spalten können je nach System
 *    variieren (siehe [url]http://phplens.com/phpeverywhere/node/view/21[/url])
 */
function cpuLast() {
	//Linux
	//Swap              : {$vmstat_stats_linux['swpd']}
	//CPU Usage (User)  : {$vmstat_stats_linux['us']}
	//CPU Usage (System): {$vmstat_stats_linux['sy']}
	//CPU Usage (Idle)  : {$vmstat_stats_linux['id']}
 
	$param_vmstat_output = "procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
	 r  b   swpd   free   buff  cache   si   so    bi    bo   in    cs us sy id wa
	 0  0  16640 119332  28760 310896    0    0    12    32    5    17  3  0 97  0";
 
	// Initialisierung
	$vmstat_assoc = array();
 
	// Aufsplitten der Zeilen
	$vmstat_lines = explode("\n", trim($param_vmstat_output));
 
	// Zeile mit Spaltennamen extrahieren
	$vmstat_columns = trim($vmstat_lines[1]);
	// Überflüssige Leerzeichen vor dem Splitten entfernen
	while(!(strpos($vmstat_columns, "  ") === false)) {
		$vmstat_columns = str_replace("  ", " ", $vmstat_columns);
	}
	$vmstat_columns = explode(" ",  $vmstat_columns);
 
	// Zeile mit den Messwerten extrahieren
	$vmstat_stats = trim($vmstat_lines[2]);
	// Überflüssige Leerzeichen vor dem Splitten entfernen
	while(!(strpos($vmstat_stats, "  ") === false)) {
		$vmstat_stats = str_replace("  ", " ", $vmstat_stats);
	}
	$vmstat_stats = explode(" ",  $vmstat_stats);
	// Assoziatives Array mit vmstat Bezeichnungen erstellen
	for($num_col = 0; $num_col < count($vmstat_columns); $num_col++) {
		$vmstat_assoc[$vmstat_columns[$num_col]] = intval($vmstat_stats[$num_col]);
	}
 
return round($vmstat_assoc['us'] + $vmstat_assoc['sy'])."%";
}
 
?>
Persönliche Werkzeuge