Skip to content

Instantly share code, notes, and snippets.

View urjitbhatia's full-sized avatar

Urjit Singh Bhatia urjitbhatia

View GitHub Profile
@urjitbhatia
urjitbhatia / dnsquerylogger.sh
Last active May 19, 2021 18:52
easy way to continuously log dns queries
#!/bin/bash
# replace any with a specific interface you want to monitor or just use any to capture all interfaces
tcpdump -i any 'dst port 53' >> /var/log/dnsqueries.log
#### If using supervisord, use this template for setting up the program entry:
# [program:dnsworker]
# command=bash dnsquerylogger.sh
# autostart=true
# autorestart=true
@urjitbhatia
urjitbhatia / ptonline.sh
Created April 21, 2021 23:16
pt-online-schema-change
# Command to run pt online schema change
pt-online-schema-change \
D=<dbname>,t=<table_name>,h=<hostname>,u=<username> \
--alter="<alter sql>" \
--ask-pass \
--execute \
--max-load 100 \
--critical-load 120
@urjitbhatia
urjitbhatia / mysql_delete_chunked.sql
Created April 20, 2021 00:56
Aurora mysql delete in chunk
--- Simple table to keep track of delete chunks
create table _delete_log
(
log varchar(200) null,
createdAt timestamp null
);
------------ Stored proc definition ---------------
DROP PROCEDURE IF EXISTS delete_chunks;
DELIMITER $$
@urjitbhatia
urjitbhatia / get-dind-sibling-ip
Created August 14, 2020 00:53
Fetch the ip address of a sibling container from a docker-in-docker run
$(docker inspect my-sibling-dind-container --format '{{ .NetworkSettings.IPAddress }}')
-- Make sure both run on the same "network" (bridge by default in most cases)
@urjitbhatia
urjitbhatia / makefile
Last active August 12, 2020 01:49
make recipe to tag docker images with git tags
### example for setting up a make workflow that can tag images based on the git tag
### the trailing ; are important: runs the commands in the same "shell" and the $$tag variable flows through
docker-build:
@echo "Latest 3 tags: "; \
git ls-remote --sort='v:refname' --tags ./. | tail -n 3; \
read -p "Enter New Tag:" tag; \
echo "Releasing new tag: $$tag"; \
git tag $$tag; \
git push origin $$tag -f; \
@urjitbhatia
urjitbhatia / bash_trap_example.sh
Last active June 11, 2020 19:31
Catching and throwing errors in bash
#!/usr/bin/env bash
###### ERROR TRAP - DONT WRITE OTHER COMMANDS BEFORE THIS ############
set -eEx
function HANDLE_ERROR() {
echo "SCRIPT FAILED"
echo "FAILED AT: $(caller)"
# Do any other commands here
exit 1
}
@urjitbhatia
urjitbhatia / container_signal_dockersock.sh
Created June 11, 2020 19:26
Send signal to docker container via docker socket from commandline script
# This script will send a signal to a container
# Sleep for a random time b/w 1 to 3 seconds to avoid restarting at the exact same time
# This bc format of finding random doesn't print scientific notation which sometimes sleep doesn't like a lot
/bin/sleep "$(echo "scale=3; 0.5 + 2.5 * $RANDOM / 32767" | bc)s"
## Running in silent curl mode (-s and > /dev/null) - remember while debugging
if curl --silent --show-error --output /dev/null --unix-socket /var/run/docker.sock -X POST "http/containers/${CONTAINER_ID}/kill?signal=SIGUSR2" ; then
echo "Successfully sent signal to $CONTAINER_ID"
else
echo "Failed to send signal to $CONTAINER_ID. Triggering alarms."
@urjitbhatia
urjitbhatia / datadog_events.sh
Created June 11, 2020 19:23
Examples of sending datadog events via commandline
# https://help.datadoghq.com/hc/en-us/articles/206441345-Send-metrics-and-events-using-dogstatsd-and-the-shell
# Send an event to DD based on success or failure
echo "_e{${#title},${#text}}:$title|$text|t:${alert_type}" | nc -u -w1 "${HOST_IP}" 8125
if [ "$CMD_EXIT_CODE" -eq 0 ];
echo "foo.bar.success:1|g" | nc -u -w1 "${HOST_IP}" 8125
then
echo "foo.bar.failure:1|g" | nc -u -w1 "${HOST_IP}" 8125
fi
@urjitbhatia
urjitbhatia / wait-for-dependencies.sh
Created February 29, 2020 00:24
Wait for process dependencies
#!/bin/bash
######################################################################
# Waits for Mongodb, Redis and Mysql to be ready
# usage examples:
# wait-for-dependencies.sh echo "READY TO GO"
# wait-for-dependencies.sh make build
# Exit Codes: 0 for success, 2 if wait command timed out
######################################################################
@urjitbhatia
urjitbhatia / consulLock.sh
Last active February 7, 2020 23:43
Consul Lock/Unlock with timeout
#!/bin/bash
set -e
export CONSUL_HTTP_ADDR="https://my-consul-address"
VALUE=$(consul kv get <key>)
if [ "$VALUE" != "locked" ]; then
# Unlocked, check the timeout
TIMEOUT=$(echo $VALUE | jq 'reduce .[] as $num (0; .+$num)')