Skip to content

Instantly share code, notes, and snippets.

@dansimau
dansimau / ldif-to-csv.sh
Created November 12, 2010 15:14
Shell script that reads LDIF data from STDIN and outputs as CSV.
#!/bin/bash
#
# Converts LDIF data to CSV.
# Doesn't handle comments very well. Use -LLL with ldapsearch to remove them.
#
# 2010-03-07
# dsimmons@squiz.co.uk
#
# Show usage if we don't have the right params
@dansimau
dansimau / bash-processor.sh
Created February 24, 2011 16:31
A bash script/template for adding multi-processing to something (ie. another script)
#!/bin/bash
#
# A bash script/template for adding multi-processing to "stuff".
#
# Designed to be used for syncing files though. Takes "strings of stuff" (eg.
# filenames) into a queue, flattens duplicates, then spawns a worker after a
# few seconds that calls the processing script with the "stuff" as params.
#
# dsimmons@squiz.co.uk
# 2011-02-24
@dansimau
dansimau / gist:842415
Created February 24, 2011 16:41
Bash function for running a command, checking the return code, and re-trying it x times after y sleep seconds.
#
# "<cmd>" <retry times> <retry wait>
#
do_retry()
{
cmd="$1"
retry_times=$2
retry_wait=$3
c=0
@dansimau
dansimau / bandwidth-usage.sh
Created August 3, 2011 12:31
Display realtime bandwidth usage for a given interface (Linux only)
#!/bin/bash
#
# Display the realtime bandwidth usage for the specified interface.
#
# dsimmons@squiz.co.uk
# 2011-08-03
#
# Number of seconds to average data over
avgsecs=3
@dansimau
dansimau / generate-class-code.sh
Created September 20, 2011 09:25
Test scripts for comparing the performance of declaring and instantiating classes dynamically in PHP using eval versus including from files.
#!/bin/bash
mkdir $1 || exit
c=0;
while [ $c -lt $1 ]; do
cat <<EOF >"$1/Foo_$c.php"
<?php
Class Foo_$c
{
function __construct() {
@dansimau
dansimau / benchmark.sh
Created October 13, 2011 12:52
Takes a list of URLs from a file, curls them and saves the load times to a CSV file
#!/bin/bash
FILE=$1
NUM=$2
[ "$3" == "-v" ] && VERBOSE=1
usage() {
echo "Usage: $0 <file of URLs> <number of requests to do per URL> [-v]" >&2
}
@dansimau
dansimau / daemon-launcher.sh
Created October 28, 2011 11:31
Launches program as a daemon and keeps it running. Adds log and pid file support to the program.
#!/bin/bash
#
# Daemoniser: starts a program as a daemon and relaunches it if it exits. Adds a pidfile and
# logfile capabilities.
#
# dsimmons@squiz.co.uk
# 2011-10-28
#
usage()
@dansimau
dansimau / ping-csv.sh
Created December 23, 2011 11:01
Ping a host and output each reply in CSV format
#!/bin/bash
#
# Do a ping and output results as CSV.
#
# dsimmons@squiz.co.uk
# 2011-12-23
#
if [ $# -lt 1 ]; then
echo "Usage: $0 [--add-timestamp] <ping host>"
@dansimau
dansimau / pgprepmgr.sh
Created January 9, 2012 10:57
Wrapper for pgpools' pcp tools to help you manage your pgpool setup and postgresql cluster. See usage in the comment below.
#!/bin/bash
#
# pgpool-II replication manager
#
# Interfaces with pgpool's pcp command-line tools to provide access to common functions for managing
# load-balancing and failover.
#
# dsimmons@squiz.co.uk
# 2011-08-28
@dansimau
dansimau / pglag.sh
Created January 9, 2012 11:58
Calculate the replication lag between postgresql master and one or more slaves in streaming replication mode.
#!/bin/bash
#
# Show replication lag for one or more postgresql slaves in streaming replication.
#
# dsimmons@squiz.co.uk
# 2012-01-09
#
psql="which psql"
psql_extra_opts=""