Solved

linux shell script

Posted on 2004-03-29
15
676 Views
Last Modified: 2010-04-20
linux shell script to check directory diskspace if over 100mb it will email me !

i tried using the below php script yet i still cannot get the information into a variable

<?php
$tmp=passthru('du -hs /var/www/html/*');
//if($tmp > 100mb then email me something !
echo $tmp;
?>
0
Comment
Question by:aot2002
  • 6
  • 5
  • 2
  • +1
15 Comments
 
LVL 7

Expert Comment

by:troopern
ID: 10711369
Uour PHP script doesn't work, because you need to cut out the trailing M/G in your "du -hs /var/www/html/" result.
And just exactly how do to that, I'm not perfectly sure right now. if you don't want to use "du -s" instead and use "if ($tmp > 100000) then mail;"
0
 
LVL 7

Expert Comment

by:troopern
ID: 10711375
Your* ... alittle typo there.
0
 
LVL 8

Expert Comment

by:da99rmd
ID: 10711998
here you have the script but in bash:

#!/bin/bash
USAGE=`du -s /var/www/html/ | awk '{ print $1 }'`
if [ $USAGE -ge 100000 ]; then
(echo "To: (yourmail adress here)"; echo "Subject: 100 Mb or more on html drive";  echo "Dont drink and drive." )  | sendmail -t
fi
#End

maybe you have to put the entire path to sendmail if you run it as a crontab
0
 
LVL 5

Expert Comment

by:willy134
ID: 10714416
except 100000 is not 100 MB it is 100000 Kilobytes and 1024 kilobytes is one megabyte therefore 102400 is the number you should be comparing against.

BTW php ---- do you want this to be a web script?  If not go for the bash version, there is no reason to kick on the php engine just to run this simple script.
0
 
LVL 1

Author Comment

by:aot2002
ID: 10714779
da99rmd and willy134
thanks i upped the amount of points for ya now i want one more thing !

say i got multiple directories under the /var/www/html
like
/var/www/html/mrgood
/var/www/html/howareya
/var/www/html/testw
/var/www/html/wert


now i want to evaluate each one without multiple scripts because i could remove and add or rename the names of the folders at any given time !
but basically the above script should email me if one of those folders goes above 100mb !
RAISED 100points more total 150!
0
 
LVL 1

Author Comment

by:aot2002
ID: 10714794
maybe some kinda for loop in there or something
0
 
LVL 5

Expert Comment

by:willy134
ID: 10716228
we can create a for loop that loops through each directory in /var/www/html/
#!/bin/bash
USAGE=`du -s /var/www/html/ | awk '{ print $1 }'`
if [ $USAGE -ge 100000 ]; then
(echo "To: (yourmail adress here)"; echo "Subject: 100 Mb or more on html drive";  echo "Dont drink and drive." )  | sendmail -t
fi
#End
for dirname in /var/www/html/*
do
      if [ -d $dirname ]; then
           USAGE=`du -s /var/www/html/ | awk '{ print $dirname }'`
         if [ $USAGE -ge 102400 ]; then
            (echo "To: (yourmail adress here)"; echo "Subject: 100 Mb or more on  $dirname";  echo "Dont drink and drive." )  | sendmail -t
         fi
       fi
done

--warning I did not test this because I don't have my webserver set up but I dont' know the dirname stuff works (I did test that) if da99rmd's stuff works this should work also

0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 5

Assisted Solution

by:willy134
willy134 earned 20 total points
ID: 10716235
sorry I duplicated stuff up top it should only be...

ated stuff up top it should only be...

#!/bin/bash
for dirname in /var/www/html/*
do
      if [ -d $dirname ]; then
           USAGE=`du -s /var/www/html/ | awk '{ print $dirname }'`
         if [ $USAGE -ge 102400 ]; then
            (echo "To: (yourmail adress here)"; echo "Subject: 100 Mb or more on  $dirname";  echo "Dont drink and drive." )  | sendmail -t
         fi
       fi
done
#end
0
 
LVL 5

Expert Comment

by:willy134
ID: 10716302
actually that is bad also I just noticed the du statement returns the wrong thing


change the USAGE line to this

 USAGE=`du -s /var/www/html/$dirname | awk '{ print $1 }'`

0
 
LVL 5

Expert Comment

by:willy134
ID: 10716386
change the USAGE line to this

 USAGE=`du -s $dirname | awk '{ print $1 }'`
0
 
LVL 1

Author Comment

by:aot2002
ID: 10720842
sorry can you reprint a working version real quick ?
and recommend a tutorial on shell scripting
0
 
LVL 1

Author Comment

by:aot2002
ID: 10721551
or better yet set me the way to make it appear in my logwatch daily ?
0
 
LVL 1

Author Comment

by:aot2002
ID: 10721552
or better yet set me the way to make it appear in my logwatch daily ?
0
 
LVL 8

Accepted Solution

by:
da99rmd earned 130 total points
ID: 10721599
This is the working version
#!/bin/bash
for dirname in /var/www/html/*
do
      if [ -d $dirname ]; then
           USAGE=`du -s $dirname | awk '{ print $1 }'`
         if [ $USAGE -ge 102400 ]; then
            (echo "To: (yourmail adress here)"; echo "Subject: 100 Mb or more on  $dirname";  echo "Dont drink and drive." )  | sendmail -t
         fi
       fi
done
#end

Here are som bash scripting howtos, dont just forget the man page man bash its usefull too.
http://www.tldp.org/LDP/abs/html/
and
http://www-106.ibm.com/developerworks/linux/library/l-bash.html
0
 
LVL 1

Author Comment

by:aot2002
ID: 10730580
great job ~!

its working as should !
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now