
########################################################
# Please file all bug reports, patches, and feature
# requests under:
#      https://sourceforge.net/p/logwatch/_list/tickets
# Help requests and discusion can be filed under:
#      https://sourceforge.net/p/logwatch/discussion/
########################################################

#######################################################
## Copyright (c) 2008 Laurent Dufour
##                    Francis Borras
## Covered under the included MIT/X-Consortium License:
##    http://www.opensource.org/licenses/mit-license.php
## All modifications and contributions by other persons to
## this script are assumed to have been donated to the
## Logwatch project and thus assume the above copyright
## and licensing terms.  If you want to make contributions
## under your own copyright or a different license this
## must be explicitly stated in the contribution an the
## Logwatch project reserves the right to not accept such
## contributions.  If you have made significant
## contributions to this script and want to claim
## copyright please contact logwatch-devel@lists.sourceforge.net.
#########################################################

$| = 1;

use POSIX qw(uname);
use strict;

my (%Config);

$ENV{PRINTING} = "y";

my $Debug = $ENV{'LOGWATCH_DEBUG'} || 0;
my $Detail = $ENV{'LOGWATCH_DETAIL_LEVEL'} || 0;


my $pathto_chkconfig = $ENV{'pathto_chkconfig'} || '/sbin/chkconfig';
my $pathto_sysctl_conf = $ENV{'pathto_sysctl_conf'} || '/etc/sysctl.conf';
my $pathto_vtysh = $ENV{'pathto_vtysh'} || '/usr/bin/vtysh';
my $pathto_routeadm = $ENV{'pathto_routeadm'} || '/usr/sbin/routeadm';
my $pathto_ip = $ENV{'pathto_ip'} || '/sbin/ip';
my $pathto_ifconfig = $ENV{'pathto_ifconfig'} || '/sbin/ifconfig';

my @ethernet_iface_list;
my @other_iface_list;
my @short_ethernet_iface_list;
my @short_other_iface_list;
my $total_iface=0;
my $total_ethernet_iface=0;
my $total_other_iface=0;
my $DebugCounter=0;

my ($OSname, $hostname, $release, $version, $machine) = POSIX::uname();

if ( $Debug >= 5 ) {
    print STDERR "\n\nDEBUG: Inside zz-network Filter \n\n";
    $DebugCounter = 1;
}



sub show_which {
   my ($cmd) = $_[0];
   my $path = $ENV{'PATH'} || '.';
   my @path_array = split(/:/,$path);
   foreach my $element (@path_array) {
   my $testfile=$element .'/'.$cmd;
   if ( -f $testfile) { print "$testfile\n"; }
   }
}

sub chkcfg {
   my ($service) = $_[0];
   if (($OSname eq "Linux") && ( -f $pathto_chkconfig)) {
      system("$pathto_chkconfig --list $service");
   } else {
      show_which($service);
   }
}

sub routingCapabilities () {
   print "\n\n------------- Routing capabilities----------\n\n";
   chkcfg('routed');
   chkcfg('gated');
   chkcfg('zebra');
   chkcfg('ripd');
   chkcfg('ripngd');
   chkcfg('isisd');
   chkcfg('ospfd');
   chkcfg('ospf6d');
   chkcfg('bgpd');
   if ( -f $pathto_vtysh ) {
      print "\n";
      system("$pathto_vtysh -e 'sh ver'");
   }

   if ($OSname eq "SunOS") {
      if ( ($release eq "5.10") || ($release eq "5.11") ) {
         if ( -f $pathto_routeadm) {
            open(FILE1, "$pathto_routeadm -p |")  || die "can't open $!";
            while (<FILE1>) {
               print $_;
            }
            close(FILE1) || die "can't close $!";
         }
      }
   }
   print "\n\n------------- Routing capabilities----------\n\n";
   print "\n";
}

sub routingState () {
   print "\n\n------------- Routing states ---------------\n\n";
   if ($OSname eq "Linux") {
      if ( -f $pathto_sysctl_conf) {
         open(SYSCTL, "< $pathto_sysctl_conf")  || die "can't open $!";
         while (<SYSCTL>) {
            if ($_ =~ /ip_forward/ ) {
               print "IP Forwarding enabled";
            }
         }
      }
      else {
         open(SYSCTL,"< /proc/sys/net/ipv4/ip_forward")   || die "can't open $!";
         while (<SYSCTL>) {
            print "/proc/sys/net/ipv4/ip_forward set to $_";
         }
         close(SYSCTL) || die "can't close $!";
      }
   }
   elsif ($OSname eq "SunOS") {
      if ( ($release eq "5.10") || ($release eq "5.11") ) {
         if ( -f $pathto_routeadm) {
            open(FILE1, "$pathto_routeadm -p |")  || die "can't open $!";
            while (<FILE1>) {
               if (/ipv(\d+)-forwarding .*default=(\S+) current=(\S+)/) {
                  print "IPv$1 forwarding is $3 (normal state is $2)\n";
               }
            }
            close(FILE1) || die "can't close $!";
         }
      }
   }

   else {
      print "Unable to find routing information in your system.\n";
   }
   print "\n\n------------- Routing states ---------------\n\n";
   print "\n";
}

sub routingReport () {
   print "\n\n------------- Network routes ---------------\n\n";
   if ($OSname eq "Linux") {
      open(NET, "netstat -r -n |")  || die "can't run netstat: $!";
      while (<NET>) {
         print $_;
      }
      close(NET)                    || die "can't close netstat: $!";
   }
   else {
      open(NET, "netstat -r -n |")  || die "can't run netstat: $!";
      while (<NET>) {
         print $_;
      }
      close(NET)                    || die "can't close netstat: $!";
   }
   print "\n\n------------- Network routes ---------------\n\n";
   print "\n";
}

sub ListeningSockets () {
   print "\n\n------------- Listening sockets ---------------\n\n";
   if ($OSname eq "Linux") {
      open(NET, "netstat -lnptu |")  || die "can't run netstat: $!";
      while (<NET>) {
         print $_;
      }
      close(NET)                    || die "can't run netstat: $!";
   }
   else {
      open(NET, "netstat -a -n |")  || die "can't run netstat: $!";
      while (<NET>) {
         if ($_ =~ /LISTEN/ ) {# grep LISTEN
            if (($_ =~ /LISTENING/ ) == 0 ) { # grep -v LISTENING
               print $_;
            }
         }
      }
      close(NET)                    || die "can't run netstat: $!";
   }
   print "\n\n------------- Listening sockets ---------------\n\n";
   print "\n";
}

sub NetworkStats() {
   print "\n\n------------- Network statistics ---------------\n\n";
   if ($OSname eq "Linux") {
      my $cmd_to_show_int="";
      if ( -f $pathto_ip) {
         $cmd_to_show_int=$pathto_ip." -s -h a";
      }
      else {
         $cmd_to_show_int="$pathto_ifconfig -s -a";
      }
      open(NET, "$cmd_to_show_int |")  || die "can't run $cmd_to_show_int: $!";
      while (<NET>) {
         print $_;
      }
      close(NET)                    || die "can't close $cmd_to_show_int: $!";
   }
   else {
      my $netstat_cmd = "netstat -i";
      if ($OSname eq "SunOS") {
         $netstat_cmd .= " -a";
      }
      open(NET, "$netstat_cmd |")  || die "can't run netstat: $!";
      while (<NET>) {
         print $_;
      }
      close(NET)                    || die "can't close netstat: $!";
   }
   print "\n\n------------- Network statistics ---------------\n\n";
   print "\n";
}

NetworkStats();
ListeningSockets();
if ($Detail > 5) {
   routingState();
   routingCapabilities();
   routingReport();
}

# vi: shiftwidth=3 tabstop=3 syntax=perl et
# Local Variables:
# mode: perl
# perl-indent-level: 3
# indent-tabs-mode: nil
# End:
