omd icon indicating copy to clipboard operation
omd copied to clipboard

Creation of dynamic property Special_Controller::$templates is deprecated

Open infraweavers opened this issue 1 year ago • 9 comments

Since upgrading our monitoring boxes to Debian 12 (Bookworm), this broke our pnp4nagios graphs.

This happened because due to the new PHP version installed and the dynamic properties being deprecated.

Example of error: "Creation of dynamic property Special_Controller::$templates is deprecated

file [line]: application/controllers/special.php [16]:"

The solution for this is to update the special.php on the OMD box.

The path for the file is: /opt/omd/versions/5.30-labs-edition/share/pnp4nagios/htdocs/application/controllers/special.php

The script needs to include all the properties so it should look like that:

<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
 * Graph controller.
 *
 * @package    PNP4Nagios
 * @author     Joerg Linge
 * @license    GPL
 */
 
class Special_Controller extends System_Controller {

    // Explicitly declare the dynamic properties
    public $templates;
    public $url;
    public $template;
    public $tpl;
    public $data;
    public $view;

    public function __construct()
    {
        parent::__construct();
        $this->template        = $this->add_view('template');
        $this->template->graph = $this->add_view('graph');
        $this->templates       = $this->data->getSpecialTemplates();
        $this->data->GRAPH_TYPE = 'special';
        if ($this->tpl == '') {
            if ($this->templates)
                $this->tpl = $this->templates[0];
                url::redirect('special?tpl=' . $this->tpl, 302);
        }
    }

    public function index()
    {
        $this->url = "?tpl=" . $this->tpl;
        $this->template->zoom_header   = $this->add_view('zoom_header');
        $this->template->zoom_header->graph_width  = ($this->config->conf['zgraph_width'] + 140);
        $this->template->zoom_header->graph_height = ($this->config->conf['zgraph_height'] + 230);
        $this->template->graph->graph_content = $this->add_view('graph_content_special');
        $this->template->graph->graph_content->graph_width = ($this->config->conf['graph_width'] + 85);
        $this->template->graph->graph_content->timerange_select = $this->add_view('timerange_select');
        $this->template->graph->header        = $this->add_view('header');
        $this->template->graph->search_box    = $this->add_view('search_box');
        $this->template->graph->service_box   = $this->add_view('special_templates_box');
        #$this->template->graph->status_box    = $this->add_view('status_box');
        #$this->template->graph->basket_box    = $this->add_view('basket_box');
        $this->template->graph->widget_menu   = $this->add_view('widget_menu');
        $this->template->graph->graph_content->widget_graph  = $this->add_view('widget_graph');
        #print Kohana::debug($services);
        $this->data->buildDataStruct('__special', $this->tpl, $this->view);
        $this->template->graph->icon_box      = $this->add_view('icon_box');
        $this->template->graph->icon_box->position = "special";
        $this->template->graph->logo_box      = $this->add_view('logo_box');
        // Timerange Box Vars
        $this->template->graph->timerange_box = $this->add_view('timerange_box');
        $this->template->graph->timerange_box->timeranges = $this->data->TIMERANGE;
        $this->template->graph->header->title        = $this->data->MACRO['TITLE'];
        //print Kohana::debug($this->data);
    }
}

If I put this in a pull request, is this likely to be accepted?

infraweavers avatar Nov 20 '24 11:11 infraweavers

Have you tried updating OMD? I have a feeling these issues are solved already?

sni avatar Nov 20 '24 12:11 sni

We have not. We are still on 5.30 and didn't really think to check that. We shall update and if the issue persists we can update.

Thank you

infraweavers avatar Nov 21 '24 08:11 infraweavers

@infraweavers did you update and what was the result?

lausser avatar Jan 18 '25 13:01 lausser

Hello and apologies for the late reply. We did update from 5.30 to 5.50 version but the issue remains the same. It seems that the default files that are pulled when installing 5.50-labs-edition are not compliant with the new version of PHP. I do say files because except the one mentioned (special.php) there is another one as well, /opt/omd/versions/5.50-labs-edition/share/pnp4nagios/htdocs/application/helpers/pnp.php

There is a single line change needed in this script but the entire script that works is the below:

<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
*
*
*/
class pnp_Core {

    public static function clean($string = FALSE){
        if($string === FALSE){
            return;
        }
        if($string == ""){
            return $string;
        }
        $string = preg_replace('/[ :\/\\\]/', "_", $string);
        $string = htmlspecialchars($string);
        return $string;

    }
    public static function shorten($string = FALSE, $length = 25){
        if($string === FALSE){
            return;
        }
        if(strlen($string) > $length){
            $string = substr($string, 0, $length) . "...";
        }
        return $string;
    }
    /*
    *
    */
    public static function xml_version_check($string = FALSE){
        if($string === FALSE){
            return FALSE;
        }
        if( $string == XML_STRUCTURE_VERSION ){
            $string = "valid";
        }else{
            $string = Kohana::lang('error.xml-structure-mismatch', $string, XML_STRUCTURE_VERSION);
        }
        return $string;
    }
    /*
    *
    */
    public static function zoom_icon($host,$service,$start,$end,$source,$view,$graph_width,$graph_height){
        $service = urlencode(urlencode($service));
        $host = urlencode(urlencode($host));
        print "<a href=\"javascript:Gzoom('".url::base(TRUE)."zoom?host=$host&srv=$service&view=$view&source=$source&end=$end&start=$start&graph_width=$graph_width&graph_height=$graph_height');\" title=\"Zoom into the Graph\"><img src=\"".url::base()."media/images/zoom.png\"></a>\n";
    }

