Talk: TDC BB-TEL efterår 2009

From Teknologisk videncenter
Jump to: navigation, search

Perl løsninger

Tomas Ping program

device fil

# Liste over hosts der skal pinges
# Tomme linier eller linier der starter med # ignoreres
# Felt 1 hostnavn eller ip adresse
# Felt 2 protocoltype

www.mitsubishi.co.jp      tcp
83.90.47.30               udp
#80.80.12.112             icmp
www.cpan.org              syn
83.90.239.188             tcp
 vejr.tomas.dk            tcp

Crontab

12 */2 * * * /home/tcj/bin/PingHosts.pl

PingHosts.pl

 1 #!/usr/bin/perl
 2 
 3 ### INITIERING ###
 4 use strict;
 5 use warnings;
 6 use Net::Ping;
 7 
 8 my $HOSTS = "/home/tcj/devices";        # Enheder der skal pinges
 9 my $LOG   = "/home/tcj/devices.log";    # Ping log
10 my $p;          # linie fra HOST fil
11 my $host;       # Host der skal pinges
12 my $proto;      # Hvilken protokol der benyttes
13 my $ts;         # Timestamp
14 
15 ### SUBRUTINER ###
16 
17 sub TilLogFil {
18 ## skriver tekst til logfil
19 # Input : Tekststreng
20 # Input : log fil
21   my $log_tekst = shift;
22   my $log_fil   = shift;
23   my $logopen   = 0;
24 
25   open LOGFIL, ">>$log_fil" or $logopen = 1;
26   # Hvis $logopen = 0, kunne filen ikke <E5>bnes - IKKE kritisk!
27   if ( $logopen == 0 ) {
28         print LOGFIL "$log_tekst";
29         close LOGFIL;
30   } else {
31         warn "Logfil: $log_fil kunne ikke <E5>bnes\n";
32         warn "- Der er ikke skrevet i $log_fil\n";
33   }
34 }
35 
36 sub TimeStamp {
37 ## Returnerer tidsstempel
38 # Output: Tidsstempel: YYYYMMDD TT:MM:SS
39   my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
40   my $tstamp;
41 
42   $year += 1900;
43 
44   $tstamp = sprintf"%d%02d%02d %02d:%02d:%02d", $year, $mon+1, $mday, $hour, $min, $sec;
45   return $tstamp;
46 }
47 
48 sub PingDevice {
49 ## Ping device med en given protokol
50 # Input: host
51 # Input: protocol
52 # Output: ok = 0; notok = 1
53   my $host     = shift;
54   my $protocol = shift;
55   my $timeout  = 1;
56   my $pr;       # Ping result: undef = no host; 0 = succes; 1 = no reply
57 
58   my $po = Net::Ping->new($protocol, $timeout);
59 
60   $pr = $po->ping($host);
61   if ( (! defined $pr) or ( $pr == 1) ) {
62         return 1;
63   } else {
64         return 0;
65   }
66 }
67 
68 
69 ### MAIN ###
70 
71 $ts = TimeStamp;
72 TilLogFil("$ts ___ START PING HOSTS i $HOSTS ___\n", $LOG);
73 
74 # <C5>ben Host fil
75 open HOST, "<$HOSTS" or die "Kunne ikke <E5>bne $HOSTS\n";
76 
77 while ( $p = <HOST> ) {
78         # Tomme og kommentar linier springes over
79         if (  $p =~ /^\s*#|^\s+[^\S+]/ ) { next; }
80         $p =~  /\s*(\S+)\s+(\S+).*/;
81 
82         $host = $1;
83         $proto = $2;
84 
85         $ts = TimeStamp;
86         TilLogFil("$ts Ping: $host Protokol: $proto\t ",$LOG);
87 
88         # Udf<F8>r ping
89         if ( PingDevice($host, $proto) == 0 ) {
90         # OK !!!
91                 TilLogFil("OK\n",$LOG);
92         } else {
93         # NOT OK !!!
94                 TilLogFil("NOT OK\n",$LOG);
95         }
96 }
97 close HOST;
98 $ts = TimeStamp;
99 TilLogFil("$ts ___ SLUT PING HOSTS i $HOSTS ___\n", $LOG);

Logfil

20091006 12:21:10 ___ START PING HOSTS i /home/tcj/devices ___
20091006 12:21:10 Ping: www.mitsubishi.co.jp Protokol: tcp       OK
20091006 12:21:10 Ping: 83.90.47.30 Protokol: udp        NOT OK
20091006 12:21:10 Ping: www.cpan.org Protokol: syn       NOT OK
20091006 12:21:10 Ping: 83.90.239.188 Protokol: tcp      OK
20091006 12:21:11 Ping: vejr.tomas.dk Protokol: tcp      OK
20091006 12:21:12 ___ SLUT PING HOSTS i /home/tcj/devices ___

--Tomas Christian Jensen 12:29, 6 October 2009 (CEST)