修复perl文本> xml转换器脚本

时间:2016-07-22 16:26:17

标签: xml linux perl text ipmi

尝试使用IPMI模板从这里制作脚本:http://www.tune-it.ru/web/tiamat/home/-/blogs/17465使用第一个perl脚本。所以我得到了下一个输出:

# template|sensor|type|dsc|unit|lnr|lcr|lnc|unc|ucr|unr
|1.05V PCH|Threshold|Voltage|Volts|0.870|0.897|0.942|1.194|1.221|1.248
|1.2V BMC|Threshold|Voltage|Volts|1.020|1.047|1.092|1.344|1.371|1.398
|1.5V PCH|Threshold|Voltage|Volts|1.320|1.347|1.401|1.644|1.671|1.698
|12V|Threshold|Voltage|Volts|10.173|10.299|10.740|12.945|13.260|13.386
|3.3VCC|Threshold|Voltage|Volts|2.789|2.823|2.959|3.554|3.656|3.690
|3.3VSB|Threshold|Voltage|Volts|2.789|2.823|2.959|3.554|3.656|3.690
|5VCC|Threshold|Voltage|Volts|4.246|4.298|4.480|5.390|5.546|5.598
|5VSB|Threshold|Voltage|Volts|4.246|4.298|4.480|5.390|5.546|5.598
|CPU Temp|Threshold|Temperature|degrees C|0.000|0.000|0.000|85.000|90.000|90.000
|Chassis Intru|Discrete|Physical Security|discrete||||||
|DIMMA1 Temp|Threshold|Temperature|degrees C|1.000|2.000|4.000|80.000|85.000|90.000
|DIMMA2 Temp|Threshold|Temperature|degrees C|1.000|2.000|4.000|80.000|85.000|90.000
|DIMMB1 Temp|Threshold|Temperature|degrees C|1.000|2.000|4.000|80.000|85.000|90.000
|DIMMB2 Temp|Threshold|Temperature|degrees C|1.000|2.000|4.000|80.000|85.000|90.000
|DIMMC1 Temp|Threshold|Temperature|||||||
|DIMMC2 Temp|Threshold|Temperature|||||||
|DIMMD1 Temp|Threshold|Temperature|||||||
|DIMMD2 Temp|Threshold|Temperature|||||||
|FAN1|Threshold|Fan|RPM|300.000|500.000|700.000|25300.000|25400.000|25500.000
|FAN2|Threshold|Fan|RPM|300.000|500.000|700.000|25300.000|25400.000|25500.000
|FAN3|Threshold|Fan|RPM|300.000|500.000|700.000|25300.000|25400.000|25500.000
|FAN4|Threshold|Fan|RPM|300.000|500.000|700.000|25300.000|25400.000|25500.000
|FAN5|Threshold|Fan|||||||
|FANA|Threshold|Fan|||||||
|PCH Temp|Threshold|Temperature|degrees C|-11.000|-8.000|-5.000|90.000|95.000|100.000
|PS1 Status|Discrete|Power Supply|discrete||||||
|PS2 Status|Discrete|Power Supply|discrete||||||
|Peripheral Temp|Threshold|Temperature|degrees C|-9.000|-7.000|-5.000|80.000|85.000|90.000
|System Temp|Threshold|Temperature|degrees C|-9.000|-7.000|-5.000|80.000|85.000|90.000
|VBAT|Threshold|Voltage|Volts|2.375|2.487|2.599|3.775|3.887|3.999
|VDIMMAB|Threshold|Voltage|Volts|0.948|0.975|1.047|1.344|1.425|1.443
|VDIMMCD|Threshold|Voltage|Volts|0.948|0.975|1.047|1.344|1.425|1.443
|Vcpu|Threshold|Voltage|Volts|1.242|1.260|1.395|1.899|2.088|2.106
|VcpuVRM Temp|Threshold|Temperature|degrees C|-9.000|-7.000|-5.000|95.000|100.000|105.000
|VmemABVRM Temp|Threshold|Temperature|degrees C|-9.000|-7.000|-5.000|95.000|100.000|105.000
|VmemCDVRM Temp|Threshold|Temperature|degrees C|-9.000|-7.000|-5.000|95.000|100.000|105.000

当我运行第二个脚本时:

#!/usr/bin/perl

use strict;
use POSIX qw(strftime);

my $date = strftime("%d.%m.%y", localtime);
my $time = strftime("%H.%M", localtime);

my $delay = 300;
my $history = 7;
my $trends = 365;

# 0 - Line
# 1 - Fill
# 2 - Bold line
# 3 - Dots
# 4 - Striped line
# 5 - Gradient line

my $drawtype = 1;

# 3 - Number (Integer)
# 0 - Number (Folating point)
# 1 - Symbol
# 2 - Log
# 4 - Text

my %sensor_type = (
        Discrete => 3,
        Analog => 0,
);

# 0 - Unclassified
# 1 - Notification
# 2 - Warning
# 3 - Middle
# 4 - High
# 5 - Extreme

my @thresh_type = (
        { pri => 4, dsc => 'Lower Non-Recoverable' },
        { pri => 3, dsc => 'Lower Critical' },
        { pri => 2, dsc => 'Lower Non-Critical' },
        { pri => 2, dsc => 'Upper Non-Critical' },
        { pri => 3, dsc => 'Upper Critical' },
        { pri => 4, dsc => 'Upper Non-Recoverable' },
);

my $head = <<EOF;
<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export version="1.0" date="%s" time="%s">
        <hosts>
                <host name="%s">
                        <proxy_hostid>0</proxy_hostid>
                        <useip>0</useip>
                        <dns></dns>
                        <ip>0.0.0.0</ip>
                        <port>10050</port>
                        <status>3</status>
                        <useipmi>0</useipmi>
                        <ipmi_ip></ipmi_ip>
                        <ipmi_port>623</ipmi_port>
                        <ipmi_authtype>0</ipmi_authtype>
                        <ipmi_privilege>2</ipmi_privilege>
                        <ipmi_username></ipmi_username>
                        <ipmi_password></ipmi_password>
                        <groups>
                                <group>Templates</group>
                        </groups>
                        <items>
%s
                        </items>
                        <triggers>
%s
                        </triggers>
                        <graphs>
%s
                        </graphs>
                        <templates/>
                        <macros/>
                </host>
        </hosts>
        <dependencies/>
</zabbix_export>
EOF

my $item = <<EOF;
                                <item type="12" key="%s" value_type="%d">
                                        <description>%s</description>
                                        <ipmi_sensor>%s</ipmi_sensor>
                                        <delay>$delay</delay>
                                        <history>$history</history>
                                        <trends>$trends</trends>
                                        <status>0</status>
                                        <data_type>0</data_type>
                                        <units>%s</units>
                                        <multiplier>0</multiplier>
                                        <delta>0</delta>
                                        <formula>1</formula>
                                        <lastlogsize>0</lastlogsize>
                                        <logtimefmt></logtimefmt>
                                        <delay_flex></delay_flex>
                                        <authtype>0</authtype>
                                        <username></username>
                                        <password></password>
                                        <publickey></publickey>
                                        <privatekey></privatekey>
                                        <params></params>
                                        <trapper_hosts></trapper_hosts>
                                        <snmp_community></snmp_community>
                                        <snmp_oid></snmp_oid>
                                        <snmp_port>161</snmp_port>
                                        <snmpv3_securityname></snmpv3_securityname>
                                        <snmpv3_securitylevel>0</snmpv3_securitylevel>
                                        <snmpv3_authpassphrase></snmpv3_authpassphrase>
                                        <snmpv3_privpassphrase></snmpv3_privpassphrase>
                                        <applications/>
                                </item>
EOF

my $trigger = <<EOF;
                                <trigger>
                                        <description>%s</description>
                                        <type>0</type>
                                        <expression>%s</expression>
                                        <url></url>
                                        <status>0</status>
                                        <priority>%d</priority>
                                        <comments>%s</comments>
                                </trigger>
EOF

my $graph = <<EOF;
                                <graph name="%s" width="900" height="200">
                                        <ymin_type>0</ymin_type>
                                        <ymax_type>0</ymax_type>
                                        <ymin_item_key></ymin_item_key>
                                        <ymax_item_key></ymax_item_key>
                                        <show_work_period>1</show_work_period>
                                        <show_triggers>1</show_triggers>
                                        <graphtype>0</graphtype>
                                        <yaxismin>0.0000</yaxismin>
                                        <yaxismax>100.0000</yaxismax>
                                        <show_legend>0</show_legend>
                                        <show_3d>0</show_3d>
                                        <percent_left>0.0000</percent_left>
                                        <percent_right>0.0000</percent_right>
                                        <graph_elements>
                                                <graph_element item="%s">
                                                        <drawtype>$drawtype</drawtype>
                                                        <sortorder>0</sortorder>
                                                        <color>009900</color>
                                                        <yaxisside>0</yaxisside>
                                                        <calc_fnc>2</calc_fnc>
                                                        <type>0</type>
                                                        <periods_cnt>$delay</periods_cnt>
                                                </graph_element>
                                        </graph_elements>
                                </graph>
EOF

my ($tname, $items, $triggers, $graphs);
while (<>) {
        chomp;
        next if /^#/;
        my ($template,$sensor,$type,$dsc,$unit,$lnr,$lcr,$lnc,$unc,$ucr,$unr) =
                split(/\|/);

        $template = "Template_$template";
        $tname = $template;

        my $key = sprintf("%s_%s_%s", $type, $dsc, $unit);
        $key =~ s/[\s\\\/]+/_/g;
        $key = sprintf("%s[%s]", $key, $sensor);

        $items .= sprintf($item, $key, $sensor_type{$type},
                        "$type sensor for $dsc $sensor ($unit)", $sensor, $unit);

        if ($type eq 'Discrete') {
                $triggers .= sprintf($trigger, 
                        "$type sensor for $dsc $sensor on {HOSTNAME} was changed",
                        sprintf("{%s:%s.diff(0)}#0", $template, $key), 4, '');
        } elsif ($type eq 'Analog') {
                my $index = -1;
                my $op = '#';
                foreach my $tv ($lnr,$lcr,$lnc,$unc,$ucr,$unr) {
                        $index++;
                        next if ($tv =~ /^$/);
                        my $td = $thresh_type[$index]->{dsc};
                        my $tp = $thresh_type[$index]->{pri};
                        $op = '&lt;' if ($index < 3);
                        $op = '&gt;' if ($index > 2);
                        $triggers .= sprintf($trigger,
                                "$type sensor for $dsc $sensor on {HOSTNAME} changed to $td",
                                sprintf("{%s:%s.last(0)}%s%s", $template, $key, $op, $tv), $tp, '');
                }
                $graphs .= sprintf($graph, "$type sensor for $dsc $sensor ($unit)",
                                "$template:$key");
        } else {
                die "unknown sensor type: $type\n";
        }
}

printf($head, $date, $time, $tname, $items, $triggers, $graphs);

使用此命令

# perl /usr/local/etc/zabbix3/zabbix/externalscripts/get_ipmi.pl -H 192.168.1.100 -U ADMIN -P ADMIN | perl /usr/local/etc/zabbix3/zabbix/externalscripts/make_ipmi.pl > Supermicro_SYS-5018R_MR.xml

我收到了消息:

unknown sensor type: Threshold

我认为,第二列已经是“阈值”类型,而不是数字。这就是错误的原因。

如何更改此部分来源:

my %sensor_type = (
        Discrete => 3,
        Analog => 0,
);

我的文字输出示例? 我不是一个perl程序员。所以任何帮助都会受到赞赏。

1 个答案:

答案 0 :(得分:0)

不幸的是,事情并非那么简单 您还可以在

中仅查看离散和模拟的代码
if ($type eq 'Discrete') {
    ...
} elsif ($type eq 'Analog') {
    ...
}  else {
            die "unknown sensor type: $type\n";
}

您需要添加一个专门处理“阈值”的部分,或者允许它使用“离散”部分或“模拟”部分中的代码。

由于您在阈值行的字段中有小数,您可以尝试以下两种编辑:

my %sensor_type = (
    Discrete => 3,
    Analog => 0,
    Threshold => 0,  # <-- this is the added line
);

并将行elsif ($type eq 'Analog')更改为

} elsif ($type eq 'Analog' or $type eq 'Threshold') {

但是,只是简单地查看了代码,它可能仍然有其他问题试图使用我忽略的Threshold。