Jemand zu Hause?
1
2
3
4
5
6
hoststatus
{
host_name:P20001
..... # viele weitere werte
scheduled_downtime_depth=1 (1 oder 0)
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
my %status; my $file = '/var/status.dat'; { local $/ = "hoststatus\n{"; if ( open my $fh, '<', $file ) { while ( my $block = <$fh> ) { my ($hostname,$status) = $block =~ m! host_name: (.*?) \n.*? scheduled_downtime_depth=(\d+) !xms; $status{$hostname} = $status; } } } for my $host ( keys %status ) { print $host,"\n" if $status{$host} == 1; }
2011-07-19T08:46:58 reneeVon eval() würde ich - wann immer es geht - die Finger lassen. Was, wenn böse Sachen in der Datei stehen? Außerdem ist der gezeigte Ausschnitt kein gültiges Perl...
QuoteDu gehst davon aus, dass die Daten der Appliance niemals von Innen oder Außen korrumpiert werden können.Ich gehe davon aus, dass die Datei von einer Appliance erzeugt wird
2011-07-19T09:23:39 JEnsBei fragen darf ich bestimmt auf euch zurück kommen ?
2011-07-19T09:17:19 GwenDragonQuoteDu gehst davon aus, dass die Daten der Appliance niemals von Innen oder Außen korrumpiert werden können.Ich gehe davon aus, dass die Datei von einer Appliance erzeugt wird
Das ist eine mutige Einstellung.
Ich kann nur sagen, traut keinen Daten, die von Außen ins Programm kommen.
2011-07-19T08:52:59 rosti2011-07-19T08:46:58 reneeVon eval() würde ich - wann immer es geht - die Finger lassen. Was, wenn böse Sachen in der Datei stehen? Außerdem ist der gezeigte Ausschnitt kein gültiges Perl...
Wird es aber mit ein paar gezielten Ersetzungen ;)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/perl
my %status;
my $file = '/var/nagios/status.dat';
#print TEST;
{
local $/ = "hoststatus\s{";
if ( open my $fh, '<', $file ) {
while ( my $block = <$fh> ) {
my ($hostname,$status) = $block =~ m!
host_name: (.*?)
\n.*?
scheduled_downtime_depth=(\d+)
!xms;
$status{$hostname} = $status;
#print TEST;
}
}
}
for my $host ( keys %status ) {
print $host,"\n" if $status{$host} == 1;
}
1 2 3 4 5 6
if ( open my $fh, '<', $file ) { # while-Block } else { print "Fehler beim Einlesen von $file: $!"; }
host_name:\s*(.+?)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
hoststatus {
        host_name=P20002
        modified_attributes=0
        check_command=check_ping!100.0,20%!500.0,40%
        check_period=24x7
        notification_period=24x7
        check_interval=15.000000
        retry_interval=1.000000
        event_handler=
        has_been_checked=1
        should_be_scheduled=1
        check_execution_time=4.126
        check_latency=174.889
        check_type=0
        current_state=0
        last_hard_state=0
        last_event_id=0
        current_event_id=0
        current_problem_id=0
        last_problem_id=0
...
}2011-07-19T08:43:41 rostiDas Dateiformat schreit nach eval();
Wie frage ich & perlintro 
brian's Leitfaden für jedes Perl-Problem1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#!/usr/bin/perl use strict; use warnings; my %status; my $file = './status.dat'; { local $/ = "hoststatus\n{"; if ( open my $fh, '<', $file ) { while ( my $block = <$fh> ) { my ($hostname,$status) = $block =~ m! host_name:([^\n]+) .*? scheduled_downtime_depth=(\d+) !xms; next if !( $hostname && $status ); $status{$hostname} = $status; } } else { print "Fehler beim Einlesen von $file: $!\n"; } } for my $host ( keys %status ) { print $host,"\n" if $status{$host} == 1; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
hoststatus
{
host_name:P20001
..... # viele weitere werte
scheduled_downtime_depth=1 (1 oder 0)
}
hoststatus
{
host_name:P20002
..... # viele weitere werte
scheduled_downtime_depth=1 (1 oder 0)
}
hoststatus
{
host_name:P20003
..... # viele weitere werte
scheduled_downtime_depth=0 (1 oder 0)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/perl
use strict;
use warnings;
my %status;
my $file = './status.dat';
{
local $/ = "hoststatus{\n";
if ( open my $fh, '<', $file ) {
while ( my $block = <$fh> ) {
my ($hostname,$status) = $block =~ m!
host_name:([^\n]+)
.*?
scheduled_downtime_depth=(\d+)
!xms;
next if !( $hostname && $status );
$status{$hostname} = $status;
}
}
else {
print "Fehler beim Einlesen von $file: $!\n";
}
}
for my $host ( keys %status ) {
print $host,"\n" if $status{$host} == 1;
}
P20001
"hoststatus{\n"1 2 3 4 5
my ($value1) = '<td>5</td><td>10</td>' =~ m!<td>(.*)</td>!; print $value1,"\n"; # vs my ($value2) = '<td>5</td><td>10</td>' =~ m!<td>(.*?)</td>!; print $value2,"\n";
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
########################################
# NAGIOS STATUS FILE
#
# THIS FILE IS AUTOMATICALLY GENERATED
# BY NAGIOS. DO NOT MODIFY THIS FILE!
########################################
info {
created=1311076602
version=3.2.3
last_update_check=0
update_available=0
last_version=
new_version=
}
programstatus {
modified_host_attributes=0
modified_service_attributes=0
nagios_pid=5142
daemon_mode=1
program_start=1310972438
last_command_check=1311076545
last_log_rotation=1311026402
enable_notifications=1
active_service_checks_enabled=1
passive_service_checks_enabled=1
active_host_checks_enabled=1
passive_host_checks_enabled=1
enable_event_handlers=1
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/perl
use strict;
use warnings;
my %status;
my $file = './status.dat';
{
local $/ = "hoststatus {\n";
if ( open my $fh, '<', $file ) {
while ( my $block = <$fh> ) {
my ($hostname,$status) = $block =~ m!
host_name:([^\n]+)
.*
scheduled_downtime_depth=(\d+)
!xms;
next if !( $hostname && $status );
$status{$hostname} = $status;
print $status;
}
}
else {
print "Fehler beim Einlesen von $file: $!\n";
}
}
for my $host ( keys %status ) {
print $host,"\n" if $status{$host} == 1;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
hoststatus {
        host_name=P20002
        modified_attributes=0
        check_command=check_ping!100.0,20%!500.0,40%
        check_period=24x7
        notification_period=24x7
        check_interval=15.000000
        retry_interval=1.000000
        event_handler=
        has_been_checked=1
        should_be_scheduled=1
        check_execution_time=4.120
        check_latency=179.616
        check_type=0
        current_state=0
        last_hard_state=0
        last_event_id=0
        current_event_id=0
        current_problem_id=0
        last_problem_id=0
        plugin_output=PING OK - Packet loss = 0%, RTA = 1.58 ms
        long_plugin_output=
        performance_data=rta=1.583000ms;100.000000;500.000000;0.000000 pl=0%;20;40;0
        last_check=1311076259
        next_check=1311077181
        check_options=0
        current_attempt=1
        max_attempts=1
        state_type=1
        last_state_change=1310471633
        last_hard_state_change=1310471633
        last_time_up=1311076281
        last_time_down=0
        last_time_unreachable=0
        last_notification=0
        next_notification=0
        no_more_notifications=0
        current_notification_number=0
        current_notification_id=0
        notifications_enabled=1
        problem_has_been_acknowledged=0
        acknowledgement_type=0
        active_checks_enabled=1
        passive_checks_enabled=1
        event_handler_enabled=1
        flap_detection_enabled=1
        failure_prediction_enabled=1
        process_performance_data=1
        obsess_over_host=1
        last_update=1311076602
        is_flapping=0
        percent_state_change=0.00
        scheduled_downtime_depth=0
        }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/perl
use strict;
use warnings;
my %status;
my $file = '/var/nagios/status.dat';
{
local $/ = "hoststatus {\n";
if ( open my $fh, '<', $file ) {
while ( my $block = <$fh> ) {
my ($hostname,$status) = $block =~ m!
host_name=([^\n]+)
.*
scheduled_downtime_depth=(\d+)
!xms;
next if !( $hostname && $status );
$status{$hostname} = $status;
}
}
else {
print "Fehler beim Einlesen von $file: $!\n";
exit 2;
}
}
my $ausgabe;
for my $host ( keys %status ) {
if ( $status{$host} == 1){
$ausgabe = $ausgabe." $host \n";
}
}
print $ausgabe;
exit 0;