我需要从 XML 文件中获取一些值...该文件位于多个站点中.. 示例: http: //1.1.1.1/apache2-default/status.xml, http: //1.1.1.2/apache2- default/status.xml等。XML文件的格式为:
<?xml version="1.0"?>
<gateway>
<version>Kannel bearerbox version `1.4.3' </version>
<status>running, uptime 0d 14h 19m 49s</status>
<wdp>
<received><total>0</total><queued>0</queued></received>
<sent><total>0</total><queued>0</queued></sent>
</wdp>
<sms>
<received><total>50569</total><queued>0</queued></received>
<sent><total>38</total><queued>0</queued></sent>
<storesize>-1</storesize>
<inbound>1.53,1.52,0.98</inbound>
<outbound>0.00,0.00,0.00</outbound>
</sms>
<smscs><count>5</count>
<smsc>
<name>SMPP:1.2.3.4:3802/3802:hng123:HNDSMPP</name>
<id>hng123</id>
<status>online 51589s</status>
<received>0</received>
<sent>0</sent>
<failed>0</failed>
<queued>0</queued>
</smsc>
<smsc>
<name>SMPP:1.2.3.4:3757/3757:vashn:HNDSMPP</name>
<id>vas1</id>
<status>online 51589s</status>
<received>40872</received>
<sent>32</sent>
<failed>0</failed>
<queued>0</queued>
</smsc>
</smscs>
</gateway>
这是我创建的代码...
#!/usr/bin/perl -w
use strict;
use warnings;
use XML::Twig;
use Data::Dumper;
use LWP::Simple;
my $download_dir= "/root/lab_xml_perl/v2/Script_perl/bajados";
my @ips_server = ("192.168.246.129", "192.168.246.130");
for my $servers (@ips_server) {
my $bajo = getstore("http://$servers/apache2-default/status.xml", "$download_dir/status.xml");
my $file = "$download_dir/status.xml";
my $twig = XML::Twig->new();
$twig->parsefile($file);
my $root = $twig->root->first_child('smscs');
for my $connection ($root->children){
my $nombre_server = $connection->first_child_text('name');
my $id_server = $connection->first_child_text('id');
my $status_server = $connection->first_child_text('status');
my @status_gral = split(/\s+/, ${status_server});
my $recibidos_msg_server = $connection->first_child_text('received');
my $enviados_msg_server = $connection->first_child_text('sent');
my $encolados_msg_server = $connection->first_child_text('queued');
my $fallidos_msg_server = $connection->first_child_text('failed');
print "miId.String.id = Tabla_$id_server\n";
print "stringProp.String = $nombre_server\n";
print "idProp = $id_server\n";
print "status_idObs.StringObservation.obs = $status_gral[0]\n";
print "recvTotal.Metric = $recibidos_msg_server\n";
print "sentTotal.Metric = $enviados_msg_server\n";
print "queuedTotal.Metric = $encolados_msg_server\n";
print "faildTotal.Metric = $fallidos_msg_server\n";
}
}
然而,当运行脚本时,我在开始时得到了一些空字段......和一个警告......每个服务器的每个 xml 文件......这是输出......为什么会这样?......我没有需要它们……只需要与 XML 相关的字段……
miId.String.id = Tabla_
stringProp.String =
idProp =
Use of uninitialized value $status_gral[0] in concatenation (.) or string at ./v25.pl line 32.
status_idObs.StringObservation.obs =
recvTotal.Metric =
sentTotal.Metric =
queuedTotal.Metric =
faildTotal.Metric =
miId.String.id = Tabla_hng123
stringProp.String = SMPP:1.2.3.4:3802/3802:hng123:HNDSMPP
idProp = hng123
status_idObs.StringObservation.obs = online
recvTotal.Metric = 0
sentTotal.Metric = 66173
queuedTotal.Metric = 0
faildTotal.Metric = 0
miId.String.id = Tabla_vas1
stringProp.String = SMPP:1.2.3.4:3757/3757:vashn:HNDSMPP
idProp = vas1
status_idObs.StringObservation.obs = online
recvTotal.Metric = 0
sentTotal.Metric = 45492
queuedTotal.Metric = 0
faildTotal.Metric = 0
Use of uninitialized value $status_gral[0] in concatenation (.) or string at ./v25.pl line 32.
status_idObs.StringObservation.obs =
recvTotal.Metric =
sentTotal.Metric =
queuedTotal.Metric =
faildTotal.Metric =
miId.String.id = Tabla_hng123
stringProp.String = SMPP:1.2.3.4:3802/3802:hng123:HNDSMPP
idProp = hng123
status_idObs.StringObservation.obs = online
recvTotal.Metric = 0
sentTotal.Metric = 0
queuedTotal.Metric = 0
faildTotal.Metric = 0
miId.String.id = Tabla_vas1
stringProp.String = SMPP:1.2.3.4:3757/3757:vashn:HNDSMPP
idProp = vas1
status_idObs.StringObservation.obs = online
recvTotal.Metric = 40872
sentTotal.Metric = 32
queuedTotal.Metric = 0
faildTotal.Metric = 0
非常感谢您的帮助。
迈克尔。