Skip to content

Instantly share code, notes, and snippets.

@rlipscombe
rlipscombe / gist:5852628
Created June 24, 2013 19:12
Discovering empegs over IP, in C#
// To discover empegs connected over Ethernet, we need to broadcast a '?', and listen for the response
var dgram = new byte[] { 0x3F }; // ASCII '?'
var client = new UdpClient(new IPEndPoint(IPAddress.Any, 8300));
client.EnableBroadcast = true;
client.Client.ReceiveTimeout = 500;
client.Send(dgram, dgram.Length, new IPEndPoint(IPAddress.Broadcast, 8300));
const int retryCount = 10;
for (int i = 0; i < retryCount; ++i)
{

Follow these steps to install graphite on OS X Mavericks.

Prerequisites

  • Homebrew
  • Python 2.7
  • Git

Install dependencies

Install Cairo and friends

@rlipscombe
rlipscombe / example_server.erl
Created November 13, 2014 13:53
Boilerplate gen_server
-module(example_server).
-export([start_link/0, start/0]).
-behaviour(gen_server).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
start() ->
gen_server:start({local, ?MODULE}, ?MODULE, [], []).
@rlipscombe
rlipscombe / partitionzipwith.erl
Created February 25, 2015 13:05
partitionzipwith.erl
-module(partitionzipwith).
-export([partitionzipwith/3]).
%% @doc A combination of lists:partition/2 and lists:zipwith/3.
%% Given two lists of equal length, call Combine(X, Y) for each.
%% If it returns {true, T}, then T is added to Satisfying.
%% If it returns {false, F}, then F is added to NotSatisfying.
-spec partitionzipwith(Combine, List1, List2) ->
{Satisfying, NotSatisfying}
when
@rlipscombe
rlipscombe / gist:c8f92f47fe1e0ecba757
Created August 17, 2015 16:30
ssl:setopts (keepalive)
%% from /usr/include/netinet/in.h (Linux and OS X)
-define(IPPROTO_TCP, 6).
%% from /usr/include/netinet/tcp.h (Linux)
-define(TCP_MAXSEG, 2).
-define(TCP_KEEPIDLE, 4).
-define(TCP_KEEPINTVL, 5).
-define(TCP_KEEPCNT, 6).
%% @doc If you pass multiple raw options to ssl:listen, it appears to drop all
@rlipscombe
rlipscombe / initial_calls.erl
Last active March 25, 2017 00:54
Finding leaked processes in Erlang
lists:sort(fun({_, X}, {_, Y}) -> X > Y end,
dict:to_list(lists:foldl(
fun(Pid, Dict) ->
InitialCall = case erlang:process_info(Pid, initial_call) of
{initial_call,{proc_lib,init_p,A}} ->
case erlang:process_info(Pid, dictionary) of
{dictionary, D} -> proplists:get_value('$initial_call', D, undefined);
_ -> {proc_lib,init_p,A}
end;
{initial_call,{erlang,apply,A}} ->
@rlipscombe
rlipscombe / mss.cpp
Last active August 28, 2021 14:36
Playing with TCP_MAXSEG
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <stdio.h>
#include <memory.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
@rlipscombe
rlipscombe / chef_list_nodes.erl
Created December 4, 2015 19:57
Listing chef nodes using Erlang
#!/usr/bin/env escript
%%! -pa deps/chef_authn/ebin deps/mochijson2/ebin
% Deps:
% https://github.com/chef/chef_authn
% https://github.com/electricimp/mochijson2
-record(opts, {
node_name,
client_key,
#!/usr/bin/env escript
%%% This script converts an arbitrary binary file to an Erlang module with a
%%% single exported 'bin/0' function that returns the original binary.
%%%
%%% See the end of the file for how I figured out the correct terms.
main(["+debug_info" | Files]) ->
io:format("Embedding binaries with debug_info...\n"),
lists:foreach(fun(X) -> embed_file_in_beam(X, [debug_info]) end, Files);