:: Anbieterverzeichnis :: Globale Branchen
:: SELFPHP Forum ::
Fragen rund um die Themen PHP?
In über
130.000 Beiträgen finden Sie sicher die passende
Antwort!
:: Newsletter ::
Abonnieren Sie hier den kostenlosen
SELFPHP Newsletter!
:: Qozido ::
Die Bilderverwaltung mit Logbuch für
Taucher und Schnorchler.
Datenbank-Traffic seit dessen Start ermitteln
Beispielaufgabe Datenbank-Traffic seit dessen Start ermitteln.
Beschreibung
Die Funktion
mysqlDbTraffic() berechnet den Traffic des MySQL-Servers seit dessen Start. Als Rückgabewert werden der gesendete Traffic, der empfangene Traffic sowie der Gesamt-Traffic in Bytes in einem Array geliefert. Zusätzlich formatiert die Funktion die jeweiligen Byte-Werte in die entsprechenden Größen ( z.B. MB, GB etc. ).
[bytes_received] => Die empfangenen Bytes
[bytes_sent] => Die gesendeten Bytes
[bytes_total] => Gesamt-Traffic in Bytes
[format_received] => Die empfangenen Bytes formatiert in MB, GB usw.
[format_sent] => Die gesendeten Bytes formatiert in MB, GB usw.
[format_total] => Der Gesamt-Traffic formatiert in MB, GB usw.
<?PHP
function mysqlDbTraffic ( ) {
$sql = "SHOW STATUS LIKE 'Bytes_%'" ;
$result = @ mysql_query ( $sql );
while( ( $row = @ mysql_fetch_array ( $result , MYSQL_ASSOC ) ) == true ){
if ( $row [ 'Variable_name' ] == 'Bytes_received' )
$Bytes_received = $row [ 'Value' ];
if ( $row [ 'Variable_name' ] == 'Bytes_sent' )
$Bytes_sent = $row [ 'Value' ];
}
$bytes [ 'bytes_received' ] = $Bytes_received ;
$bytes [ 'bytes_sent' ] = $Bytes_sent ;
$bytes [ 'bytes_total' ] = $Bytes_received + $Bytes_sent ;
$bytes [ 'format_received' ] = binary_multiples ( $Bytes_received , false );
$bytes [ 'format_sent' ] = binary_multiples ( $Bytes_sent , false );
$bytes [ 'format_total' ] = binary_multiples ( $Bytes_received + $Bytes_sent , false );
return $bytes ;
}
?>
Zusatzfunktionen
<?PHP
function binary_multiples ( $size , $praefix = true , $short = true )
{
if( $praefix === true )
{
if( $short === true )
{
$norm = array( 'B' , 'kB' , 'MB' , 'GB' , 'TB' , 'PB' ,
'EB' , 'ZB' , 'YB' );
}
else
{
$norm = array( 'Byte' ,
'Kilobyte' ,
'Megabyte' ,
'Gigabyte' ,
'Terabyte' ,
'Petabyte' ,
'Exabyte' ,
'Zettabyte' ,
'Yottabyte'
);
}
$factor = 1000 ;
}
else
{
if( $short === true )
{
$norm = array( 'B' , 'KiB' , 'MiB' , 'GiB' , 'TiB' , 'PiB' ,
'EiB' , 'ZiB' , 'YiB' );
}
else
{
$norm = array( 'Byte' ,
'Kibibyte' ,
'Mebibyte' ,
'Gibibyte' ,
'Tebibyte' ,
'Pebibyte' ,
'Exbibyte' ,
'Zebibyte' ,
'Yobibyte'
);
}
$factor = 1024 ;
}
$count = count ( $norm ) - 1 ;
$x = 0 ;
while ( $size >= $factor && $x < $count )
{
$size /= $factor ;
$x ++;
}
$size = sprintf ( "%01.2f" , $size ) . ' ' . $norm [ $x ];
return $size ;
}
?>
Anwendungsbeispiel
<?PHP
$conn = @ mysql_connect ( "localhost" , "mysql_user" , "mysql_password" );
if (! $conn ) {
echo "Fehlende Verbindung zur DB: " . mysql_error ();
exit;
}
print_r ( mysqlDbTraffic ( ) );
?>
Ausgabebeispiel: Browseransicht Array
(
[bytes_received] => 734298481
[bytes_sent] => 1497365812
[bytes_total] => 2231664293
[format_received] => 700.28 MiB
[format_sent] => 1.39 GiB
[format_total] => 2.08 GiB
)
:: Anbieterverzeichnis ::
Webhosting/Serverlösungen
Suchen Sie den für Sie passenden IT-Dienstleister für Ihr Webhosting-Paket oder Ihre Serverlösung?
Sie sind nur ein paar Klicks davon entfernt!
Ausgewählter Tipp im Bereich PHP-Skripte
Passwortschutz für Verzeichnisse
Weitere interessante Beispiele aus dem SELFPHP Kochbuch finden Sie im Bereich PHP-Skripte