<?php
/**
* @author: Jörg Reinholz, fastix WebDesign & Consult, Kassel - http://www.fastix.org/
**/

function ip2link($str) {
    $arS[]='0.0.0.0';
    $arR[]='<span title="(0&dot;0&dot;0&dot;0) - Alle IPv4-Adressen">ALL_IP4</span> ';
    $arS[]='127.0.0.1';
    $arR[]='<span title="(127&dot;0&dot;0&dot;1) - Localhost, IPv4">LOCALHOST</span> ';
    $arS[]=':::';
    $arR[]='<span title="(:::) - Alle IPv6-Adressen">IP6</span>:';
    $arS[]='::1:';
    $arR[]='<span title="(::1) - Localhost, IPv6">LOC6</span>:';
    $arS[]='92.51.129.206';
    $arR[]='<span title="(92&dot;51&dot;129&dot;206) - Dieser Server">[THIS_ HOST ] </span>';
    $str=str_replace($arS, $arR, $str);
    $pattern='/([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})/';
    $replace='<a href="http://www.fastix.org/netztools/?addr=$1&amp;action=whois">$1</a>';
    return preg_replace($pattern, $replace, $str);
}


function sys_uptimes() {
    $arUptimes=explode(' ', trim(`uptime`));
    $a['prozessors']=trim(`grep processor /proc/cpuinfo |wc -l`);
    $a['days']=$arUptimes[2];
    $s = $s-$a['days']*60*60*24;
    list( $a['hours'], $a['minuts'] ) = explode(':', $arUptimes[4]);
    $a['minuts'] = str_replace(',', '', $a['minuts']);
    $a['syslast'] = round( str_replace( ',', '', $arUptimes[11] ) / $a['prozessors']*100, 2 );
    return $a;
}

function sys_mem_table() {
    $table='<table>
    <tr>
        <th> </th>
        <th>Gesamt:</th>
        <th>Benutzt:</th>
        <th>Frei:</th>
        <th>Shared:</th>
        <th>Puffer:</th>
        <th>Cached:</th>
    </tr>';
    $data=`/usr/bin/free -m | /bin/grep Mem 2>&1`;
    $data=trim(preg_replace('/[^0-9 \n]/', '', $data));
    $data=preg_replace('/ {2,}/', ' ', $data);
    $data=preg_replace('/\t/', ' ', $data);
    $table .= '             <tr>
                                <th>RAM:</th>
                                <td>' .  str_replace(' ', ' MB</td><td>', $data) . 'MB</td>
                        </tr>';
    $data=`/usr/bin/free -m | /bin/grep -E Swapi 2>&1`;
    $data=trim(preg_replace('/[^0-9 \n]/', '', $data));
    $data=preg_replace('/ {2,}/', ' ', $data);
    $data=preg_replace('/\t/', ' ', $data);
    $table .= '<tr><th>Swap:</th><td>' .  str_replace(' ', ' MB</td><td>', $data) . 'MB</td></tr>';
    $table.='</table>';
    return $table;
}

header("Content-Type: text/html; charset=UTF-8");
?><!doctype html>
<html>
  <head>
    <title>Server-Zustand</title>
    <meta http-equiv="refresh" content="60">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <style type="text/css">
      html,body {margin:0; padding:0;background-color:#fafafa;}
      h1 {margin-top:0;padding:.3em .5em .3em .5em;background-color:#ddd;border-bottom: 3px solid black;}
      #content {padding:0 1em 0 1em;}
      table {background-color:#ddd; font-size:.8em; margin-bottom:1em;}
      table tr {background-color:#fafafa;}
      table tr:hover {background-color:#cfc;}
      th,td {padding:.3em; min-width:8em;}
      th {padding-right:1em; text-align:left;}
      th,td.str {text-align:left;}
      td,th.int,th.dec {text-align:right;}
      tr.aktiv td {background-color:#ffc;}
      .buttonleiste {font-size:1em;}
      #start_ping_button, #stop_ping_button,#reset_button,#cache_button {display:inline-block;font-size:.8em;width:10em;border:none;border-radius:1em;padding:.5em 1em .5em 1em;box-shadow:2px 2px 2px gray;margin:0;vertical-align:middle;text-align:center;cursor:pointer;}
      #cache_button {background-color:#ffc;}
      .download {margin-top:2em;padding-top:1em;border-top:1px solid black;}
    </style>
  </head>
  <body>
    <h1>Dieser Webserver funktioniert!</h1>
    <div id="content">
<h2>Systemüberblick:</h2>
<?php
$a=sys_uptimes();
print "<p>". htmlspecialchars(`lsb_release -d | sed "s/Description://"`) ." auf " . $a['prozessors'] . " Prozessoren läuft seit " . $a['days'] . " Tagen, " . $a['hours'] . " Stunden und " . $a['minuts'] . " Minuten<br />
Aktuelle Systemauslastung: " . $a['syslast'] . "%</p>";
print "<p>Kernel: " . htmlspecialchars(`uname -ri`) . "</p>";
print sys_mem_table("", "meminfo");
print "<h2>Plattenauslastung:</h2><pre>";
print `df -h`;
print "</pre>";
print "<h2>Aktuelle Prozessorlast:</h2><pre>";
print `uptime`;
print "</pre>";
print "<h2>Offene (Standard-) Ports:</h2><pre>";
print '<a href="http://www.fastix.org/netztools/?addr=fastix.org&amp;action=nmap">von außen testen</a>';
print "</pre>";
print "<h2>Erreichbarkeit:</h2><pre>";
print '<a href="http://www.fastix.org/netztools/?addr=fastix.org&amp;action=ping">von außen testen</a>';
print "</pre>";
print "<h2>TCP-Verbindungen:</h2><pre>";
print ip2link(`netstat -an | grep "tcp"`);
print "</pre>";
print "<h2>UDP-Verbindungen:</h2><pre>";
print ip2link(`netstat -an | grep "udp"`);
print "</pre>";
print '<h2>Netzwerk:</h2><pre>';
print ip2link(`ifconfig`);
print "</pre>";
?>
    </div>
  </body>
</html>