nagios 配合 pnp 作图xml error问题

Monday, January 18, 2010

nagios配合pnp作图,只要nagios吐出perfdata就可以了。

在nagios为了能在页面上显示中文,配置了主机的hostgroup中的alias为中文,pnp作图问题就来了

打开被设置过中文的服务器pnp页面显示

XML error: Invalid character at line 107 in /var/spool/nagios/pnp/rrd/10.80.0.10/_HOST_.xml

而其他的没有设置过中文的能正常显示图形

查看了/var/spool/nagios/pnp/rrd/10.80.0.10/_HOST_.xml文件的第107行,有中文字符

上服务器,到pnp的web路径下

grep "XML error" * -rn

找到
include/function.inc.php:1510:            $debug->doCheck("xml_err","XML error: ".xml_error_string(xml_get_error_code($xml_parser))." at line ".xml_get_current_line_number($xml_parser)." in ".$rrddef);

打开include/function.inc.php
if (!xml_parse_into_struct($xml_parser, $data, $vals, $index)) {
            $debug->doCheck("xml_err","XML error: ".xml_error_string(xml_get_error_code($xml_parser))." at line ".xml_get_current_line_number($xml_parser)." in ".$rrddef);
}

原来在xml_parse_into_struct时有错误抛出,所以就无法作图,分析是$data中有中文字符造成。

那就用iconv转一下字符喽
$xml_parser = xml_parser_create();
        if(($handle = fopen($rrddef, "rb")) === false ){
        return $NAGIOS;
    }
        $contents = '';
        while (!feof($handle)) {
          $data .= fread($handle, 8192);
        }
        fclose($handle);
        $data=iconv("GB2312","UTF-8",$data);
        if (!xml_parse_into_struct($xml_parser, $data, $vals, $index)) {
            $debug->doCheck("xml_err","XML error: ".xml_error_string(xml_get_error_code($xml_parser))." at line ".xml_get_current_line_number($xml_parser)." in ".$rrddef);
        }

好了,大功告成,现在中文别名也能作图了。

Posted by Michael.Ding at 4:59 PM

0 comments:

Post a Comment