Skip to content

Instantly share code, notes, and snippets.

View ejoubaud's full-sized avatar

Emmanuel Joubaud ejoubaud

View GitHub Profile
@ejoubaud
ejoubaud / waiting_transactions.sql
Created April 11, 2017 13:35
Mysql Waiting transactions diagnostic
SELECT
r.trx_id as waiting_trx_id,
r.trx_mysql_thread_id as waiting_thread,
r.trx_query as waiting_query,
b.trx_id as blocking_trx_id,
b.trx_mysql_thread_id as blocking_thread,
b.trx_query as blocking_query,
bp.host as blocking_host,
rp.host as waiting_host,
bp.command as blocking_command,
@ejoubaud
ejoubaud / get-rds-slow-logs.sh
Last active March 30, 2017 08:31
Fetch all available RDS slow logs into single file
aws-vault exec customer -- aws rds describe-db-log-files --db-instance-identifier market-database-clustercluster-16apkpdp1bgkx-us-east-1a --region us-east-1 --query 'DescribeDBLogFiles[].LogFileName' --output text | tr '\t' '\n' | grep slowquery | while read file; do aws-vault exec customer -- aws rds download-db-log-file-portion --db-instance-identifier market-database-clustercluster-16apkpdp1bgkx-us-east-1a --region us-east-1 --log-file-name $file --output text >> slowlogs.log; done
@ejoubaud
ejoubaud / trace_files_git_history.rb
Created December 22, 2016 15:58
Say you have a file that's been duplicated a few and you want to get an overwiew of how each copy diverged, this gives you a TSV you can import in Excel
#!/usr/bin/env ruby
`for i in market*.sh.erb; do echo $i; git log --format="%ad%x09%h%x09%s" --date=iso -- $i; done > history1.log`
lines = File.readlines('history1.log')
commits_by_file = lines.reduce(['', Hash.new([])]) { |(current_file, commits_by_file), line| line.include?("\t") ? [current_file, commits_by_file.merge(current_file => (commits_by_file[current_file] + [line]))] : [line, commits_by_file] }.last
commits_by_file_parsed = Hash[commits_by_file.map{|file, commits| [file, commits.map{|c|c.split("\t")}.map{|s|{:date => s[0], :sha => s[1], :msg => s[2]}}]}]
flat_commits = commits_by_file_parsed.flat_map{|file, commits| commits.map {|commit| commit.merge(:file => file)}}
commits = flat_commits.reduce({}) { |mem, commit| new_commit = mem[commit[:sha]] ? mem[commit[:sha]].merge(:files => (mem[commit[:sha]][:files] + [commit[:file]])) : commit.select{|k| k != :file}.merge(:files => [commit[:file]]); mem.merge(commit[:sha] => new_commit) }.values.sort_by{|c| c[:date]}
files = commits_by_file.keys
@ejoubaud
ejoubaud / cf_asg_instances_status_check.rb
Created December 14, 2016 17:46
Checks the status and AMI ID of instances in the ASG of a CloudFormation stack (used to test an AMI rotation script)
#!/usr/bin/env ruby
require 'aws-sdk'
require 'forwardable'
DEFAULT_REGION = "us-east-1".freeze
STACK_NAMES = ['market-dj'].freeze
class Instance
extend Forwardable
@ejoubaud
ejoubaud / keydown.js
Last active April 9, 2024 19:00
Trigger keyboard event in Javascript, works in OSX Chrome 58
// Based on http://stackoverflow.com/a/10520017/1307721
// which wouldn't work for my use-case on my Mac because metaKey would always be true, for some reason
// Adapted to override metaKey to false
Podium = {};
Podium.keydown = function(k) {
var oEvent = document.createEvent('KeyboardEvent');
// Chromium Hack
Object.defineProperty(oEvent, 'keyCode', {
get : function() {
@ejoubaud
ejoubaud / list_instance_private_ips.sh
Last active June 20, 2016 14:54
One-liner to list instance private IPs with the AWS CLI
aws ec2 describe-instances --region us-east-1 --query 'Reservations[].Instances[].[ Tags[?Key==`Name`].Value | [0], PrivateIpAddress, PublicIpAddress ]' --output text | sort
@ejoubaud
ejoubaud / docker-machine-hosts.sh
Last active February 2, 2016 20:40
Docker-machine: Update domain name in /etc/hosts, a bash/zsh one-liner
mark="# DOCKER_MACHINE:" && sudo sed -i '' "/$mark/d" /etc/hosts && docker-machine ls -q | while read m; do echo "$(docker-machine ip $m) $m.docker ${mark} $m" | sudo tee -a /etc/hosts; done
@ejoubaud
ejoubaud / multicom.sh
Created August 7, 2015 08:35
Open variations of a command in multiple iTerm2 panes. Works with bash and zsh.
multicom () {
echo Usage: multicom command arg1 arg2 arg3 arg4...
echo e.g. multicom "ssh %s" web1 web2 web3
echo
read -r -d '' code <<-APPLESCRIPT
activate application "iTerm"
tell application "System Events" to keystroke "n" using command down
APPLESCRIPT
@ejoubaud
ejoubaud / simulate_keypress.js
Last active April 9, 2024 18:38
Simulate keypress that works in Google nav in Chrome
// Based on http://stackoverflow.com/a/10520017/1307721 and http://stackoverflow.com/a/16022728/1307721
Podium = {};
Podium.keydown = function(k) {
var oEvent = document.createEvent('KeyboardEvent');
// Chromium Hack
Object.defineProperty(oEvent, 'keyCode', {
get : function() {
@ejoubaud
ejoubaud / cleanup-kernels.sh
Created October 30, 2014 15:51
Remove old kernels on a ubuntu box
# Identify the revision of kernel running:
uname -r
# Identify installed kernel packages:
dpkg --get-selections | awk '((/^linux-/) && (/[0-9]./)) {print $1}'
# Reboot box if not running the latest installed kernel.
sudo reboot
# Remove kernel packages (being careful not to uninstall the currently running kernel).