Skip to content

Instantly share code, notes, and snippets.

@drsnyder
drsnyder / redis-size-test.py
Created April 16, 2012 20:47
Testing storage of millions of keys in Redis (and sets)
#! /usr/bin/env python26
# original from https://gist.github.com/1329319
import redis
import random
import sys
r = redis.Redis(host = 'localhost', port = 6380)
REDIS_SETGET = False
@drsnyder
drsnyder / get-site-ip-addresses.sh
Created June 13, 2012 20:43
Get site ipaddresses
#!/bin/bash
for host in `cat /etc/huddler/site-config.txt | grep -v ^# | awk '{ print $2 }' | sed "s/www\.//"`; do
echo -n "$host: ";
dig $host a +short | tail -1;
done
@drsnyder
drsnyder / user_albums.sql
Created June 18, 2012 16:46
All user albums
-- replacement
-- explain analyze @ 0.32ms
WITH a_user_albums AS (
SELECT *
FROM gallery_albums
WHERE
user_id = 1 AND
type != 'private' AND
category_id IN (SELECT id FROM user_gallery_album_category_visibility(0)) AND
(status & 1)::bool
@drsnyder
drsnyder / ocn5.scm
Created June 29, 2012 23:55
ocn programming problem 5
; [SPOILER=Iterative][CODE]#include <stdio.h>
(define (print-* n)
(if (= n 0)
(display "\n")
(and
(display "* ")
(print-* (- n 1)))))
(define (print-full-* n)
(and
@drsnyder
drsnyder / cache-size-estimate.pl
Created July 17, 2012 19:16
Simplistic estimate of varnish cache size
#!/usr/bin/perl
use strict;
my %object_map = {};
while (my $line = <>) {
if ($line =~ /.*?"GET (.*) HTTP\/\d\.\d" \d+ (\d+).*HIT/) {
my $url = $1;
my $size = $2;
$object_map{$url}{'size'} = $size;
@drsnyder
drsnyder / most-frequent.sh
Created July 20, 2012 21:28
Finding the most frequent source ip in an access log
cat access.log | awk '{ a[$2]++ } END { for (d in a) { printf("%d %s\n", a[d], d) } }' | sort -nr | less
@drsnyder
drsnyder / top-requesters.sh
Created July 21, 2012 00:44
Top IP access log requesters by frequency
#!/bin/bash
TMP=$(mktemp)
awk '{ a[$2]++ } END { for (d in a) { printf("%d %s\n", a[d], d) } }' | sort -nr > $TMP
head -20 $TMP
echo "see $TMP for the whole list"
@drsnyder
drsnyder / whois-requesting-us.sh
Created July 21, 2012 00:45
Top IP access log requesters by frequency including company owner
#!/bin/bash
TMP=$(mktemp)
awk '{ a[$2]++ } END { for (d in a) { printf("%d %s\n", a[d], d) } }' | sort -nr > $TMP
count=0
while read line; do
count=$(echo $line | awk '{ print $1 }')
ip=$(echo $line | awk '{ print $2 }')
company=$(jwhois $ip | grep -i OrgName | awk -F':' '{ print $2 }')
echo "$count $ip $company"
@drsnyder
drsnyder / non-bot-hitrate
Created July 27, 2012 17:47
Gathering hit rate statistics for bots and non
#!/usr/bin/perl
# vim: set ts=4
use strict;
my $hitsize = 0;
my $misssize = 0;
my $hitcount = 0;
my $misscount = 0;
my $botcount = 0;
@drsnyder
drsnyder / protocol-with-crlf-termination.clj
Created August 9, 2012 04:13
Protocol with CRLF termination.
(def C (string :utf-8 :delimiters " "))
(def K (string :utf-8 :delimiters " "))
(def V (string :utf-8 :delimiters " "))
(def CR (string :utf-8 :delimiters ["\r\n"]))
defcodec SET ["SET" CR])
(defcodec STORED ["STORED" CR])
;STORED\r\n
(defcodec GET ["GET" CR])