From 9db670ee113c89e416b80b71cf17f59aa5e78e61 Mon Sep 17 00:00:00 2001 From: Olaf Rempel Date: Thu, 3 Aug 2006 17:57:11 +0200 Subject: [PATCH] add graph script --- sammler.conf | 4 +- sammler.php | 482 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 484 insertions(+), 2 deletions(-) create mode 100644 sammler.php diff --git a/sammler.conf b/sammler.conf index f714ba3..855c22a 100644 --- a/sammler.conf +++ b/sammler.conf @@ -11,10 +11,10 @@ plugin p_uptime.so plugin p_netdev.so plugin p_mount.so #plugin p_ctstat.so -plugin p_rtstat.so +#plugin p_rtstat.so plugin p_random.so -rrd_dir rrd +rrd_dir /var/lib/rrd step 10 diff --git a/sammler.php b/sammler.php new file mode 100644 index 0000000..7683c00 --- /dev/null +++ b/sammler.php @@ -0,0 +1,482 @@ +sammler graph setup\n"; + echo "\n"; + echo "\n"; + echo "
\n"; + echo "\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo "\n"; + foreach ($conf['views'] as $name => $value) { + echo "\n"; + echo "\n"; + if ($conf['default_view'] == $value) + echo "\n"; + else + echo "\n"; + + echo "\n"; + } + + echo "\n"; + + echo "\n"; + foreach ($conf['hosts'] as $hostid => $host) { + echo "\n"; + echo "\n"; + + echo "\n"; + + if ($host['show']) + echo ""; + else + echo ""; + + echo "\n"; + + foreach ($host['rrds'] as $rrdid => $rrd) { + echo "\n"; + echo "\n"; + + echo "\n"; + + if ($rrd['show']) + echo ""; + else + echo ""; + + echo "\n"; + } + + echo "\n"; + } + + echo "\n"; + echo "
Graph Height:
Graph Width:

Available Views:default
({$value}s)
add view:

Hostname:up \n"; + echo "downshowshow
  {$rrd['rrd']}up \n"; + echo "downshowshow

\n"; + echo " \n"; + echo " \n"; + echo "\n"; + echo "
\n"; + echo "\n"; +} + +function show_html($conf) { + echo "Stats\n"; + echo "\n"; + echo "\n"; + + $hostid = isset($_GET['host']) ? $_GET['host'] : 0; + $view = isset($_GET['view']) ? $_GET['view'] : $conf['default_view']; + + echo "Select Host:\n"; + echo " \n"; + + echo "

\n"; + + + foreach ($conf['hosts'][$hostid]['rrds'] as $rrdid => $rrd) { + if (!$rrd['show']) + continue; + + echo "

