Skip to content

Instantly share code, notes, and snippets.

View cobusc's full-sized avatar

Cobus Carstens cobusc

View GitHub Profile
@cobusc
cobusc / beam_swap_usage.sh
Created April 16, 2013 13:29
Quick check if Erlang VMs have swap memory
#!/bin/bash
# Check swap usage of Erlang VMs
for p in $(ps -e | grep beam | cut -d" " -f 1); do
SWAP=$(grep "Swap:" /proc/${p}/smaps | awk '{swap = swap + $2} END {print swap}')
CMDPREFIX=$(head -c 100 /proc/${p}/cmdline)
printf "PID ${p} SWAP %dKB CMDPREFIX '%s...'\n" ${SWAP} ${CMDPREFIX}
done
@cobusc
cobusc / map.erl
Last active December 18, 2015 22:49
Linear to non-linear sequence mapper and base62 encoder.
-module(map).
-export([map/1,
to_base_62/1,
test/1]).
%%
%% @doc Deterministically map a linear integer sequence to a non-linear sequence.
%%
-spec map(N::non_neg_integer()) -> string().
@cobusc
cobusc / postgresql_date_function_index_howto.md
Last active July 26, 2024 09:16
Short explanation of the issues faced when trying to create a PostgreSQL index using the date() function and how to resolve it.
@cobusc
cobusc / hstore_erlang_json-postgresql9.1.md
Last active December 19, 2015 16:29
HSTORE to Erlang to JSON for PostgreSQL 9.1

PostgreSQL 9.1 does not support the JSON datatype.

I want to use the HSTORE column type for metadata which eventually will be returned as part of JSON encoded structure. Unfortunately PostgreSQL 9.1 does not have the funky JSON goodies (which are available from 9.2).

Retrieving data

{ok, Cols, Rows} = dbutils:equery("select msisdn, operator, subscribe_datetime, uuid, 
@cobusc
cobusc / erlang_reload_sys.config.md
Created July 25, 2013 06:36
Example of how to reload the application sys.config file without restarting the node.

Taken from http://comments.gmane.org/gmane.comp.lang.erlang.general/15718 with minor fix:

reload_config(AppNames, ConfigFile) ->
     {ok, [Config]} = file:consult(ConfigFile),
     Apps = [{application, A, element(2,application:get_all_key(A))}
             || {A,_,_} <- application:which_applications()],
     application_controller:change_application_data(Apps,Config).
@cobusc
cobusc / s3_log_analysis.sh
Created August 5, 2013 06:54
S3 log analysis using webalizer
#!/bin/bash
S3_LOG_LOCATION="s3://thelogbucket/logdir"
LOCAL_LOG_LOCATION="/tmp/log/"
REPORT_TITLE="My report title"
HTML_OUTPUT_DIR="/tmp/reporthtml"
REPORT="/tmp/report.ps"
# Sync log files to a local directory
s3cmd -v sync ${S3_LOG_LOCATION} ${LOCAL_LOG_LOCATION}
@cobusc
cobusc / HMAC_cmd_line.md
Last active December 15, 2020 14:15
HMAC signature calculation: command line example

Example of how to calculate an HMAC signature using command line utilities:

echo -n "/path/subscribe?account_id=my_account&club_id=club%20id&msisdn=27820000000&timestamp=2012-05-29T15%3A00%3A12Z" | openssl dgst -sha256 -hmac "secret_value" -binary | base64    
@cobusc
cobusc / gfm2html.php
Last active December 3, 2020 19:30
Example script that will convert Github Flavoured Markdown text into HTML (using Github's API).
#!/usr/bin/env php
<?php
$RENDER_URL = "https://api.github.com/markdown";
$INPUT = $argv[1];
$OUTPUT = $argv[2]
$payload["text"] = file_get_contents($INPUT);
@cobusc
cobusc / carbon.php
Created June 17, 2014 06:19
Publishing to graphite/carbon via HTTP
<?php
#
# This script provides an HTTP interface to carbon/graphite.
# The intention is that this script will
# [1] allow for easy integration with core apps,
# [2] can provide access control via Apache's authentication mechanisms.
#
function publish_data($path, $value, $timestamp)
@cobusc
cobusc / jsonvi.sh
Created November 12, 2014 07:11
Utility to validate, pretty-print and syntax highlight JSON data
#!/bin/bash
#
# Utility to display JSON data in a pretty-printed, syntax highlighted form.
# If the data is not valid JSON, an error will be reported.
#
# An optional filename can be specified, otherwise input is read from stdin.
#
set -o pipefail