Skip to content

Instantly share code, notes, and snippets.

@shokoe
shokoe / mon-put-instance-data-patch-load.pl
Last active February 5, 2017 14:23
Added load metrics (1,5, and 15 minuted) to 'Amazon CloudWatch Monitoring Scripts for Linux' from https://aws.amazon.com/code/8720044071969977. Adds hostname to all metrics. Requires datatime perl lib (apt-get install libdatetime-perl).
#!/usr/bin/perl -w
# Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You may not
# use this file except in compliance with the License. A copy of the License
# is located at
#
# http://aws.amazon.com/apache2.0/
#
@shokoe
shokoe / aws-scripts-mon-cron.sh
Last active February 5, 2017 14:21
Cron script for report all possible metrics to CloudWatch using 'Amazon CloudWatch Monitoring Scripts for Linux' from https://aws.amazon.com/code/8720044071969977 (requires the load patch).
#!/bin/bash
# crontab line:
# * * * * * <PATH_TO>/aws-scripts-mon-cron.sh
/opt/aws-scripts-mon/mon-put-instance-data-patch-load.pl --aws-access-key-id='AKIAxxxxxxxxxxxxxxxx' --aws-secret-key='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' \
--mem-util --mem-used --mem-avail --swap-util --swap-used \
$(df -h -x "tmpfs" -x "devtmpfs" | sed 1d | awk '{print $6}' | sort -u | while read i; do echo -n " --disk-path=$i"; done) \
--disk-space-util --disk-space-used --disk-space-avail \
--load-1 --load-5 --load-15 \
--from-cron
@shokoe
shokoe / es_clean.sh
Created December 11, 2016 12:36
Local Elasticsearch Logstash indices retention cron script.
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
local_indices='/var/lib/elasticsearch/elasticsearch/nodes/0/indices'
# retention
i=14
# history verification for skipped runs
h=10
I=$i
log='/var/log/log.log'
log_out(){ echo -e "`date +'%Y-%d-%m %H:%M:%S'` (pid $$) -- $1" >> $log; }
log_pipe(){
[ ! -z "$1" ] && p="$1: " || p=""
while read data; do
echo -e "`date +'%Y-%d-%m %H:%M:%S'` (pid $$) -- ${p}$data" >> $log
done
}
pid_file="/var/run/file.lock"
lock_retry_timeout=60
check_lock(){
if [ -e $pid_file ]; then
log_out log "lock file ($pid_file) found, verifying"
sleep 2
if grep $0 /proc/`cat $pid_file`/cmdline &>/dev/null; then
log_out log "Script $0 is locked ($pid_file)!!!!"
return 1
@shokoe
shokoe / EC2ulz_ops.sh
Last active February 5, 2018 19:39
EC2ulz_ops - minimal tools set for working with AWS from command line
# EC2ulz_ops
echo -ne "\033]0;`hostname`\007"
ec_key='/root/.ssh/my-keypair.pem'
# awscli autocomplete
complete -C '/usr/bin/aws_completer' aws
## _EC2hosts_cache - hostname completion (dynamic cahced)
###################
_EC2hosts_cache='/tmp/EC2hosts.cache'
@shokoe
shokoe / rds_getlogs.sh
Last active March 23, 2018 19:19
AWS RDS audit log copy to local dir and to S3. Handles rotation to timestamp naming (so nothing is lost), monthly dirs and S3 encryption. Has a readable and easily monitored log.
#!/bin/bash
# currently support server_audit only!!
# Requires:
# working awscli
# apt-get install lockfile-progs
# required monitoring:
# logwatch on $admin_log for the word 'ERROR'
# log integrity check
# locations:
# script log - /var/log/rds/rds_getlogs.log
@shokoe
shokoe / locktest.sh
Last active January 4, 2017 14:51
Simple bash lock file example using lockfile-progs.
#!/bin/bash
# Requires: apt-get install lockfile-progs
# Test: /root/locktest.sh& sleep 2; /root/locktest.sh
lock_file='/var/run/locker'
# 0 for immidiate fail, see 'man lockfile-progs' for details
lock_retry=0
lock(){
lockfile-create -r $lock_retry -p $lock_file && return 0
Get instance Name tag value:
aws ec2 describe-tags --filters "Name=resource-id,Values=i-11111111111111111" "Name=key,Values=Name" --output text
Pidstat aggregator - Aggregates statistics from pidstat by process name or username mainly for
feeding it to carbon through crontab. Can output stats by pid, with or without username and
process name. Can also output plain table of aggregations.
Syntax: /usr/local/bin/pidstat_aggregator.sh <operation> <aggregations> <interval> [graphite/carbon server:port]
operation:
help - This help
table - Print aggregation table
graphite - Graphite/carbon compatible metric output
debug - Print pre-aggregation pidstat table, table output and graphite output