This repository has been archived by the owner on Dec 6, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
gggeek
committed
Jun 26, 2012
1 parent
57a99c9
commit 4d3413d
Showing
5 changed files
with
290 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
<?php | ||
/** | ||
* Munin plugin script | ||
* | ||
* This script implements a munin wildcard plugin. | ||
* Designed to be invoked via the shell script ezmuninperflogger_ | ||
* | ||
* @author G. Giunta | ||
* @copyright (C) G. Giunta 2012 | ||
* @license Licensed under GNU General Public License v2.0. See file license.txt | ||
* | ||
*/ | ||
|
||
require 'autoload.php'; | ||
|
||
$cli = eZCLI::instance(); | ||
$script = eZScript::instance( array( 'description' => 'Makes available perf. variables for munin graphs', | ||
'use-session' => false, | ||
'use-modules' => true, | ||
'use-extensions' => true ) ); | ||
$script->startup(); | ||
$options = $script->getOptions( | ||
'[variable:][range:]', // options | ||
'[command]', // params | ||
array() ); | ||
$script->initialize(); | ||
|
||
if ( count( $options['arguments'] ) ) | ||
{ | ||
$command = $options['arguments'][0]; | ||
} | ||
else | ||
{ | ||
$command = 'fetch'; | ||
} | ||
|
||
// the way that munin wildcard plugins work is that many symlinks are created to the | ||
// plugin, appending graph name to the original plugin file name. The shell script | ||
// will pass us its own filename in the 'variable' option | ||
$variable = isset( $options['variable'] ) ? $options['variable'] : ''; | ||
$variable = preg_replace( '/$ezmuninperflogger_/', '', $variable ); | ||
|
||
// default munin range: 5 minutes | ||
$range = $options['range'] ? $options['range'] : 60 * 5; | ||
|
||
$ini = eZINI::instance( 'ezperformancelogger.ini' ); | ||
|
||
switch ( $command ) | ||
{ | ||
case 'autoconf': | ||
/// @todo | ||
$siteIni = eZINI::instance(); | ||
if ( !in_array( 'ezperformancelogger', $siteIni->variable( 'ExtensionSettings', 'ActiveExtensions' ) ) ) | ||
{ | ||
$cli->output( "no (extension ezperformancelogger not enabled)" ); | ||
$script->shutdown(); | ||
} | ||
if ( !in_array( 'csv', $ini->variable( 'GeneralSettings', 'LogMethods' ) ) ) | ||
{ | ||
$cli->output( "no (extension ezperformancelogger is not logging data to csv log files)" ); | ||
$script->shutdown(); | ||
} | ||
$cli->output( "yes" ); | ||
$script->shutdown(); | ||
break; | ||
|
||
case 'suggest': | ||
// this command is called by munin to get a list of graphs that this plugin supports | ||
// see http://munin-monitoring.org/wiki/ConcisePlugins | ||
foreach( $ini->variable( 'GeneralSettings', 'TrackVariables' ) as $var ) | ||
{ | ||
echo "$var\n"; | ||
} | ||
break; | ||
|
||
case 'config': | ||
echo "graph_category eZ Performance Logger\n"; | ||
$title = false; | ||
foreach( array( '', '_avg', '_min', '_max' ) as $suffix ) | ||
{ | ||
if ( $ini->hasVariable( 'MuninSettings', 'VariableDescription_' . $variable . $suffix ) ) | ||
{ | ||
foreach( $ini->variable( 'MuninSettings', 'VariableDescription_' . $variable . $suffix ) as $item => $value ) | ||
{ | ||
echo $suffix == '' ? "$item $value\n" : "{$variable}{$suffix}.$item $value\n"; | ||
if ( $item == 'graph_title' ) | ||
{ | ||
$title = true; | ||
} | ||
} | ||
} | ||
} | ||
if ( !$title ) | ||
{ | ||
echo "graph_title $variable\n"; | ||
|
||
} | ||
break; | ||
|
||
case 'fetch': | ||
default: | ||
if ( $variable == '' || !in_array( $variable, $ini->variable( 'GeneralSettings', 'TrackVariables' ) ) ) | ||
{ | ||
$cli->output( "Error: '$variable' is not a tracked perf. variable" ); | ||
$script->shutdown( -1 ); | ||
} | ||
|
||
$logMethods = $ini->variable( 'GeneralSettings', 'LogMethods' ); | ||
if ( in_array( 'csv', $logMethods ) ) | ||
{ | ||
$samples = 0; | ||
$values = array( 'total' => 0 ); | ||
|
||
if ( file_exists( $logfile = $ini->variable( 'csvSettings', 'FileName' ) ) ) | ||
{ | ||
$fp = fopen( $logfile, 'r' ); | ||
if ( $fp ) | ||
{ | ||
$now = time(); | ||
$then = $now - $range; | ||
$separator = $ini->variable( 'csvSettings', 'Separator' ); | ||
// we count from 1, as 1st element in csv is timestamp | ||
$i = 1; | ||
foreach( $ini->variable( 'GeneralSettings', 'TrackVariables' ) as $varname ) | ||
{ | ||
if ( $varname == $variable ) | ||
{ | ||
$pos = $i; | ||
break; | ||
} | ||
++$i; | ||
} | ||
|
||
while ( ( $buffer = fgets( $fp, 8192 ) ) !== false ) | ||
{ | ||
$ts = substr( $buffer, 0, strpos( $buffer, $separator ) ); | ||
/// @todo !important optimization: do not check ts for lower bound after 1st valid one found | ||
if ( $ts >= $then ) | ||
{ | ||
if ( $ts > $now ) | ||
{ | ||
break; | ||
} | ||
|
||
++$samples; | ||
$data = explode( $separator, $buffer ); | ||
$val = $data[$pos]; | ||
if ( !isset( $values['min'] ) || $val < $values['min'] ) | ||
{ | ||
$values['min'] = $val; | ||
} | ||
if ( !isset( $values['max'] ) || $val > $values['max'] ) | ||
{ | ||
$values['max'] = $val; | ||
} | ||
$values['total'] = $values['total'] + $val; | ||
} | ||
} | ||
fclose( $fp ); | ||
} | ||
} | ||
|
||
if ( $samples ) | ||
{ | ||
$values['avg'] = $values['total'] / $samples; | ||
echo "{$variable}_avg.value {$values['avg']}\n"; | ||
echo "{$variable}_min.value {$values['min']}\n"; | ||
echo "{$variable}_max.value {$values['max']}\n"; | ||
} | ||
else | ||
{ | ||
// no logfile or no samples in range | ||
echo "{$variable}_avg.value U\n"; | ||
echo "{$variable}_min.value U\n"; | ||
echo "{$variable}_max.value U\n"; | ||
} | ||
|
||
} | ||
else if ( in_array( 'logfile', $logMethods ) || in_array( 'apache', 'logmethods' ) ) | ||
{ | ||
/// @todo | ||
$cli->output( "Error: can not report variables because ezperflogger set to write data to Apache-formatted log files. Only csv files supported so far" ); | ||
$script->shutdown( -1 ); | ||
} | ||
else | ||
{ | ||
$cli->output( "Error: can not report variables because ezperflogger is not set to write data to log files" ); | ||
$script->shutdown( -1 ); | ||
} | ||
} | ||
|
||
$script->shutdown(); | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#!/bin/sh | ||
|
||
######################################## | ||
# | ||
# This is a Munin plugin, that allows display via Munin graphs of all performance | ||
# variables tracked by ezperformancelogger. | ||
# NB: for this to work, the logging method 'csv' must be enabled | ||
# (see ezperformancelogger.ini for details) | ||
# | ||
# This file has to be symlinked (or copied) into the Munin plugins directory. | ||
# It needs 2 config variables, which can be set in file /etc/munin/plugin-conf.d/ezmuninperflogger | ||
# | ||
# [ezmuninperflogger_*] | ||
# env.php /path/to/php | ||
# env.ezpublishroot /path/to/the/ez/publish/directory | ||
# | ||
######################################## | ||
|
||
# Magic markers: | ||
#%# family=manual | ||
#%# capabilities=autoconf suggest | ||
|
||
# Config variables setup | ||
|
||
# Directory where eZ Publish is installed. | ||
# @todo if it is not set in config file, use the 'find' command to look for extension/ezperformancelogger/bin/php/muninplugin.php, | ||
# and if only one is found, we can set up $EZPUBLISHROOT automagically | ||
EZPUBLISHROOT=${ezpublishroot:-/path/to/the/ez/publish/directory} | ||
|
||
# Location of the PHP Command Line Interface binary. | ||
# We init it to 'which php' if it is not set in config file | ||
PHP=${php} | ||
if [ -z "$PHP" ]; then | ||
PHP=`which php` | ||
if [ -z "$PHP" ]; then | ||
PHP=/usr/local/bin/php | ||
fi | ||
fi | ||
|
||
# Support automatic configuration of the plugin: | ||
# if $1 == "autoconf", test 1st for proper php and ez variables, and if they are, | ||
# call the php script with autoconf argument | ||
if [ "$1" = "autoconf" ]; then | ||
if [ ! -x "$PHP" ]; then | ||
echo "No ('$PHP' is not an executable, need to configure /etc/munin/plugin-conf.d/ezmuninperflogger)" | ||
exit 0 | ||
fi; | ||
# @todo test if $PHP is THE php executable by running eg. php -v ... | ||
if [ ! -f "$EZPUBLISHROOT/index.php" ]; then | ||
echo "No ('$EZPUBLISHROOT' is not an eZ Publish install, need to configure /etc/munin/plugin-conf.d/ezmuninperflogger)" | ||
exit 0 | ||
fi; | ||
if [ ! -f "$EZPUBLISHROOT/extension/ezperformancelogger/bin/php/muninplugin.php" ]; then | ||
echo "No ('$EZPUBLISHROOT' does not contain ezperformancelogger extension, probably need to configure /etc/munin/plugin-conf.d/ezmuninperflogge\)" | ||
exit 0 | ||
fi; | ||
fi | ||
|
||
# @todo if this is invoked via ". scriptname", $0 will be "-bash" ... | ||
VARIABLE=`basename $0` | ||
cd $EZPUBLISHROOT && $PHP extension/ezperformancelogger/bin/php/muninplugin.php --variable=$VARIABLE $* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters