Skip to content

Instantly share code, notes, and snippets.

View peacefixation's full-sized avatar

Matt Jarvis peacefixation

  • Melbourne, Australia
View GitHub Profile
@peacefixation
peacefixation / Makefile
Last active March 27, 2019 01:36
Set version variable to Git hash at compile time
# https://goenning.net/2017/01/25/adding-custom-data-go-binaries-compile-time/
# https://golang.org/cmd/link/
GIT_HASH=$(shell git rev-parse HEAD)
FLAGS=-ldflags "-X main.version=$(GIT_HASH)"
build:
@go build $(FLAGS)
run:
@peacefixation
peacefixation / php-ami.php
Last active March 27, 2019 01:36
Asterisk AMI client
// based on an example at https://www.voip-info.org/asterisk-manager-example-php/
<?php
/**
* Connect to Asterisk Manager Interface (AMI).
*
* Setup an AMI user in /etc/asterisk/manager.conf
*/
class AsteriskManager {
@peacefixation
peacefixation / app.conf
Last active March 27, 2019 01:37
Combine multiline logs with Beaver
# all lines that don't start with [ are combined with the previous line
[/var/log/some.log]
type: some-type
multiline_regex_before = ^[^[]].*
@peacefixation
peacefixation / log-type.conf
Last active March 27, 2019 01:38
Test logstash filter with an input file
# test.conf
input {
file {
path => "/tmp/some-file.log"
start_position => beginning
sincedb_path => "/dev/null"
type => "some-type"
}
}
@peacefixation
peacefixation / postgres-cheat-sheet.sql
Last active October 9, 2023 00:05
Postgres Cheat Sheet
-- copy query to CSV
\copy (select * from some_table) TO '/tmp/query-xxx.csv' CSV HEADER
-- dump table to SQL inserts
pg_dump -h <host> -U <user> --schema=<schema> --table=<schema.table> --data-only --column-inserts <database> > output.sql
@peacefixation
peacefixation / postgres-activity.sql
Last active March 27, 2019 01:39
Find pid of running postgres query
# find running queries
SELECT * FROM pg_stat_activity;
# cancel the query
SELECT pg_cancel_backend(<pid>)
# if it won't die:
SELECT pg_terminate_backend(<pid>)
@peacefixation
peacefixation / ssh-agent-forward.sh
Last active March 27, 2019 01:39
Forward SSH agent to remote host
# https://www.ssh.com/ssh/agent
# https://developer.github.com/v3/guides/using-ssh-agent-forwarding/#setting-up-ssh-agent-forwarding
# on the remote host
# /etc/ssh/sshd_config must have 'AllowAgentForwarding yes'
# on your local machine
# /etc/ssh/ssh_config must have 'ForwardAgent yes'
cat >> ~/.ssh/config <<EOF
@peacefixation
peacefixation / clean-asterisk-db.sh
Last active March 27, 2019 01:39
Remove keys from Asterisk database
# remove each family of keys that match the specified pattern from the Asterisk database
# 4 digits
FAMILY_PATTERN="[0-9][0-9][0-9][0-9]"
for FAMILY in `asterisk -rx "database show" | cut -d' ' -f1 | egrep -a "/${FAMILY_PATTERN}/" | cut -d'/' -f2 | uniq`; do
asterisk -rx "database deltree ${FAMILY}";
done
@peacefixation
peacefixation / find-active-user.sh
Last active March 27, 2019 01:39
Find the active user by checking /dev/console
# format the output of stat and show the active user (as a string)
stat -f "%Su" /dev/console
@peacefixation
peacefixation / find-replace.sh
Last active March 27, 2019 01:40
Find files containing text and replace the text
find /some/path -type f -exec grep 'find this text' -l {} \; -exec sed -i '' 's/find this text/replace with this text/g' {} \;