Skip to content

Instantly share code, notes, and snippets.

@Elektordi
Created June 1, 2018 12:46
Show Gist options
  • Save Elektordi/f536c5891c8d3c227574b0fbf87c451b to your computer and use it in GitHub Desktop.
Save Elektordi/f536c5891c8d3c227574b0fbf87c451b to your computer and use it in GitHub Desktop.
Custom cacti-like graphs for observium
<?php
$observium = "/var/www/observium";
$padoffset = 30;
if(empty($_GET['graph'])) die('Missing graph parameter.');
$graph = $_GET['graph'];
$ds = '';
if($graph=='in') {
$title = "Incoming flow";
$ds = 'INOCTETS';
$types = array('transit' => 'T', 'peering' => 'P');
} elseif($graph=='out') {
$title = "Outgoing flow";
$ds = 'OUTOCTETS';
$types = array('transit' => 'T', 'peering' => 'P');
} elseif($graph=='down') {
$title = "Downstream flow";
$ds = 'OUTOCTETS';
$types = array('l2tp' => 'L', 'collecte' => 'V');
} else {
die('Unknow graph');
}
$out = "cache/$graph.png";
$colors = array('F51D30', '4444FF', '74C366', 'FF5F00', '8D00BA', 'FFF200', 'FF00FF', '55D6D3', /*loop*/ 'F51D30', '4444FF', '74C366', 'FF5F00', '8D00BA', 'FFF200', 'FF00FF', '55D6D3');
if(!file_exists($out) || filemtime($out)<time()-300 || isset($_GET['nocache'])) {
include "$observium/includes/sql-config.inc.php";
$options = array(
"--start", "-1d",
"--title=$title",
"--width=600",
"--height=120",
"--lower-limit=0",
"--alt-autoscale-max",
"--base=1000",
"--vertical-label=bits per second",
"--slope-mode",
"--font=TITLE:10:",
"--font=AXIS:8:",
"--font=LEGEND:8:",
"--font=UNIT:8:",
);
$id = 1;
$total = '0';
foreach($types as $typek=>$typev) {
$ports = dbFetchRows("SELECT * FROM `ports` AS I, `devices` AS D WHERE `port_descr_type` LIKE '$typek' AND I.`device_id` = D.`device_id` and I.ifAdminStatus = 'up' and I.deleted=0 and D.disabled=0 and D.status=1 ORDER BY I.`ifAlias`");
foreach($ports as $p) {
$name = '['.$typev.'] '.$p['port_descr_descr'];
$pad = str_pad(' ', $padoffset-strlen($name));
$rrd = $observium."/rrd/".$p['hostname']."/port-".$p['ifIndex'].".rrd";
$c = array_shift($colors);
if(!$c) $c = '000000';
$options = array_merge($options, array(
"DEF:d$id=$rrd:$ds:AVERAGE",
"CDEF:c$id=d$id,8,*",
"AREA:c$id#$c:$name$pad:STACK",
"GPRINT:c$id:LAST:Current\:%8.2lf %s",
"GPRINT:c$id:AVERAGE: Average\:%8.2lf %s",
"GPRINT:c$id:MAX: Max\:%8.2lf %s\\n",
));
$total = "c$id,$total,+";
$id++;
}
}
$name = "GLOBAL";
$pad = str_pad(' ', $padoffset-strlen($name)+2);
$options = array_merge($options, array(
"CDEF:total=$total",
// "LINE:total#000000",
"COMMENT:$pad$name",
"GPRINT:total:LAST:Current\:%8.2lf %s",
"GPRINT:total:AVERAGE: Average\:%8.2lf %s",
"GPRINT:total:MAX: Max\:%8.2lf %s\\n"
));
if(isset($_GET['dump_opt'])) { var_dump($options); die(); }
$r = rrd_graph($out, $options);
if($r===false) {
$err = rrd_error();
if(!$err) $err = 'INTERNAL ERROR';
header('HTTP/1.0 500 RRD error', true, 500);
die($err);
}
if(isset($_GET['dump_r'])) { var_dump($r); die(); }
usleep(100); // Laisse le temps a RRD de flusher le fichier ecrit
if(!file_exists($out)) {
header('HTTP/1.0 500 RRD error', true, 500);
die('Graph generation failed: '.$out);
}
}
header("Content-Type: image/png");
header("Content-Length: " . filesize($out));
header("Cache-Control: no-cache, no-store, must-revalidate");
echo file_get_contents($out);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment