Skip to content

Instantly share code, notes, and snippets.

@amr
Created July 1, 2010 08:05
Show Gist options
  • Save amr/459708 to your computer and use it in GitHub Desktop.
Save amr/459708 to your computer and use it in GitHub Desktop.
<?php
/**
* @file
* Retrieves various load indicators from servers using SNMP.
*
* Licensed under GPL v3 or later.
* Copyright 2009 (c) Egypt Development Center.
*/
/**
* Configuration
*/
$servers = array(
'localhost' => array(
'name' => 'Localhost',
'community' => 'public',
'hostname' => 'localhost',
),
'localhost2' => array(
'name' => 'Localhost 2',
'community' => 'public',
'hostname' => 'localhost',
),
'localhost3' => array(
'name' => 'Localhost 3',
'community' => 'public',
'hostname' => 'localhost',
),
);
/**
* YOU SHOULD NOT NEED TO MODIFY ANYTHING PAST THIS POINT.
*/
/**
* Main
*/
if (!extension_loaded('snmp')) {
die('SNMP module is required.');
}
snmp_set_valueretrieval(SNMP_VALUE_PLAIN);
// Collect data
$data = array();
foreach ($servers as $id => $server) {
$data[$id] = array();
// Load average
$entries = snmpwalkoid($server['hostname'], $server['community'], 'UCD-SNMP-MIB::laLoad');
$keys = array_map('simplify_oid', array_keys($entries));
$data[$id]['load average'] = array_combine($keys, $entries);
// Memory
$entries = snmpwalkoid($server['hostname'], $server['community'], 'UCD-SNMP-MIB::memory');
$keys = array_map('simplify_oid', array_keys($entries));
$data[$id]['memory'] = array_combine($keys, $entries);
}
// Render
$output = '';
foreach ($data as $id => $info) {
$output .= theme_server($servers[$id]['name'], $info);
}
echo theme_page($output);
function simplify_oid($oid) {
list($mib, $object) = explode('::', $oid);
return $object;
}
function theme_server($name, $info) {
$output = '';
$output .= '<div class="server">';
$output .= '<h3>'. $name .'</h3>';
$output .= '<dl>';
// Load average
$output .= '<dt>Load average</dt>';
$output .= '<dd>'. theme_entry('Last minute', $info['load average']['laLoad.1']) .'</dd>';
$output .= '<dd>'. theme_entry('Last 5 minutes', $info['load average']['laLoad.2']) .'</dd>';
$output .= '<dd>'. theme_entry('Last 15 minutes', $info['load average']['laLoad.3']) .'</dd>';
$output .= '</dt>';
// Memory usage
$output .= '<dt>Memory</dt>';
$output .= '<dd>'. theme_entry('Total', round_size($info['memory']['memTotalReal.0'] * 1024)) .'</dd>';
$output .= '<dd>'. theme_entry('Free', round_size($info['memory']['memAvailReal.0'] * 1024), 'Free and not utilized') .'</dd>';
$output .= '<dd>'. theme_entry('Cached', round_size($info['memory']['memCached.0'] * 1024), 'Free, but utilized for disk cache') .'</dd>';
$output .= '<dd>'. theme_entry('Total free', round_size(($info['memory']['memAvailReal.0'] + $info['memory']['memCached.0']) * 1024), 'Free + cached') .'</dd>';
$output .= '</dt>';
$output .= '</dl>';
$output .= '</div>';
return $output;
}
function theme_entry($label, $value, $description = '') {
if (!empty($description)) {
$description = '<span class="description">('. $description .')</span>';
}
return '<span class="label">'. $label .':</span><span class="value">'. $value .'</span>'. $description;
}
// From: http://snipplr.com/view/4633/convert-size-in-kb-mb-gb-/
function round_size($size) {
$filesizename = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
return $size ? round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $filesizename[$i] : '0 Bytes';
}
function theme_page($page) {
$output = <<<PAGE_HEADER
<!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">
<head>
<title>Load watch</title>
<style type="text/css">
body {
font-size: 88%;
}
div.server {
float: left;
width: 280px;
border: 1px solid #ccc;
padding: 10px;
margin-right: 3px;
margin-bottom: 3px;
}
div.server h3 {
margin: 0;
}
dt {
font-weight: bold;
clear: both;
color: darkblue;
}
dd {
margin-left: 20px;
clear: both;
}
.label {
display: block;
float: left;
font-weight: bold;
margin-right: 5px;
width: 150px;
}
.description {
display: none;
margin-left: 10px;
color: #666;
}
</style>
</head>
<body>
PAGE_HEADER;
$output .= $page;
$output .= '</body></html>';
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment