#!/usr/bin/perl
##########################################################################
# $Id: samba,v 1.12 2003/11/26 14:36:30 kirk Exp $
##########################################################################

########################################################
# This was written and is maintained by:
#    Kirk Bauer <kirk@kaybee.org>
#
# Please send all comments, suggestions, bug reports,
#    etc, to kirk@kaybee.org.
########################################################

$Debug = $ENV{'LOGWATCH_DEBUG'};
$Detail = $ENV{'LOGWATCH_DETAIL_LEVEL'} || 0;
$SocketReadError=0;
$DbOpenFail=0;
$GetDomainMasterStatusFail=0;

if ( $Debug >= 5 ) {
	print STDERR "\n\nDEBUG: Inside Samba Filter \n\n";
}

while (defined($ThisLine = <STDIN>)) {
	chomp($ThisLine);
	if ( 
		($ThisLine =~ /Currently not implemented/) or
		($ThisLine =~ /version .+ started/) or
		($ThisLine =~ /oplock[_ ]break/) or
		($ThisLine =~ /No route to host/) or
		($ThisLine =~ /SIGTERM: going down/) or
		($ThisLine =~ /response packet id \d+ received with no matching record/) or
		($ThisLine =~ /matchname/i) or
		# Ignore entries in smbmount logfile
		($ThisLine =~ /smbmount/) or
		($ThisLine =~ /become_local_master/) or
		($ThisLine =~ /become_domain_master/) or
		($ThisLine =~ /add_domain_logon_names/) or
		($ThisLine =~ /become_logon_server/) or
		($ThisLine =~ /start_async_dns/) or
		($ThisLine =~ /timeout connecting to/) or
		($ThisLine =~ /Operation not permitted/) or
		($ThisLine =~ /Record does not exist/) or
		($ThisLine =~ /Connection reset by peer/) or
		($ThisLine =~ /Multiple .+ responses received for a query/) or
		($ThisLine =~ /Connection timed out/) or
		($ThisLine =~ /closed connection to/) or
		($ThisLine =~ /Got SIGHUP/) or
		($ThisLine =~ /current master browser/) or
		($ThisLine =~ /debug_message/) or
		($ThisLine =~ /===============================================================/)
	) {
		#Don't care about these...
	} elsif ( ($Host, $Service, $User) = ( $ThisLine =~ /([^ ]+ \([^ ]+\)) connect to service ([^ ]+) as user ([^ ]+)/ ) ) {
		$Connect{$Service}{$User}{$Host}++;
	} elsif ( ($NoService) = ( $ThisLine =~ /couldn't find service (\S+)/ ) ) {
		$NoServ{$NoService}++;
	} elsif ($ThisLine =~ s/Denied connection from\s+(\S+)$/$1/) {
		$Denied{$ThisLine}++;
	} elsif ( ($Ip,$Name,$Group,$Subnet) = ($ThisLine =~ /Denied connection from  \(([^ ]+)\)  Samba name server ([^ ]+) is now a local master browser for workgroup ([^ ]+) on subnet ([^ ]+)/ ) ) {
		$Denied{$Ip}++;
		$BeLocalMaster{$Subnet}{$Group}{$Name}++;
	} elsif (($User) = $ThisLine =~ /rejected invalid user ([^ ]+)/ ) {
		$InvalidUser{$User}++;
	} elsif (($User) = $ThisLine =~ /Couldn't find user '([^ ]+)'/) {
		$NotFoundUser{$User}++;
	} elsif (($User) = $ThisLine =~ /Rejecting user '([^ ]+)'/) {
		$RejectedUser{$User}++;
	} elsif ( ( $ThisLine =~ /^\[[^\]]+\] lib\/util_sock.c:read_data\(436\)/ ) ) {
		# This is due to a nasty bug in samba which causes it to drop connections :-(
		$SocketReadError++;
	} elsif ( ( $ThisLine =~ /unable to open passdb database.$/ ) ) {
		$DbOpenFail++;
	} elsif ( ($Server,$Ip,$Group) = ($ThisLine =~ /Server ([^ ]+) at IP ([^ ]+) is announcing itself as a local master browser for workgroup ([^ ]+) and we think we are master. Forcing election.$/ ) ) {
		$Temp = $Server . "(" . $Ip . ")";
		$ForceElection{$Group}{$Temp}++;
	} elsif ( ($Server,$Ip,undef) = ($ThisLine =~ /process_name_refresh_request: unicast name registration request received for name ([^ ]+) from IP ([^ ]+) on subnet ([^ ]+)./ ) ) {
		$Temp = "$Server ($Ip)";
		$UnicastRegister{$Temp}++;
	} elsif ( ($Group,$Subnet) = ($ThisLine =~ /standard_fail_register: Failed to register\/refresh name ([^ ]+) on subnet ([^ ]+)$/ ) ) {
		$FailedRegister{$Subnet}{$Group}++;
	} elsif ( ($Ip,$Group,undef) = ($ThisLine =~ /register_name_response: server at IP ([^ ]+) rejected our name registration of ([^ ]+) with error code ([^ ]+)\.$/ ) ) {
		$RejectRegister{$Group}{$Ip}++;
	} elsif ( ($Ip) = ($ThisLine =~ /get_domain_master_name_node_status_fail:  Doing a node status request to the domain master browser at IP ([^ ]+) failed\.  Cannot get workgroup name\.$/ ) ) {
		$CantGetGroup{$Ip}++;
	} elsif ( ($Signal,undef,$Version) = ($ThisLine =~ /INTERNAL ERROR: Signal ([^ ]+) in pid ([^ ]+) \(([^ ]+)\)  Please read the file BUGS.txt in the distribution$/ ) ) {
		$Temp = "Version $Version with signal $Signal";
		$Crash{$Temp}++;
	} elsif ( ( $ThisLine =~ /get_domain_master_name_node_status_fail\(([^ ]+)\)/ ) ) {
		$GetDomainMasterStatusFail++;
	} else {
		# Report any unmatched entries...
		$OtherList{$ThisLine}++;
	}
}

