Skip to content

Instantly share code, notes, and snippets.

@insom
insom / lxc.c
Last active December 17, 2015 01:29
Just enough LXC to create a new process in a private PID tree. (Some bits ripped from libvirt's LXC implementation)
$ sudo ./a.out
PID: 7780
Child!
ERR: 0!
Success
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 1852 344 pts/3 S+ 23:16 0:00 ./a.out
root 2 0.0 0.0 1932 504 pts/3 S+ 23:16 0:00 sh -c ps auxwf
root 3 0.0 0.0 4336 1008 pts/3 R+ 23:16 0:00 \_ ps auxwf
@insom
insom / geoip-from-apache.py
Last active December 17, 2015 03:59
Get a breakdown of traffic from an Apache log file, by country. `pip install pygeoip` first.
import re
import sys
from pygeoip import GeoIP
g = GeoIP('GeoIP.dat')
r = re.compile(r'20. ([0-9]+) "')
d = {}
@insom
insom / lxc2.c
Created May 9, 2013 22:46
Enough LXC to create separate networking stack and to bring up a usable getty.
#include <sched.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/mount.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
/*
* Copyright (C) 2013 Aaron Brady
* Copyright (C) 2008-2012 Red Hat, Inc.
* Copyright (C) 2008 IBM Corp.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
@insom
insom / fpmunix.conf
Created July 19, 2013 10:26
Monitor the state of an FPM Unix FastCGI socket, to get the "queue length" from the kernel
LoadPlugin curl_json
<Plugin curl_json>
<URL "http://localhost:5000/">
Instance "fpmunix"
<Key "connecting">
Instance "connecting"
Type "listen_queue"
</Key>
<Key "connected">
Instance "connected"
@insom
insom / git-annex-ftp.py
Created September 24, 2013 12:33
Git Annex FTP backend, particularly for iWeb FTP (create a space called "Annex" and include your username and password in the source to use, for now).
#!/usr/bin/python
# git config annex.ftp-hook = "/usr/local/bin/git-annex-ftp.py"
# git annex initremote iwebftp type=hook hooktype=ftp encryption=none
from ftplib import FTP
import os
f = FTP('prefix.iweb-storage.com', 'prefix-admin', 'password')
f.set_pasv(True)
diff -ru magento-17/app/code/core/Mage/Core/Helper/Cookie.php magento-patched/app/code/core/Mage/Core/Helper/Cookie.php
--- magento-17/app/code/core/Mage/Core/Helper/Cookie.php 2012-07-05 11:49:29.000000000 +0100
+++ magento-patched/app/code/core/Mage/Core/Helper/Cookie.php 2013-09-26 10:52:24.701527319 +0100
@@ -69,7 +69,7 @@
{
$acceptedSaveCookiesWebsites = $this->_getAcceptedSaveCookiesWebsites();
$acceptedSaveCookiesWebsites[Mage::app()->getWebsite()->getId()] = 1;
- return serialize($acceptedSaveCookiesWebsites);
+ return json_encode($acceptedSaveCookiesWebsites);
}
@insom
insom / parse-cg.py
Last active December 24, 2015 11:48
Parse a PHP-format callgrind file and produce a summary of what time was spent in what files.
import re
import sys
states = ('DEFAULT', 'HAVE_FILE')
fl = re.compile('^fl=(.*)')
num = re.compile('^\d+ (\d+)')
state = 0
current_file = ''
inclusive = False
@insom
insom / trc.py
Created October 4, 2013 11:28
Manually control a Python Turtle with the keyboard.
#!/usr/bin/env python
# Adapted from David Llewellyn-Jones's code
# http://www.flypig.co.uk/?page=list&list_id=363&list=blog
# By Aaron Brady
# http://insom.me.uk/
import turtle as t
import sys
import tty
@insom
insom / insom.zsh-theme
Created October 16, 2013 11:33
My oh-my-zsh theme.
function box_name {
[ -f ~/.box-name ] && cat ~/.box-name || hostname -s
}
local current_dir='${PWD/#$HOME/~}'
local git_info='$(git_prompt_info)'
function virtualenv_info_2 {
[ $VIRTUAL_ENV ] && (
echo "%{$terminfo[bold]$fg[red]%}v:%{$fg[yellow]%}`basename $VIRTUAL_ENV`%{$reset_color%}"