-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws-long-running-ec2.sh
112 lines (97 loc) · 3.11 KB
/
aws-long-running-ec2.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/bin/bash
#================================================================================
# Title : aws-long-running-ec2.sh
# Description : This script will scan the instances and notify users who
# have instances running longer than a week.
#
# Author : https://github.com/abiydv
# Date : 20181228
# Version : 1
# Usage : bash
# Usage : ./aws-long-running-ec2.sh
# Depends :
# Config files : configs/aws-useradd.properties
#================================================================================
function init(){
source ./configs/aws-useradd.properties
check aws s3 cp s3://"${s3_path}${inventory_file}" .
getUsers
getInstanceDetails
}
function getUsers(){
# shellcheck disable=SC2006
check aws ec2 describe-instances --region "$aws_region" --query \
"Reservations[*].Instances[*].[Tags[?Key==`User`].Value]" \
--output text | sort | uniq > ./user-list
}
function getInstanceDetails(){
while read -r line
do
# shellcheck disable=SC2000
if [ "$(echo "$line" | wc -c)" -eq 1 ];then
continue
fi
check aws ec2 describe-instances --region "$aws_region" --filters "Name=tag:User,Values=$line" \
--query 'Reservations[*].Instances[*].[InstanceId,InstanceType,State.Name,LaunchTime]' \
--output text > ./user-instance-list
sed -i "/\b\(stopped\|t2.micro\)\b/d" ./user-instance-list
setEmailHeader "$line"
while read -r instanceline
do
extract_date=$(echo "$instanceline" | cut -f4 -d " " | cut -f1 -d "T")
launch_date=$(date -d "$extract_date" +%s)
week_old_date=$(date -d 'now - 1 weeks' +%s)
if [ "$launch_date" -lt "$week_old_date" ];then
echo "$instanceline" >> ./user-email-list
fi
done < ./user-instance-list
if [ "$(grep -c "i-" ./user-email-list)" -ne 0 ]; then
setEmailFooter
emailUser
fi
done < ./user-list
}
function getUserEmail(){
grep -i "$1" ./"$inventory_file" > ./tmp
if [ "$(wc -l < ./tmp)" -gt 1 ];then
mail_to="$mail_from"
else
mail_to=$(cut -f5 -d "," ./tmp)
fi
}
function setEmailHeader(){
getUserEmail "$1"
echo "From:$mail_from" > ./user-email-list
{
echo "To:$mail_to"
echo "Cc:$mail_cc"
echo "Subject: ATTENTION: $1: Your EC2 Instances"
echo "Importance:High"
echo ""
echo "** REQUIRES YOUR IMMEDIATE ATTENTION AND ACTION **"
echo "Instances older than a week tagged to you (tag:$1)"
echo "--------------------------------------------------------"
} >> ./user-email-list
}
function setEmailFooter(){
{
echo ""
echo "We have identified these long running instances that you have created"
echo "Please review, and - "
echo "1. Downgrade the instance type to avoid paying for excess capacity."
echo "2. Terminate them immediately if you don't need them, to avoid excessive charges."
echo ""
} >> ./user-email-list
}
function emailUser(){
/usr/sbin/sendmail -f "$mail_from" "$mail_to" "$mail_cc" < ./user-email-list
}
function check (){
if ! "$@"; then
echo "FAILED - $*"
exit 1
else
echo "SUCCESS - $*"
fi
}
init