Francesco
17th July 2002, 17:43
I'm posting this script because of a question on a different forum. I am sure there are a million varieties out there (why not post some for comparisons?), but I am also sure that there are administrators who are new to Baan and can use some basic maintenance scripts as an example to get them started.

Anyway, I did this one in Perl just for the heck of it. Perl buffs will probably shoot me for it. ;)
#!/usr/local/bin/perl

#--------------------------------------------------------
#
# hoover.pl
# Version Date Author
# 2.2 2/25/01 Francesco Frentrop
# This program cleans up tmp files from the tmp directory
# that have been in there more than 2 days and
# mails out an activity report to the administrator(s)
#
#--------------------------------------------------------

# get all files from $BSE/tmp directory that are older than 2 days
system "find .../bse/tmp -mtime +1 -type f | grep -v bshell | grep -v found > oldfiles";

# READ OLDFILES
open(INPUT, "oldfiles") || die "Could not open file : $!\n";
while (<INPUT>)
{
chop;
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks)
= stat($_);
$totalsize = $totalsize + $size;
system "rm $_ $2>/dev/null" ;
}
close (INPUT);

$totalsize = sprintf("%.2f", $totalsize/1048576);
# Create list of bshells that are older than 7 days
system "find .../bse/tmp -mtime +6 -type f | grep bshell > oldbshells";

# Compose message for email
system "cat .../bse/utils/hoover/header oldfiles > hoovermsg";
open(INPUT, "oldbshells") || die "There are no bshells older than 7 days.\n";
open(OUTPUT, ">>hoovermsg") || die "Could not open file : $!\n";
print OUTPUT "\nA total of $totalsize Mb was recovered.\n\n";
while (<INPUT>)
{
chop;
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks)
= stat($_);
if ($uid ne "job100")
{
print OUTPUT "$_ , owned by $uid, is more than 7 days old, now deleting.\n";
system "rm $_";
}
}
close(OUTPUT);
close(INPUT);

# Mail message to Administrator
system "mail baan_administrator\@yourdomain.com < hoovermsg";

# Remove debris
system "rm oldfiles";
system "rm oldbshells";
system "rm hoovermsg";

Francesco
17th July 2002, 18:00
Thanks for the [CODE] tags...umm..Pat?

Looks a lot better. lol

patvdv
17th July 2002, 18:22
Yep, it's me. I like the script name: 'hoover' !

learner
27th May 2003, 19:15
I am running BaaN on Win NT box and don't know the abc of perl , is there a similar code available in a form of batch file(*.bat) ??

Waiting for your reply.

Learner

p.cole
27th May 2003, 20:18
This is very minimal, but works on NT with ActiveState perl installed.

Perl is my scripting language of choice with Baan, it can be written in a cross platform way, and has many features useful for parsing text files.

EdHubbard
21st August 2003, 19:15
Philip's perl code:

Can anybody modify this to only remove files of the type "tmp....."?

I would be grateful if somebody could as perl is a bit beyond me!


thanks

Ed

p.cole
21st August 2003, 19:25
This is the perl code to check that the first part of the filename begins with tmp

HTH

Phil

croezen
22nd August 2003, 10:21
|******************************************************************************
|* tdebndeltmp VRC B40O c4 eas0
|* Delete old files from the BaaN tmp directory
|* croezen
|* 13-03-2003 [10:10]
|******************************************************************************

declaration:
long fd
long rt
long size
long mode
string home(80)
string fname(80)

long file.size
long change.time
long modified.time
long access.time
long check.time


choice.cont.process:
on.choice:
home = bse.tmp.dir$()

