You are not logged in.

#1 2009-07-26 14:16:19

Barrucadu
Member
From: York, England
Registered: 2008-03-30
Posts: 1,158
Website

Archlinux server status script in PHP

Screenshot: http://www.barrucadu.co.uk/server/serverstatus.png

This script shows system information (you'll have to tweak the commands using sensors for your system), and daemons. Also displays a notice if a daemon isn't running. I've divided its display of daemons into three parts - all, services, and utilities. I've called a daemon a service if the outside world benefits directly from it, and a utility if it's more of an internal thing. For example, samba would be a service and crond a utility.

PHP script:

<?php

function daemon_running($daemon)
{
  $checkpath = '/var/run/daemons';
  return exec("ls -l {$checkpath} | grep {$daemon}");
}

$name = 'Eihort';

$statuses = array('Northbridge fan speed' => array('', 'sensors | grep fan1 | sed -e "s/fan1:\s*\([0-9]*\).*/\\1/"', ' RPM'),
                  'CPU temperature'       => array('', 'sensors | grep temp3 | sed -e "s/temp3:\s*+\([0-9]*\).*/\\1/"', '°C'),
                  'HDD temperature'       => array('', 'sudo hddtemp -n /dev/sda', '°C'),
                  'Memory usage'          => array('', 'free -m | grep "buffers/cache" | sed -e "s/-\/+ buffers\/cache:\s*\([0-9]*\)\s*\([0-9]*\).*/\\1 \/ \\2/"', ' MB'),
                  'Load averages'         => array('', 'uptime | sed "s/.*load average: \(.*\)/\\1/"', ''),
                  'Uptime'                => array('', 'uptime | sed "s/.*up\s*\([0-9\:]*\).*/\\1/"', ''),
                  'Package updates'       => array('', '/usr/local/bin/updates', ''),
                  'Logged in users'       => array('', '/usr/local/bin/userson', ''));

$services = array('bitlbee'  => 'Instant messaging gateway.',
                  'httpd'    => 'The Apache web server.',
                  'mysqld'   => 'MySQL Database server.',
                  'named'    => 'BIND9 DNS server.',
                  'openntpd' => 'Network time server.',
                  'rtorrent' => 'Torrent client,',
                  'samba'    => 'File-sharing system.',
                  'sshd'     => 'Secure Shell',
                  'vsftpd'   => 'Very Secure FTP Daemon.');

$utilities = array('ivman'        => 'Volume manager.',
                   'sensors'      => 'Hardware monitor.',
                   'net-profiles' => 'Network manager.',
                   'crond'        => 'Task scheduler.',
                   'hal'          => 'Hardware Abstraction Layer.',
                   'dbus'         => 'IPC Bus.',
                   'syslog-ng'    => 'System monitor.',
                   'uptimed'      => 'Uptime recorder.');

$output = array('status'    => array(),
                'services'  => array(),
                'utilities' => array());

foreach($statuses as $status => $details)
  {
    $output['status'][] = array($status, $details[0] . shell_exec($details[1]) . $details[2]);
  }

foreach($services as $service => $description)
  {
    $output['services'][] = array($service, daemon_running($service) ? "On" : "Off");
  }

foreach($utilities as $utility => $description)
  {
    $output['utilities'][] = array($utility, daemon_running($utility) ? "On" : "Off");
  }
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
  <head>
    <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf8"/>
    <meta name="author" content="Michael Walker"/>
    <meta name="robots" content="FOLLOW,INDEX"/>

    <title><?php echo $name; ?> Status</title>

    <link rel="stylesheet" href="style.css" type="text/css" />
  </head>

  <body>
    <div id="header">
      <h1><?php echo $name; ?></h1>      
        <table>
<?php

      $half = count($output['status']) / 2;
      for($i = 0; $i < count($output['status']); $i ++)
        {
          if($i == $half) echo '</table><table>';

          $j = ($i < $half) ? $i + $half : $i - $half;

          echo '<tr>';
          echo "<td class=\"head\">{$output['status'][$j][0]}</td>";
          echo "<td>{$output['status'][$j][1]}</td>";
          echo '</tr>';
        }
?>
      </table>
    </div>

    <ul id="tabs">
      <li><a href="/">All</a></li>
      <li><a href="/?type=services">Services</a></li>
      <li><a href="/?type=utilities">Utilities</a></li>
    </ul>

    <div id="content">
      <table>
<?php
$pool = array_merge($output['services'], $output['utilities']);
if(isset($_GET['type']) && isset($output[$_GET['type']])) {
  $pool = $output[$_GET['type']];
}

$odd = True;

foreach($pool as $info)
  {
    $summary = (isset($services[$info[0]])) ? $services[$info[0]] : $utilities[$info[0]];
    echo ($odd) ?'<tr class="odd">' : '<tr>';
    echo "<td class=\"head\">{$info[0]}<br/><span class=\"summary\">{$summary}</span></td>";
    echo ($info[1] == "On") ? '<td class="good">Up</td>' : '<td class="bad">Down</td>';
    echo '</tr>';
    $odd = !$odd;
  }
?>
      </table>
    </div>
  </body>
</html>

This is configured for my server, but I'm sure you can figure out what you need to change.

The CSS file:

body
{
    background-color:#E2E5B4;
    font-family:Liberation Sans, DejaVu Sans, Verdana, Arial, sans-serif;
    font-size:13px;
    color:#474750;
    padding:0px 0px 10px 0px;
    margin:0px;
}

div#header
{
    border-bottom:3px solid #C8A654;
    padding:16px;
    background-color:#313D40;
}

div#header h1
{
    display:inline;
    font-size:7em;
    color:#74B331 ;
}

div#header table
{
    position:relative;
    right:64px;
    margin:0px 16px 0px 16px;
    float:right;
    color:#93FF20;
    border:1px solid #;
}

div#header table td
{
    padding:3px;
}

div#header table td.head
{
    width:200px;
    font-weight:bold;
}

ul#tabs
{
    list-style:none;
    position:relative;
    top:-3px;
    padding:0px;
    margin:0px;
}

ul#tabs li {
    float:left;
    margin:0px 16px 0px 16px;
    width:100px;
    border-left:3px solid #C8A654;
    border-right:3px solid #C8A654;
    border-bottom:3px solid #C8A654;
    background-color:#313D40;
    text-align:center;
}

