The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Bandwidth Usage Mail Script

Discussion in 'E-mail Discussions' started by roliboli, Oct 7, 2004.

  1. roliboli

    roliboli Active Member

    Joined:
    Sep 3, 2003
    Messages:
    37
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    Switzerland
    Is there a script which generate an overview of all domains and their traffic?

    The output should then be sent by email to the server administrator.

    My idea:
    For every domain in /var/cpanel/bandwidth/ cat the last lines (http, all, pop) and sort this to a list.

    Does anybody have already such a script/solution?

    thanks
     
  2. PWSowner

    PWSowner Well-Known Member

    Joined:
    Nov 10, 2001
    Messages:
    2,948
    Likes Received:
    3
    Trophy Points:
    38
    Location:
    ON, Canada
    I like that idea. I'll put it on my "to do" scripts list.
     
  3. asmithjr

    asmithjr Well-Known Member

    Joined:
    Jun 13, 2003
    Messages:
    475
    Likes Received:
    1
    Trophy Points:
    18
    would you start working this with something like

    grep "10.7.2004" /var/cpanel/bandwidth/*
     
  4. PWSowner

    PWSowner Well-Known Member

    Joined:
    Nov 10, 2001
    Messages:
    2,948
    Likes Received:
    3
    Trophy Points:
    38
    Location:
    ON, Canada
    Good idea

    I will make the script so it sends a list of each domains bandwidth usage for the current and previous day, showing the sources of bandwidth. The reason for current and previous day is because the current day wouldn't be for the full day unless the script was set to run right after the final bandwidth update for the day. It would show the current bandwidth for today and the final total for the previous day.

    It will list the domains and bandwidth used, followed by the total of all bandwidth.
    Like this:

    Code:
    Today:
    domain1.com   http: 300MB   pop3: 5MB   smtp: 8MB   Total: 313MB
    domain2.com   http: 612MB   pop3: 4MB   smtp: 9MB   Total: 625MB
    domain3.com   http:   5MB   pop3: 1MB   smtp: 1MB   Total:   7MB
    
    Yesterday:
    domain1.com   http: 2450MB   pop3: 21MB   smtp: 25MB   Total: 2496MB
    domain2.com   http:  124MB   pop3:  5MB   smtp:  2MB   Total:  131MB
    domain3.com   http: 1374MB   pop3: 18MB   smtp:  9MB   Total: 1401MB
    
     
  5. asmithjr

    asmithjr Well-Known Member

    Joined:
    Jun 13, 2003
    Messages:
    475
    Likes Received:
    1
    Trophy Points:
    18
    wow,
    Now that will be nice.
     
  6. PWSowner

    PWSowner Well-Known Member

    Joined:
    Nov 10, 2001
    Messages:
    2,948
    Likes Received:
    3
    Trophy Points:
    38
    Location:
    ON, Canada
    It would be pretty easy to do. The only trick to it is finding the spare time. I'll try to get it up by sometime next week.
     
  7. asmithjr

    asmithjr Well-Known Member

    Joined:
    Jun 13, 2003
    Messages:
    475
    Likes Received:
    1
    Trophy Points:
    18
    Here is something until you make it pretty.
    Code:
    #!/bin/sh
    # bandwith.sh - print a bandwidth report
    # run from cron at 11:59 PM
    
    DIR=/var/cpanel/bandwidth
    tmpfile=/tmp/bandwidth
    tmpdomains=/tmp/domains
    outfile=/tmp/bandreport
    tmpwork=/tmp/workingfile
    admin=email@domain.com          # change this to your email address
    cd /tmp
    
    # Set the date in the format the bandwidth reports use
    DATE=`date '+%m.%e.%Y'| sed -e 's: ::'`
    
    # get just the information lines we want to deal with into a tmp file
    grep "$DATE" $DIR/*.com > $tmpfile
    
    cd $DIR
    ls *.com > $tmpdomains
    cd /tmp
    
    # Create a header for our report
    echo "Bandwidth report for `uname -n` $DATE" > $outfile
    echo " " >> $outfile
    echo ""|awk '{ printf("%28s\t%10s\t%10s\t%10s\t%10s\n", "DOMAIN","ALL","HTTP","POP","SMTP") }' >> $outfile
    
    for i in `cat $tmpdomains`
    do
            grep $i $tmpfile > $tmpwork
            awk 'BEGIN {FS = "=" }
            /all=/ { ALL = $2 }
            /http=/ { HTTP = $2 }
            /pop3=/ { POP = $2 }
            /smtp=/ { SMTP = $2 }
            END {
                    printf("%28s\t%10d\t%10d\t%10d\t%10d\n", DOMAIN,ALL,HTTP,POP,SMTP)
            }' $tmpwork DOMAIN=$i >> $outfile
    done
    
    # clean up temp files
    rm $tmpfile $tmpdomains $tmpwork
    # email the report
    mail -s "band" $admin  < $outfile
    
    #
    # End
    #
    
    It can run from cron daily just before midnight.
     
    #7 asmithjr, Oct 8, 2004
    Last edited: Oct 8, 2004
  8. PWSowner

    PWSowner Well-Known Member

    Joined:
    Nov 10, 2001
    Messages:
    2,948
    Likes Received:
    3
    Trophy Points:
    38
    Location:
    ON, Canada
    Nice work. I just tested it. The output is easy enough for me to read.
     
  9. asmithjr

    asmithjr Well-Known Member

    Joined:
    Jun 13, 2003
    Messages:
    475
    Likes Received:
    1
    Trophy Points:
    18
    thanks, now for a disk usage report
     
  10. PWSowner

    PWSowner Well-Known Member

    Joined:
    Nov 10, 2001
    Messages:
    2,948
    Likes Received:
    3
    Trophy Points:
    38
    Location:
    ON, Canada
    So, when are you going to write that? ;)
     
  11. asmithjr

    asmithjr Well-Known Member

    Joined:
    Jun 13, 2003
    Messages:
    475
    Likes Received:
    1
    Trophy Points:
    18
    actually started checking a few things last night,

    need to determine what the real size is, looks to me like the du -hs /home/username will not get it all because of mysql .

    cpanel uses a function diskusage()

    Al
     
  12. asmithjr

    asmithjr Well-Known Member

    Joined:
    Jun 13, 2003
    Messages:
    475
    Likes Received:
    1
    Trophy Points:
    18
    Disk Usage Mail Script

    Here is something I worked out

    Code:
    #!/bin/sh
    # diskusage.sh - create a disk usage report
    # Al Smith - 10-09-2004
    #
    # Set the ALARM in the awk below. See if (ALARM < 5)
    # Default is set to 5 MB
    # This script also assumes you have /home mounted as a seperate FS
    
    tmpquota=/tmp/tmpquota           # tmp file
    report=/tmp/dureport                  # tmp file
    admin=email@yourdomain.com    # Admin Email - change this one first
    
    # update the mysql quota first
    /scripts/updatemysqlquota
    
    # create out temporaty file to work from - change the greps to remove unwanted users
    /usr/sbin/repquota -v /home | grep -v "root" | grep -v "nobody"| grep -v "^#"| grep "\-\-"
    | grep -v "\-\-\-" > $tmpquota
    
    # Lets build our report header
    echo "Disk Usage report for `uname -n` on `date '+%D'`" > $report
    echo ""| awk '{
            printf("\n%-10s %10s %10s  %10s\n", "User","Quota","Used","Available")
    }' >> $report
    
    # Now format our report
    awk '
    {
            DOMAIN = $1
            USED = ($3/1024)
            AVAIL = (($4-$3)/1024)
            QUOTA = ($4/1024)
            if (AVAIL < 5 ) ALARM = "*"
            else ALARM = ""
            printf("%-10s %10s %10.6s %10.5s %s\n",DOMAIN,QUOTA,USED,AVAIL,ALARM)
    }' $tmpquota >> $report
    
    # E-Mail our report
    mail -s "Disk Usage Report for `uname -n`" $admin < $report
    
    # clean up our file
    rm $tmpquota $report
    
    #
    # End
    #
    
    Check for line wraps in the repquota line if you cut and paste into SSH, this is a single line.

    This script also assumes you have /home mounted as a seperate FS
     
    #12 asmithjr, Oct 9, 2004
    Last edited: Oct 9, 2004
  13. webits

    webits Well-Known Member

    Joined:
    May 15, 2004
    Messages:
    114
    Likes Received:
    0
    Trophy Points:
    16
    I really dig this idea of script would be nice to see who is spam etc ;))
    How much bandwidth they chow from you a day etc...

    I'd like to see such a script to be in cpanel would be nice :)
     
  14. dezignguy

    dezignguy Well-Known Member

    Joined:
    Sep 26, 2004
    Messages:
    534
    Likes Received:
    0
    Trophy Points:
    16
    Nice bandwidth script... I have a few issues I'm working on to make it better for me... first is, it only works for .com domains... I'm trying to find out how to tell ls to look for .net and .org as well, but haven't gotten that far yet.

    Also, for very long domains, the columns are shifted over... this should be easy to fix, once I find out how to add a few more tabs or spaces between columns... haven't yet looked up the codes for that yet.
     
  15. webits

    webits Well-Known Member

    Joined:
    May 15, 2004
    Messages:
    114
    Likes Received:
    0
    Trophy Points:
    16
    Where exactly do i install this .sh script where must it be exactly if you don't mind helping.
     
  16. PWSowner

    PWSowner Well-Known Member

    Joined:
    Nov 10, 2001
    Messages:
    2,948
    Likes Received:
    3
    Trophy Points:
    38
    Location:
    ON, Canada

    Change the line that says:
    ls *.com > $tmpdomains
    to:
    ls *.com *.net *.org > $tmpdomains



    Where it says:
    printf("%28s\t%10s\t%10s\t%10s\t%10s\n
    just change the 28 to 40 or whatever.
     
  17. PWSowner

    PWSowner Well-Known Member

    Joined:
    Nov 10, 2001
    Messages:
    2,948
    Likes Received:
    3
    Trophy Points:
    38
    Location:
    ON, Canada
    Anywhere you want. In the /root folder, the /scripts folder, or your home folder.
     
  18. asmithjr

    asmithjr Well-Known Member

    Joined:
    Jun 13, 2003
    Messages:
    475
    Likes Received:
    1
    Trophy Points:
    18
    ok as for .net and .org, good catch
    see:
    Code:
    cd $DIR
    ls *.com > $tmpdomains
    cd /tmp
    
    use:
    Code:
    cd $DIR
    ls *.com > $tmpdomains
    ls*.net >> $tmpdomains
    ls *.org >> $tmpdomains
    cd /tmp
    
    I am formatting the report columns using the printf statment here:
    Code:
    # Lets build our report header
    echo "Disk Usage report for `uname -n` on `date '+%D'`" > $report
    echo ""| awk '{
            printf("\n%-10s %10s %10s  %10s\n", "User","Quota","Used","Available")
    }' >> $report
    
    and here
    Code:
    printf("%-10s %10s %10.6s %10.5s %s\n",DOMAIN,QUOTA,USED,AVAIL,ALARM)
    
    The 10 is for 10 spaces just make the number larger to spread things out, but beware it will be more than 80 columns.

    This code can be placed anywhere because we are writing our temporary files in /tmp then cleaning them up once complete. The script is run as root.
     
  19. dezignguy

    dezignguy Well-Known Member

    Joined:
    Sep 26, 2004
    Messages:
    534
    Likes Received:
    0
    Trophy Points:
    16
    Thank you very much, squirrel and asmithjr...

    Weeeird... I could have sworn I did that and it didn't work... I knew that pipe separated (like a regex) wouldn't work and so was going to read the docs and find out how I could fit it all into one line.

    Hmm, so it just appends any further filenames into the tmp file? Is this method to be preferred over squirrel's 'ls *.com *.net *.org' which also seems to work... at least on the command line?

    Yes, I think can handle it being larger than 80 columns... I just have some domains + subdomains that are longer than 30-32 characters, and so they mess up the columns.

    I need to get more familiar with shell scripting... it seems pretty handy. ;-D
     
  20. PWSowner

    PWSowner Well-Known Member

    Joined:
    Nov 10, 2001
    Messages:
    2,948
    Likes Received:
    3
    Trophy Points:
    38
    Location:
    ON, Canada
    His method would list all .com's, then .net's, then .org's.
     
Loading...

Share This Page