check.time = (date.num() - date.to.num(1970,1,1) - 7) * (24 * 3600)
fd = dir.open(home)
repeat
fname = dir.entry(fd, TDIR+TFILE, rt, size, mode)
if not isspace(fname) then
if file.stat( home & "\" & fname,
file.size,
change.time,
modified.time,
access.time) = 0 then
if access.time < check.time then
file.rm(home & "\" & fname)
endif
endif
endif
until (isspace(fname))
e = dir.close(fd)

NvanBeest
22nd August 2003, 10:51
Not bad at all! Just edit your post please, and add the tags [ code=baan ]...[ /code ] (without the spaces) around the code to make it more readable.

croezen
22nd August 2003, 11:07
Hi Nico,

I do not seem to get the layout right. When i am editing the post the TABS I used make the layout readable but when I post it the TABS disapaere.

<> this stupid dutchman can't even typ correctly :- )

EdHubbard
22nd August 2003, 14:44
Thanks to Phil & croezen for your code.

I modified croezen's to only include files beginning with "tmp" by putting the following code before "if not isspace(fname) then":

if fname(1; 3) = "tmp" then...

bamnsour
22nd August 2003, 16:57
For those who do not want to go through installing PERL, you can make a package using a product called perl2exe. You can make stand alone packages with this product from perl code for NT/2000 and also for all the Unix flavours.

The product can be downloaded from http://www.indigostar.com/perl2exe.htm


- Bader

kceinfor
19th September 2003, 10:00
I like very much your perl toy, Francesco! The email sending idea is the kind I never think about...
My own really very simple way of getting rid of those tmp files that crowd my disks is:
find $BSE_TMP -name "tmp*" -atime +0 -exec rm {} \;

With this single line I delete every tmp* file on the Baan temp directory which is more than 24h old.
I run it by means of cron every morning.

Francesco
19th September 2003, 18:43
Well folks, all the renewed interest in my little hoover util motivated me to rebuild the thing and get it right this time.

(not saying that the old one doesn't work, its just not very scalable, not very portable, and quite frankly it isn't really in perl) ;)

I ran the first test of the new and (very much) improved hoover 3.0 last night, and it held up. It is better than it was. Better, stronger...faster. This baby really CLEANS.

On the basis of this script lie two thoughts.
1. Clean up _everything_ that has no more value, regardless of age.
2. Leave everything alone that is in use.

In other words, I don't care if a bshell is 2 months old. If it still has a running process associated with it, it stays. I don't care if a tmp file is an hour old. If nobody is claiming it, it goes.
(Forget about reprint functionality. How often do you actually use that?).

However, hoover 3.0 should be adjustable to meet whatever requirements you have. It needs to become the mother of all Baan clean-up scripts.

I will post the as-is-pre-release-wip version below and update it as it grows.

I can certainly use the collective brain trust here to make this happen. Some of the issues I still need to address are:

- portability (although it is now more true perl, it should work on all systems).
- flexibility (cover all your wants and needs with the flick of a switch)
- security (always an issue when scripting)
- stability (need lots n lots of testing)
- avoid non-standard modules (currently using two)

So anybody who actually understands Perl (yea right), or wants to mess around with it, give it your best shot.

Francesco
19th September 2003, 18:46
#!/usr/local/bin/perl

#--------------------------------------------------------
#
# hoover.pl
# Version Date Author
# 3.0a 8/25/03 Francesco Frentrop
#
# Co-author(s): YOUR-NAME-HERE
# This program cleans up the Baan tmp directory
#
#--------------------------------------------------------
#* Platform : This tool was written on perl, v 5.005_03 built for Solaris 2.6
#* It should be fairly easy to port around.
#* Copyright 2001 by Francesco Frentrop
#
#* All Rights Reserved
#*
#* Permission to use, copy, modify, and distribute this software and its
#* documentation for any purpose and without fee is hereby granted,
#* provided that the above copyright notice appear in all copies and that
#* both that copyright notice and this permission notice appear in
#* supporting documentation.
#*
#* Francesco Frentrop and Baanboard.com DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
#* SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
#* AND FITNESS, IN NO EVENT SHALL Francesco Frentrop nor Baanboard.com BE LIABLE FOR ANY
#* SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
#* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
#* WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
#* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
#* OR PERFORMANCE OF THIS SOFTWARE.
#******************************************************************************


use strict;
use File::Find;
use Filehandle;
use Number::Format;

