« Newer Snippets
Older Snippets »
8 total  XML / RSS feed 

File buffer chunked processing

For future reference from: http://forum.textdrive.com/viewtopic.php?pid=105585#p105585
(Johan via Bernard)

Reading file buffer in chunks to prevent reaching process memory limits.

File.open("", "wb") do |f|
  while buff = file_field.read(4096)
    f.write(buff)
  end
end

rsrcmeter for TxD

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


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.*

showbandwidth (for TxD)

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

Daily MySQL backups on Textdrive, rotated weekly

This cron job will create a compressed backup of all the mysql databases under your account. The backup will be stored as "\daily-backup\Mon.gz" - and so forth, one for each day of the week. In this way you will have rotating backups going back seven days.

First, create the "daily-backup" folder under your home directory.

Go into the System - Cron Jobs section in webmin and paste this in as a new cron job (all one line)

/usr/local/bin/mysqldump --skip-opt -uUSERNAME -pPASSWORD --quote-names --complete-insert --extended-insert --quick --compact --lock-tables=false --skip-add-locks --all-databases | gzip > /home/USERNAME/daily-backup/sql-alldb-`date "+%a"`.gz 


Make sure to replace the USERNAME and PASSWORD with your own info.

You can set it up to run on any kind of daily schedule; I have it set to run daily at an early-morning time that I picked randomly.

cannot "apply changes" to restart Apache post-setup of proxy

Failed to apply changes :
[Fri Nov 18 05:06:50 2005] [notice] suEXEC mechanism enabled (wrapper: /usr/local/sbin/suexec)
[Fri Nov 18 05:06:52 2005] [notice] FastCGI: wrapper mechanism enabled (wrapper: /usr/local/sbin/suexec)
[Fri Nov 18 05:06:52 2005] [notice] FastCGI: process manager initialized (pid 77504)
[Fri Nov 18 05:06:52 2005] [notice] Digest: generating secret for digest authentication ...
[Fri Nov 18 05:06:52 2005] [notice] Digest: done

Killing rogue dispatch.fcgi processes and starting lighttpd on TextDrive

By using this script to start lighttpd you can be sure you won't be leaving rogues dispatchers around sucking up memory and generally pissing jason off. Don't get caught with your pants down!

Make sure to change USERNAME below, as well as any paths that may be different in your case.

#!/usr/local/bin/ruby
#Modified from Julik's original code posted to TextDrive forums.

pstab = `ps axww`

def kill_fcgi_proc(line)
  its = line.strip.split(/\s+/)
  pid = its[0]
  puts "KILLING #{line}"
  `kill -9 #{pid}`
  sleep(3)
end

if pstab =~ /\/usr\/local\/sbin\/lighttpd -f/
  
  puts "Lighttpd still running."

else

  pstab.scan(/^.*dispatch\.fcgi\s*$/) do |line|
    kill_fcgi_proc line
  end

  pstab = `ps axww`
  
  if pstab =~ /dispatch\.fcgi/
    puts "Error, rogue dispatch.fcgi's still pissing jason off."
  else
    puts "Rogue dispatch.fcgi's cleared, starting lighty!"
    `/usr/local/sbin/lighttpd -f /home/USERNAME/lighttpd/lighttpd.conf`
  end
  
end

Pinging/resolving to the Textdrive IP's

    _______________________________________
________| |_________
\ | davie.textdrive.com | /
\ | | /
\ | "kinda like a little brother" | /
/ |_______________________________________| \
/___________) (___________\



rsimplicio@davie$ ping simplicio.com
PING simplicio.com (207.7.108.101): 56 data bytes
64 bytes from 207.7.108.101: icmp_seq=0 ttl=64 time=0.033 ms
64 bytes from 207.7.108.101: icmp_seq=1 ttl=64 time=0.021 ms
64 bytes from 207.7.108.101: icmp_seq=2 ttl=64 time=0.029 ms
64 bytes from 207.7.108.101: icmp_seq=3 ttl=64 time=0.029 ms

Typo current error

NoMethodError in Admin/content#new 
undefined method `text_filter=' for #
app/controllers/admin/content_controller.rb:24:in `new'
Show framework trace 
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/base.rb:1200:in `method_missing'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/base.rb:756:in `send'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/base.rb:756:in `perform_action_without_filters'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/filters.rb:295:in `perform_action_without_benchmark'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/benchmarking.rb:41:in `perform_action_without_rescue'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/benchmarking.rb:41:in `measure'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/benchmarking.rb:41:in `perform_action_without_rescue'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/rescue.rb:80:in `perform_action'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/base.rb:356:in `send'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/base.rb:356:in `process'
/usr/local/lib/ruby/gems/1.8/gems/rails-0.13.1/lib/dispatcher.rb:32:in `dispatch'
/home/rsimplicio/web/public/typo/dispatch.fcgi:20
/home/rsimplicio/web/public/typo/dispatch.fcgi:18:in `each_cgi'
/usr/local/lib/ruby/gems/1.8/gems/fcgi-0.8.6.1/./fcgi.rb:597:in `each'
/usr/local/lib/ruby/gems/1.8/gems/fcgi-0.8.6.1/./fcgi.rb:597:in `each_cgi'
/home/rsimplicio/web/public/typo/dispatch.fcgi:18
    
Request
Parameters: None

Show session dump

--- 
:user: !ruby/object:User 
  attributes: 
    name: Robert
    id: "1"
    password: <snipped for security reasons>
    login: Robert
    email: 
:return_to: 
flash: !ruby/hash:ActionController::Flash::FlashHash {}
Response
Headers: {"cookie"=>[], "Cache-Control"=>"no-cache"}
« Newer Snippets
Older Snippets »
8 total  XML / RSS feed