Help needed to create a UNIX Space monitoring script

Hi All,

Its urgent.. pls help me out.. I want to create a KSH which should generate a report with the list of users and the files larger than 5 GB created by them in a direcorty and send autogenerated e-mail to them.
my input would be users list,directory path and the file size (say 5 GB)

Thanks in advance.

What have you tried so far?

Hello CarloM,

I have jus written the below script which will generate the top 10 large directories in the given path with size and sends an e-mail.

Likewise, I would like to have a script which takes users,path and file size as input and sends auto mail to those users in the output who has utilized more space in the given path/directory

 
#!/bin/ksh 
# Test if shell script is being invoked from directory where script resides or from a different directory
if [[ $(dirname ${0}) = . ]] then
  CURRDIR=$PWD
  PROGNAME=${0}
  PROGDIR=${CURRDIR}
else
  CURRDIR=${0}
  PROGNAME=$(basename ${CURRDIR})
  PROGDIR=$(dirname ${CURRDIR})
fi
CURRENT_DT=$(date +"%Y%m%d_%H%M%S")
CURRENT_DT_MAIL=$(date +"%m-%d-%Y")

CORE=""
 while [ "$CORE" == "" ]
 do
 printf "\n Please enter the path: "
 read CORE
  # echo "\n Mail sent.."
 if [[ ! -d $CORE || "$CORE" == "" ]]
 then
  CORE=""
  echo "\n Invalid path or Path does not exist! Please enter a valid path." 
 fi
 done
### create file to save the output ###
OFILE=${CURRDIR}/unix_message_test4.txt

### command to find the top 10 directories####
echo "\n Reading directory..."
echo ${CORE}
echo ${CURRDIR}
USRSMSG=`du -k ${CORE}|sort -rn|head -10 > ${OFILE}`
exec 1>> ${USRSMSG}
exec 2>&1
# mailx -s "****Urgent**** Action Required | Unix Space Utilization | ${CURRENT_DT_MAIL}" "test@email.com" < ${OFILE}
echo "Mail sent successfully.."
 

---------- Post updated at 08:20 AM ---------- Previous update was at 08:14 AM ----------

I have also tried this - to get the list of users in the unix box, and redirected theo/p to a file.

USERS=`ls -ld /home/*/ |cut -c 17-24 > ${CURRDIR}/users_list.txt`

Now am getting the below error when i tried to pass this users list in the for loop.
"m_input_5.ksh[72]: 0403-057 Syntax error at line 72 : `for' is not matched."

for users in `cat ${USERS}`
do
MAILTO="${users}@email.com"
CONTENT="${OFILE}"
SUBJECT="****Urgent**** Action Required | Unix Space Utilization | ${CURRENT_DT_MAIL}"
(
echo "To: ${MAILTO}"
echo "From: informatics@email.com"
echo "Subject: ${SUBJECT}"
echo "MIME-Version: 1.0"
echo "Content-Type: text/html"
echo "Content-Disposition: inline"
cat ${CONTENT}
) | /usr/sbin/sendmail -t

Ever thought of using quotas? It does simplify your life once correctly set...

Are you sure? What OS?

By the way the "done" instruction keyword looks missing from the code block

for users in ...
do
....
 done
1 Like

Might I suggest you look at the output from these:-

IFS=":"
cut -f1,5 -d ":" /etc/passwd | while read userid comment       # Loop for all userids & comments

du                                                             # Display usage

cd ~abc123                                                     # Change to the home directory of user abc123

find . -type f -exec ls -l {} \; | sort -nk 5                  # List out files from current directory and below sorted in ascending size

find / -type f -user userid -exec ls -l {} \; | sort -nk 5     # List out all files owner by nominated user in ascending size

I would agree that quota-setting could sort things out better, but you have to consider the risk of write failures when a quota is exceeded. Is that a problem?

You need to be clear on what you are searching for and why. Can you explain a little more?

I hope that these suggestions help.

Robin
Liverpool/Blackburn
UK

Thanks all :slight_smile: Finally,done with my script at the first level which is getting the directories as input,generates the result and sends it to the list of users via email. !!:slight_smile:

It would be polite to share your code so that others finding this thread have a suggested solution.

Thanks, in advance,
Robin

Below is the script

#!/bin/ksh 
#############################################################################
# 7_script.ksh
# This script will generate unix space utilisation report
#############################################################################
### Date ########
CURRENT_DT=$(date +"%Y%m%d_%H%M%S")
CURRENT_DT_MAIL=$(date +"%m-%d-%Y")
TS=$(date +"%H%M%S")
# Test if shell script is being invoked from directory where script resides or from a different directory
if [[ $(dirname ${0}) = . ]] then
CURRDIR=$PWD
PROGNAME=${0}
PROGDIR=${CURRDIR}
else
CURRDIR=${0}
PROGNAME=$(basename ${CURRDIR})
PROGDIR=$(dirname ${CURRDIR})
fi
### Create file to save the output ###
OFILE=${CURRDIR}/unix_message_test${TS}.txt
########### Getting path as input file and running a loop for getting the files greater than 1GB in the input paths########
IPATH=${CURRDIR}/unix_path.txt
for paths in `cat ${IPATH}`
do
USRSMSG=`find ${paths} -type f -size +1073741824c -ls >> ${OFILE}` #### files greater than 1GB
done
exec 1>> ${USRSMSG}
exec 2>&1

######## Sending the o/p as e-mail ##########

# USERS=`ls -ld /home/*/ |cut -c 17-24 > ${CURRDIR}/users_list.txt` ## to get the list of users from df-k
USERS=${CURRDIR}/users_list.txt ### input file which has the mail recipients###
for user in `cat ${USERS}`
do
MAILTO=${user}@abc.com
CONTENT=${OFILE}
SUBJECT="****Urgent**** Action Required | Unix Space Utilization | ${CURRENT_DT_MAIL}"
(
echo "To: ${MAILTO}"
echo "From: test@abc.com"
echo "Subject: ${SUBJECT}"
echo "MIME-Version: 1.0"
echo "Content-Type: text"
echo "Content-Disposition: inline"
cat ${CONTENT}
) | /usr/sbin/sendmail -t
done
# mailx -s "****Urgent**** Action Required | Unix Space Utilization | ${CURRENT_DT_MAIL}" test@abc.com < ${OFILE}
echo "Mail sent successfully.."
1 Like

Hi All,
Need your help again for the below issue

I have created a script which will give the files greater than 1GB with the userids,filesize,timestamp and the filename.

a 2312321 TS filename.txt
b 2423423 TS filename.txt
b 7676575 TS filename.txt
b 7567544 TS filename.txt
c 2435346 TS filename.txt
d 9786544 TS filename.txt
 

Now my objective is extract the userids and send mail with the files under their under name. i have used the cut cmd to get the userid's

cut -f 1 -d ' '

say am getting the o/p as below

a
b
b
b
c
d

Now i want to send e-mail to these users. when i run a for loop, multiple emails are sent to the same user.
(Eg) o/p expected
an e-mail with 3 lines for user 'b' to be sent only one time.

Please help me in sorting this out.

You should really start a new thread for this as it is a separate query, but consider your logic:-

  • You want one email per id (assuming first field)
  • Do you want any detail from the report?

So:-

  • Capture the output to a file.
  • Get the unique values from the first column
  • For each user:-
    [list]
  • Get all the detail from the file for that user
  • Send the mail.
    [/list]

Have a look at the sort command to get unique userids, and then think about how to get the detail for just the user required, searching from beginning of line with the user id and then a space.

Does this help?

Robin

Thanks for the sugesstion Robin :slight_smile:

I had used the

grep

command to solve this and it is working fine now.

I had got the unique users from the o/p using

uniq

command and grep'd the o/p file using this uniq users list

Below is the script

for user in `cat ${USERS}`
do
MAILTO="${user}@abc.com"
#echo `cat ${OFILE}| grep ${user}`
CONTENT=`cat ${OFILE}| grep ${user}`
SUBJECT="****Urgent**** Action Required | Unix Space Utilization | ${CURRENT_DT_MAIL}"
(echo "To: ${MAILTO}"
echo "From: `whoami`@abc.com" 
echo "Subject: ${SUBJECT}"
echo "MIME-Version: 1.0"
echo "Content-Type: text"
echo "Content-Disposition: inline"
echo ${CONTENT}
) | /usr/sbin/sendmail -t
done
1 Like