Skip to content

Instantly share code, notes, and snippets.

@ktheory
ktheory / fib.rb
Created January 31, 2012 16:52
Ruby benchmarking
#!/usr/bin/env ruby
def slow_fib(n)
case n
when 0, 1
1
else
slow_fib(n-1) + slow_fib(n-2)
end
end
@ktheory
ktheory / check_aws_status.rb
Created January 13, 2012 05:11
Nagios plugin to check AWS Status RSS feeds
#!/usr/bin/env ruby
require 'rubygems'
require 'nokogiri'
require 'net/http'
# check_aws_status.rb
# A nagios plugin for fetching RSS feeds from http://status.aws.amazon.com.
# Source: https://gist.github.com/1604786
# Written by Aaron Suggs: https://github.com/ktheory
@ktheory
ktheory / gist:1596165
Created January 11, 2012 18:51
chef log
[Wed, 11 Jan 2012 17:02:10 +0000] INFO: Processing service[nagios-nrpe-server] action enable (nagios::client line 63)
[Wed, 11 Jan 2012 17:02:10 +0000] INFO: Processing nagios_nrpecheck[check_load] action add (nagios::client line 69)
[Wed, 11 Jan 2012 17:02:10 +0000] INFO: Adding check_load to /etc/nagios/nrpe.d/
[Wed, 11 Jan 2012 17:02:10 +0000] INFO: Processing template[/etc/nagios/nrpe.d/check_load.cfg] action create (/var/chef/cache/cookbooks/nagios/providers/nrpecheck.rb line 23)
...
[Wed, 11 Jan 2012 17:02:10 +0000] INFO: template[/etc/nagios/nrpe.d/check_load.cfg] sending restart action to service[nagios-nrpe-server] (delayed)
[Wed, 11 Jan 2012 17:02:10 +0000] ERROR: Running exception handlers
@ktheory
ktheory / gist:1440328
Created December 6, 2011 22:27
ActiveRecord::Base#to_yaml
def to_yaml( opts = {} )
YAML::quick_emit( self, opts ) do |out|
out.map( taguri, to_yaml_style ) do |map|
map.add( 'id', self.id )
end
end
ende
@ktheory
ktheory / .bashrc
Created September 11, 2011 21:46
Never forget `bundle exec` before running rake again...
# In your ~/.bashrc:
do_rake () {
if [ -e Gemfile ]; then
bundle exec rake $@
else
rake $@
fi
}
alias rake='do_rake'
@ktheory
ktheory / gist:1147904
Created August 15, 2011 21:24
I wish this worked...
@posts.each do |post|
post.body
else
"No posts"
end
@ktheory
ktheory / proxy.rb
Created July 25, 2011 00:52
em-proxy test with Rack
#!/usr/bin/env ruby
require 'em-proxy'
Proxy.start(:host => "0.0.0.0", :port => 3000) do |conn|
conn.server :fast, :host => '127.0.0.1', :port => 3001
conn.server :slow, :host => '127.0.0.1', :port => 3002
conn.on_data do |data|
data
end
@ktheory
ktheory / gist:966948
Created May 11, 2011 17:54
Nokogiri end_element demo
xml = %{
<doc>
<key1>value1</key1>
<key2>
Value with
line breaks
</key2>
</doc>
}
@ktheory
ktheory / README.rdoc
Created April 14, 2011 18:12
An example for logging Fog requests in a Rails app

Instrument Fog Requests in Rails

This code adds debug statements to the rails log for each fog reqests (similar to how active record shows SQL queries):

Fog AWS Compute Request (803.0ms)  [ {"Action"=>"DescribeInstances", ...} ]

The end of the request includes cumulative Fog info:

Completed 200 OK in 6043ms (Views: 2955.0ms | ActiveRecord: 319.9ms | Fog: 1642.6ms, 4 reqs)
@ktheory
ktheory / brew_completer.sh
Created January 11, 2011 15:27
Bash tab-completion for homebrew
# Bash tab-completion for homebrew (https://github.com/mxcl/homebrew)
###############
# Installation:
# mkdir ~/.bash_completion.d
# curl https://gist.github.com/raw/774554/brew_completer.sh > ~/.bash_completion.d/brew_completer.sh
# echo 'source ~/.bash_completion.d/brew_completer.sh' >> ~/.bashrc
# source ~/.bashrc