Baanboard.com

Go Back   Baanboard.com > Forum > Baan SIGs > Code & Utilities

User login

Frontpage Sponsor

Main

Poll
For ERP LN feature pack upgrade, what method of install are you using?
Installation Wizard into existing VRC
37%
Installation Wizard into new VRC
39%
Manual into existing VRC
3%
Manual into new VRC
21%
Total votes: 38

Baanboard at LinkedIn


Reference Content

Reply
 
Thread Tools Display Modes
  #1  
Old 17th July 2002, 17:43
Francesco's Avatar
Francesco Francesco is offline
Guru
 
Join Date: Aug 2001
Location: Antwerp, BE
Posts: 727
Francesco is on a distinguished road
Baan: 5b - DB: Oracle - OS: Solaris
CODE: Temp directory clean-up (hoover.pl)

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. ;)
Code:
#!/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";
__________________
Cheers,

Francesco
..............................................................

Admiral Business Solutions | My World | Baan Board | IT Happens!

"If everyone is thinking alike, then somebody isn't thinking" -- George Patton
"It's easy to cry 'bug' when the truth is that you've got a complex system and sometimes it takes a while to get all the components to co-exist peacefully." -- Doug Vargas
Reply With Quote
  #2  
Old 17th July 2002, 18:00
Francesco's Avatar
Francesco Francesco is offline
Guru
 
Join Date: Aug 2001
Location: Antwerp, BE
Posts: 727
Francesco is on a distinguished road
Baan: 5b - DB: Oracle - OS: Solaris
Wink

Thanks for the [code] tags...umm..Pat?

Looks a lot better. lol
__________________
Cheers,

Francesco
..............................................................

Admiral Business Solutions | My World | Baan Board | IT Happens!

"If everyone is thinking alike, then somebody isn't thinking" -- George Patton
"It's easy to cry 'bug' when the truth is that you've got a complex system and sometimes it takes a while to get all the components to co-exist peacefully." -- Doug Vargas
Reply With Quote
  #3  
Old 17th July 2002, 18:22
patvdv's Avatar
patvdv patvdv is offline
Board Master
 
Join Date: Aug 2001
Location: Belgium
Posts: 2,167
patvdv is on a distinguished road
Baan: n/a - DB: n/a - OS: AIX, HP-UX, Linux
Talking

Yep, it's me. I like the script name: 'hoover' !
__________________
Regards,

Patrick Van der Veken - Admin & Founder - (c) 2001-2017 baanboard.com/baanforums.com
Reply With Quote
  #4  
Old 27th May 2003, 19:15
learner's Avatar
learner learner is offline
Guru
 
Join Date: Dec 2002
Location: New zealand
Posts: 562
learner is on a distinguished road
Baan: BaaN Vc with sp 28 - DB: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit - OS: HP-UX B.11.31
running on win NT !!

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
Reply With Quote
  #5  
Old 27th May 2003, 20:18
p.cole's Avatar
p.cole p.cole is offline
Senior Member
 
Join Date: Nov 2001
Location: Portsmouth, UK
Posts: 265
p.cole is on a distinguished road
Baan: Baan IVc4 SP20, ERP LN FP3 - DB: MSSQL 2005 - OS: W2K3
Very minimal version we use on Windows 2000

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.
Attached Files
File Type: txt cleartmp.pl.txt (641 Bytes, 113 views)
Reply With Quote
  #6  
Old 21st August 2003, 19:15
EdHubbard's Avatar
EdHubbard EdHubbard is offline
Guru
 
Join Date: Mar 2002
Location: Malvern, England
Posts: 309
EdHubbard is on a distinguished road
Baan: 4c4 MCR - DB: MS SQL Server 2000 - OS: Windows 2003 Server
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
Reply With Quote
  #7  
Old 21st August 2003, 19:25
p.cole's Avatar
p.cole p.cole is offline
Senior Member
 
Join Date: Nov 2001
Location: Portsmouth, UK
Posts: 265
p.cole is on a distinguished road
Baan: Baan IVc4 SP20, ERP LN FP3 - DB: MSSQL 2005 - OS: W2K3
Only tmp* files

This is the perl code to check that the first part of the filename begins with tmp

HTH

