Skip to content

Instantly share code, notes, and snippets.

@shokoe
shokoe / wait4 scriptlet
Created January 18, 2018 14:02
Wait4 something scriptlet with some examples - useful for AWS api automation
wait4(){
local data_func=$1; local data_func_var=$2; local data_expect=$3;
local timeout=$4; local sleep_time=$5; local exit_on_timeout=${6:-0};
local wait4_s=`date +%s`
local data_out data_prog
while true; do
# error on getting info
read data_out data_state <<< "`$data_func $data_func_var`"
@shokoe
shokoe / check_rds_disk.sh
Last active July 5, 2018 20:36
AWS RDS disk usage check for Naemon/Nagios
#!/bin/bash
# size units are in GB
# syntax: check_rds_disk.sh <1:db ident> <2:max delay> <3:warn> <4:crit>
# e.g: check_rds_disk.sh rds.db1 300 70 85
db_name="${1/*./}"
# max_daly in seconds
max_delay=$2
warn=$3
crit=$4
@shokoe
shokoe / check_rds_cpu.sh
Created December 21, 2017 17:25
Check AWS RDS cpu usage - A simple example for using Cloudwatch metrics in Naemon/Nagios
#!/bin/bash
# syntax: check_rds_cpu.sh <1:db ident> <2:max delay> <3:warn> <4:crit>
# e.g: check_sqs_age.sh db1 300 70 85
db_name="${1/*./}"
# max_daly in seconds
max_delay=$2
warn=$3
crit=$4
@shokoe
shokoe / check_aws_guardduty.sh
Last active April 5, 2018 13:34
Naemon/Nagios plugin for AWS GaurdDuty (outputs status and full table with resolve of instance id to Name tag)
#!/bin/bash
awscmd='aws guardduty --output json'
frmt='%-6s %-9s %-6s %-20s %-20s %-11s %-11s %-10s %s\n'
# damn aws stupid reports with utf8
export LC_ALL='en_US.UTF-8'
map_id(){
res_sed=`aws ec2 describe-instances --output json |\
jq -r '.Reservations[].Instances[] | "\(.InstanceId) \(if .Tags and ([.Tags[] | select ( .Key == "Name" )] != []) then .Tags[] | select ( .Key == "Name" ) | .Value else "%" end)"' |\
grep -v % | sed 1d | sort -r |\
@shokoe
shokoe / check_spot_price_adv.sh
Last active October 24, 2017 11:57
AWS spot price nagios and info script - Based on running spot instances, on-demand prices and spot prices the script calculates average spot price over a set time frame, displays lots of information and alerts (nagios style) on spot-price % on-demand-price.
#!/bin/bash
export PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
# Requires:
# jq 1.5
# gawk
Help="Overview:
Based on running spot instances, on-demand prices and spot prices the script
calculates average spot price over a set time frame, displays lots of
@shokoe
shokoe / naemon_alerts.sh
Created September 28, 2017 15:26
Naemon Host/Service notification summary
#!/bin/bash
d=${1:-3}
u='Shoko-sms'
b='/opt/omd/sites/techdrift/var/naemon/archive/naemon.log'
O=$( for i in `seq $d`; do
D=`date -d "-$i days" +%Y%m%d`
D2=`date -d "-$i days" +%Y.%m.%d`
#echo "$D2"
grep 'SERVICE NOTIFICATION: ' $b-$D |\
@shokoe
shokoe / check_sqs_age.sh
Created September 25, 2017 08:54
Nagios SQS checks for age, size and throughput
#!/bin/bash
# syntax: check_sqs_age.sh <1:queue name> <2:max delay> <3:age sec warn> <4:age sec crit>
# e.g: check_sqs_age.sh items 900 120 300
# if (warn > crit) alert on high values and vise versa
queue_name=$1
max_delay=$2
warn=$3
crit=$4
@shokoe
shokoe / nagios_host_state.sh
Last active September 5, 2017 16:50
Simple Nagios status.dat parser with static fields. Receives host name regex.
#!/bin/bash
get_regex(){
awk -v R="$1" 'BEGIN {RS="\n\n"; FS="\n";}
{
if ($1=="hoststatus {" && match($2, "^\thost_name="R))
print $2
};' /var/cache/nagios3/status.dat | sed "s#[\t ][\t ]*[^=]*=# #g; s# ##"
}
# output - <type=service>:<host_name>:<service_description>:<sec since last_check>:<sec since last_state_change>:<current_state>
@shokoe
shokoe / jdbc_jmx.sh [all]
Last active August 18, 2017 13:37
Get tomcat 8 jdbc jmx info using jolokia from in app jdbc connectors (not tested with central connectors in tomcat)
#!/bin/bash
# requires JSON.sh
# curl 'https://raw.githubusercontent.com/dominictarr/JSON.sh/master/JSON.sh' > /usr/local/bin/JSON.sh
# chmod 700 !$
# requires jolokia at /opt/jolokia
# wget -O jolokia-jvm-1.3.7-agent.jar http://search.maven.org/remotecontent?filepath=org/jolokia/jolokia-jvm/1.3.7/jolokia-jvm-1.3.7-agent.jar
# mkdir /opt/jolokia
# mv jolokia-jvm-1.3.7-agent.jar /opt/jolokia
# requires jq
# apt-get install jq
@shokoe
shokoe / check_wasted_aws_reserv.sh
Last active September 17, 2018 22:07
Nagios script for checking AWS reservation usage full coverage. Will alert on any wasted reservation capacity. Support latest feature from AWS - instance types inter-family conversion, includes tables with all the data. Doesn't work with region specific reservations.
#!/bin/bash
# tested with aws-cli/1.11.123 Python/2.7.9 Linux/3.16.0-4-amd64 botocore/1.5.86
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
#set -x
family_conversion="Type Factor
nano 1
micro 2
small 4