my $oneday = time - 60*60*24;
my $twodays = time - 60*60*24*2;
my $threedays = time - 60*60*24*3;
my $fourdays = time - 60*60*24*4;
my $fivedays = time - 60*60*24*5;
my $sixdays = time - 60*60*24*6;
my $sevendays = time - 60*60*24*7;
my $fourteendays = time - 60*60*24*14;
my $thirtydays = time - 60*60*24*30;
my ($temptime, $bshelltime, $dev, $ino, $mode, $nlink, $uid,
$gid, $rdev, $size, $atime, $mtime, $ctime, $blksize,
$blocks, $pscmd, $dfcmd, $runtime, $directory, $x, $y,
$othertime, $psucmd, $hooverhome, $ducmd, $psucmd, $fcount,
$mailcmd, $Email, $file, $StdOut, $StdErr, $owner);
my ($dirstring, $reasoncode, $reportline, $dirsize, $no_of_files);
my $freedsize;
my @report; #Array to hold temporary report information.

#**************************************************************
#--------------------------------------------------------------
#
# SYSTEM SETTINGS
#
# set the retention time for files in the temp directories to
# any of the defined variables above, or use a custom number.
# Use a value of 0 for indefinite retention.
# To avoid misbehavior, hard code the full path to the tmp
# directory. There is no guarantee that the $BSE_TMP variable
# will be set at time of execution.

$directory = $BSE_TMP;
$hooverhome = "$BSE/utils/hoover";
$temptime = $twodays; # lifetime for tmp files.
$bshelltime = $threedays; # lifetime for bshells.
$othertime = $sevendays; # lifetime for other files.

# For portability reasons, all system calls in this program are
# listed below. (nah, some are. I would like to get rid of all
# of these. Suggestions are welcome - FF)

$pscmd = "ps -fp"; # list process
$psucmd = "ps -fu"; # list all processes for user
$dfcmd = "df -kt"; # disk free space in kB
$ducmd = "du -ks"; # directory usage in kB
$mailcmd = "mail"; # send mail

#
#--------------------------------------------------------------
#**************************************************************

# Initiations

# Redirect standard output and standard error to file. Unbuffer
# output for on-the-fly processing.

$StdOut = "$hooverhome/output";
$StdErr = "$hooverhome/hoover.log";
$Email = "$hooverhome/hoovermsg";

# Save current stdout and stderr.
open(SAVESTDOUT, ">&STDOUT") || die "Unable to save STDOUT, $!\n";
open(SAVESTDERR, ">&STDERR") || die "Unable to save STDERR, $!\n";

open(STDOUT, ">$StdOut") || die "Can't create output file $StdOut, $!\n";
open(STDERR, ">$StdErr") || die "Can't create log file $StdErr, $!\n";

STDOUT->autoflush(1);
STDERR->autoflush(1);

open(EMAIL, ">$Email") || die "Can't create email message.\n";

# beginstatus
$dirsize = (split /\s+/, `$ducmd $directory`)[0];
$dirsize = format_bytes($dirsize * 1024, 0);
$no_of_files = (split / +/, `ls $directory | wc -l`)[1];

print EMAIL "From: Hoover\n";
print EMAIL "Subject: Clean-up diary.\n\n";
print EMAIL "This is an automated email message generated by Hoover.\n\n";
print EMAIL "Activity Report.\n";
print EMAIL "Initial status: Directory $directory contains $dirsize MB in $no_of_files files.\n\n";

# main routine
sub wanted
{
# Possible options:
# 1. File is a tmp* file. -> remove if owner not online or older than 'temptime'.
# 2. File is a bshell.* file. -> remove if associated process no longer active
# notify if older than 'bshelltime'.
# 3. File is a directory. -> remove when empty
# Because the finddepth function is used, the directory tree will be processed
# from the bottom up. Any directory with only old files should therefore be empty.
# 4. File is something else -> remove if older than othertime.
# 5. File is (part of) lost+found or other 'untouchable' -> let it be.
# 6. File is supposed to be (and stay) in the tmp.

# Get file from directory. Then get file statistics and owner.

my $fullfile = $directory . "/" . $_;
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks)
= stat($_);
my $owner = (getpwuid($uid))[0];

