Help with File Monitoring Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with File Monitoring Script
# 8  
Old 03-08-2012
One of the many methods of finding the top 10 largest files in a directory is as follows:
Code:
find "${dir}" -type f -exec ls -lad {} \; | sort -n -r +4 | head -10
# If you have knowledge of the size of files you could only look at big files
#  -size +1000000c

Imho the smallest unit of storage returned by "du" (one block) is too large for this exercise on files. However my method is totally useless on systems which return file sizes in "human readable" format.
This User Gave Thanks to methyl For This Post:
# 9  
Old 03-08-2012
You guys are really helpful, allow me to make some changes to the code, I will post once more, and confirm that I understand what I should have changed

---------- Post updated at 01:48 PM ---------- Previous update was at 01:44 PM ----------

Code:
#!/usr/bin/ksh
# PURPOSE: This script is going to view the top 10 largest home directory Folders, then go in each directory and list top 10 largest files.
#
#Author: Emmanuel Iroanya Jr
#Date: March 8,2012
#
##### DEFINE FILES AND VARIABLES HERE ####[/FONT]
WORKFILE="/tmp/homedf.work"  # Holds filesystem data
>$WORKFILE                   # Initialize to empty
OUTFILE="/tmp/homedf.outfile" # Output display file
>$OUTFILE                   # Initialize to empty
THISHOST=`hostname`      # Hostname of this machine
USER=`du -sk /home/* | sort -n -r | awk '{print $2}' | head -10`    # Define User
counter=0                  # Sets Counter to 0
######## START OF MAIN #############[/FONT]

cd /home
du -sk /home | sort -n -r | head -n 10 > $WORKFILE 
ls /home | sort -n -r | head -n 10
while read $USER
do 
    du -a $USER | sort -n -r | head -n 10 > $OUTFILE
    let counter=$counter+1
    echo $counter