    /*
    *
    */
    public static function zoom_icon_special($tpl,$start,$end,$source,$view,$graph_width,$graph_height){
        print "<a href=\"javascript:Gzoom('".url::base(TRUE)."zoom?tpl=$tpl&view=$view&source=$source&end=$end&start=$start&graph_width=$graph_width&graph_height=$graph_height');\" title=\"Zoom into the Graph\"><img src=\"".url::base()."media/images/zoom.png\"></a>\n";
    }

    /*
    *
    */
    public static function add_to_basket_icon($host,$service,$source=FALSE){
        if($source === FALSE){
            print "<span id=\"basket_action_add\"><a title=\"".Kohana::lang('common.basket-add-service')."\" id=\"".$host."::".$service."\"><img width=12px height=12px src=\"".url::base()."media/images/add.png\"></a></span>\n";
        }else{
            print "<span id=\"basket_action_add\"><a title=\"".Kohana::lang('common.basket-add-item')."\" id=\"".$host."::".$service."::".$source."\"><img width=16px height=16px src=\"".url::base()."media/images/add.png\"></a></span>\n";
        }
    }

    /*
    *
    */
    public static function multisite_link($base_url=FALSE,$site=FALSE,$host=FALSE,$service=FALSE){
        if($host && $service){
            $link = sprintf("'%s/view.py?view_name=service&site=%s&host=%s&service=%s'", $base_url,$site,urlencode($host),urlencode($service));
            return $link;
        }
        if($host){
            $link = sprintf("'%s/view.py?view_name=host&site=%s&host=%s'", $base_url,$site,urlencode($host));
            return $link;
        }
    }

    public static function addToUri($fields = array(),$base = True){
        if(!is_array($fields)){
            return false;
        }
        $get = $_GET;
        if($base === True){
            $uri  = url::base(TRUE);
            $uri .= Router::$current_uri;
        }else{
            $uri  = "";
        }
        $uri .= '?';
        foreach($fields as $key=>$value){
            $get[$key] = $value;
        }
        foreach($get as $key=>$value){
            if($value === ''){
                continue;
            }
            $uri .= $key."=".urlencode($value ?? '')."&";
        }
        return rtrim($uri,"&");
    }

    public static function adjust_unit($value,$base=1000,$format='%.3lf'){
        preg_match('/^(-?[0-9\.,]+)\s*(\S?)(\S?)/',$value,$matches);

        $mag = 0;
        $value = $matches[1];
        while ($value >= $base){
            $value /= $base;
            $mag++;
        }
        $pos = 0;
        if ($matches[2] == "%") {
            $unit = '%';
        } else {
            if ($matches[2] == "") {
                $matches[2] = " ";
            }
            if (($matches[2] == "B") or ($matches[2] == "b")) {
                $matches[3] = $matches[2];
                $matches[2] = " ";
            }
            $pos = strpos(' KMGTP',strtoupper($matches[2]));
            $unit = substr(' KMGTP',$mag+$pos,1).$matches[3];
        }
        $val_unit = sprintf ("$format %s", $value, $unit);
        $val_fmt = sprintf ($format, $value);
        $val_fmt = str_replace(' ','',$val_fmt);
        return array ($val_unit,$val_fmt,$unit,pow($base,$mag));
    }

    public static function print_version(){
        return PNP_NAME . "-" . PNP_VERSION . " [ " .  PNP_REL_DATE  . " ]";
    }

}

Please let me know if you require more information.

Thank you

infraweavers avatar Feb 26 '25 14:02 infraweavers

is there any way to reproduce the issue?

sni avatar Feb 27 '25 09:02 sni

Yes. Every time we rebuild any of our OMD hosts to the omd-5.50-labs-edition we get the same issue.

infraweavers avatar Feb 27 '25 11:02 infraweavers

The source for pnp in OMD is here: https://github.com/ConSol-Monitoring/pnp So if you want to open a PR, please do so there.

sni avatar Feb 27 '25 15:02 sni

Thank you,

Just opened a PR.

infraweavers avatar Feb 28 '25 09:02 infraweavers

please open the PR here: https://github.com/ConSol-Monitoring/pnp That's the source of PNP in OMD and it already contains lots of fixes.

sni avatar Feb 28 '25 09:02 sni