if ($_ =~ /^\./ || $_ =~ /lost/) { # file is a booboo
$dirstring = "File in exception list.";
$reasoncode = 5;
}
elsif ($mode < 32768 && $mode > 0) { # file is directory
# attempt to remove directory
if (unlink ($_)) {
$dirstring = "Removed empty directory";
$reasoncode = 3;
} else {
$dirstring = "ERROR! Directory could not be removed.";
$reasoncode = 13;
}
}
elsif ($_ =~ /tmp/) { # file is tempfile
# test for associated user
my $logins = system $psucmd . " " . $owner . " > /dev/null";
if ($mtime <= $temptime || $logins != 0) { # file obsolete
# attempt to remove the file
if (unlink ($_)) {
$dirstring = "Temporary file obsolete.";
$reasoncode = 1;
} else {
$dirstring = "ERROR! Unable to remove file. ";
$reasoncode = 11;
}
} else {
$reasoncode = 6;
}
}
elsif ($_ =~ /bshell/) { # file is bshell
my $pid= substr($_, 7);
my $rc = system $pscmd . " " . $pid . " > /dev/null"; # test if associated bshell is running
if ($rc == 0) {
if ($mtime <= $bshelltime) {
my $hrs = (time - $mtime)/60/60;
$dirstring = "WARNING! bshell PID $pid has been running for $hrs hours";
$reasoncode = 22;
}
} else {
my $hrs = (time - $mtime)/60/60;
# attempt to remove file
if (unlink ($_)) {
$dirstring = "No process associated with bshell file.";
$reasoncode = 2;
} else {
$dirstring = "ERROR! Unable to remove file. ";
$reasoncode = 12;
}
}
}
else {
if ($mtime <= $temptime) {
# attempt to remove the file
if (unlink ($_)) {
$dirstring = "Temporary file too old.";
$reasoncode = 4;
} else {
$dirstring = "ERROR! Unable to remove file. ";
$reasoncode = 14;
}
} else {
# This file is legit
$dirstring = "No action taken.";
$reasoncode = 6;
}
}

$reportline = "$reasoncode|$_|$size|$owner|$dirstring";

# Write reportline to report array
if ($reasoncode != 5 && $reasoncode != 6) {
push @report, $reportline;
my $temp = "stall line";
}
}

finddepth(\&wanted, $directory);

# end status
$dirsize = (split /\s+/, `$ducmd $directory`)[0];
$dirsize = format_bytes($dirsize * 1024, 0);
$no_of_files = (split / +/, `ls $directory | wc -l`)[1];

print EMAIL "\nEnd status: Directory $directory contains $dirsize MB in $no_of_files files.\n\n";
print EMAIL "A total of ... was cleaned from the temp directory.\n\n";
print EMAIL "Activity log:\n";

# Sort reportlines and output to file
sort @report;

format STDOUT_TOP =
No File Size Owner Reason
==========================================================================================
.

format STDOUT =
@> @<<<<<<<<<<<<<<< @>>>>>>>>>>> @<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$reasoncode, $file, $size, $owner, $dirstring
.

while ( $reportline = pop(@report))
{
($reasoncode, $file, $size, $owner, $dirstring) = split(/\|/, $reportline);
write;
}

# Undo Output redirects
open(STDOUT, ">&SAVESTDOUT");
open(STDERR, ">&SAVESTDERR");

jpvdgiessen
19th September 2003, 20:33
Instead of $ducmd = "du -ks"; you can use the following idea of getting the size of a directory.
I suppose no subdirectories are in it.


#!/usr/bin/perl
$dir = $BSE_TMP ;
opendir(DIR, "$dir"); # read all files
my @Files = sort( grep -f, map "$dir/$_", grep !/^\./, readdir DIR);
closedir(DIR);
foreach $file (@Files) {

$Size += -s $file ; # read size of file and add to $Size
}
print "$file $Size\n" ;


For getting information about the disksize:


use Filesys::DiskSpace;
($fs_type, $fs_desc, $used, $avail, $fused, $favail) = df $dir;


or with the following code (is more de unix like df command):

use Filesys::DiskFree;
$handle = new Filesys::DiskFree;
$handle->df();
print "The root device is ".$handle->device("/")."\n";
print "It has ".$handle->avail("/")." bytes available\n";
print "It has ".$handle->total("/")." bytes total\n";
print "It has ".$handle->used("/")." bytes used\n";

afripack
23rd April 2008, 11:59
Hi Learner, did you ever get a response for this or how did you overcome it? Am also on Win2K and don't know anything about Perl.

Mike

TimoJanssen
10th November 2008, 16:42
Schedule this simple batchfile to run for example every week.
set BSE=d:\baan
d:
cd %BSE%\TMP
for /f %%a in (%BSE%\TMP\cleanlist) do @del %%a
del %BSE%\TMP\cleanlist
for /f %%b in ('dir /a-d /b %BSE%\TMP\tmp*') do @echo %%b >> %BSE%\TMP\cleanlist

It simply dumps all tmp* filenames in 'cleanlist'. Ans in the second run (1 week later) it deletes all files from the 'cleanlist'.

Timo

afripack
11th November 2008, 08:44
Hi Timo, thanks for the reply... our developer came up with this script which is really great, we run it as a job every night.

|******************************************************************************
|* txcom9998 0 VRC B40C c4 afpt
|* Clear the tmp directory
|* afpk
|* 01-05-08 [16:02]
|******************************************************************************
|* Script Type: 4
|******************************************************************************
| This program will clear the $BSE\tmp directory of old files and files too large in order to maintain
| the tmp directory size
| It will run based on a) age of file in days and b) size of files in Megabytes as input
| by the user
|
|****************************** DECLARATION SECTION ***************************

declaration:
extern domain tcdate date.inp
extern domain tcmcs.long days.del
extern domain tcmcs.long filsiz
extern domain tcmcs.long bytsiz
long fd, sfd
long rt
string home(80)
string fname(80)
string dfname(80)
string dname(80)
long file.size
long change.time
long modified.time
long access.time
long check.time

long ret
long size, mode, inode, dev, uid, gid, nlink, ctime, atime
long modtime



before.program:
date.inp = date.num()
display("date.inp")

form.1:
init.form:
get.screen.defaults()

|****************************** CHOICE SECTION ***************************
field.days.del:
check.input:
if days.del < 7 then
message("You cannot set to delete for less that 7 days old",1)
set.input.error("")
else
if days.del < 30 then
message("Warning - you are setting to delete before the recommended allowed days - 30",1)
endif
endif

field.filsiz:
check.input:
if filsiz < 1 then
message("Enter a size in Megabytes - recommended 50 or greater",1)
set.input.error("")
endif

choice.cont.process:
on.choice:

home = bse.tmp.dir$()
bytsiz = filsiz * 1000000
check.time = (date.num() - date.to.num(1970,1,1) - days.del) * (24 * 3600)
fd = dir.open(home)
repeat
fname = dir.entry(fd, TDIR+TFILE, rt, size, mode)
if not isspace(fname) then
if stat.info( home & "\" & fname,
size,
mode,
inode,
dev,
uid,
gid,
nlink,
ctime,
modtime,
atime ) = 0 then
if ( S_ISDIR( mode ) ) then
if modtime < check.time then
dname = home & "\" & fname
clear.directory()
rmdir(dname)
endif
else
if modtime < check.time then
file.rm(home & "\" & fname)
endif
if size > bytsiz then
file.rm(home & "\" & fname)
endif
endif
endif
endif
until (isspace(fname))
e = dir.close(fd)


functions:
function clear.directory()
{
sfd = dir.open(dname)
repeat
dfname = dir.entry(sfd, TDIR+TFILE, rt, size, mode)
if not isspace(dfname) then
if file.stat( dname & "/" & dfname,
file.size,
change.time,
modified.time,
access.time) = 0 then
if modified.time < check.time then
file.rm(dname & "/" & dfname)
endif
endif
endif
until (isspace(dfname))
e = dir.close(sfd)

}