-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport_tag.sh
executable file
·530 lines (458 loc) · 15.5 KB
/
report_tag.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
#!/bin/bash
# Authored by Antoine CICHOWICZ
# Copyright: Apache License 2.0
# Yriser - https://github.com/yris-ops/yriser
contains_element() {
local e
for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
return 1
}
SOURCE_FILE=${1}
OUTPUT_FILE=${2}
OUTPUT_CSV=${3}
ACCOUNT=${4}
REGION=${5}
# Report VAR
YRIS_SCORE=0
TOTAL_RESOURCES=0
NUMBER_OF_ERRORS=0
NUMBER_OF_OK=1
TAG_CORRECT=0
TAG_CPT=1
## Error VAR int
line_number=1
number_RESOURCETAGMAPPINGLIST=0
number_RESOURCETAGMAPPINGLIST_without_tag=0
# Error Tags Value
# Comparative STRING
substr1=RESOURCETAGMAPPINGLIST
substr2=TAGS
# Progress bar VAR
file_line=$(wc -l < "$SOURCE_FILE")
percent=0
# Lists for variables
# Variable for capturing errors in RESOURCE TAG MAPPING LIST
RESOURCETAGMAPPINGLIST_errors_without_tag=""
# Get the current date and time in the desired format
ASSESSMENT_START_TIME=$(date +%Y-%m-%dT%H:%M:%SZ)
start=$(date +%s)
while read -r l; do
RESOURCETAGMAPPINGLIST=$(echo "$l" | sed -n '/RESOURCETAGMAPPINGLIST/p' | sed 's/^[^:]*://' | sed 's/^[^:]*://' | sed 's/^[^:]*://' | sed 's/^[^:]*://' | sed 's/^[^:]*://')
if [ -n "$RESOURCETAGMAPPINGLIST" ]; then
RESOURCETAGMAPPINGLIST_type=""
RESOURCETAGMAPPINGLIST_name=""
RESOURCETAGMAPPINGLIST_id=""
RESOURCETAGMAPPINGLIST_type=$(echo "$RESOURCETAGMAPPINGLIST" | sed 's/\/.*//')
RESOURCETAGMAPPINGLIST_name=$(echo "$RESOURCETAGMAPPINGLIST" | sed -e 's/.*\/\(.*\)\/.*/\1/')
RESOURCETAGMAPPINGLIST_name2=$(echo "$RESOURCETAGMAPPINGLIST_name" | sed 's/.*\///g')
RESOURCETAGMAPPINGLIST_id=$(echo "$RESOURCETAGMAPPINGLIST" | sed 's/.*\///g')
if [ "$RESOURCETAGMAPPINGLIST_type" = "$RESOURCETAGMAPPINGLIST_name" ]; then
echo "$line_number -> $RESOURCETAGMAPPINGLIST_type" >> "output/txt/$OUTPUT_FILE.txt"
TOTAL_RESOURCES=$((TOTAL_RESOURCES + 1))
elif [ "$RESOURCETAGMAPPINGLIST_name2" = "$RESOURCETAGMAPPINGLIST_id" ] 2>/dev/null; then
echo "$line_number -> $RESOURCETAGMAPPINGLIST_type $RESOURCETAGMAPPINGLIST_name2 " >> "output/txt/$OUTPUT_FILE.txt"
TOTAL_RESOURCES=$((TOTAL_RESOURCES + 1))
else
echo "$line_number -> $RESOURCETAGMAPPINGLIST_type $RESOURCETAGMAPPINGLIST_name $RESOURCETAGMAPPINGLIST_id" >> "output/txt/$OUTPUT_FILE.txt"
TOTAL_RESOURCES=$((TOTAL_RESOURCES + 1))
fi
fi
# Check if RESOURCE has one tag
TAG_CPT=$((TAG_CPT + 1))
line_number_tag=$((line_number + 1))
if [[ $l == *"$substr1"* ]]; then
number_RESOURCETAGMAPPINGLIST=$((number_RESOURCETAGMAPPINGLIST + 1))
CHECK_TAG_EXIST=$(sed -n "$line_number_tag"p "$SOURCE_FILE")
if [[ $CHECK_TAG_EXIST == *"$substr2"* ]]; then
ll=$line_number_tag
CHECK_TAG_EXIST_2=$substr2
while [[ $CHECK_TAG_EXIST_2 == *"$substr2"* ]]; do
CHECK_TAG_EXIST_2=$(sed -n "$ll"p "$SOURCE_FILE")
ll=$((ll + 1))
FIND_KEY=$(echo "$CHECK_TAG_EXIST_2" | awk '{print $2;}')
FIND_VALUE=$(echo "$CHECK_TAG_EXIST_2" | awk '{print $3;}')
# Check if Key exists
*)
echo "OK | TAG $FIND_KEY $FIND_VALUE" >> "output/txt/$OUTPUT_FILE.txt"
;;
esac
fi
done
else
RESOURCETAGMAPPINGLIST_errors_without_tag+="$RESOURCETAGMAPPINGLIST "
number_RESOURCETAGMAPPINGLIST_without_tag=$((number_RESOURCETAGMAPPINGLIST_without_tag + 1))
echo " > Warning : $RESOURCETAGMAPPINGLIST_type -> Without tag" >> "output/txt/$OUTPUT_FILE.txt"
echo "" >> "output/txt/$OUTPUT_FILE.txt"
fi
fi
progress_bar=$((line_number * 100 / file_line))
time="$(($(date +%s) - $start))"
hours=$((time / 3600))
minutes=$(( (time % 3600) / 60 ))
seconds=$((time % 60))
echo -en "\r-> Scan in progress! $ACCOUNT [$REGION] analysis: \033[33m[$progress_bar%]\033[0m in $hours:$minutes:$seconds"
line_number=$((line_number + 1))
done < "$SOURCE_FILE"
ALL_TAG=$((ROCK_TAG + FLEX_TAG))
TAG_CPT=$((TAG_CPT - TOTAL_RESOURCES))
TAG_CORRECT_PER=$(echo "scale=2; $NUMBER_OF_OK / $TAG_CPT * 100" | bc)
NUMBER_OF_ERRORS_PER=$(echo "scale=2; $NUMBER_OF_ERRORS / $TAG_CPT * 100" | bc)
number_RESOURCETAGMAPPINGLIST_without_tag_PER=$(echo "scale=2; $number_RESOURCETAGMAPPINGLIST_without_tag / $TAG_CPT * 100" | bc)
NUMBER_OF_ERRORS_CAL=$((TAG_CPT - NUMBER_OF_OK - NUMBER_OF_ERRORS - number_RESOURCETAGMAPPINGLIST_without_tag))
NUMBER_OF_ERRORS_CAL_PER=$(echo "scale=2; $NUMBER_OF_ERRORS_CAL / $TAG_CPT * 100" | bc)
TOTAL_ERROR=$((NUMBER_OF_ERRORS_CAL + NUMBER_OF_ERRORS + number_RESOURCETAGMAPPINGLIST_without_tag))
YRIS_SCORE=$(echo "scale=2; $TOTAL_ERROR / $TAG_CPT * 100" | bc)
YRIS_SCORE=$(echo "scale=2; 100 - $YRIS_SCORE" | bc)
echo -en "\r\r-> Scan completed! $ACCOUNT [$REGION] analysis: $TAG_CPT/$TAG_CPT \033[33m[100%]\033[0m in $hours:$minutes:$seconds"
echo ""
## Output CLI
## Generate CSV output
## Resources without tag
IFS=' ' read -ra ASSOCIATE_WITH_ERRORS <<< "$RESOURCETAGMAPPINGLIST_errors_without_tag"
for error in "${ASSOCIATE_WITH_ERRORS[@]}"; do
echo "$ASSESSMENT_START_TIME,without tag,none,$error,$ACCOUNT,$REGION" >> "output/csv/$OUTPUT_CSV"
done
## 2Generate CSV output
## 3.2 Generate CSV Error Tag value
OUTPUT_FILE_TAG_VALUE="output/txt/$OUTPUT_FILE.txt"
OUTPUT_FILE_TAG_VALUE_CSV="output/csv/$OUTPUT_CSV"
TEMP_FILE_TAG_VALUE=$(mktemp)
while IFS= read -r ligne_tag_value
do
if [[ $ligne_tag_value == ERROR* ]]; then
last_word=$(echo $ligne_tag_value | awk '{print $NF}')
list_error_tag_value+=("$last_word")
fi
done < "$OUTPUT_FILE_TAG_VALUE"
if [[ ${#list_error_tag_value[@]} -gt 0 ]]; then
for erreur in "${list_error_tag_value[@]}"
do
sed "s/,/,$erreur,/" "$OUTPUT_FILE_TAG_VALUE_CSV" > "$TEMP_FILE_TAG_VALUE"
done
fi
if [[ ${#list_error_tag_value[@]} -gt 0 ]]; then
for ((i=0; i<${#list_error_tag_value[@]}; i++))
do
erreur="${list_error_tag_value[$i]}"
ligne_tag_value_numero=$((i+1))
sed "${ligne_tag_value_numero}s/,/,$erreur,/2" "$OUTPUT_FILE_TAG_VALUE_CSV" > "$TEMP_FILE_TAG_VALUE"
mv "$TEMP_FILE_TAG_VALUE" "$OUTPUT_FILE_TAG_VALUE_CSV"
done
fi
## 3.1 Generate CSV clear multi
file="output/csv/mul-$OUTPUT_CSV"
data=$(cat "$file")
tmp_file_mul=$(mktemp)
count_occurrences() {
local element=$1
local list=("${@:2}")
local count=0
for item in "${list[@]}"; do
if [[ "$item" == "$element" ]]; then
((count++))
fi
done
echo "$count"
}
new_data=""
current_section=""
current_list=()
while IFS= read -r line; do
if [[ "$line" == *":" ]]; then
if [[ -n "$current_section" ]]; then
unique_elements=($(printf "%s\n" "${current_list[@]}" | sort -u))
for element in "${unique_elements[@]}"; do
count=$(count_occurrences "$element" "${current_list[@]}")
new_data+="\n$element,$count"
done
new_data+="\n"
fi
current_section="$line"
current_list=()
new_data+="$line\n"
elif [[ -n "$line" ]]; then
current_list+=("$line")
fi
done <<< "$data"
unique_elements=($(printf "%s\n" "${current_list[@]}" | sort -u))
for element in "${unique_elements[@]}"; do
count=$(count_occurrences "$element" "${current_list[@]}")
new_data+="\n$element,$count"
done
new_data+="\n"
echo -e "$new_data" > "$tmp_file_mul"
grep . "$tmp_file_mul" > "$file"
if [ "$cli_output_csv" = "no" ]; then
find output/csv -type f -name "*$PATH_DATE*" -exec rm {} +
fi
# Generate HTML report
if [ "$cli_output_html" = "yes" ]; then
echo "<html lang="en">
<head>
<title>Yriser - Report Tag Assessments in AWS</title>
<meta name="keywords" content="Yriser"/>
<meta name="description" content ="Yriser is an Open Source FinOps tool to perform AWS tagging best practices, tagging strategy, continuous adjustments in cloud optimization."/>
<meta name="author" content="Antoine CICHOWICZ"/>
<meta name="copyright" content="Copyright © 2023 Antoine CICHOWICZ - Yriser - Apache License 2.0" />
<style>
body {
font-family: Arial, sans-serif;
margin: auto;
background-color: #f7f7f7;
color: #444;
}
.top {
background-color: #343a40!important;
}
h1 {
color: #FFF;
font-size: 24px;
margin-left: 20px;
padding: 14px 16px;
}
h2 {
font-size: 20px;
}
h3 {
font-size: 20px;
margin-bottom: 10px;
}
table {
border-collapse: collapse;
width: 98%;
background-color: #fff;
border: 1px solid #ddd;
margin-left: 10px;
}
th, td {
text-align: left;
padding: 8px;
border-bottom: 1px solid #ddd;
}
th {
background-color: #f2f2f2;
font-weight: bold;
}
tr:hover {
background-color: #f5f5f5;
}
span.a {
display: flex;
padding: 10px;
}
#assessement_table_css { margin-left: 10px; }
</style>
<script>
function searchTable() {
var input = document.getElementById(\"searchInput\").value.toLowerCase();
var table = document.getElementById(\"assessement_table\");
var rows = table.getElementsByTagName(\"tr\");
for (var i = 0; i < rows.length; i++) {
var rowData = rows[i].getElementsByTagName(\"td\");
var found = false;
for (var j = 0; j < rowData.length; j++) {
var cellData = rowData[j];
if (cellData.innerHTML.toLowerCase().indexOf(input) > -1) {
found = true;
break;
}
}
if (found) {
rows[i].style.display = \"\";
} else {
rows[i].style.display = \"none\";
}
}
}
</script>
</head>
<body>
<div class="top">
<h1>Yriser - AWS Report Tag Assessments </h1>
</div>
<span class="a">
<table>
<caption><h2>Report Information:</h2></caption>
<tr>
<th>Version:</th>
<td>$VERSION</td>
</tr>
<tr>
<th>Parameters used:</th>
<td>-$SOURCE_FILE -$ACCOUNT -$REGION</td>
</tr>
<tr>
<th>Date:</th>
<td>$ASSESSMENT_START_TIME</td>
</tr>
<tr>
<th>Ressources scanning</th>
<td>$TOTAL_RESOURCES<td>
</tr>
<tr>
<th>Tagging Rules:</th>
<td>$ALL_TAG</td>
</tr>
<tr>
<th>Unique tag:</th>
<td>$ROCK_TAG</td>
</tr>
<tr>
<th>Multiple tag:</th>
<td>$FLEX_TAG</td>
</tr>
</table>
<table>
<caption><h2><h2>Assessment Summary:</h2></caption>
<tr>
<th>AWS Account:</th>
<td>$ACCOUNT</td>
</tr>
<tr>
<th>API Region:</th>
<td>$REGION</td>
</tr>
<tr>
<th>UserID:</th>
<td>$AWSUSERID</td>
</tr>
<tr>
<th>Caller Identity ARN:</th>
<td>$AWSARN</td>
</tr>
<caption align="BOTTOM"><img src=https://github.com/Yris-ops/yriser/blob/main/file/yriser-logo.png?raw=true alt=Yriser Logo><caption>
</table>
<table>
<caption><h2><h2>Scoring Information:</h2></caption>
<tr>
<th>Yris score:</th>
<td>$YRIS_SCORE%</td>
</tr>
<tr>
<th>Total Tags Scanning:</th>
<td>$TAG_CPT</td>
</tr>
<tr>
<th>Correctly tagged</th>
<td>$NUMBER_OF_OK</td>
</tr>
<tr>
<th>Number of errors:</th>
<td>$NUMBER_OF_ERRORS</td>
</tr>
<tr>
<th>Ressources with other tags:</th>
<td>$NUMBER_OF_ERRORS_CAL</td>
<tr>
<tr>
<th>Resources without tag:</th>
<td>$number_RESOURCETAGMAPPINGLIST_without_tag</td>
<tr>
</table>
</span>
<center><h3>Tag Assessment Details:</h3></center>
<span id="assessement_table_css">Search: </span><input type=\"text\" id=\"searchInput\" placeholder=\"...\" onkeyup=\"searchTable()\">
<br><br>
<table id="assessement_table">
<tr>
<th>ASSESSMENT START TIME</th>
<th>TAG KEY</th>
<th>TAG VALUE</th>
<th>RESOURCE ID</th>
<th>ACCOUNT ID</th>
<th>REGION</th>
</tr>" > "output/html/$OUTPUT_FILE.html"
while IFS= read -r line; do
echo -e " <tr>
<td>${line//,/</td>\n <td>}</td>
</tr>" >> "output/html/$OUTPUT_FILE.html"
done < "output/csv/$OUTPUT_CSV"
echo " </table>
<table>
<caption><h2>Multiple Tag:</h2></caption>
<tr>
<th>TAG VALUE</th>
<th>NUMBER</th>
</tr>" >> "output/html/$OUTPUT_FILE.html"
while IFS= read -r line; do
echo " <tr>
<td>${line//,/</td><td>}</td>
</tr>" >> "output/html/$OUTPUT_FILE.html"
done < "output/csv/mul-$OUTPUT_CSV"
echo " </table>
</body>
</html>" >> "output/html/$OUTPUT_FILE.html"
echo "Detailed results are in:"
echo " - HTML: $(pwd)/output/html/$OUTPUT_FILE.html"
fi
if [ "$cli_output_csv" = "yes" ]; then
echo " - CSV: $(pwd)/output/csv/$OUTPUT_CSV"
echo " - CSV: $(pwd)/output/csv/mul-$OUTPUT_CSV"
fi
if [ "$cli_output_json" = "yes" ]; then
echo " - JSON: $(pwd)/output/json/$OUTPUT_FILE.json"
fi
rm -rf output/.txt 2> /dev/null
# Slack message
if [ "$WEBHOOK_URL" != "" ]; then
MESSAGE='{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Hi there 👋\n\n*Yriser*, FinOps tool to perform AWS tagging best practices, tagging strategy, continuous adjustments in cloud optimization 🛣️\n\n I have just finished the scan on your AWS Account *'"$ACCOUNT"'* ('"$REGION"') with a total *'"$TOTAL_RESOURCES"' ressources scanning*. "
},
"accessory": {
"type": "image",
"image_url": "https://avatars.githubusercontent.com/u/124205798?s=200&v=4",
"alt_text": "cute cat"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "plain_text",
"text": "🏷️ '"$TAG_CPT"' Total Tags\n✅ '"$NUMBER_OF_OK"' Correctly tagged ('"$TAG_CORRECT_PER"'%)\n📊 '"$NUMBER_OF_ERRORS"' Errors ('"$NUMBER_OF_ERRORS_PER"'%)\n🛑 '"$NUMBER_OF_ERRORS_CAL"' Ressources with other tags ('"$NUMBER_OF_ERRORS_CAL_PER"'%)\n❌ '"$number_RESOURCETAGMAPPINGLIST_without_tag"' Without tag ('"$number_RESOURCETAGMAPPINGLIST_without_tag_PER"'%)",
"emoji": true
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Used parameters `-'"$SOURCE_FILE"' -'"$OUTPUT_FILE"' -'"$OUTPUT_CSV"'`"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Version '"$VERSION"' | Join us"
},
"accessory": {
"type": "button",
"text": {
"type": "plain_text",
"text": "Yris :slack:",
"emoji": true
},
"value": "click_me_123",
"url": "https://join.slack.com/t/yrisgroupe/shared_invite/zt-1q51z8dmv-GC0XzUSclzBnUQ0tpKhznw",
"action_id": "button-action"
}
}
]
}'
curl -X POST -H 'Content-type: application/json' --data "$MESSAGE" "$WEBHOOK_URL" &> /dev/null
echo ""
fi