done 
if [[ -s "$WORKFILE" -a -s "$OUTFILE" ]]
    then
    cat "$WORKFILE" "$OUTFILE"
    print
    mailx -s "Home Folder Size Files" [FONT=Courier New]exxxxxxxl_ixxxxxa@xxxxxx.com[ < "$WORKFILE" && "$OUTFILE"
fi

Now if i understand, you said the counter portion is useless,, so after i take that out, I should be good to go correct?

---------- Post updated at 02:14 PM ---------- Previous update was at 01:48 PM ----------

One Last error when I post the edited code I get this error as well

Code:
ksh: syntax error: `-a' unexpected

I am assuming its coming from the line:

Code:
du -a $USER | sort -n -r | head -n 10 > $OUTFILE


Last edited by methyl; 03-09-2012 at 07:30 PM.. Reason: Remove Microsoft font tags, add code tags. remove more funny fonts
# 10  
Old 03-09-2012
Note :

. If he then need to scan the list, the awk '{print $2}' can be made avoided by then using | while read a a (still in ksh).
Indeed : the second assignment of the variable a will overwrite the first one so you will get the second
field only.

. the -10 is not necessary since it head already takes 10 lines by default.

So just as a quick shot:

Code:
#!/usr/bin/ksh
TMPF=mymail.tmp
>$TMPF
du -sk /home/* | sort -n -r | head | while read a a
do
# make sure the output entry refer to a directory
echo "=== $a ===" >>$TMPF
[[ -d "$a" ]] && \
find $a -type f -ls | sort -k 7n | head >>$TMPF
done
mailx -s "Top 10 Big Folders & Files in it" whoever@whereever.whatever <${TMPF}

(assuming the size of file appear in the 7th column of the -ls of find command)
# 11  
Old 03-09-2012
After changes

What I am getting not in an e-mail:
Code:
xmladm
williamb
tranadm
trainadm
tonyd
tombe
toddm
tiat
testuser
testadm
/usr/bin/ksh: $USRDIR: is not an identifier

What I am expecting in an e-mail:

Code:
1484373 krishnaa
160446  joe
148463  kimh
112804  michaelr
89522   craign
79895   solomond
78694   brandonp
63859   charlesc
63769   miker
39251   michael 
krishnaa:
2968746 .
2956268 ./bin
262688  ./bin/EVServices
256352  ./bin/Bureau
154288  ./bin/CharacteristicCitizens
137712  ./bin/CharacteristicReg
134048  ./bin/Initiator
132480  ./bin/CalculationSTD
129568  ./bin/CharacteristicCmp
127024  ./bin/VerificationService
etc...

CODE:

Code:
#!/usr/bin/ksh
#PURPOSE: This script is going to view the top 10 largest home directory Folders, then go in each directory and list top 10 largest files.
#
#Author: Emmanuel Iroanya Jr
#Date: March 8,2012
#
##### DEFINE FILES AND VARIABLES HERE ####
WORKFILE="/tmp/homedf.work"             # Holds filesystem data
>$WORKFILE                              # Initialize to empty
OUTFILE="/tmp/homedf.outfile"           # Output display file
>$OUTFILE         # Initialize to empty
THISHOST=`hostname`      # Hostname of this machine
USER=`du -sk /home/* | sort -n -r | head -10 | awk '{print $2}'`      # Defines User
counter=0           # Sets Counter to 0
######## START OF MAIN #############
cd /home
du -sk | sort -n -r | head -n 10 > $WORKFILE
ls /home | sort -n -r | head -n 10
for $USRDIR in $USERS
do  
 du -a $USRDIR | sort -n -r | head -n 10 >> $OUTFILE
 let counter=$counter+1
 echo $counter
done
if [[ -s "$WORKFILE" && -s "$OUTFILE" ]]
 then
    cat "$WORKFILE" "$OUTFILE"
    print
 mailx -s "Home Folder Size Files" (email address) < "$WORKFILE" && "$OUTFILE"
fi

Moderator's Comments:
Mod Comment Please use next time code tags for your code and data

Last edited by methyl; 03-09-2012 at 07:22 PM.. Reason: removed Microsoft fonts so we can read the post. Next time copy/past via notepad.
# 12  
Old 03-12-2012
Check your variable names ($USERS vs $USER) ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script for continuously monitoring log file

Hi I have written below log monitoring script to egrep multiple words and redirect the output to a text file and its working fine but I want to add some more below given functionality to it, which is very advance and im not very good in it, so please help if you can :) I am egrepping all the... (1 Reply)
Discussion started by: scazed
1 Replies

2. UNIX for Beginners Questions & Answers

Monitoring script for Log file

Hi, Iam new to unix , plz help me to write below script. I need to write a script for Monitoring log file when any error occurs it has to send a mail to specified users and it should be always pick latest error not the existing one and the script should be able to send mail all errors (more... (1 Reply)
Discussion started by: vij05
1 Replies

3. Shell Programming and Scripting

Monitoring script for a log file

Hi, I need to get a script working to monitor a log file and throw an alert via mailx as soon as a particular error is encountered. I do not want repeatative email notifications of same error so simply cat logfile and grepping the error would not work. Here is what i planned but it seems... (2 Replies)
Discussion started by: roshan.171188
2 Replies

4. Shell Programming and Scripting

script to mail monitoring output if required or redirect output to log file

Below script perfectly works, giving below mail output. BUT, I want to make the script mail only if there are any D-Defined/T-Transition/B-Broken State WPARs and also to copy the output generated during monitoring to a temporary log file, which gets cleaned up every week. Need suggestions. ... (4 Replies)
Discussion started by: aix_admin_007
4 Replies

5. UNIX for Advanced & Expert Users

ldapsearch in monitoring script without bind password written in script

Hi I do a very simple monitoring of our OpenLDAP (runs in cronjob and generate alerts if unsuccessfull) $ ldapsearch -h hostname.domain -D "cn=monitor_user,ou=People,dc=organisation" -w "password" -b "dc=organisation" -x "(&(cn=monitor_user)(ou=People))" dn | grep -v version dn:... (4 Replies)
Discussion started by: slashdotweenie
4 Replies

6. Shell Programming and Scripting

help needed - log file monitoring script

hi Gurus, Need to pick your brains on this minor script project. I would like to continuously monitor a log file with sample log messages as below, and if PSOldGen percentage is either 99% or 100% for consecutively 10 times, alert someone. {Heap before gc invocations=46516: PSYoungGen ... (6 Replies)
Discussion started by: kenchen722
6 Replies

7. Shell Programming and Scripting

[Solved] File System Monitoring Script

Hello Scripts Guru I had created a shell script to monitor the threshold of the file system, but some where it is not giving the correct output. Request to all to hel me out I am getting the following output /dev/vg00/lvol3 mounted on 1865224 10% / is 2097152% /dev/vg00/lvol1 mounted on... (2 Replies)
Discussion started by: indrajit_renu
2 Replies

8. Shell Programming and Scripting

Shell Script for monitoring File system.

Hi, need help to write one shell script to monitor UNIX file systems and if any changes happend like deletion of any file, adding new file, time stamp changed, permisson changed etc. Script need to send alert mail to defined person/mail id. I request someone to help me to create the... (1 Reply)
Discussion started by: vjauhari
1 Replies

9. Shell Programming and Scripting

File monitoring script

Team, Attached 2 scripts for your validation(main script,mail script) Problem description: When its red it should wait for 10 seconds and then send a mail. If not red it should not send the mail i have done the below changes in the main script: if then if ... (4 Replies)
Discussion started by: whizkidash
4 Replies

10. Shell Programming and Scripting

file system monitoring script

Hi Does anyone know any website which contains examples of unix system monitoring scripts example? cheers (1 Reply)
Discussion started by: g-e-n-o
1 Replies
Login or Register to Ask a Question