########################################################### # IIS Log File Archiver # Resurses through directories and zips all files, # removes the old file if successful, then emails a # detailed log file to the specified email address. ########################################################### $directory = 'K:\'; $zipApp = 'D:\IISArchive\wzzip.exe'; $logFile = ''; $SMTPServer = '10.0.1.2'; $mailTo = 'me@me.com'; $reportSubject = 'Daily IISArchive Report'; ########################################################### use Socket; $|++; &recurseDirectory($directory); &emailLogFile(); exit; ########################################################### sub recurseDirectory { my $zipApp = 'D:\IISArchive\wzzip.exe'; my $thisDirectory = $_[0] . "\\"; opendir (THISDIR, $thisDirectory); my @theseContents = readdir (THISDIR); closedir (THISDIR); foreach my $item (@theseContents) { if (!($item =~ /\.zip$/)) { if (!($item =~ /^\./)) { my $temp = $thisDirectory.$item; if (-d $temp) { $::logFile .= "\n_____________________\n$item\n"; &recurseDirectory($temp); } else { my $tempZipped = $temp; $tempZipped =~ s/\.log/\.zip/g; if (¬TodaysLogFile($item)) { $::logFile .= "$::zipApp \"$tempZipped\" \"$temp\"\n"; my $result = `$::zipApp -m \"$tempZipped\" \"$temp\"\n`; $::logFile .= $result."\n"; if ($result =~ /Unable to erase the file:/) { unlink($tempZipped); } } else { $::logFile .= " --> Skipping today's log file, $item\n"; } } } } } } ########################################################### sub notTodaysLogFile { my @times = localtime(time); $times[5] = $times[5] - 100; if ($times[5] < 10) { $times[5] = "0".$times[5]; } $times[4]++; if ($times[4] < 10) { $times[4] = "0".$times[4]; } if ($times[3] < 10) { $times[3] = "0".$times[3]; } if ($_[0] =~ /$times[5]$times[4]$times[3]\.log/) { return 0; } else { return 1; } } ########################################################### sub emailLogFile { my $smtp_server = $::SMTPServer; my $pop_server = $smtp_server; my $to = $::mailTo; my $subject = $::reportSubject; my $message = $::logFile; my $proto = getprotobyname('tcp'); socket(SOCK, AF_INET, SOCK_STREAM, $proto); my $iaddr = gethostbyname($smtp_server); my $port = getservbyname('smtp', 'tcp'); my $sin = sockaddr_in($port, $iaddr); connect(SOCK, $sin); my $err = ''; send SOCK, "HELO localhost\r\n", 0; recv SOCK, $junk, 512, 0; if ($junk =~ /^5/) {$err = "true";} send SOCK, "mail from: \r\n", 0; recv SOCK, $poo, 512, 0; if ($poo =~ /^5/) {$err = "true";} send SOCK, "RCPT To:<$to>\r\n", 0; recv SOCK, $poo, 512, 0; if ($poo =~ /^5/) {$err = "true";} send SOCK, "DATA\r\n", 0; recv SOCK, $poo, 512, 0; if ($poo =~ /^5/) {$err = "true";} send SOCK, "From: IISArchive\@webloyalty.com\r\n", 0; send SOCK, "Date: ".localtime()."\r\n", 0; send SOCK, "To: $to\r\n", 0; send SOCK, "Subject: $subject\r\n", 0; send SOCK, "\r\n$message\r\n.\r\n", 0; recv SOCK, $poo, 512, 0; if ($poo =~ /^5/) {$err = "Yes";} send SOCK, "QUIT\r\n", 0; recv SOCK, $poo, 512, 0; if ($poo =~ /^5/) {$err = "Yes";} close SOCK; }