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!)

rsrcmeter for TxD (See related posts)

Newer version at http://textsnippets.com/posts/show/842

TextDrive: Simple disk usage and HTTP bandwidth counts
This is an updated version of http://textsnippets.com/posts/show/621

#!/usr/local/bin/ruby
Dir.chdir(ENV['HOME'])
# Disk usage
quotaline = `quota -g | tail -n 1`
usage = `echo -n "#{quotaline}" | awk '{print $2}'`.to_f
quota = `echo -n "#{quotaline}" | awk '{print $3}'`.to_f
percent_used = (usage / quota) * 100
puts "Disk usage: " + sprintf("%.4f", usage/1024) + " MiB (Quota: " + sprintf("%.4f", quota/1024/1024) +" GiB; " + sprintf("%.1f", percent_used) + "% used)"

# HTTP Bandwidth
print "Calculating Bandwidth Usage..."
month = `date +"%B %Y"`.chomp
access_logs="access_log." + `date +%Y%m`.chomp + "??"
system("cat logs/access_log 2>/dev/null > temp-bandwidthcount") # Today's log
system("cat domains/*/logs/access_log 2>/dev/null >> temp-bandwidthcount")
system("cat logs/#{access_logs} 2>/dev/null >> temp-bandwidthcount") # Any logs not (yet) gzipped
system("cat domains/*/logs/#{access_logs} 2>/dev/null >> temp-bandwidthcount")
system("zcat logs/#{access_logs}.gz 2>/dev/null >> temp-bandwidthcount") # Gzipped logs from previous days
system("zcat domains/*/logs/#{access_logs}.gz 2>/dev/null >> temp-bandwidthcount")

usage = `cat temp-bandwidthcount | awk '{sum += $10} END {print sum}'`.chomp.to_f / 1024 / 1024
File.delete("temp-bandwidthcount")

30.times {print "\b"}
puts "Bandwidth used for #{month}: " + sprintf("%.4f", usage) + " MiB"


Installing:
Easy as 1..2..3:
curl -o rsrcmeter http://ajz.textdriven.com/rsrcmeter.txt
chmod u+x rsrcmeter
./rsrcmeter

Using in Webmin (Under Run Processes, Command to run):
export HOME="/users/home/YOURUSERNAME"; $HOME/rsrcmeter


MD5 hash: 0364b47675fee1902db1a32703cecf3f

Sample output:
Disk usage: 2.2910 MiB (Quota: 1.9073 GiB; 0.1% used)
Bandwidth used for August 2006: 0.5773 MiB


Confirmed to work on:
- Cardero,
- Davie (by rsimplicio),
- Pendrell (by iolaire),
- Thurlow (by janovetz),
- Bidwell & Jervis (by springworks),
- Chilco (by atl),
- Burnaby (by igner),
- Broughton (Biz Server, by Rich),
- Howe (scoobyfoo),
- Nicola (lderezinski),
- Jervis (mjboyle), and
- One (robertb)
*Should work on any TxD shared/business server.*

Comments on this post

ajz posts on Aug 19, 2006 at 18:23
An uglier version that now supports e-mailing the result. Just call ./rsrcmeter [email protected] and it will e-mail the results
instead of printing to the shell.
#!/usr/local/bin/ruby
Dir.chdir(ENV['HOME'])
email_result = false

if ARGV.include?('-h')
  puts <<EOM
TextDrive (Unofficial) Disk Usage and Bandwidth Reporter
To have results e-mailed, just provide an e-mail address
(i.e. rsrcmeter [email protected])
EOM
  Kernel.exit(0)
end
if ARGV[0] != nil and ARGV[0] =~ /^[-A-Za-z0-9!\#\$%&'*+\/\=?_`{|}~][-A-Za-z0-9!\#\$%&'*+\/\=?_`{|}~.]*[-A-Za-z0-9!\#\$%&'*+\/\=?_`{|}~]@[-A-Za-z0-9][-A-Za-z0-9.]*[-A-Za-z0-9]$/
  send_to = ARGV[0]
  email_result = true
else
  puts "--> WARNING: Invalid e-mail address specified. Cannot e-mail results."
end

# Disk usage
quotaline = `quota -g | tail -n 1`
usage = `echo -n "#{quotaline}" | awk '{print $2}'`.to_f
quota = `echo -n "#{quotaline}" | awk '{print $3}'`.to_f
percent_used = (usage / quota) * 100
du_result = "Disk usage: " + sprintf("%.4f", usage/1024) + " MiB (Quota: " + sprintf("%.4f", quota/1024/1024) +" GiB; " + sprintf("%.1f", percent_used) + "% used)"
du_result += "\nWARNING: Using over 90% of your disk quota!" if percent_used > 90.0
puts du_result unless email_result

# HTTP Bandwidth
print "Calculating Bandwidth Usage..." unless email_result
month = `date +"%B %Y"`.chomp
access_logs="access_log." + `date +%Y%m`.chomp + "??"
system("cat logs/access_log 2>/dev/null > temp-bandwidthcount")
system("cat domains/*/logs/access_log 2>/dev/null >> temp-bandwidthcount")
system("cat logs/#{access_logs} 2>/dev/null >> temp-bandwidthcount")
system("cat domains/*/logs/#{access_logs} 2>/dev/null >> temp-bandwidthcount")
system("zcat logs/#{access_logs}.gz 2>/dev/null >> temp-bandwidthcount")
system("zcat domains/*/logs/#{access_logs}.gz 2>/dev/null >> temp-bandwidthcount")


usage = `cat temp-bandwidthcount | awk '{sum += $10} END {print sum}'`.chomp.to_f / 1024 / 1024
File.delete("temp-bandwidthcount")

bw_result = "Bandwidth used for #{month}: " + sprintf("%.4f", usage) + " MiB"
unless email_result
  30.times {print "\b"}
  puts bw_result
end

if email_result
  message = <<EOM
From: TxD Resource Meter <#{ENV['USER']}-noreply@#{`/bin/hostname`.chomp}>
To: #{send_to}
Subject: [TxD Resource Meter] #{Time.now} report for #{ENV['USER']}
X-Mailer: rsrcmeter / http://textsnippets.com/posts/show/632

Resource Report - #{Time.now}
#{du_result} 
#{bw_result}
EOM
File.open("temp-emailresult", "w") { |file| file.print message }
`cat temp-emailresult | sendmail -t`
File.delete("temp-emailresult")
end
rsimplicio posts on Oct 24, 2006 at 18:25
I can confirm that this works on Davie.
scoobyfoo posts on Oct 26, 2006 at 00:17
Works on Howe.
lderezinski posts on Oct 26, 2006 at 11:08
works on Nicola
jacques posts on Nov 20, 2006 at 21:45
Can we get a version that stores monthly usage to some sort of file? So one can see in October one used say 400 MiB of transfer and 40% of quota?
ajz posts on Nov 21, 2006 at 19:57
jacques: working on it now ;) (I love it when you guys give me a reason to procrastinate)

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


Related Posts