Skip to content

Instantly share code, notes, and snippets.

View SegFaultAX's full-sized avatar

Michael-Keith Bernard SegFaultAX

View GitHub Profile
@SegFaultAX
SegFaultAX / diskstats.rb
Last active May 11, 2017 16:04
Parse diskstats (/proc/diskstats) [Ruby]
EXAMPLE = <<-EOE
1 0 ram0 0 0 0 0 0 0 0 0 0 0 0
1 1 ram1 0 0 0 0 0 0 0 0 0 0 0
1 2 ram2 0 0 0 0 0 0 0 0 0 0 0
1 3 ram3 0 0 0 0 0 0 0 0 0 0 0
1 4 ram4 0 0 0 0 0 0 0 0 0 0 0
1 5 ram5 0 0 0 0 0 0 0 0 0 0 0
1 6 ram6 0 0 0 0 0 0 0 0 0 0 0
1 7 ram7 0 0 0 0 0 0 0 0 0 0 0
1 8 ram8 0 0 0 0 0 0 0 0 0 0 0
@SegFaultAX
SegFaultAX / points.py
Created April 18, 2017 16:09
Simulate flakey and flapping counters in Graphite to differentiate "max" and "last" aggregations [python]
#!/usr/bin/env python
import pprint
import csv
import random
import functools
import itertools
def coroutine(f):
@functools.wraps(f)
@SegFaultAX
SegFaultAX / transitive.py
Created March 10, 2017 22:12
Simple Dependency graph / Transitive closure [python]
def dependencies(partials):
shallow_dep_list = { m.name: m.inherits for m in partials }
def transitive1(otl):
deps = []
rest = list(shallow_dep_list[otl])
while rest:
e = rest.pop()
deps.append(e)
rest += [d for d in shallow_dep_list[e] if d not in deps]
return deps
@SegFaultAX
SegFaultAX / agg.py
Created February 15, 2017 04:35
Demonstration of timeseries data, windowing, and aggregation [python]
import datetime
import random
import itertools
import pprint
TIME_FORMAT = "%Y-%m-%dT%H:%M:%S"
NUM_USERS = 1e6
BROWSERS = ["Chrome", "Firefox", "IE"]
def logstream(start_time, end_time, max_step=10000):
@SegFaultAX
SegFaultAX / traverse.py
Created February 3, 2017 21:34
Traverse a Python dict and find all key paths
def iter_paths(d):
def iter1(d, path):
paths = []
for k, v in d.items():
if isinstance(v, dict):
paths += iter1(v, path + [k])
paths.append((path + [k], v))
return paths
return iter1(d, [])
@SegFaultAX
SegFaultAX / nginx_logs.py
Created January 23, 2017 23:24
Parse nginx logs to sqlite3
#!/usr/bin/env python
# The MIT License (MIT)
# Copyright (c) 2016 Michael-Keith Bernard
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
@SegFaultAX
SegFaultAX / coroutine.py
Created November 17, 2016 10:04
Python Simple Coroutine Decorator
import functools
def coroutine(f):
@functools.wraps(f)
def coro(*args, **kwargs):
c = f(*args, **kwargs)
next(c)
def cb(*args, **kwargs):
if not (args or kwargs):
next(c)
@SegFaultAX
SegFaultAX / .gitconfig
Last active October 3, 2016 23:25
gitconfig
[user]
name = <fullname>
email = <email>
signingkey = <key>
[alias]
st = status
co = checkout
up = !git pull --ff-only --all -p && git submodule update --init --recursive
fup = !git fetch --multiple upstream origin
ci = commit
@SegFaultAX
SegFaultAX / sums.py
Created June 30, 2016 00:15
AWS Billing Data parser
import csv
import argparse
import operator
# {'AvailabilityZone': '',
# 'BlendedCost': '0.00',
# 'BlendedRate': '0.00',
# 'InvoiceID': 'xxx',
# 'ItemDescription': '$0.05 per GB-month of Magnetic provisioned storage - US East (Northern Virginia)',
# 'LinkedAccountId': 'xxx',
@SegFaultAX
SegFaultAX / fabfile.py
Last active April 29, 2016 20:59
Simple restart manager
# Example fabric task
import elasticsearch
from restartmanager import RestartManager
@task
def do_the_restart():
# fab qa:es-master do_the_restart
r = RestartManager("/path/to/db")
r.schedule(*env.hosts)