Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added logic to collect logs on bottlerocket AMI #870

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions log-collector-script/linux/eks-bottlerocket-log-collector.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
export LANG="C"
Copy link
Member

@guessi guessi Aug 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing shebang? #!/bin/sh, !/bin/bash or !/usr/bin/env bash. and Copyrights?
https://github.com/awslabs/amazon-eks-ami/blob/master/log-collector-script/linux/eks-log-collector.sh#L1-L18

export LC_ALL="C"

# Global options
BOTTLEROCKET_ROOTFS="/.bottlerocket/rootfs"
readonly CURRENT_TIME=$(date --utc +%Y-%m-%d_%H%M-%Z)
readonly PROGRAM_VERSION="0.6.2"
readonly LOG_DIR="/var/log"
INSTANCE_ID=""

BOTTLEROCKET_UTILS=(
tar
)

is_diskfull() {
local threshold
local result

# 1.5GB in KB
threshold=1500000
result=$(df / | grep --invert-match "Filesystem" | awk '{ print $4 }')

# If "result" is less than or equal to "threshold", fail.
if [[ "${result}" -le "${threshold}" ]]; then
die "Free space on root volume is less than or equal to $((threshold>>10))MB, please ensure adequate disk space to collect and store the log files."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't see die and warning (Line 35) definition.

Try to run with control

[ssm-user@control]$ die
sh: die: command not found

and admin

[root@admin]# die
bash: die: command not found

or sheltie

[root@admin]# sudo sheltie die
nsenter: failed to execute die: No such file or directory

fi
}

collect_logs_bottlerocket() {
echo "Fetching INSTANCE_ID"
readonly INSTANCE_ID=$(curl --max-time 10 --retry 5 http://169.254.169.254/latest/meta-data/instance-id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if IMDSv2 enforced with SCP for Org. (or IAM policy enforced)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

related topic,
#938 (comment)

if [ 0 -eq $? ]; then # Check if previous command was successful.
echo "${INSTANCE_ID}"
else
warning "Unable to find EC2 Instance Id. Skipped Instance Id."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here. bash: warning: command not found

fi

if [ ! -d "${BOTTLEROCKET_ROOTFS}/tmp/ekslogs" ]; then
echo "Creating ekslogs directory"
mkdir ${BOTTLEROCKET_ROOTFS}/tmp/ekslogs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering, why not execute with sudo sheltie mkdir /tmp/ekslogs?

fi

for utils in ${BOTTLEROCKET_UTILS[*]}; do
# If exit code of "command -v" not equal to 0, fail
if ! command -v "${utils}" >/dev/null 2>&1; then
echo -e "\nApplication \"${utils}\" is missing, will install \"${utils}\"."
sudo yum install -y "${utils}"
fi
done

cp ${BOTTLEROCKET_ROOTFS}/var/log/aws-routed-eni/* ${BOTTLEROCKET_ROOTFS}/tmp/ekslogs/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it could be simplified as follow,

[root@admin]# sudo sheltie mkdir /tmp/ekslogs

[root@admin]# sudo sheltie cp --recursive --verbose /var/log/aws-routed-eni/ /tmp/ekslogs/

[root@admin]# sudo sheltie ls -l /tmp/ekslogs/aws-routed-eni
total 64
-rw-r--r--. 1 root root 62674 Aug  6 00:48 ipamd.log

sudo sheltie logdog
sudo sheltie cp /var/log/support/bottlerocket-logs.tar.gz /tmp/ekslogs
tar -cvzf "${LOG_DIR}"/eks_"${INSTANCE_ID}"_"${CURRENT_TIME}"_"${PROGRAM_VERSION}".tar.gz "${BOTTLEROCKET_ROOTFS}"/tmp/ekslogs > /dev/null 2>&1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, it should be an optional action as we already have bottlerocket-logs.tar.gz under /tmp/ekslogs?
... and we could even more cleaner, with no tar installed and no need to declare BOTTLEROCKET_ROOTFS.

}

finished() {
cleanup
echo -e "\n\tDone... your bundled logs are located in ${LOG_DIR}/eks_${INSTANCE_ID}_${CURRENT_TIME}_${PROGRAM_VERSION}.tar.gz\n"
}

cleanup() {
# bottlerocket AMI
if [ -d "/.bottlerocket/" ]; then
rm --recursive --force {BOTTLEROCKET_ROOTFS}/tmp/ekslogs > /dev/null 2>&1
else
echo "Unable to Cleanup as {COLLECT_DIR} variable is modified. Please cleanup manually!"
fi
}
Comment on lines +62 to +69
Copy link
Member

@guessi guessi Aug 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about...

cleanup() {
  # bottlerocket AMI
  sudo sheltie rm --recursive --force --verbose /tmp/ekslogs # verbose might be useful for debug purpose?
}

Why we need a else... block here?
also, seeing no COLLECT_DIR defined and maybe need a \$ before {COLLECT_DIR}.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think PR was work in progress, we are anyway not merging in this repo.


echo "Detected Bottlerocket AMI"
is_diskfull
collect_logs_bottlerocket
finished