Thread \n wird ausgegeben und nicht ausgeführt (7 answers)
Opened by fmhweb at 2012-11-13 12:45

fmhweb
 2012-11-13 14:24
#163295 #163295
User since
2012-11-13
3 Artikel
BenutzerIn
[default_avatar]
Ja, es gibt keine Zeilenumbrüche in der Datei sondern es steht \n drin. Bei diesem Beitrag hatte ich wohl einen kleinen Blackout. Das man \\n nehmen musste bei regex replace hätte ich eigentlich wissen müssen. Ich entwickel derzeit täglich mehrere Nagios plugins und manchmal sieht man halt den Wald vor lauter Bäumen nicht mehr.

Kritik und sonstige Hilfen höhre ich mir aber gerne an.

more (33.3kb):
Code (perl): (dl )
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/perl -w

## ======================== ##
## Author:                  ##
## Email:                   ##
## Date:    13.11.2012      ##
## Version: 1.0             ##
## ======================== ##

## Info:

use strict;
use warnings;
use File::Basename;
use Getopt::Long;
use LWP;
use Data::Dumper;
use XML::LibXML;

use vars qw(
        $opt_hostname
        $opt_query_url
        $opt_basic_user
        $opt_basic_pass
        $opt_timeout
        $opt_verbose
        $opt_help
);

my $progname = basename($0);
my %ERRORS = ('UNKNOWN'  => '3',
              'OK'       => '0',
              'WARNING'  => '1',
              'CRITICAL' => '2');

sub print_help();
sub get_xml_value;

my $opt_servicename = "HOST";
my $opt_basic_realm = "Notify XML Kerberos Access";

Getopt::Long::Configure('bundling');
GetOptions(
        "H=s" => \$opt_hostname, "hostname=s" => \$opt_hostname,
        "S=s" => \$opt_servicename, "servicename=s" => \$opt_servicename,
        "U=s" => \$opt_query_url, "query-url=s" => \$opt_query_url,
        "u=s" => \$opt_basic_user, "basic-user=s" => \$opt_basic_user,
        "p=s" => \$opt_basic_pass, "basic-pass=s" => \$opt_basic_pass,
        "r=s" => \$opt_basic_realm, "basic-realm=s" => \$opt_basic_realm,
        "v" => \$opt_verbose, "verbose" => \$opt_verbose,
        "t=s" => \$opt_timeout, "timeout=s" => \$opt_timeout,
        "h" => \$opt_help, "help" => \$opt_help
) || die "Try `$progname --help' for more information.\n";

local $SIG{ALRM} = sub{
        print "Timeout after $opt_timeout seconds";
        exit $ERRORS{'UNKNOWN'};
};

if($opt_timeout){alarm($opt_timeout);}

if(!$opt_help && $opt_hostname && $opt_query_url && $opt_basic_user && $opt_basic_pass){
        my $use_port = 80;
        my $base_url = "";
        my $query_url_regex = 'http([s]*):\/\/([\w-]+(\.[\w-]+|\.[\w-]+\.[\w-]+))\/.+/';
        if($opt_query_url =~ m/$query_url_regex/){
                if($1){$use_port = 443;}
                $base_url = $2;
        }
        else{
                print "ERROR - Unkonwn query url format: '$opt_query_url' != '$query_url_regex'\n";
                exit $ERRORS{'UNKNOWN'};
        }
        
        my $query_url = $opt_query_url.$opt_hostname."/".$opt_servicename.".xml";
        if($opt_verbose){print "Attempting to query url: $query_url - Base URL: $base_url\n";}

        my $user_agent = LWP::UserAgent->new;
        $user_agent->agent("Nagios/0.1");

        $user_agent->credentials(
                ($base_url.":".$use_port),
                $opt_basic_realm,
                $opt_basic_user => $opt_basic_pass
        );

        my $request = HTTP::Request->new(POST => $query_url);
        $request->content_type('text/plain');
        $request->content('query=libwww-perl&mode=dist');

        my $response = $user_agent->request($request);
        if(!$response->is_success){
                print $response->status_line."\n";
                if($opt_verbose){"Dumping user agent:\n".Dumper($user_agent)."Dumping response:\n".Dumper($response);}
                exit $ERRORS{'UNKNOWN'};
        }
        if($opt_verbose){print $response->content."\n";}
        my $document = XML::LibXML->new->parse_string($response->content);
        my $check_type = "HOST";
        if($opt_servicename){$check_type = "SERVICE";}
        my $nagios_perfdata = get_xml_value($document,"NAGIOS_".$check_type."PERFDATA");
        if($nagios_perfdata){$nagios_perfdata = "|$nagios_perfdata";}
        my $output = get_xml_value($document,"NAGIOS_".$check_type."OUTPUT").get_xml_value($document,"NAGIOS_LONG".$check_type."OUTPUT");.$nagios_perfdata;
        $output =~ s/\\n/\n/g;
        print $output;
        exit get_xml_value($document,"NAGIOS_".$check_type."STATEID");
}
else{print print_help();exit $ERRORS{'UNKNOWN'};}

sub get_xml_value{
        my ($document,$value) = @_;
        my $returnvalue = "";
        for($document->findnodes("//status/$value/text()")){$returnvalue = $_->toString();}
        return $returnvalue;
}

sub print_help () {
        print "\n$progname - \n\n";
        print "Options are:\n";
        print "  -H, --hostname                  Nagios hostname\n";
        print "  -S, --servicename               Nagios servicename\n";
        print "  -U, --query-url                 The URL where xml files are found\n";
        print "  -u, --basic-user                Basic authentication user\n";
        print "  -p, --basic-pass                Basic authentication pass\n";
        print "  -r, --basic-realm               Basic authentication realm\n";
        print "  -v, --verbose                   verbose output - not for Nagios\n";
        print "  -t, --timeout                   timeout in seconds\n";
        print "  -h, --help                      display this help and exit\n\n";
        print "Example:\n";
        print "  ./$progname\n\n";
}

print "End of script. Should not be reached\n";
exit $ERRORS{'UNKNOWN'};


modedit Editiert von pq: more-tag hinzugefügt
Last edited: 2012-11-13 14:30:43 +0100 (CET)

View full thread \n wird ausgegeben und nicht ausgeführt