#########################################
#

if (%Crash) {
	print "\nWARNING!!!!!!\n";
	print "Server crashed:\n";
	foreach $Dead (sort {$a cmp $b} keys %Crash) {
		print "   $Dead : $Crash{$Dead} Time(s)\n";
	}
}

if (($Detail >= 5) and (keys %Connect)) {
   print "\nOpened Sessions:\n";
   foreach $Serv (sort {$a cmp $b} keys %Connect) {
      print "   Service $Serv as user:\n";
      foreach $Us (sort {$a cmp $b} keys %{$Connect{$Serv}}) {
         print "      $Us from host:\n";
         foreach $Ho (sort {$a cmp $b} keys %{$Connect{$Serv}{$Us}}) {
            print "         $Ho : $Connect{$Serv}{$Us}{$Ho} Time(s)\n";
         }
      }
   }
}

if (keys %Denied) {
   print "\nConnections Denied:\n";
   foreach $Line (keys %Denied) {
      print "   $Line : $Denied{$Line} Time(s)\n";
   }
}

if (keys %ForceElection) {
	print "\nForced Election:\n";
	foreach $Group (sort {$a cmp $b} keys %ForceElection) {
		print "   In workgroup $Group when announced server was:\n";
		foreach $Host (sort {$a cmp $b} keys %{$ForceElection{$Group}}) {
			print "      $Host : $ForceElection{$Group}{$Host} Time(s)\n";
		}
	}
}

if (keys %BeLocalMaster) {
	print "\nChanged Local Master Browser:\n";
	foreach $Subnet (sort {$a cmp $b} keys %BeLocalMaster) {
		print "   On subnet $Subnet:\n";
		foreach $Group (sort {$a cmp $b} keys %{$BeLocalMaster{$Subnet}}) {
			print "      For workgroup $Group:\n";
			foreach $Name (sort {$a cmp $b} keys %{$BeLocalMaster{$Subnet}{$Group}}) {
				print "         $Name : $BeLocalMaster{$Subnet}{$Group}{$Name} Time(s)\n";
			}
		}
	}
}

if (keys %CantGetGroup) {
	print "\nCannot get workgroup name from domain name browser:\n";
	foreach $Ip (sort {$a cmp $b} keys %CantGetGroup) {
		print "   $Ip : $CantGetGroup{$Ip} Time(s)\n";
	}
}

if ($GetDomainMasterStatusFail > 0) {
	print "\nFailed to get Domain Master node name: $GetDomainMasterStatusFail Time(s)\n";
}

if (($Detail >= 5) and (keys %NoServ)) {
   print "\nCouldn't find services:\n";
   foreach $ThisOne (sort {$a cmp $b} keys %NoServ) {
      print "   $ThisOne : $NoServ{$ThisOne} Time(s)\n";
   }
}

if (($Detail >= 5) and (keys %UnicastRegister)) {
	print "\nUnicast name registration requests:\n";
	foreach $ThisOne (sort {$a cmp $b} keys %UnicastRegister) {
		print "   $ThisOne : $UnicastRegister{$ThisOne} Time(s)\n";
	}
}

if (keys %FailedRegister) {
	print "\nFailed to register/refresh:\n";
	foreach $Subnet (sort {$a cmp $b} keys %FailedRegister) {
		print "   On subnet $Subnet:\n";
		foreach $Group (sort {$a cmp $b} keys %{$FailedRegister{$Subnet}}) {
			print "      $Group : $FailedRegister{$Subnet}{$Group} Time(s)\n";
		}
	}
}

if (keys %RejectRegister) {
	print "\nRejected our name registration:\n";
	foreach $Group (sort {$a cmp $b} keys %RejectRegister) {
		print "   Name $Group at IP:\n";
		foreach $Ip (sort {$a cmp $b} keys %{$RejectRegister{$Group}}) {
			print "      $Ip : $RejectRegister{$Group}{$Ip} Time(s)\n";
		}
	}
}

if ($DbOpenFail > 0) {
	print "\nFailed to open passwd database: $DbOpenFail Time(s)\n";
}

if (keys %InvalidUser) {
   print "\nInvalid Users:\n";
   foreach $Line (keys %InvalidUser) {
      print "   $Line : $InvalidUser{$Line} Time(s)\n";
   }
}

if (keys %NotFoundUser) {
   print "\nUsers not found in UNIX Database:\n";
   foreach $Line (keys %NotFoundUser) {
      print "   $Line : $NotFoundUser{$Line} Time(s)\n";
   }
}

if (keys %RejectedUser) {
   print "\nRejected Users:\n";
   foreach $Line (keys %RejectedUser) {
      print "   $Line : $RejectedUser{$Line} Time(s)\n";
   }
}

if ($SocketReadError > 0) {
	print "\nSocket Read Error (Samba bug): $SocketReadError Time(s)\n";
}

if (keys %OtherList) {
	print "\n**Unmatched Entries**\n";
	foreach $Line (sort {$a cmp $b} keys %OtherList) {
		print "$Line : $OtherList{$Line} Time(s)\n";
	}
}

exit(0);

# vi: shiftwidth=4 tabstop=4
