Gradient Graphs for Cacti

Addons for Cacti and discussion about those addons

Moderators: Developers, Moderators

User avatar
phalek
Developer
Posts: 2838
Joined: Thu Jan 31, 2008 6:39 am
Location: Kressbronn, Germany
Contact:

Gradient Graphs for Cacti

Post by phalek »

Hi folks,

I've played around a bit with the RRD graphing and implemented a quick gradient support for them. It's based on several other scripts and if someone is cabable of re-writing the "colourBrightness" function to be GPLv2 I would greatly appreciate it. The code changes ALL AREA related graphs dynamically.

Nothing fency and a quick&dirty hack, but maybe someone still likes it :-)

Look here for additional information: http://blog.network-outsourcing.de/2015 ... ti-graphs/

Here are some samples graphs:
graph_image_localhost_gradient_1.png
graph_image_localhost_gradient_1.png (37.22 KiB) Viewed 12735 times
graph_image_localhost_gradient_3.png
graph_image_localhost_gradient_3.png (41.5 KiB) Viewed 12731 times
graph_image_localhost_gradient_2.png
graph_image_localhost_gradient_2.png (35.83 KiB) Viewed 12724 times
graph_image_localhost_gradient_4.png
graph_image_localhost_gradient_4.png (23.67 KiB) Viewed 12724 times
graph_image_localhost_gradient_5.png
graph_image_localhost_gradient_5.png (74.32 KiB) Viewed 12723 times
Here's the code.
You can change the effect of the gradient by changing the following line accoringly:
$end_color = colourBrightness("#" . $graph_item["hex"],-0.4); // End color is a 40% (0.4) darkened (negative number) version of the original color


File: lib/rrd.php, Line: 1375

Code: Select all

                        if (preg_match("/^AREA$/", $graph_item_types{$graph_item["graph_type_id"]})) {
                                $graph_item_stack_type = $graph_item_types{$graph_item["graph_type_id"]};
                                $graph_variables["text_format"][$graph_item_id] = str_replace(":", "\:", $graph_variables["text_format"][$graph_item_id]); /* escape colons */
//                              $txt_graph_items .= $graph_item_types{$graph_item["graph_type_id"]} . ":" . $data_source_name . $graph_item_color_code . ":" . cacti_escapeshellarg($graph_variables["text_format"][$graph_item_id] . $hardreturn[$graph_item_id]) . " ";
                                $end_color =  colourBrightness("#" . $graph_item["hex"],-0.4); // End color is a 40% (0.4) darkened (negative number) version of the original color
                                $txt_graph_items .= gradient($data_source_name,$graph_item_color_code,$end_color.$graph_item["alpha"],cacti_escapeshellarg($graph_variables["text_format"][$graph_item_id] . $hardreturn[$graph_item_id]),20,false,$graph_item["alpha"]);
                                $need_rrd_nl = FALSE;
                        }elseif (preg_match("/^LINE[123]$/", $graph_item_types{$graph_item["graph_type_id"]})) {
                                $graph_item_stack_type = $graph_item_types{$graph_item["graph_type_id"]};
                                $graph_variables["text_format"][$graph_item_id] = str_replace(":", "\:", $graph_variables["text_format"][$graph_item_id]); /* escape colons */
                                $txt_graph_items .= $graph_item_types{$graph_item["graph_type_id"]} . ":" . $data_source_name . $graph_item_color_code . ":" . cacti_escapeshellarg($graph_variables["text_format"][$graph_item_id] . $hardreturn[$graph_item_id]) . " ";

                        }elseif ($graph_item_types{$graph_item["graph_type_id"]} == "STACK") {
at the end of rrd.php

Code: Select all

// from: http://lab.clearpixel.com.au/2008/06/darken-or-lighten-colours-dynamically-using-php/  License: unknown
// The code is based on the CSSColor project of the same person from here: http://www.barelyfitz.com/projects/csscolor/ License: GPLv2
	function colourBrightness($hex, $percent) {
        // Work out if hash given
        $hash = '';
        if (stristr($hex,'#')) {
                $hex = str_replace('#','',$hex);
                $hash = '#';
        }
        /// HEX TO RGB
        $rgb = array(hexdec(substr($hex,0,2)), hexdec(substr($hex,2,2)), hexdec(substr($hex,4,2)));
        //// CALCULATE
        for ($i=0; $i<3; $i++) {
                // See if brighter or darker
                if ($percent > 0) {
                        // Lighter
                        $rgb[$i] = round($rgb[$i] * $percent) + round(255 * (1-$percent));
                } else {
                        // Darker
                        $positivePercent = $percent - ($percent*2);
                        $rgb[$i] = round($rgb[$i] * (1-$positivePercent)); // round($rgb[$i] * (1-$positivePercent));
                }
                // In case rounding up causes us to go to 256
                if ($rgb[$i] > 255) {
                        $rgb[$i] = 255;
                }
        }
        //// RBG to Hex
        $hex = '';
        for($i=0; $i < 3; $i++) {
                // Convert the decimal digit to hex
                $hexDigit = dechex($rgb[$i]);
                // Add a leading zero if necessary
                if(strlen($hexDigit) == 1) {
                $hexDigit = "0" . $hexDigit;
                }
                // Append to the hex string
                $hex .= $hexDigit;
        }
        return $hash.$hex;
}

 // from https://github.com/lingej/pnp4nagios/blob/master/share/pnp/application/helpers/rrd.php / License: GPLv2
 function gradient($vname=FALSE, $start_color='#0000a0', $end_color='#f0f0f0', $label=FALSE, $steps=20, $lower=FALSE, $alpha='FF'){
        $label = preg_replace("/'/","",$label);
        if($vname === FALSE){
//            throw new Kohana_exception("rrd::". __FUNCTION__ . "() First Parameter 'vname' is missing");
        }
        if(preg_match('/^#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/i',$start_color,$matches)){
            $r1=hexdec($matches[1]);
            $g1=hexdec($matches[2]);
            $b1=hexdec($matches[3]);
        }else{
  //          throw new Kohana_exception("rrd::". __FUNCTION__ . "() Wrong Color Format: '".$start_color."'");
        }
        if(preg_match('/^#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/i',$end_color,$matches)){
            $r2=hexdec($matches[1]);
            $g2=hexdec($matches[2]);
            $b2=hexdec($matches[3]);
        }else{
//            throw new Kohana_exception("rrd::". __FUNCTION__ . "() Wrong Color Format: '".$end_color."'");
        }
        $diff_r=$r2-$r1;
        $diff_g=$g2-$g1;
        $diff_b=$b2-$b1;
        $spline =  "";
        $spline_vname = "var".substr(sha1(rand()),1,4);
        $vnamet = $vname.substr(sha1(rand()),1,4);
        if(preg_match('/^([0-9]{1,3})%$/', $lower, $matches)){
  //          if($matches[1] > 100)
  //              throw new Kohana_exception("rrd::". __FUNCTION__ . "() Lower gradient start > 100% is not allowed: '".$lower."'");

            $lower   = $matches[1];
            $spline .= sprintf("CDEF:%sminimum=%s,100,/,%d,* ".RRD_NL, $vnamet, $vname, $lower);
        }elseif(preg_match('/^([0-9]+)$/', $lower, $matches)){
            $lower   = $matches[1];
            $spline .= sprintf("CDEF:%sminimum=%s,%d,- ".RRD_NL, $vnamet, $vname, $lower);
        }else{
            $lower   = 0;
            $spline .= sprintf("CDEF:%sminimum=%s,%s,- ".RRD_NL, $vnamet, $vname, $vname);
        }
        for ($i=$steps; $i>0; $i--){
            $spline .=  sprintf("CDEF:%s%d=%s,%sminimum,-,%d,/,%d,*,%sminimum,+ ".RRD_NL,$spline_vname,$i,$vname,$vnamet,$steps,$i,$vnamet);
        }
        // We don't use alpha blending for the area right now
        $alpha = 'ff';
        for ($i=$steps; $i>0; $i--){
            $factor=$i / $steps;
            $r=round($r1 + $diff_r * $factor);
            $g=round($g1 + $diff_g * $factor);
            $b=round($b1 + $diff_b * $factor);
            if (($i==$steps) and ($label!=FALSE) and (strlen($label)>2) ){
                $spline .=  sprintf("AREA:%s%d#%02X%02X%02X%s:\"%s\" ".RRD_NL, $spline_vname,$i,$r,$g,$b,$alpha,$label);
            }else{
                $spline .=  sprintf("AREA:%s%d#%02X%02X%02X%s ".RRD_NL, $spline_vname,$i,$r,$g,$b,$alpha);
            }
        }
        $spline .=  sprintf("AREA:%s%d#%02X%02X%02X%s ".RRD_NL, $spline_vname,$steps,$r2,$g2,$b2,'00',$label);
        return $spline;
    }
Last edited by phalek on Wed Sep 23, 2015 9:02 am, edited 2 times in total.
Greetings,
Phalek
---
Need more help ? Read the Cacti documentation or my new Cacti 1.x Book
Need on-site support ? Look here Cacti Workshop
Need professional Cacti support ? Look here CereusService
---
Plugins : CereusReporting
hmorandell
Cacti User
Posts: 73
Joined: Mon May 03, 2010 11:48 am

Re: Gradient Graphs for Cacti

Post by hmorandell »

thanks! this is cool :-)
_________________
Cacti - 1.2.14
Poller Type - SPINE 1.2.14
Devices 3,892
Graphs 21,483
hmorandell
Cacti User
Posts: 73
Joined: Mon May 03, 2010 11:48 am

