/usr/share/serverstats/update.php is in serverstats 0.8.2-10.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | #!/usr/bin/php
<?php
/**
* $Id$
*
* Author: David Danier, david.danier@team23.de
* Project: Serverstats, http://serverstats.berlios.de/
* License: GPL v2 or later (http://www.gnu.org/licenses/gpl.html)
*
* Copyright (C) 2005 David Danier
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// Abort if the script is run by the webserver
if (!isset($_SERVER["argv"][0])) {
die('<br /><strong>Read the README</strong>');
}
// Load all needed classes, function and everything else
require_once('init.php');
foreach ($config['sources'] as $sourcename => $sourcedata)
{
echo "Working on $sourcename" . PHP_EOL;
$source = $sourcedata['module'];
$cachefile = CACHEPATH . $sourcename . '.sav';
// The classes may throw exceptions, so we need to catch them
try
{
if (!($source instanceof source))
{
throw new Exception('Source "' . $sourcename . '" not instanceof source');
}
// Init the source
$source->init();
// Init/load cache if needed
if ($source instanceof source_cached)
{
if (file_exists($cachefile))
{
$cache = unserialize(file_get_contents($cachefile));
$source->loadCache($cache);
unset($cache);
}
else
{
$source->initCache();
}
}
// Refresh data
$source->refreshData();
// Save cache (if needed)
if ($source instanceof source_cached)
{
$cache = serialize($source->getCache());
file_put_contents($cachefile, $cache);
unset($cache);
}
// Receive values
$sourcevalues = $source->fetchValues();
// If the source needs an RRD: create and update it
if ($source instanceof source_rrd)
{
// Needed vars
$rrdfile = RRDPATH . $sourcename . '.rrd';
$sourcerrd = new rrd($config['main']['rrdtool'], $rrdfile);
// Create RRD-file if it does not exist
if (!file_exists($rrdfile))
{
if ($sourcerrd->checkVersion('<', '1.2'))
{
throw new Exception('rrdtool >= 1.2 required');
}
echo "\tCreating RRD-file" . PHP_EOL;
$config['log']['logger']->logString(logger::INFO, 'Creating RRD-file for source "' . $sourcename . '"');
$source->initRRD($sourcerrd);
$sourcerrd->setStep($config['main']['step']);
if (isset($sourcedata['rra']))
{
$sourcerra = $sourcedata['rra'];
}
else
{
$sourcerra = 'default';
}
foreach ($config['rra'][$sourcerra] as $rra)
{
$sourcerrd->addArchive($rra['cf'], $rra['xff'], $rra['steps'], $rra['rows']);
}
$sourcerrd->create();
}
// Update RRD-file
echo "\tUpdating RRD-file" . PHP_EOL;
$config['log']['logger']->logString(logger::INFO, 'Updating source "' . $sourcename . '"');
foreach ($sourcevalues as $valuename => $value)
{
$sourcerrd->setValue($valuename, $value);
}
$sourcerrd->update();
}
// If the source is monitored: check all values
if (isset($config['monitor'][$sourcename]))
{
// Needed vars
$monitorcachefile = CACHEPATH . $sourcename . '.monitor.sav';
$monitorcache = array();
// Load cache if needed
if (file_exists($monitorcachefile))
{
$monitorcache = unserialize(file_get_contents($monitorcachefile));
}
// Check all values
echo "\tMonitoring values" . PHP_EOL;
foreach ($config['monitor'][$sourcename] as $rulenum => $rule)
{
// Init cache if needed
if (!isset($monitorcache[$rulenum]))
{
$monitorcache[$rulenum] = false;
}
$matches = array();
// Test if we have a valid test
if (preg_match('/^([a-zA-Z0-9_]+)\s*(>=?|<=?|!?=)\s*(.*)$/', $rule, $matches))
{
$name = $matches[1];
$operator = $matches[2];
$limit = $matches[3];
// Test if we know the datasource
if (!isset($sourcevalues[$name]))
{
$config['log']['logger']->logString(logger::WARN, "Invalid or undefined datasource in rule: '$rule' (Source '$sourcename')");
$value = 'U';
}
else
{
$value = $sourcevalues[$name];
}
// Test if limit is hit
if (value_compare($value, $limit, $operator))
{
echo "\t\tLimit hit: $rule ($sourcename), current: $value" . PHP_EOL;
if (!$monitorcache[$rulenum])
{
$config['log']['logger']->logString(logger::ERR, "Limit hit: '$rule' (Source '$sourcename'), current value: $value");
}
$monitorcache[$rulenum] = true;
}
else
{
$monitorcache[$rulenum] = false;
}
}
// Handle invalid rules
else
{
throw new Exception("Invalid rule: '$rule' (Source '$sourcename')");
}
}
// Save Cache
file_put_contents($monitorcachefile, serialize($monitorcache));
}
}
catch (Exception $e)
{
echo "Error:" . PHP_EOL;
echo $e;
echo PHP_EOL;
$config['log']['logger']->logException(logger::ERR, $e);
}
}
?>
|