Talk: TDC BB-TEL efterår 2009

From Teknologisk videncenter
Revision as of 11:28, 6 October 2009 by Tcj (talk | contribs) (New page: =Perl løsninger== == Tomas Ping program == === device fil === <pre> # Liste over hosts der skal pinges # Tomme linier eller linier der starter med # ignoreres # Felt 1 hostnavn eller ip a...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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

<source lang=perl line=0>

  1. !/usr/bin/perl
      1. INITIERING ###

use strict; use warnings; use Net::Ping;

my $HOSTS = "/home/tcj/devices"; # Enheder der skal pinges my $LOG = "/home/tcj/devices.log"; # Ping log my $p; # linie fra HOST fil my $host; # Host der skal pinges my $proto; # Hvilken protokol der benyttes my $ts; # Timestamp

      1. SUBRUTINER ###

sub TilLogFil {

    1. skriver tekst til logfil
  1. Input : Tekststreng
  2. Input : log fil
 my $log_tekst = shift;
 my $log_fil   = shift;
 my $logopen   = 0;
 open LOGFIL, ">>$log_fil" or $logopen = 1;
 # Hvis $logopen = 0, kunne filen ikke <E5>bnes - IKKE kritisk!
 if ( $logopen == 0 ) {
       print LOGFIL "$log_tekst";
       close LOGFIL;
 } else {
       warn "Logfil: $log_fil kunne ikke <E5>bnes\n";
       warn "- Der er ikke skrevet i $log_fil\n";
 }

}

sub TimeStamp {

    1. Returnerer tidsstempel
  1. Output: Tidsstempel: YYYYMMDD TT:MM:SS
 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
 my $tstamp;
 $year += 1900;
 $tstamp = sprintf"%d%02d%02d %02d:%02d:%02d", $year, $mon+1, $mday, $hour, $min, $sec;
 return $tstamp;

}

sub PingDevice {

    1. Ping device med en given protokol
  1. Input: host
  2. Input: protocol
  3. Output: ok = 0; notok = 1
 my $host     = shift;
 my $protocol = shift;
 my $timeout  = 1;
 my $pr;       # Ping result: undef = no host; 0 = succes; 1 = no reply
 my $po = Net::Ping->new($protocol, $timeout);
 $pr = $po->ping($host);
 if ( (! defined $pr) or ( $pr == 1) ) {
       return 1;
 } else {
       return 0;
 }

}


      1. MAIN ###

$ts = TimeStamp; TilLogFil("$ts ___ START PING HOSTS i $HOSTS ___\n", $LOG);

  1. <C5>ben Host fil

open HOST, "<$HOSTS" or die "Kunne ikke <E5>bne $HOSTS\n";

while ( $p = <HOST> ) {

       # Tomme og kommentar linier springes over
       if (  $p =~ /^\s*#|^\s+[^\S+]/ ) { next; }
       $p =~  /\s*(\S+)\s+(\S+).*/;
       $host = $1;
       $proto = $2;
       $ts = TimeStamp;
       TilLogFil("$ts Ping: $host Protokol: $proto\t ",$LOG);
       # Udf<F8>r ping
       if ( PingDevice($host, $proto) == 0 ) {
       # OK !!!
               TilLogFil("OK\n",$LOG);
       } else {
       # NOT OK !!!
               TilLogFil("NOT OK\n",$LOG);
       }

} close HOST; $ts = TimeStamp; TilLogFil("$ts ___ SLUT PING HOSTS i $HOSTS ___\n", $LOG); <source>

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 ___