Skip to content

Instantly share code, notes, and snippets.

@mlf4aiur
mlf4aiur / associative_arrays.sh
Created May 28, 2013 06:32
Bash associative arrays example
# Associative Arrays
declare -A characters
AA="abcdabcaba"
for ((x=0; x<${#AA}; x++))
do
tmp=${AA:$x:1}
((characters[$tmp]++))
done
@mlf4aiur
mlf4aiur / httpdate2unixtimestamp.awk
Created May 28, 2013 06:35
Convert http date to unix timestamp AWK function
function httpdate2unixtimestamp(date, array, time_string, timestamp) {
# input: [02/Dec/2011:04:02:42 -0500]
# output: 1322816562
# date is argument
# array, time_string, timestamp is local variable
split(date, array, "[]/: []")
if (array[3]=="Jan") month="01"
else if (array[3]=="Feb") month="02"
else if (array[3]=="Mar") month="03"
else if (array[3]=="Apr") month="04"
@mlf4aiur
mlf4aiur / auto_init_instance_var.py
Created May 28, 2013 06:42
Automatically Initializing Instance Variables from _ _init_ _ Arguments
# Recipe 6.18. Automatically Initializing Instance Variables from _ _init_ _ Arguments
# Solution
# You can "factor out" the attribute-assignment task to an auxiliary function:
def set_attributes_from_dict(d):
self = d.pop('self')
for n, v in d.iteritems():
setattr(self, n, v)
# Now, the typical boilerplate code for an _ _init_ _ method such as:
@mlf4aiur
mlf4aiur / save-mysql-query-results-into-a-text-or-csv-file.md
Created February 27, 2014 06:32
Save MySQL query results into a text or CSV file

Save MySQL query results into a text or CSV file

Given a query such as

SELECT order_id,product_name,qty FROM orders

which returns three columns of data, the results can be placed into the file /tmo/orders.txt using the query:

SELECT order_id,product_name,qty FROM orders

@mlf4aiur
mlf4aiur / json_loads_dump.py
Created February 28, 2014 06:06
json loads dump
graphite_index = json.loads(open('index.json').read())
result = json.dumps(instances, sort_keys=True, indent=4, encoding='utf-8')
@mlf4aiur
mlf4aiur / bash_skeleton.sh
Last active November 17, 2016 09:10
Bash script skeleton
#!/bin/bash
app_path="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
app_name="$(basename "${BASH_SOURCE[0]}")"
pid_file="${app_path}/run.pid"
VERSION="${app_name} ver. 0.9.0"
function help () {
cat <<EOF
usage: ${app_name} [-h] [-v]
@mlf4aiur
mlf4aiur / convert_unixtimestamp.sh
Created March 26, 2014 06:16
Convert unix timestamp
utime(){ date -d @$1; }
utime(){ date -d "1970-01-01 GMT $1 seconds"; }
utime(){ awk -v d=$1 'BEGIN{print strftime("%a %b %d %H:%M:%S %Y", d)}'; }
utime(){ perl -e "print localtime($1).\"\n\""; }
utime(){ python -c "import time; print(time.strftime('%a %b %d %H:%M:%S %Y', time.localtime($1)))"; }
[root@test test]# utime(){ date -d @$1; }
[root@test test]# utime 1273817956
Fri May 14 14:19:16 CST 2010
@mlf4aiur
mlf4aiur / Grafana Templated Graphs.json
Created June 15, 2014 02:09
Grafana-Templated Graphs
{
"title": "Templated Graphs",
"services": {
"filter": {
"list": [
{
"type": "filter",
"name": "host",
"query": "sensu.*",
"includeAll": true,
@mlf4aiur
mlf4aiur / setup_docker_on_osx.md
Last active August 29, 2015 14:18
Setup Docker on OSX

Setup Docker on OSX

Install Homebrew on your laptop

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Install some packages you like

brew update

@mlf4aiur
mlf4aiur / Grafana-cAdvisor-Templated.json
Last active February 16, 2016 15:30
Grafana cAdvisor Templated dashboard
{
"id": 3,
"title": "cAdvisor Templated",
"originalTitle": "cAdvisor Templated",
"tags": [],
"style": "dark",
"timezone": "browser",
"editable": true,
"hideControls": false,
"sharedCrosshair": false,