Skip to content

Instantly share code, notes, and snippets.

@manics
manics / check_utf8.py
Created February 4, 2013 15:17
Check whether a file contains valid UTF-8. Returns 0 for valid UTF-8, prints an error message to STDOUT and returns 1 for invalid.
#!/usr/bin/env python
# Check whether a file contains valid UTF-8
# From http://stackoverflow.com/a/3269323
import codecs
import sys
import os
def checkFile(filename):
try:
@manics
manics / lastfm_matrix.py
Last active December 19, 2015 12:59
Create a simple HTML visualisation of similarity scores between last.fm users. Replace API_KEY with your Last.fm api key, and USERS_* with a list of usernames.
# Create a simple HTML visualisation of similarity scores between last.fm users.
# Replace API_KEY with your Last.fm api key, and USERS_* with a list of usernames.
import sys
import time
import urllib2
import xml.dom.minidom
api_key = API_KEY
@manics
manics / process-tree-monitor.py
Created March 20, 2014 17:25
Prints out a tree of processes, repeats after one second.
#!/usr/bin/env python
# Prints out a tree of processes with cpu and memory use
# Usage: process-tree-monitor pid [-v]
import psutil
from datetime import datetime
import sys
import time
@manics
manics / cpu-usage.py
Last active August 29, 2015 14:05
ASCII/terminal CPU usage graph. Bars are split into coloured blocks for each CPU if termcolor module is available, otherwise a single bar is shown
#!/usr/bin/env python
import math
import os
import psutil
import sys
import time
try:
import termcolor
TERMCOLORS = termcolor.COLORS.keys()
DROP TABLE IF EXISTS test_edges;
DROP TABLE IF EXISTS test_nodes;
CREATE TABLE test_nodes(id SERIAL PRIMARY KEY);
CREATE TABLE test_edges(
a INTEGER REFERENCES test_nodes(id), b INTEGER REFERENCES test_nodes(id));
INSERT INTO test_nodes(id) VALUES (0);
-- Create a graph with 100,000 nodes
@manics
manics / README.md
Last active July 13, 2023 09:39
etcd self-signed client certificates
@manics
manics / README.md
Last active April 29, 2016 21:40
Update Etcd SkyDNS with OpenStack instances

Openstack Etcd SkyDNS

A very simple polling agent that updates an Etcd database with SkyDNS entries for all OpenStack instances in a tenancy.

Example

docker run -d --name etcd quay.io/coreos/etcd \
    --listen-client-urls http://0.0.0.0:2379 \

--advertise-client-urls http://0.0.0.0:2379 --debug

@manics
manics / get-pg-stat-statements.sh
Created August 2, 2017 07:58
Get PostgreSQL pg_stat_statements as a CSV. Requires pg_stat_statements extension on the server
#!/bin/bash
# Requires pg_stat_statements extension on the server
# https://www.postgresql.org/docs/9.6/static/pgstatstatements.html
if [ $# -ne 1 ]; then
echo Usage: $(basename "$0") dbname
exit 1
fi
sudo -u postgres psql "$1" -c '
@manics
manics / README.md
Last active June 21, 2021 14:40
Setup a single node Kubernetes cluster for development using kubeadm

kubeadm cloudinit

Setup a single node Kubernetes cluster for development using kubeadm: https://kubernetes.io/docs/setup/independent/install-kubeadm/

Create a CentOS 7 or Ubuntu Xenial (16.04) machine, and run kubeadm.sh as root.

You can pass this script as user-data to cloud-init so that it will be automatically run. For example, on openstack:

openstack server create NAME --flavor FLAVOR --key-name KEY --image 'Ubuntu Xenial' --network NET --security-group SECGROUP --user-data kubeadm.sh

@manics
manics / lsof-parse.py
Created January 25, 2018 16:08
Parse the output of `lsof -F`
#!/usr/bin/env python
# Convert the output of `lsof -F` into PID USER CMD OBJ
import sys
rs = []
pid = -1
cmd = ''
user = ''
for line in sys.stdin: