Skip to content

Instantly share code, notes, and snippets.

@djk29a
djk29a / bookshelf-du.rb
Last active October 9, 2015 15:47
[Chef] Get Bookshelf Usage by-org
# Retrieve the size of Chef cookbooks used by different organizations on the Chef server it runs on
# Should run as root normally since we use su to hop across to another user
full_org_list=''
full_file_list=''
orgs={}
# Getting orgs can take a while, let's hold onto the results for repeat invocations
if not File.exist?('.orgs-cached')
# NOTE: this produces 2 header lines and a footer line that has the rowcount
@djk29a
djk29a / export-chef-roles.sh
Last active October 9, 2015 15:48
Download Chef roles in all environments to JSON files
mkdir environments
for env in $(knife environment list); do
knife environment show "${env}" --format=json > "environments/${env}.json"
done
@djk29a
djk29a / mtr_mpls.sh
Created October 9, 2015 16:04
Usage of mtr to get mpls label info
sudo mtr --port 443 --show-ips -T $1 -4 --no-dns --mpls # not sure why I needed both --show-ips and --no-dns, hmm
scala> for(masked <- 0 until 8) { println("Bits: 0xff & masked bits (" + masked + ") => " + (256 - (255 & (1 << masked)))) }
Bits: 0xff & masked bits (0) => 255
Bits: 0xff & masked bits (1) => 254
Bits: 0xff & masked bits (2) => 252
Bits: 0xff & masked bits (3) => 248
Bits: 0xff & masked bits (4) => 240
Bits: 0xff & masked bits (5) => 224
Bits: 0xff & masked bits (6) => 192
Bits: 0xff & masked bits (7) => 128
@djk29a
djk29a / zscaler-status.py
Last active March 22, 2016 00:28
Parse zScaler client info page; suitable for Sensu/Nagios/Check_MK monitoring checks
from HTMLParser import HTMLParser
import sys
"""
ZScaler IP Page Parser
$ curl -sL http://ip.zscaler.com/cgi-bin/index.cgi | python zscaler-status.py
{"proxycloud": "zscalertwo.net",
"proxyhost":"zs2-*redacted*",
"ip": "*redacted_ip*"}
@djk29a
djk29a / prune-chef-nodes.sh
Created March 22, 2016 00:30
Delete stale / not checking in Chef nodes
for node in $(knife search node "ohai_time:[* TO $(date +%s -d '5hours ago')]" -i); do
knife client delete -y $node
knife node delete -y $node
done
@djk29a
djk29a / useradd.bash
Created March 31, 2016 20:53
Add users in a list
for u in username1 username2; do
# Ok, the groups are probably off but you get the point
useradd -p $(openssl passwd -1 ABC123def) -G primarygroup -g additionalgroups ${u}
end
@djk29a
djk29a / deconsolidate.rb
Last active September 30, 2016 18:38
Remove duplicate files in iTunes as a result of Consolidate Files
require 'find'
root = '/Volumes/music' # pick your path
Find.find(root) do |path|
if FileTest.directory? path and File.basename(path)[0] == '.'
Find.prune
else
d, f = File.split path
if f =~ /^(\d+(\-\d+)?.+)(\s\d+)\.(mp3)$/i
orig_file = File.join(d, $1 + '.' + $4) # check for existence of original file name including extension case
if File.readable? orig_file
@djk29a
djk29a / timer_obj.scala
Last active October 4, 2016 01:28
Simple Scala timer I haven't tried to use since Scala 2.08
object Timer {
def time[R](block: => R): Pair[Long,_] = {
val t0 = System.nanoTime()
val result = block
val diff = System.nanoTime() - t0
// Post-timing processing can be added here, maybe
// an optional argument to this method?
Pair(diff, result)
}
}
@djk29a
djk29a / gist:4888b852aea250d2ae836e939a19064f
Last active February 22, 2017 03:13 — forked from hummus/gist:8592113
aws cli + jq example
wget http://stedolan.github.io/jq/download/linux64/jq || sudo apt-get -y --force-yes install jq || sudo yum install -y jq
aws ec2 describe-instances --filters "Name=tag:Name,Values=$NAME" \
"Name=instance-state-name,Values=running" \
| jq -r \
".Reservations[] | .Instances[] | .InstanceId" \
aws ec2 describe-volumes --filters \
"Name=status,Values=available" \
| jq -r ".Volumes[] | .VolumeId" \