ul#tabs li a
{
    color:#FF3709;
    font-weight:bold;
    font-size:10px;
    text-decoration:none;
}

ul#tabs li a:hover
{
    font-weight:normal;
}

div#content
{
    clear:both;
    padding:24px 128px 0px 128px;
    margin:0px;
}

div#content table
{
    border-spacing:0px;
    border:3px solid #C8A654;
    color:#FDFED2;
    width:100%;
}

div#content td.head
{
    font-weight:bold;
    width:95%;
}

div#content tr td
{
    background-color:#3E4147;
    padding:10px;
}

div#content tr.odd td
{
    background-color:#27333A;
}

div#content span.summary
{
    font-weight:normal;
    font-size:10px;
}

div#content td.good
{
    color:#A4D933;
}

div#content td.bad
{
    color:#FF3709;
}

Note: in the services and utilities array it's not the name of the daemon, but the name of the file it creates in /var/run/daemons/.
Note 2: The /usr/local/bin/updates is just a script to grab the number of updates from pacman.

Last edited by Barrucadu (2009-07-26 14:54:28)

Offline

#2 2009-07-28 03:16:06

sand_man
Member
From: Australia
Registered: 2008-06-10
Posts: 2,164

Re: Archlinux server status script in PHP

I like it!
I'll give this a crack when I get home tonight.


neutral

Offline

Board footer

Powered by FluxBB