mirror of
				https://github.com/librenms/librenms.git
				synced 2024-10-07 16:52:45 +00:00 
			
		
		
		
	Use namespaces for classes
CollectdColor, move into it's own file, change functions to camelCase Plugins use LibreNMS\Plugins
This commit is contained in:
		
							
								
								
									
										109
									
								
								html/includes/collectd/CollectdColor.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										109
									
								
								html/includes/collectd/CollectdColor.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,109 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace LibreNMS;
 | 
			
		||||
 | 
			
		||||
class CollectdColor
 | 
			
		||||
{
 | 
			
		||||
    private $r = 0;
 | 
			
		||||
    private $g = 0;
 | 
			
		||||
    private $b = 0;
 | 
			
		||||
 | 
			
		||||
    public function __construct($value = null)
 | 
			
		||||
    {
 | 
			
		||||
        if (is_null($value)) {
 | 
			
		||||
        } else {
 | 
			
		||||
            if (is_array($value)) {
 | 
			
		||||
                if (isset($value['r'])) {
 | 
			
		||||
                    $this->r = $value['r'] > 0 ? ($value['r'] > 1 ? 1 : $value['r']) : 0;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                if (isset($value['g'])) {
 | 
			
		||||
                    $this->g = $value['g'] > 0 ? ($value['g'] > 1 ? 1 : $value['g']) : 0;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                if (isset($value['b'])) {
 | 
			
		||||
                    $this->b = $value['b'] > 0 ? ($value['b'] > 1 ? 1 : $value['b']) : 0;
 | 
			
		||||
                }
 | 
			
		||||
            } else {
 | 
			
		||||
                if (is_string($value)) {
 | 
			
		||||
                    $matches = array();
 | 
			
		||||
                    if ($value == 'random') {
 | 
			
		||||
                        $this->randomize();
 | 
			
		||||
                    } else {
 | 
			
		||||
                        if (preg_match('/([0-9A-Fa-f][0-9A-Fa-f])([0-9A-Fa-f][0-9A-Fa-f])([0-9A-Fa-f][0-9A-Fa-f])/',
 | 
			
		||||
                            $value, $matches)) {
 | 
			
		||||
                            $this->r = (('0x' . $matches[1]) / 255.0);
 | 
			
		||||
                            $this->g = (('0x' . $matches[2]) / 255.0);
 | 
			
		||||
                            $this->b = (('0x' . $matches[3]) / 255.0);
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                } else {
 | 
			
		||||
                    if (is_a($value, 'CollectdColor')) {
 | 
			
		||||
                        $this->r = $value->r;
 | 
			
		||||
                        $this->g = $value->g;
 | 
			
		||||
                        $this->b = $value->b;
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }//end if
 | 
			
		||||
 | 
			
		||||
    }//end __construct()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public function randomize()
 | 
			
		||||
    {
 | 
			
		||||
        $this->r = (rand(0, 255) / 255.0);
 | 
			
		||||
        $this->g = (rand(0, 255) / 255.0);
 | 
			
		||||
        $this->b = 0.0;
 | 
			
		||||
        $min = 0.0;
 | 
			
		||||
        $max = 1.0;
 | 
			
		||||
 | 
			
		||||
        if (($this->r + $this->g) < 1.0) {
 | 
			
		||||
            $min = (1.0 - ($this->r + $this->g));
 | 
			
		||||
        } else {
 | 
			
		||||
            $max = (2.0 - ($this->r + $this->g));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $this->b = ($min + ((rand(0, 255) / 255.0) * ($max - $min)));
 | 
			
		||||
 | 
			
		||||
    }//end randomize()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public function fade($bkgnd = null, $alpha = 0.25)
 | 
			
		||||
    {
 | 
			
		||||
        if (is_null($bkgnd) || !is_a($bkgnd, 'CollectdColor')) {
 | 
			
		||||
            $bg_r = 1.0;
 | 
			
		||||
            $bg_g = 1.0;
 | 
			
		||||
            $bg_b = 1.0;
 | 
			
		||||
        } else {
 | 
			
		||||
            $bg_r = $bkgnd->r;
 | 
			
		||||
            $bg_g = $bkgnd->g;
 | 
			
		||||
            $bg_b = $bkgnd->b;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $this->r = ($alpha * $this->r + ((1.0 - $alpha) * $bg_r));
 | 
			
		||||
        $this->g = ($alpha * $this->g + ((1.0 - $alpha) * $bg_g));
 | 
			
		||||
        $this->b = ($alpha * $this->b + ((1.0 - $alpha) * $bg_b));
 | 
			
		||||
 | 
			
		||||
    }//end fade()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public function toArray()
 | 
			
		||||
    {
 | 
			
		||||
        return array(
 | 
			
		||||
            'r' => $this->r,
 | 
			
		||||
            'g' => $this->g,
 | 
			
		||||
            'b' => $this->b,
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
    }//end as_array()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public function toString()
 | 
			
		||||
    {
 | 
			
		||||
        $r = (int)($this->r * 255);
 | 
			
		||||
        $g = (int)($this->g * 255);
 | 
			
		||||
        $b = (int)($this->b * 255);
 | 
			
		||||
        return sprintf('%02x%02x%02x', $r > 255 ? 255 : $r, $g > 255 ? 255 : $g, $b > 255 ? 255 : $b);
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,5 +1,4 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (C) 2009  Bruno Prémont <bonbons AT linux-vserver.org>
 | 
			
		||||
 *
 | 
			
		||||
@@ -17,6 +16,10 @@
 | 
			
		||||
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
require 'includes/collectd/CollectdColor.php';
 | 
			
		||||
 | 
			
		||||
use LibreNMS\CollectdColor;
 | 
			
		||||
 | 
			
		||||
define('REGEXP_HOST', '/^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/');
 | 
			
		||||
define('REGEXP_PLUGIN', '/^[a-zA-Z0-9_.-]+$/');
 | 
			
		||||
 | 
			
		||||
@@ -368,112 +371,6 @@ function collectd_flush($identifier) {
 | 
			
		||||
 | 
			
		||||
}//end collectd_flush()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class CollectdColor {
 | 
			
		||||
 | 
			
		||||
    private $r = 0;
 | 
			
		||||
 | 
			
		||||
    private $g = 0;
 | 
			
		||||
 | 
			
		||||
    private $b = 0;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    function __construct($value=null) {
 | 
			
		||||
        if (is_null($value)) {
 | 
			
		||||
        }
 | 
			
		||||
        else if (is_array($value)) {
 | 
			
		||||
            if (isset($value['r'])) {
 | 
			
		||||
                $this->r = $value['r'] > 0 ? ($value['r'] > 1 ? 1 : $value['r']) : 0;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (isset($value['g'])) {
 | 
			
		||||
                $this->g = $value['g'] > 0 ? ($value['g'] > 1 ? 1 : $value['g']) : 0;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (isset($value['b'])) {
 | 
			
		||||
                $this->b = $value['b'] > 0 ? ($value['b'] > 1 ? 1 : $value['b']) : 0;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        else if (is_string($value)) {
 | 
			
		||||
            $matches = array();
 | 
			
		||||
            if ($value == 'random') {
 | 
			
		||||
                $this->randomize();
 | 
			
		||||
            }
 | 
			
		||||
            else if (preg_match('/([0-9A-Fa-f][0-9A-Fa-f])([0-9A-Fa-f][0-9A-Fa-f])([0-9A-Fa-f][0-9A-Fa-f])/', $value, $matches)) {
 | 
			
		||||
                $this->r = (('0x'.$matches[1]) / 255.0);
 | 
			
		||||
                $this->g = (('0x'.$matches[2]) / 255.0);
 | 
			
		||||
                $this->b = (('0x'.$matches[3]) / 255.0);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        else if (is_a($value, 'CollectdColor')) {
 | 
			
		||||
            $this->r = $value->r;
 | 
			
		||||
            $this->g = $value->g;
 | 
			
		||||
            $this->b = $value->b;
 | 
			
		||||
        }//end if
 | 
			
		||||
 | 
			
		||||
    }//end __construct()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    function randomize() {
 | 
			
		||||
        $this->r = (rand(0, 255) / 255.0);
 | 
			
		||||
        $this->g = (rand(0, 255) / 255.0);
 | 
			
		||||
        $this->b = 0.0;
 | 
			
		||||
        $min     = 0.0;
 | 
			
		||||
        $max     = 1.0;
 | 
			
		||||
 | 
			
		||||
        if (($this->r + $this->g) < 1.0) {
 | 
			
		||||
            $min = (1.0 - ($this->r + $this->g));
 | 
			
		||||
        }
 | 
			
		||||
        else {
 | 
			
		||||
            $max = (2.0 - ($this->r + $this->g));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $this->b = ($min + ((rand(0, 255) / 255.0) * ($max - $min)));
 | 
			
		||||
 | 
			
		||||
    }//end randomize()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    function fade($bkgnd=null, $alpha=0.25) {
 | 
			
		||||
        if (is_null($bkgnd) || !is_a($bkgnd, 'CollectdColor')) {
 | 
			
		||||
            $bg_r = 1.0;
 | 
			
		||||
            $bg_g = 1.0;
 | 
			
		||||
            $bg_b = 1.0;
 | 
			
		||||
        }
 | 
			
		||||
        else {
 | 
			
		||||
            $bg_r = $bkgnd->r;
 | 
			
		||||
            $bg_g = $bkgnd->g;
 | 
			
		||||
            $bg_b = $bkgnd->b;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $this->r = ($alpha * $this->r + ((1.0 - $alpha) * $bg_r));
 | 
			
		||||
        $this->g = ($alpha * $this->g + ((1.0 - $alpha) * $bg_g));
 | 
			
		||||
        $this->b = ($alpha * $this->b + ((1.0 - $alpha) * $bg_b));
 | 
			
		||||
 | 
			
		||||
    }//end fade()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    function as_array() {
 | 
			
		||||
        return array(
 | 
			
		||||
            'r' => $this->r,
 | 
			
		||||
            'g' => $this->g,
 | 
			
		||||
            'b' => $this->b,
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
    }//end as_array()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    function as_string() {
 | 
			
		||||
        $r = (int) ($this->r * 255);
 | 
			
		||||
        $g = (int) ($this->g * 255);
 | 
			
		||||
        $b = (int) ($this->b * 255);
 | 
			
		||||
        return sprintf('%02x%02x%02x', $r > 255 ? 255 : $r, $g > 255 ? 255 : $g, $b > 255 ? 255 : $b);
 | 
			
		||||
 | 
			
		||||
    }//end as_string()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
}//end class
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Helper function to strip quotes from RRD output
 | 
			
		||||
 * @str RRD-Info generated string
 | 
			
		||||
@@ -564,8 +461,8 @@ function rrd_get_color($code, $line=true) {
 | 
			
		||||
        $c_f = new CollectdColor('random');
 | 
			
		||||
        $c_h = new CollectdColor($c_f);
 | 
			
		||||
        $c_h->fade();
 | 
			
		||||
        $config['rrd_colors']['f_'.$code] = $c_f->as_string();
 | 
			
		||||
        $config['rrd_colors']['h_'.$code] = $c_h->as_string();
 | 
			
		||||
        $config['rrd_colors']['f_'.$code] = $c_f->toString();
 | 
			
		||||
        $config['rrd_colors']['h_'.$code] = $c_h->toString();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return $config['rrd_colors'][$name];
 | 
			
		||||
@@ -952,8 +849,8 @@ function collectd_draw_meta_stack(&$opts, &$sources) {
 | 
			
		||||
        $area_color = new CollectdColor($line_color);
 | 
			
		||||
        $area_color->fade();
 | 
			
		||||
 | 
			
		||||
        $cmd[] = 'AREA:'.$inst_name.'_stk#'.$area_color->as_string();
 | 
			
		||||
        $cmd[] = 'LINE1:'.$inst_name.'_stk#'.$line_color->as_string().':'.$legend;
 | 
			
		||||
        $cmd[] = 'AREA:'.$inst_name.'_stk#'.$area_color->toString();
 | 
			
		||||
        $cmd[] = 'LINE1:'.$inst_name.'_stk#'.$line_color->toString().':'.$legend;
 | 
			
		||||
        if (!(isset($opts['tinylegend']) && $opts['tinylegend'])) {
 | 
			
		||||
            $cmd[]             = 'GPRINT:'.$inst_name.'_avg:LAST:'.$number_format.'';
 | 
			
		||||
            $cmd[] = 'GPRINT:'.$inst_name.'_avg:AVERAGE:'.$number_format.'';
 | 
			
		||||
@@ -1072,7 +969,7 @@ function collectd_draw_meta_line(&$opts, &$sources) {
 | 
			
		||||
            $line_color = new CollectdColor('random');
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $cmd[] = 'LINE1:'.$inst_name.'_avg#'.$line_color->as_string().':'.$legend;
 | 
			
		||||
        $cmd[] = 'LINE1:'.$inst_name.'_avg#'.$line_color->toString().':'.$legend;
 | 
			
		||||
        if (!(isset($opts['tinylegend']) && $opts['tinylegend'])) {
 | 
			
		||||
            $cmd[] = 'GPRINT:'.$inst_name.'_min:MIN:'.$number_format.'';
 | 
			
		||||
            $cmd[] = 'GPRINT:'.$inst_name.'_avg:AVERAGE:'.$number_format.'';
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace LibreNMS;
 | 
			
		||||
 | 
			
		||||
class Plugins {
 | 
			
		||||
 | 
			
		||||
    private static $plugins = array();
 | 
			
		||||
@@ -31,7 +33,14 @@ class Plugins {
 | 
			
		||||
    public static function load($file, $pluginName)
 | 
			
		||||
    {
 | 
			
		||||
        include $file;
 | 
			
		||||
        $plugin = new $pluginName;
 | 
			
		||||
        $pluginFullName = 'LibreNMS\\Plugins\\' . $pluginName;
 | 
			
		||||
        if (class_exists($pluginFullName)) {
 | 
			
		||||
            $plugin = new $pluginFullName;
 | 
			
		||||
        } elseif (class_exists($pluginName)) {
 | 
			
		||||
            $plugin = new $pluginName;
 | 
			
		||||
        } else {
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
        $hooks  = get_class_methods($plugin);
 | 
			
		||||
 | 
			
		||||
        foreach ($hooks as $hookName) {
 | 
			
		||||
 
 | 
			
		||||
@@ -68,6 +68,8 @@ require 'includes/functions.inc.php';
 | 
			
		||||
require 'includes/vars.inc.php';
 | 
			
		||||
require 'includes/plugins.inc.php';
 | 
			
		||||
 | 
			
		||||
use LibreNMS\Plugins;
 | 
			
		||||
 | 
			
		||||
$config['memcached']['ttl'] = $config['time']['now']+300;
 | 
			
		||||
 | 
			
		||||
Plugins::start();
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace LibreNMS\Plugins;
 | 
			
		||||
 | 
			
		||||
class Test {
 | 
			
		||||
 | 
			
		||||
    public function menu() {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user