# Script: OldAgents.pl # Find all old Patchlink Agents # TFiedler # We are dependant on these... use Win32::ODBC; use Win32::NetResource; use Win32::WinError; use POSIX qw(strftime); use Net::SMTP; # Connect to the udrive my %NetResource = ( LocalName => "U:", RemoteName => "\\\\SERVER\\share" ); my $User = "DOMAIN\\USER"; my $Password = "PASSWORD"; Win32::NetResource::AddConnection( \%NetResource, $Password, $User, 1 ); # If you are having problems connecting uncomment the following few lines and comment # out the line above... #if( Win32::NetResource::AddConnection( \%NetResource, $Password, $User, 1 ) ) #{ # print "Successful\n"; #} #else #{ # print NetError(); #} # End connection to udrive routine... if ( -f "oldagents.txt" ) { print "Deleting old data file\n"; unlink("oldagents.txt"); print "Continuing...\n"; } else { print "Data file does not exist.\nContinuing...\n"; } open (OLDAGENTS,">oldagents.txt") || die "unable to open file c:\working\oldagents.txt\n"; # Lets calculate 60 days ago from NOW! # We are doing this in seconds. # The next 3 lines of code were gotten # from Perlmonks.org... my $thisday = time; my $SixtyDaysPrevious = $thisday - 60*24*60*60; $SixtyDaysPrevious = strftime "%m/%d/%Y", (localtime($SixtyDaysPrevious)); # End of perlmonks code. my $db = new Win32::ODBC( "DSN=patchlink_PLUS" ); # does this DB actually exist? if ( ! $db ) { die "Error connecting: " . Win32::ODBC::Error() . "\n"; } # YES! get the data... if ( $db->Sql ( "SELECT AgentName, LastContactDate, AgentVersion from PLUS.dbo.UP_Agents where LastContactDate < '$SixtyDaysPrevious' order by LastContactDate" )) { # Ah you screwed UP! print "Error submitting SQL statement: " . $db->Error() . "\n"; } else { # Show me the money use Time::localtime; while( $db->FetchRow() ) { my @DATA=$db->Data ("AgentName", "LastContactDate", "AgentVersion"); my (undef, undef, $agent) = split(/\\/, $DATA[0]); # if you think you are getting bad data uncomment the # line below and watch the output. You should see # pcnames one per line w/o the \\ ie FIEDLER not \\FIEDLER # print "$agent\n"; my $file="u:\\usr\\NAV\\OS\\$agent\.txt"; # Added 12/07/04 (TF) if ( -f $file ) { open (PCFILE, "$file") || warn "Unable to open $agent.txt\n"; while (my @line = ) { foreach my $info(@line) { my (undef, $login, $os, $sp, $lc, $ie, $ip, undef) = split(",",$info); my $modtime = (stat($file))[9]; $modtime = $modtime - 86400; if ( $modtime == "" ) { $modtime = "null"; } else { $modtime = POSIX::strftime ("%B %d",$modtime,0,0,0,0,0,0,0,0); #$modtime = POSIX::strftime ("%D",$modtime,0,0,0,0,0,0,0,0); } printf OLDAGENTS "Agent: %s\nAgent Version: %s\nLast Contact Date: %s\nUser: %s\nLast login: %s\nLast IP: %s\n\n", $DATA[0], $DATA[2], $DATA[1], $login, $modtime, $ip; # NOW Lets NAG the user... open (LUSER, ">tmp/$agent.txt") || die "unable to create $agent.txt\n"; printf LUSER "Agent: %s\nAgent Version: %s\nLast Contact Date: %s\nUser: %s\nLast in: %s\nLast IP: %s\n\n", $DATA[0], $DATA[2], $DATA[1], $login, $modtime, $ip; close (LUSER); open (LUSER, "tmp/$agent.txt") || die "unable to open $agent.txt\n"; $/=undef; # Sluuuuurp... while (my @EMAIL=) { $smtp=Net::SMTP->new('smtpserver.domain.com'); $smtp->mail( $ENV{USER} ); #$smtp->to('USER\@domain.com');# For testing $smtp->to("$login\@domain.com"); #$smtp->cc('USER2\@domain.com'); #$smtp->bcc('USER3\@domain.com'); $smtp->data(); $smtp->datasend("FROM: help\@domain.com\n"); $smtp->datasend("SUBJECT: PC name $agent\n"); $smtp->datasend("Below is a record of a PC that was last logged into by you. This PC has not been\n"); $smtp->datasend("logged into the domain since $modtime. If you use this PC occasionally,\n"); $smtp->datasend("please make sure that it is turned on for at least four hours per month. If you no\n"); $smtp->datasend("longer are in posession of this PC, please contact the help desk with the information\n"); $smtp->datasend("below. If you use this PC on a regular basis please contact the help desk with the\n"); $smtp->datasend("information below.\n\nThank You\n\n"); foreach my $line(@EMAIL) { $smtp->datasend($line); } $smtp->dataend(); $smtp->quit; } #unlink ("tmp/$agent.txt"); } } } else { my $modtime = (stat($file))[9]; $modtime = $modtime - 86400; if ( $modtime == "" ) { $modtime = "null"; } else { $modtime = POSIX::strftime ("%B %d",$modtime,0,0,0,0,0,0,0,0); #$modtime = POSIX::strftime ("%F",$modtime,0,0,0,0,0,0,0,0); } printf OLDAGENTS "PC: %s\nAgent Version: %s\nLast Patchlink contact: %s\nLast domain login: %s\n\n", $DATA[0], $DATA[2], $DATA[1], $modtime; } } } close(OLDAGENTS); open (OLDAGENTS, "oldagents.txt") || die "unable to open oldagents.txt\n"; $/=undef; # Sluuuuurp... while (@OLDA=) { $smtp=Net::SMTP->new('smtp.domain.com'); $smtp->mail( $ENV{USER} ); $smtp->to('USER1\@domain.com'); $smtp->cc('USER2\@domain.com'); $smtp->bcc('USER3\@domain.com'); $smtp->data(); $smtp->datasend("FROM: Patchlink\@Patchlink.domain.com\n"); $smtp->datasend("SUBJECT: Expired patchlink agents\n"); $smtp->datasend("Patchlink agents 60 days old or older:\n\n\n"); foreach $line(@OLDA) { $smtp->datasend($line); } $smtp->dataend(); $smtp->quit; } # Clean up... close(OLDAGENTS); # close open files $db->Close(); # close the DB Win32::NetResource::CancelConnection( "U:", 1, 1); # close the udrive!!! # Subs sub NetError { my( $Error, $Text, $Provider ); $Error = Win32::GetLastError(); if( ERROR_EXTENDED_ERROR == $Error ) { Win32::NetResource::WNetGetLastError( $Error, $Text, $Provider ); $Result = "Error: $Error: $Text (Genereated by $Provider)"; } else { $Text=Win32::FormatMessage( $Error ); $Result="Error: $Error: $Text"; } return $Result }