Re: Gradient Graphs for Cacti

Post by hmorandell »

P.S.:
can you please tell which colours you have set for your WAN interface traffic statistics (upload & download)...
_________________
Cacti - 1.2.14
Poller Type - SPINE 1.2.14
Devices 3,892
Graphs 21,483
User avatar
phalek
Developer
Posts: 2838
Joined: Thu Jan 31, 2008 6:39 am
Location: Kressbronn, Germany
Contact:

Re: Gradient Graphs for Cacti

Post by phalek »

Thanks.

Look at the attached screenshot for the GraphTemplate Definition:
GraphTemplate_Interfaces.png
GraphTemplate_Interfaces.png (40.11 KiB) Viewed 12702 times
Greetings,
Phalek
---
Need more help ? Read the Cacti documentation or my new Cacti 1.x Book
Need on-site support ? Look here Cacti Workshop
Need professional Cacti support ? Look here CereusService
---
Plugins : CereusReporting
Hanseat
Posts: 17
Joined: Thu Sep 27, 2012 5:24 am

Re: Gradient Graphs for Cacti

Post by Hanseat »

sooo pretty :)

cheers, Phalek!


edit:
Now, if only I could remove the " ' " apostrophes from all my graphs, that´d be even better!
routenull0
Posts: 16
Joined: Tue Jul 09, 2013 5:32 pm

Re: Gradient Graphs for Cacti

Post by routenull0 »

Hanseat wrote:sooo pretty :)
edit:
Now, if only I could remove the " ' " apostrophes from all my graphs, that´d be even better!
Yes, where is this coming from? I don't recall it being there prior.

Edit: Reverting the changes removes the apostrophes.
User avatar
phalek
Developer
Posts: 2838
Joined: Thu Jan 31, 2008 6:39 am
Location: Kressbronn, Germany
Contact:

Re: Gradient Graphs for Cacti

Post by phalek »

Is that for the title only or for the comments ?
Greetings,
Phalek
---
Need more help ? Read the Cacti documentation or my new Cacti 1.x Book
Need on-site support ? Look here Cacti Workshop
Need professional Cacti support ? Look here CereusService
---
Plugins : CereusReporting
User avatar
phalek
Developer
Posts: 2838
Joined: Thu Jan 31, 2008 6:39 am
Location: Kressbronn, Germany
Contact:

Re: Gradient Graphs for Cacti

Post by phalek »

Just fixed it. Right at the first line of the "gradient" function there's a new line removing these chars:

Code: Select all

        $label = preg_replace("/'/","",$label);
I've updated the blog posting and the first post in this thread.
Greetings,
Phalek
---
Need more help ? Read the Cacti documentation or my new Cacti 1.x Book
Need on-site support ? Look here Cacti Workshop
Need professional Cacti support ? Look here CereusService
---
Plugins : CereusReporting
Hanseat
Posts: 17
Joined: Thu Sep 27, 2012 5:24 am

Re: Gradient Graphs for Cacti

Post by Hanseat »

Many thanks, will give it a try tomorrow!