Phil
Attached Files
File Type: txt cleartmp.pl.txt (750 Bytes, 54 views)
Reply With Quote
  #8  
Old 22nd August 2003, 10:21
croezen's Avatar
croezen croezen is offline
Senior Member
 
Join Date: Aug 2002
Location: Netherlands
Posts: 116
croezen is on a distinguished road
Baan: IVc4 - DB: SQL2000 - OS: Win2003
how about this ?

Code:
|******************************************************************************
|* 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)
__________________
Greetings,
Eddy Croezen, Eastborn Slaapsystemen BV
Reply With Quote
  #9  
Old 22nd August 2003, 10:51
NvanBeest's Avatar
NvanBeest NvanBeest is offline
Guru
 
Join Date: May 2003
Location: South Africa
Posts: 521
NvanBeest is on a distinguished road
Baan: BaanIVc4 - DB: Oracle, TBase, SQL Server - OS: AIX, Linux, Window$
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.
__________________
Regards,
Nico
Reply With Quote
  #10  
Old 22nd August 2003, 11:07
croezen's Avatar
croezen croezen is offline
Senior Member
 
Join Date: Aug 2002
Location: Netherlands
Posts: 116
croezen is on a distinguished road
Baan: IVc4 - DB: SQL2000 - OS: Win2003
edit postings

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 :- )
__________________
Greetings,
Eddy Croezen, Eastborn Slaapsystemen BV
Reply With Quote
  #11  
Old 22nd August 2003, 14:44
EdHubbard's Avatar
EdHubbard EdHubbard is offline
Guru
 
Join Date: Mar 2002
Location: Malvern, England
Posts: 309
EdHubbard is on a distinguished road
Baan: 4c4 MCR - DB: MS SQL Server 2000 - OS: Windows 2003 Server
Thanks

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...
Reply With Quote
  #12  
Old 22nd August 2003, 16:57
bamnsour's Avatar
bamnsour bamnsour is offline
Senior Member
 
Join Date: Nov 2001
Location: Nazareth
Posts: 156
bamnsour is on a distinguished road
Baan: 4b, 4c, 5b, 5c - DB: all - OS: Unix, NT
for those who do not want to install perl...

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
Reply With Quote
  #13  
Old 19th September 2003, 10:00
kceinfor kceinfor is offline
Newbie
 
Join Date: May 2003
Posts: 1
kceinfor is on a distinguished road
Baan: Baan IV - DB: Oracle - OS: Windows 2003 Server
my way ;-)

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.
Reply With Quote
  #14  
Old 19th September 2003, 18:43
Francesco's Avatar
Francesco Francesco is offline
Guru
 
Join Date: Aug 2001
Location: Antwerp, BE
Posts: 727
Francesco is on a distinguished road
Baan: 5b - DB: Oracle - OS: Solaris
Lightbulb Re: my way ;-)

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.
__________________
Cheers,

Francesco
..............................................................

Admiral Business Solutions | My World | Baan Board | IT Happens!

"If everyone is thinking alike, then somebody isn't thinking" -- George Patton
"It's easy to cry 'bug' when the truth is that you've got a complex system and sometimes it takes a while to get all the components to co-exist peacefully." -- Doug Vargas
Reply With Quote
  #15  
Old 19th September 2003, 18:46
Francesco's Avatar
Francesco Francesco is offline
Guru
 
Join Date: Aug 2001
Location: Antwerp, BE
Posts: 727
Francesco is on a distinguished road
Baan: 5b - DB: Oracle - OS: Solaris
Hoover 3.0a

Code:
#!/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");
__________________
Cheers,

Francesco
..............................................................

Admiral Business Solutions | My World | Baan Board | IT Happens!

"If everyone is thinking alike, then somebody isn't thinking" -- George Patton
"It's easy to cry 'bug' when the truth is that you've got a complex system and sometimes it takes a while to get all the components to co-exist peacefully." -- Doug Vargas
Reply With Quote
Sponsored Links
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to read files from a directory one by one vishbaan Tools Development 9 3rd December 2014 21:57
NEW: vbcode CODE extensions patvdv About Baanboard.com 3 13th March 2003 12:19


All times are GMT +2. The time now is 01:35.


©2001-2017 - Baanboard.com - Baanforums.com