Leser: 26
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#!/usr/bin/perl use strict; use warnings; # Config-Datei my $config = '/path/to/config.txt'; # Oeffne Datei zum Lesen, bei Fehler brich ab open my $fh, '<', $config or die $!; # Lese jede Zeile ein while( my $line = <$fh> ) { # teile Zeile an Tabulator und nimm 4. Element my ($url) = (split /\t/, $line)[3]; # uebergib URL an wget my $content = qx{ wget $url | grep "String to check" }; print $content } close $fh;
system
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
use strict;
use warnings;
# Config-Datei
my $config = './webpage_check.cfg';
# Oeffne Datei zum Lesen, bei Fehler brich ab
open my $fh, '<', $config or die $!;
# Lese jede Zeile ein
while( my $line = <$fh> ) {
# teile Zeile an Tabulator und nimm die Elemente
my ($fqdn) = (split /\t/, $line)[0];
print $fqdn;
my ($svc_id) = (split /\t/, $line)[1];
print $svc_id;
my ($comment) = (split /\t/, $line)[2];
print $comment;
my ($url) = (split /\t/, $line)[3];
print $url;
my ($pattern) = (split /\t/, $line)[4];
print $pattern;
# uebergib URL an wget
my $content = qx{ /usr/sfw/bin/wget -t 2 -T 5 -S -O - $url | grep "$pattern" };
print $content
}
close $fh;
1
2
3
4
5
6
7
8
9
10
11
12
# teile Zeile an Tabulator und nimm die Elemente
my ($fqdn) = (split /\t/, $line)[0];
print $fqdn;
my ($svc_id) = (split /\t/, $line)[1];
print $svc_id;
my ($comment) = (split /\t/, $line)[2];
print $comment;
my ($url) = (split /\t/, $line)[3];
print $url;
my ($pattern) = (split /\t/, $line)[4];
print $pattern;
my ($fqdn, $svc_id, $comment $url, $pattern) = split /\t/, $line;
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; # Config-Datei my $config = './webpage_check.cfg'; # Oeffne Datei zum Lesen, bei Fehler brich ab open my $fh, '<', $config or die $!; # Lese jede Zeile ein while( my $line = <$fh> ) { # teile Zeile an Tabulator und nimm die Elemente my ($fqdn,$svc_id,$comment,$url,$pattern) = split(/\t/, $line); print "fqdn: $fqdn\n"; print "svc_id: $svc_id\n"; print "comment: $comment\n"; print "url: $url\n"; print "pattern: $pattern\n"; # uebergib URL an wget my $content = qx{ /usr/sfw/bin/wget -t 2 -T 5 -S -O - '$url' | grep '$pattern' }; print $content } close $fh;
Guest werCode (perl): (dl )my $content = qx{ /usr/sfw/bin/wget -t 2 -T 5 -S -O - '$url' | grep '$pattern' };
$url=~s/'/%27/g;
1
2
3
4
5
6
7
8
9
10
11
12
13
# uebergib URL an wget
# my $content = qx{ /usr/sfw/bin/wget -t 2 -T 5 -S -O - $url | grep '$pattern' };
my $wget = system "/usr/sfw/bin/wget", "-t", "2", "-T", "5", "-S", "-O", "-", $url;
my @string = grep $pattern $wget;
print @string;
#system "/usr/sfw/bin/wget", "-t", "2", "-T", "5", "-S", "-O", "-", $url, "|", "grep", $pattern;
# print $content
}
close $fh;
1 2 3 4
@result = system ("/usr/sfw/bin/wget", "-t", "2", "-T", "5", "-S", "-O", "-", $url) == 0 or die system "/opt/OV/bin/opcmsg", "o=BBMAG_Web_Monitor", "a=$svc_id", "msg_g=Test", "msg_t=TEST"; grep /$pattern/, @result;
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
# ... my $resultat = safe_backticks( "/usr/sfw/bin/wget", "-t", "2", "-T", "5", "-S", "-O", "-", $url ); my @lines = split /\n/, $resultat; my $anzahl = grep /$pattern/, @lines; # ... sub safe_backticks { my ($cmd, @args) = @_; open (my $pipe, '-|', $cmd, @args) or die "Could not run $cmd @args: $!\n"; my $output = do {local $/; <$pipe>}; defined ($output) or die "read: $!"; unless( close($pipe) ) { my $error = ($? & 0x7f) ? 'Signal ' . ($? & 0x7f) : 'Exit status ' . ($? >> 8); die "$error: $cmd @args"; } return $output; }
Guest guest daveCode: (dl )1
2my $wget = system "/usr/sfw/bin/wget", "-t", "2", "-T", "5", "-S", "-O", "-", $url;
my @string = grep $pattern $wget;
Guest guest daveob ich das modul hab weiß ich nicht. Weiß auch nicht wie ich das rausfinde.. meine manpage gibt mir keine Hinweise auf modul Nutzung.
QuoteMit den backticks das funzt leider nicht aufgrund der $url Variable.
Guest guest daveob ich das modul hab weiß ich nicht. Weiß auch nicht wie ich das rausfinde..
2009-09-25T11:08:05 reneeGuest guest daveob ich das modul hab weiß ich nicht. Weiß auch nicht wie ich das rausfinde..
Am einfachsten, indem Du auf der Kommandozeile perl -MModulename -e 1 machst. Kommt ein Fehler, ist das Modul nicht installiert...
1
2
3
4
5
6
use LWP::Simple;
my $content = get('http://url.com') or die "got nothing";
if ($content =~ /suche/) {
print "gefunden!";
}
1
2
3
/opt/OV/nonOV/perl/a/bin/perl -MLWP::Simple -e 1
Can't locate LWP/Simple.pm in @INC (@INC contains: /opt/OV/nonOV/perl/a/lib/5.8.8/sun4-solaris-thread-multi /opt/OV/nonOV/perl/a/lib/5.8.8 /opt/OV/nonOV/perl/a/lib/site_perl/5.8.8/sun4-solaris-thread-multi /opt/OV/nonOV/perl/a/lib/site_perl/5.8.8 /opt/OV/nonOV/perl/a/lib/site_perl .).
BEGIN failed--compilation aborted.
perl -MLWP::UserAgent -e 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
#!/usr/bin/perl use Data::Dumper; use miniwget; use strict; use warnings; my $site=miniwget->new(shift(@ARGV)); if($site->success()) { print "#"x80,"\n"; print $site->body(); print "#"x80,"\n"; } else { print "#"x80,"\n"; print "MESSAGE:".$site->message()."\n"; print "STATUS:".$site->status()."\n"; print "#"x80,"\n"; print "HOST:".$site->host()."\n"; print "PORT:".$site->port()."\n"; print "PATH:".$site->path()."\n"; print "#"x80,"\n"; print "HEADER:\n"; print Dumper($site->header()); print "#"x80,"\n"; print "BODY:\n"; print $site->body()."\n"; print "#"x80,"\n"; }
test_miniwget 'http://www.test.de/'
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#!/usr/bin/perl use miniwget; use strict; use warnings; my $site=miniwget->new(); if($site->parse('http://www.test.de/')) { if($site->load()) { print $site->body(); } }
1 2 3 4 5 6 7 8 9 10 11 12 13
#!/usr/bin/perl use miniwget; use strict; use warnings; my $site=miniwget->new(); $site->host('www.test.de'); $site->path('/index.html'); $site->load(); print $site->body() if($site->success());
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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
package miniwget; use strict; use warnings; use IO::Socket; my $send_header=<<'EOH'; GET %s HTTP/1.1 Accept: text/html,*/* Accept-Language: de-DE,de,de-de,en-us,en Accept-Encoding: deflate User-Agent: Mozilla/5.0 (X11; U; Linux; cl-CL; rv:42) Gecko/2229082006 Fake/42 (Clingon) Host: %s Connection: Close EOH # get an website # accepts url like http://test.com/a/smal/path?option=txt... # $site=wget_mini->new([$url]) sub new { my $class=shift; my $url=shift; my $self= bless({},$class); $self->load($url) if($url); return $self; } # request success sub success { my $stat=shift()->{status}; return $stat >= 200 && $stat < 300; } # get the header hash sub header { return shift()->{header}; } # get the body sub body { return shift->{body}; } # get messages sub message { return shift->{message}; } # get statusnumber sub status { return shift->{status}; } # get/set host sub host { my $self=shift; my $host=shift; if($host) { my $old=$self->{host}; $self->{host}=$host; return $old; } else { return $self->{host}; } } # get/set port sub port { my $self=shift; my $port=shift || 0; if($port >0 ) { my $old=$self->{port}; $self->{port}=$port; return $old; } else { return $self->{port}; } } #get/set path sub path { my $self=shift; my $path=shift; if($path) { my $old=$self->{path}; $self->{path}=$path; return $old; } else { return $self->{path}; } } # load site # $ok=$site->load([$url]); sub load { my $self=shift; my $url=shift; $self->{header}={}; $self->{body}=''; $self->{message}=''; $self->{status}=-1; if($url && !$self->parse($url)) { $self->{message}='no url'; $self->{status}='000'; return 0; } if(!$self->{host}) { $self->{message}='no host'; $self->{status}='001'; return 0; } $self->{port}=80 unless($self->{port}); $self->{path}='/' unless($self->{path}); my $socket=IO::Socket::INET->new(PeerAddr => $self->{host}, PeerPort => $self->{port} ); if(!$socket) { $self->{message}='no connectinon to host'; $self->{status}='002'; return 0; } binmode($socket); # create header my $header=sprintf($send_header,$self->{path},$self->{host}); # cleanup header $header=~s/[\x0D\x0A]+/\x0D\x0A/gs; $header.="\x0D\x0A"; # send header: print $socket $header; # read header { local $/="\x0A"; while (my $line = <$socket>) { $line=~s/[\x0D\x0A]+//; last unless ($line); $self->{header}->{lc($1)}=$2 if($line=~m!^\s*([\w-]+)\s*:\s*(.+?)[\x0D\x0A]*$!); if($line=~m!\s*HTTP/[\d.]+\s+(\d+)\s+(.+?)[\x0D\x0A]*$!) { $self->{status}=$1; $self->{message}=$2; } } } # read body if($self->{header}->{'content-length'}) { my $length=$self->{header}->{'content-length'}; $socket->read($self->{body},$length) if($length>0); } else { # read all you get while(!$socket->eof()) { $self->{body}.=$socket->getc(); } } return 1; } # parse url # $ok=$site->parse($url); sub parse { my $self=shift; my $url=shift || ''; if($url=~m"^http://((?:[-\w]+\.)+\w{1,3})((?::\d{1,5})?)(.*)$") { $self->{host}=$1; $self->{port}=$2 || 80; $self->{path}=$3 || '/'; $self->{port}=~s/^://; return 1; } return 0; } 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
/opt/OV/nonOV/perl/a/bin/perl -d webpage_check.pl Loading DB routines from perl5db.pl version 1.28 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(webpage_check.pl:24): my $MON_NAME = $ARGV[0]; DB<1> n main::(webpage_check.pl:27): my $config = '/var/opt/OV/bin/instrumentation/webpage_check.cfg'; DB<1> n main::(webpage_check.pl:30): open my $fh, '<', $config or die $!; DB<1> n main::(webpage_check.pl:33): while( my $line = <$fh> ) { DB<1> n main::(webpage_check.pl:40): my ($fqdn, $svc_id, $url, $pattern, $comment) = split (/\t/, $line); DB<1> n main::(webpage_check.pl:43): my $content = safe_backticks('/usr/sfw/bin/wget', '-t', '2', '-T', '5', '-S', '-O', '-', $url ); DB<1> n --16:16:19-- http://blablabla/ => `-' Resolving blablabla Connecting to blablabla|:80... connected. HTTP request sent, awaiting response... HTTP/1.1 200 OK Date: Mon, 28 Sep 2009 14:16:19 GMT Server: Apache/2.2.2 (Win32) mod_jk/1.2.19 Set-Cookie: JSESSIONID=AC77D1E711FE15DCDBCA27E5C4F64E7A; Path=/jupiter Pragma: No-cache Cache-Control: no-cache,no-store,max-age=0 Expires: Thu, 01 Jan 1970 00:00:00 GMT Connection: close Content-Type: text/html;charset=UTF-8 Length: unspecified [text/html] [ <=> ] 71,956 --.--K/s 16:16:19 (3.75 MB/s) - `-' saved [71956] main::(webpage_check.pl:50): my @foo = grep(/$pattern/, $content); DB<1> n main::(webpage_check.pl:52): if ( @foo ){ DB<1> n main::(webpage_check.pl:54): system ("/opt/OV/bin/opcmon", "Webpage_check=0", "-object", "Webpage_Monitor", "-option", "node=", $fqdn, "-option", "url=", $url, "-option", "svc_id=", $svc_id, "-option", "comment=", $comment); DB<1> n Invalid parameter: 'servername_blabla'. (OpC30-683) main::(webpage_check.pl:40): my ($fqdn, $svc_id, $url, $pattern, $comment) = split (/\t/, $line); DB<1> n main::(webpage_check.pl:43): my $content = safe_backticks('/usr/sfw/bin/wget', '-t', '2', '-T', '5', '-S', '-O', '-', $url ); DB<1> q
2009-09-28T14:21:59 findus2099Der Aufruf system "cmd", $var, usw. tut bei mir nicht. Laut meinem LamaBuch und dem Beitrag von 'bettwerworld' im wiki mach ich aber alles richtig!?
opcmon Webpage_check=0 -object Monitor -option node=testserver.de -option url=http://www.testserver.de
1 2 3 4 5 6 7 8 9 10 11 12
#!/opt/OV/nonOV/perl/a/bin/perl use strict; use warnings; my $node = 'bbv.......com'; my $fqdn = 'B. Jupiter Login'; my $url = 'http://server.de/Jupiter'; my $svc_id = 'Jupiter Server'; my $comment = 'WebServer 1'; system ("/opt/OV/bin/opcmon", "Webpage_check=0", "-object", "Webpage_Monitor", "-option", "node=", $fqdn, "-option", "url=", $url, "-option", "svc_id=", $svc_id, "-option", "comment=", $comment);
system ("/opt/OV/bin/opcmon", "Webpage_check=0", "-object", "Monitor", "-option", "node=$fqdn", "-option", "url=$url", "-option", "svc_id=$svc_id", "-option", "comment=$comment")
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
#!/opt/OV/nonOV/perl/a/bin/perl use strict; use warnings; sub safe_backticks { my ($cmd, @args) = @_; open (my $pipe, '-|', $cmd, @args) or die "Could not run $cmd @args: $!\n"; my $output = do {local $/; <$pipe>}; defined ($output) or die "read: $!"; unless( close($pipe) ) { my $error = ($? & 0x7f) ? 'Signal ' . ($? & 0x7f) : 'Exit status ' . ($? >> 8); die "$error: $cmd @args"; } return $output; } my $MON_NAME = $ARGV[0]; print $MON_NAME; # Config-Datei Initialisierung my $config = '/var/opt/OV/bin/instrumentation/webpage_check.cfg'; # Oeffne Datei zum Lesen, bei Fehler brich ab open my $fh, '<', $config or die $!; # einlesen des Config Files while( my $line = <$fh> ) { # teile Zeile an Tabulator und nimm die Elemente # Aufbau der Konfidatei: # Name des WebServers in 'FQDN' Tabulator 'ServiceID' Tabulator 'URL für wget' Tabulator 'zu matchendes pattern' Tabulator 'Kommentar' # fqdnTABsvc_idTABurlTABpatternTABcomment my ($fqdn, $svc_id, $url, $pattern, $comment) = split (/\t/, $line); # uebergib URL an wget my $content = safe_backticks('/usr/sfw/bin/wget', '-t', '2', '-T', '5', '-S', '-O', '-', $url ); # Suche des zu erwartenden pattern innerhalb des Abbilds my @foo = grep(/$pattern/, $content); if ( @foo ){ # wenn das $pattern im $content gefunden wurde, erzeuge einen Openview internen Aufruf (Wert 0 erzeugt ein Normal (grün) Event) system "/opt/OV/bin/opcmon", "$MON_NAME=0", "-object", "Webpage_Monitor", "-option", "node=$fqdn", "-option", "url=$url", "-option", "svc_id=$svc_id", "-option", "comment=$comment"; }else { # wenn das $pattern im $content nicht gefunden wurde, erzeuge einen Openview internen Aufruf (Wert 1 erzeugt ein major (orange) Event) system "/opt/OV/bin/opcmon", "$MON_NAME=1", "-object", "Webpage_Monitor", "-option", "node=$fqdn", "-option", "url=$url", "-option", "svc_id=$svc_id", "-option", "comment=$comment"; } } close $fh;