edit: and I´ll keep the changes now, thanks a lot!
jimcjulsonjr
Posts: 48
Joined: Fri Dec 07, 2012 11:11 am

Re: Gradient Graphs for Cacti

Post by jimcjulsonjr »

As usual, love your work!

First, is this for 0.8.8b (Which is what still comes out of apt for us Ubuntu lovers)?

Second, is there a chance you could simply upload an entire rrd.php file that has all the proper edits? I'm unclear on what it is I'm supposed to edit at line 1375 myself. Either a full copy of the rrd.php, or maybe just highlight exactly what needs to be actually changed.

Third, keep up the awesome work! Love what you've done with Cereus and Smokeping!

Thanks!
-------------------------------------

VERSION: Cacti 0.8.8b
POLLER: Spine
DATA SOURCES: 100,000K and Growing (Multiple Servers)
PLUGINS: AUTOM8, THOLD, DISCOVER, WEATHERMAP, BOOST, CLOG, NECTAR, MACTRACK, FLOWVIEW, SPIKEKILL, INTROPAGE, MONITOR
jimcjulsonjr
Posts: 48
Joined: Fri Dec 07, 2012 11:11 am

Re: Gradient Graphs for Cacti

Post by jimcjulsonjr »

As usual, love your work!

First, is this for 0.8.8b (Which is what still comes out of apt for us Ubuntu lovers)?

Second, is there a chance you could simply upload an entire rrd.php file that has all the proper edits? I'm unclear on what it is I'm supposed to edit at line 1375 myself. Either a full copy of the rrd.php, or maybe just highlight exactly what needs to be actually changed.

Third, keep up the awesome work! Love what you've done with Cereus and Smokeping!

Thanks!
-------------------------------------

VERSION: Cacti 0.8.8b
POLLER: Spine
DATA SOURCES: 100,000K and Growing (Multiple Servers)
PLUGINS: AUTOM8, THOLD, DISCOVER, WEATHERMAP, BOOST, CLOG, NECTAR, MACTRACK, FLOWVIEW, SPIKEKILL, INTROPAGE, MONITOR
User avatar
phalek
Developer
Posts: 2838
Joined: Thu Jan 31, 2008 6:39 am
Location: Kressbronn, Germany
Contact:

Re: Gradient Graphs for Cacti

Post by phalek »

Hi Jim,

0.8.8b is what this was built for. Here's a download link for the file: http://blog.network-outsourcing.de/wp-c ... t_088b.zip

I've added the link to the post as well.
Greetings,
Phalek
---
Need more help ? Read the Cacti documentation or my new Cacti 1.x Book
Need on-site support ? Look here Cacti Workshop
Need professional Cacti support ? Look here CereusService
---
Plugins : CereusReporting
jimcjulsonjr
Posts: 48
Joined: Fri Dec 07, 2012 11:11 am

Re: Gradient Graphs for Cacti

Post by jimcjulsonjr »

Outstanding! Thank you sir!
-------------------------------------

VERSION: Cacti 0.8.8b
POLLER: Spine
DATA SOURCES: 100,000K and Growing (Multiple Servers)
PLUGINS: AUTOM8, THOLD, DISCOVER, WEATHERMAP, BOOST, CLOG, NECTAR, MACTRACK, FLOWVIEW, SPIKEKILL, INTROPAGE, MONITOR
jimcjulsonjr
Posts: 48
Joined: Fri Dec 07, 2012 11:11 am

Re: Gradient Graphs for Cacti

Post by jimcjulsonjr »

Just have to say, this looks simply amazing. Loving this.
-------------------------------------

VERSION: Cacti 0.8.8b
POLLER: Spine
DATA SOURCES: 100,000K and Growing (Multiple Servers)
PLUGINS: AUTOM8, THOLD, DISCOVER, WEATHERMAP, BOOST, CLOG, NECTAR, MACTRACK, FLOWVIEW, SPIKEKILL, INTROPAGE, MONITOR
Busindre
Posts: 1
Joined: Sun Nov 29, 2015 1:13 pm

Re: Gradient Graphs for Cacti

Post by Busindre »

I love gradient, it's a nice contribution. However, it doesn't work well on graphics with transparency effects (when graphs have opacity)
Can you avoid using it on graphics with a preset opacity?

Regards
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests