Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

showbandwidth (for TxD) (See related posts)

This has been superseded by http://textsnippets.com/posts/show/632 (includes account disk usage)!

#!/usr/local/bin/bash
cd ~
export ACCESS_LOGS=access_log.`date +%Y%m`??
cat logs/access_log 2>/dev/null > temp-bandwidthcount
cat domains/*/logs/access_log 2>/dev/null >> temp-bandwidthcount
cat logs/$ACCESS_LOGS 2>/dev/null >> temp-bandwidthcount
cat domains/*/logs/$ACCESS_LOGS 2>/dev/null >> temp-bandwidthcount
zcat logs/$ACCESS_LOGS.gz 2>/dev/null >> temp-bandwidthcount
zcat domains/*/logs/$ACCESS_LOGS.gz 2>/dev/null >> temp-bandwidthcount
echo -n "Bandwidth used for `date +"%B %Y"`: "
cat temp-bandwidthcount | awk '{sum += $10} END {print sum/1024/1024,"MiB"}'
rm temp-bandwidthcount


1. Upload as showbandwidth
2. chmod u+x it
3. run it (~# ./showbandwidth) to get your current month's (HTTP) bandwidth usage.
Output Sample: Bandwidth used for August 2006: 0.46237 MB

Original credit to Filip: http://textsnippets.com/posts/show/346.

Old version (only use if you have changed where Apache saves its log files):
#!/usr/local/bin/bash
cd ~
export ACCESS_LOGS=access_log.`date +%Y%m`??
cat `find . -name 'access_log' 2>/dev/null` 2>/dev/null > temp-bandwidthcount # Today's log
cat `find . -name "$ACCESS_LOGS" 2>/dev/null` 2>/dev/null >> temp-bandwidthcount # For any not gzipped
zcat `find . -name "$ACCESS_LOGS.gz" 2>/dev/null` 2>/dev/null >> temp-bandwidthcount
echo -n "Bandwidth used for `date +"%B %Y"`: "
cat temp-bandwidthcount | awk '{sum += $10} END {print sum/1024/1024,"MB"}'
rm temp-bandwidthcount

Comments on this post

ajz posts on Aug 19, 2006 at 17:49
An updated version if you only use the default log file locations (~/logs and ~/domains/*/logs):
#!/usr/local/bin/bash
cd ~
export ACCESS_LOGS=access_log.`date +%Y%m`??
cat logs/access_log 2>/dev/null > temp-bandwidthcount
cat domains/*/logs/access_log 2>/dev/null >> temp-bandwidthcount
cat logs/$ACCESS_LOGS 2>/dev/null >> temp-bandwidthcount
cat domains/*/logs/$ACCESS_LOGS 2>/dev/null >> temp-bandwidthcount
zcat logs/$ACCESS_LOGS.gz 2>/dev/null >> temp-bandwidthcount
zcat domains/*/logs/$ACCESS_LOGS.gz 2>/dev/null >> temp-bandwidthcount
echo -n "Bandwidth used for `date +"%B %Y"`: "
cat temp-bandwidthcount | awk '{sum += $10} END {print sum/1024/1024,"MiB"}'
rm temp-bandwidthcount

Same directions as original above.

You need to create an account or log in to post comments to this site.


Related Posts