\n"; + } + + echo "\n"; +} + + +function read_config($filename) { + $handle = @fopen($filename, "r"); + if ($handle !== false && filesize($filename) > 0) { + $contents = fread($handle, filesize($filename)); + fclose($handle); + + return unserialize($contents); + } +} + +function write_config($filename, $config) { + $handle = fopen($filename, "w+"); + if ($handle !== false) { + $contents = serialize($config); + fwrite($handle, $contents); + fclose($handle); + } +} + +function get_hostdirs() { + $retval = array(); + $dh = opendir(BASE_DIR); + while (($file = readdir($dh)) !== false) + if (is_dir(BASE_DIR.$file) && $file != "." && $file != "..") + $retval[] = $file; + + closedir($dh); + return $retval; +} + +function get_rrdfiles($directory) { + $retval = array(); + $dh = opendir(BASE_DIR.$directory); + while (($file = readdir($dh)) !== false) { + if (is_dir(BASE_DIR.$file)) + continue; + + if (strstr($file, "rrd") !== false) + $retval[] = $file; + } + closedir($dh); + return $retval; +} + +function create_config() { + $conf = array(); + $conf['default_view'] = 86400; + $conf['views'] = array("6 hours" => 21600, "1 day" => 86400, "1 week" => 604800, "1 month" => 2678400); + $conf['height'] = 80; + $conf['width'] = 400; + $conf['hosts'] = array(); + + $rrds = array(); + $tmp = get_rrdfiles(''); + foreach ($tmp as $file) { + $rrds[] = array( + 'show' => true, + 'title' => str_replace(".rrd", "", basename($file)), + 'rrd' => $file, + ); + } + + $conf['hosts'][] = array( + 'hostname' => 'localhost', + 'show' => true, + 'rrds' => $rrds, + ); + + $hosts = get_hostdirs(); + foreach ($hosts as $host) { + $rrds = array(); + $tmp = get_rrdfiles($host); + foreach ($tmp as $file) { + $rrds[] = array( + 'show' => true, + 'title' => str_replace(".rrd", "", basename($file)), + 'rrd' => "$host/$file", + ); + } + + $conf['hosts'][] = array( + 'hostname' => $host, + 'show' => true, + 'rrds' => $rrds, + ); + } + + return $conf; +} + +function setup($conf) { + if (isset($_GET['sub']) && $_GET['sub'] == "move_host_up") { + $hostid = isset($_GET['host']) ? $_GET['host'] : -1; + if (isset($conf['hosts'][$hostid]) && isset($conf['hosts'][$hostid -1])) { + $a = $conf['hosts'][$hostid]; + $b = $conf['hosts'][$hostid -1]; + + $conf['hosts'][$hostid] = $b; + $conf['hosts'][$hostid -1] = $a; + } + return $conf; + + } else if (isset($_GET['sub']) && $_GET['sub'] == "move_host_down") { + $hostid = isset($_GET['host']) ? $_GET['host'] : -1; + if (isset($conf['hosts'][$hostid]) && isset($conf['hosts'][$hostid +1])) { + $a = $conf['hosts'][$hostid]; + $b = $conf['hosts'][$hostid +1]; + + $conf['hosts'][$hostid] = $b; + $conf['hosts'][$hostid +1] = $a; + } + return $conf; + + } else if (isset($_GET['sub']) && $_GET['sub'] == "move_rrd_up") { + $hostid = isset($_GET['host']) ? $_GET['host'] : -1; + $rrdid = isset($_GET['rrd']) ? $_GET['rrd'] : -1; + if (isset($conf['hosts'][$hostid]['rrds'][$rrdid]) && isset($conf['hosts'][$hostid]['rrds'][$rrdid -1])) { + $a = $conf['hosts'][$hostid]['rrds'][$rrdid]; + $b = $conf['hosts'][$hostid]['rrds'][$rrdid -1]; + + $conf['hosts'][$hostid]['rrds'][$rrdid] = $b; + $conf['hosts'][$hostid]['rrds'][$rrdid -1] = $a; + } + return $conf; + + } else if (isset($_GET['sub']) && $_GET['sub'] == "move_rrd_down") { + $hostid = isset($_GET['host']) ? $_GET['host'] : -1; + $rrdid = isset($_GET['rrd']) ? $_GET['rrd'] : -1; + if (isset($conf['hosts'][$hostid]['rrds'][$rrdid]) && isset($conf['hosts'][$hostid]['rrds'][$rrdid +1])) { + $a = $conf['hosts'][$hostid]['rrds'][$rrdid]; + $b = $conf['hosts'][$hostid]['rrds'][$rrdid +1]; + + $conf['hosts'][$hostid]['rrds'][$rrdid] = $b; + $conf['hosts'][$hostid]['rrds'][$rrdid +1] = $a; + } + return $conf; + + } else if (!empty($_POST)) { + $conf['default_view'] = $_POST['default_view']; + $conf['height'] = $_POST['height']; + $conf['width'] = $_POST['width']; + + $conf['views'] = array(); + foreach ($_POST['views'] as $name) { + if (empty($name)) + continue; + + $tmp = strtotime($name); + if ($tmp != -1) + $conf['views'][$name] = $tmp - time(); + } + natsort($conf['views']); + + foreach ($_POST['hosts'] as $hostid => $host) { + $conf['hosts'][$hostid]['hostname'] = $host['hostname']; + $conf['hosts'][$hostid]['show'] = ($host['show'] == "on"); + + foreach ($host['rrds'] as $rrdid => $rrd) { + $conf['hosts'][$hostid]['rrds'][$rrdid]['title'] = $_POST['hosts'][$hostid]['rrds'][$rrdid]['title']; + $conf['hosts'][$hostid]['rrds'][$rrdid]['show'] = ($_POST['hosts'][$hostid]['rrds'][$rrdid]['show'] == "on"); + } + } + return $conf; + + } else { + setup_html($conf); + } +} + +function show_rrd($conf) { + $hostid = isset($_GET['host']) ? $_GET['host'] : -1; + $rrdid = isset($_GET['rrd']) ? $_GET['rrd'] : -1; + + if (!isset($conf['hosts'][$hostid]['rrds'][$rrdid]['rrd'])) + return; + + $view = isset($_GET['view']) ? -$_GET['view'] : -$conf['default_view']; + $rrdfile = BASE_DIR.$conf['hosts'][$hostid]['rrds'][$rrdid]['rrd']; + $title = $conf['hosts'][$hostid]['rrds'][$rrdid]['title']; + $height = $conf['height']; + $width = $conf['width']; + + $cmd = RRDTOOL." graph - --imgformat PNG --start {$view} --end -10 --title \"{$title}\" --rigid "; + + $tmp = explode('-', basename($rrdfile)); + $tmp = explode('_', $tmp[0]); + $tmp = explode('.', $tmp[0]); + switch ($tmp[0]) { + case 'cpu': + $height *= 2; + $cmd .= "--base=1000 --height={$height} --width={$width} --alt-autoscale-max --lower-limit=0 --vertical-label=\"percent\" ". + "DEF:a={$rrdfile}:user:AVERAGE ". + "DEF:b={$rrdfile}:nice:AVERAGE ". + "DEF:c={$rrdfile}:syst:AVERAGE ". + "DEF:d={$rrdfile}:idle:AVERAGE ". + "DEF:e={$rrdfile}:wait:AVERAGE ". + "DEF:f={$rrdfile}:intr:AVERAGE ". + "DEF:g={$rrdfile}:sitr:AVERAGE ". + 'AREA:c#FF0000:"System " GPRINT:c:LAST:"Current\:%8.2lf %s" GPRINT:c:AVERAGE:"Average\:%8.2lf %s" GPRINT:c:MAX:"Maximum\:%8.2lf %s\n" '. + 'STACK:f#EA8F00:"IRQ " GPRINT:f:LAST:"Current\:%8.2lf %s" GPRINT:f:AVERAGE:"Average\:%8.2lf %s" GPRINT:f:MAX:"Maximum\:%8.2lf %s\n" '. + 'STACK:g#FFFF00:"Soft-IRQ" GPRINT:g:LAST:"Current\:%8.2lf %s" GPRINT:g:AVERAGE:"Average\:%8.2lf %s" GPRINT:g:MAX:"Maximum\:%8.2lf %s\n" '. + 'STACK:e#0000FF:"IO-Wait " GPRINT:e:LAST:"Current\:%8.2lf %s" GPRINT:e:AVERAGE:"Average\:%8.2lf %s" GPRINT:e:MAX:"Maximum\:%8.2lf %s\n" '. + 'STACK:a#00CFCF:"User " GPRINT:a:LAST:"Current\:%8.2lf %s" GPRINT:a:AVERAGE:"Average\:%8.2lf %s" GPRINT:a:MAX:"Maximum\:%8.2lf %s\n" '. + 'STACK:b#00CF00:"Nice " GPRINT:b:LAST:"Current\:%8.2lf %s" GPRINT:b:AVERAGE:"Average\:%8.2lf %s" GPRINT:b:MAX:"Maximum\:%8.2lf %s\n" '; + break; + + case 'load': + $cmd .= "--base=1000 --height={$height} --width={$width} --alt-autoscale-max --lower-limit=0 --vertical-label=\"load\" ". + "DEF:a={$rrdfile}:1min:AVERAGE ". + "DEF:b={$rrdfile}:5min:AVERAGE ". + "DEF:c={$rrdfile}:15min:AVERAGE ". + 'CDEF:x=a,b,c,MAX,MAX '. + 'AREA:a#EACC00:"1 Minute Average " GPRINT:a:LAST:"Current\:%8.2lf %s\n" '. + 'AREA:b#EA8F00:"5 Minute Average " GPRINT:b:LAST:"Current\:%8.2lf %s\n" '. + 'AREA:c#FF0000:"15 Minute Average" GPRINT:c:LAST:"Current\:%8.2lf %s\n" '. + 'LINE1:x#404040:"Total"'; + break; + + case 'memory': + $cmd .= "--base=1024 --height={$height} --width={$width} --alt-autoscale-max --lower-limit=0 --vertical-label=\"kB\" ". + "DEF:aa={$rrdfile}:total:AVERAGE ". + "DEF:bb={$rrdfile}:free:AVERAGE ". + "DEF:cc={$rrdfile}:buffers:AVERAGE ". + "DEF:dd={$rrdfile}:cached:AVERAGE ". + 'CDEF:a=aa,1024,* '. + 'CDEF:b=bb,1024,* '. + 'CDEF:c=cc,1024,* '. + 'CDEF:d=dd,1024,* '. + 'CDEF:x=aa,bb,cc,dd,+,+,-,1024,* '. + 'CDEF:xc=x,c,+ '. + 'CDEF:xcd=x,c,d,+,+ '. + 'LINE1:a#000000:"Total Memory " GPRINT:a:LAST:"Current\:%8.2lf %s\n" '. + 'AREA:x#FF0000:"Used Memory " GPRINT:x:LAST:"Current\:%8.2lf %s" GPRINT:x:AVERAGE:"Average\:%8.2lf %s" GPRINT:x:MAX:"Maximum\:%8.2lf %s\n" '. + 'STACK:c#FF7D00:"Buffer Memory " GPRINT:c:LAST:"Current\:%8.2lf %s" GPRINT:c:AVERAGE:"Average\:%8.2lf %s" GPRINT:c:MAX:"Maximum\:%8.2lf %s\n" '. + 'STACK:d#FFC73B:"Cache Memory " GPRINT:d:LAST:"Current\:%8.2lf %s" GPRINT:d:AVERAGE:"Average\:%8.2lf %s" GPRINT:d:MAX:"Maximum\:%8.2lf %s\n" '. + 'STACK:b#00CF00:"Free Memory " GPRINT:b:LAST:"Current\:%8.2lf %s" GPRINT:b:AVERAGE:"Average\:%8.2lf %s" GPRINT:b:MAX:"Maximum\:%8.2lf %s" '. + 'LINE1:x#404040 LINE1:xc#404040 LINE1:xcd#404040'; + break; + + case 'mount': + $cmd .= "--base=1024 --height={$height} --width={$width} --alt-autoscale-max --lower-limit=0 --vertical-label=\"kB\" ". + "DEF:aa={$rrdfile}:block_total:AVERAGE ". + "DEF:cc={$rrdfile}:block_free:AVERAGE ". + 'CDEF:a=aa,1024,* '. + 'CDEF:b=aa,cc,-,1024,* '. + 'CDEF:c=cc,1024,* '. + 'LINE1:a#000000:"Total " GPRINT:a:LAST:"Current\:%8.2lf %s\n" '. + 'AREA:b#FF0000:"Used " GPRINT:b:LAST:"Current\:%8.2lf %s" GPRINT:b:AVERAGE:"Average\:%8.2lf %s" GPRINT:b:MAX:"Maximum\:%8.2lf %s\n" '. + 'STACK:c#00CF00:"Free " GPRINT:c:LAST:"Current\:%8.2lf %s" GPRINT:c:AVERAGE:"Average\:%8.2lf %s" GPRINT:c:MAX:"Maximum\:%8.2lf %s" '. + 'LINE1:b#404040 '; + break; + + case 'net': + $height *= 2; + $cmd .= "--base=1000 --height={$height} --width={$width} --alt-autoscale-max --vertical-label=\"bytes per second\" ". + "DEF:imax={$rrdfile}:byte_in:MAX ". + "DEF:iavg={$rrdfile}:byte_in:AVERAGE ". + "DEF:imin={$rrdfile}:byte_in:MIN ". + "DEF:omax={$rrdfile}:byte_out:MAX ". + "DEF:oavg={$rrdfile}:byte_out:AVERAGE ". + "DEF:omin={$rrdfile}:byte_out:MIN ". + 'CDEF:omaxn=omax,-1,* CDEF:oavgn=oavg,-1,* CDEF:ominn=omin,-1,* '. + 'CDEF:imid=imax,imin,- CDEF:omid=omaxn,ominn,- '. + 'HRULE:0#FF0000 '. + 'AREA:imin STACK:imid#A0FFA0:"min/max Inbound " GPRINT:imin:MIN:"%6.2lf%s" GPRINT:imax:MAX:"%6.2lf%s" '. + 'LINE1:imin#a0a0a0 LINE1:imax#a0a0a0 LINE1:iavg#008000:"avg Inbound " '. + 'GPRINT:iavg:MIN:"min\: %6.2lf%s" GPRINT:iavg:AVERAGE:"avg\: %6.2lf%s" GPRINT:iavg:MAX:"max\: %6.2lf%s\n" '. + 'AREA:ominn STACK:omid#C0C0FF:"min/max Outbound" GPRINT:omin:MIN:"%6.2lf%s" GPRINT:omax:MAX:"%6.2lf%s" '. + 'LINE1:ominn#a0a0a0 LINE1:omaxn#a0a0a0 LINE1:oavgn#000080:"avg Outbound" '. + 'GPRINT:oavg:MIN:"min\: %6.2lf%s" GPRINT:oavg:AVERAGE:"avg\: %6.2lf%s" GPRINT:oavg:MAX:"max\: %6.2lf%s\n" '; + break; + + case 'proc': + $cmd .= "--base=1000 --height={$height} --width={$width} --alt-autoscale-max --lower-limit=0 --vertical-label=\"per second\" ". + "DEF:a={$rrdfile}:intr:AVERAGE ". + "DEF:b={$rrdfile}:ctxt:AVERAGE ". + "DEF:c={$rrdfile}:fork:AVERAGE ". + 'AREA:b#00AF00:"Context " GPRINT:b:LAST:"Current\:%8.2lf %s" GPRINT:b:AVERAGE:"Average\:%8.2lf %s" GPRINT:b:MAX:"Maximum\:%8.2lf %s\n" '. + 'LINE1:a#FF0000:"Interrupts" GPRINT:a:LAST:"Current\:%8.2lf %s" GPRINT:a:AVERAGE:"Average\:%8.2lf %s" GPRINT:a:MAX:"Maximum\:%8.2lf %s\n" '. + 'LINE1:c#0000FF:"Forks " GPRINT:c:LAST:"Current\:%8.2lf %s" GPRINT:c:AVERAGE:"Average\:%8.2lf %s" GPRINT:c:MAX:"Maximum\:%8.2lf %s\n" '; + break; + + case 'random': + $cmd .= "--base=1000 --height={$height} --width={$width} --alt-autoscale-max --lower-limit 0 --vertical-label=\"bytes\" ". + "DEF:a={$rrdfile}:entropy:AVERAGE ". + 'AREA:a#00CF00:"Available Entropy" GPRINT:a:LAST:"Current\:%8.2lf %s" GPRINT:a:AVERAGE:"Average\:%8.2lf %s" GPRINT:a:MAX:"Maximum\:%8.2lf %s" '. + 'LINE1:a#404040 '; + break; + + case 'swap': + $cmd .= "--base=1024 --height={$height} --width={$width} --alt-autoscale-max --lower-limit=0 --vertical-label=\"kB\" ". + "DEF:aa={$rrdfile}:total:AVERAGE ". + "DEF:bb={$rrdfile}:free:AVERAGE ". + 'CDEF:a=aa,1024,* '. + 'CDEF:b=bb,1024,* '. + 'CDEF:x=aa,bb,-,1024,* '. + 'LINE1:a#000000:"Total Swap " GPRINT:a:LAST:"Current\:%8.2lf %s\n" '. + 'AREA:x#FF0000:"Used Swap " GPRINT:x:LAST:"Current\:%8.2lf %s" GPRINT:x:AVERAGE:"Average\:%8.2lf %s" GPRINT:x:MAX:"Maximum\:%8.2lf %s\n" '. + 'STACK:b#00CF00:"Free Swap " GPRINT:b:LAST:"Current\:%8.2lf %s" GPRINT:b:AVERAGE:"Average\:%8.2lf %s" GPRINT:b:MAX:"Maximum\:%8.2lf %s\n" '; + break; + + case 'uptime': + $cmd .= "--base=1000 --height={$height} --width={$width} --alt-autoscale-max --lower-limit 0 --vertical-label=\"days\" ". + "DEF:ups={$rrdfile}:uptime:AVERAGE ". + "DEF:idles={$rrdfile}:idletime:AVERAGE ". + 'CDEF:up=ups,86400,/ '. + 'CDEF:idle=idles,86400,/ '. + 'AREA:up#00CF00:"Uptime " '. + 'LINE1:idle#002A97:"Idletime "'; + break; + + case 'vmstat': + $height *= 2; + $cmd .= "--base=1000 --height={$height} --width={$width} --alt-autoscale-max --vertical-label=\"allocs\" ". + "DEF:a={$rrdfile}:pgalloc_high:AVERAGE ". + "DEF:b={$rrdfile}:pgalloc_normal:AVERAGE ". + "DEF:c={$rrdfile}:pgalloc_dma:AVERAGE ". + "DEF:d={$rrdfile}:pgfree:AVERAGE ". + "DEF:e={$rrdfile}:pgfault:AVERAGE ". + "CDEF:ee=e,-1,* ". + 'AREA:c#FF0000:"pgalloc_dma " GPRINT:c:LAST:"Current\:%8.2lf %s" GPRINT:c:AVERAGE:"Average\:%8.2lf %s" GPRINT:c:MAX:"Maximum\:%8.2lf %s\n" '. + 'STACK:b#00CF00:"pgalloc_normal " GPRINT:b:LAST:"Current\:%8.2lf %s" GPRINT:b:AVERAGE:"Average\:%8.2lf %s" GPRINT:b:MAX:"Maximum\:%8.2lf %s\n" '. + 'STACK:a#00CFCF:"pgalloc_high " GPRINT:a:LAST:"Current\:%8.2lf %s" GPRINT:a:AVERAGE:"Average\:%8.2lf %s" GPRINT:a:MAX:"Maximum\:%8.2lf %s\n" '. + 'LINE1:d#FF7D00:"pgfree " GPRINT:d:LAST:"Current\:%8.2lf %s" GPRINT:d:AVERAGE:"Average\:%8.2lf %s" GPRINT:d:MAX:"Maximum\:%8.2lf %s\n" '. + 'AREA:ee#0040FF:"pgfault " GPRINT:e:LAST:"Current\:%8.2lf %s" GPRINT:e:AVERAGE:"Average\:%8.2lf %s" GPRINT:e:MAX:"Maximum\:%8.2lf %s" '. + 'LINE1:ee#002A97 '; + break; + } + + header('Content-type: image/x-png'); + passthru($cmd); + die(); +} + +$action = $_GET['action']; +switch ($action) { +case 'init': + $conf = create_config(); + write_config(CONFIG, $conf); + header("Location: {$_SERVER['SCRIPT_NAME']}?action=setup"); + break; + + +case 'setup': + $conf = read_config(CONFIG); + $conf = setup($conf); + if (isset($conf)) { + write_config(CONFIG, $conf); + header("Location: {$_SERVER['SCRIPT_NAME']}?action=setup"); + } + break; + +case 'rrd': + $conf = read_config(CONFIG); + show_rrd($conf); + break; + +default: +case 'show': + $conf = read_config(CONFIG); + show_html($conf); + break; +} + +?>