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

Conversation

cgchinmay
Copy link

@cgchinmay cgchinmay commented Feb 23, 2022

Issue #, if available:
VPC CNI - aws/amazon-vpc-cni-k8s#1316

Description of changes:
Added logic to collect logs on bottlerocket AMI

sudo bash eks-log-collector.sh 
Detected Bottlerocket AMI
Fetching INSTANCE_ID
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    19  100    19    0     0  16754      0 --:--:-- --:--:-- --:--:-- 19000
i-028cb77de07f468b9
Running: exec containerd-config containerd --config /etc/containerd/config.toml config dump
Running: exec containerd-config-host containerd --config /etc/host-containerd/config.toml config dump
Running: exec df df -h
Running: exec df-inodes df -hi
Running: exec dmesg dmesg --color=never --nopager
Running: exec iptables-filter iptables -nvL -t filter
Running: exec iptables-nat iptables -nvL -t nat
Running: exec journalctl-boots journalctl --list-boots --no-pager
Running: exec journalctl.errors journalctl -p err -a --no-pager
Running: exec journalctl.log journalctl -a --no-pager
Running: exec proc-mounts cat /proc/mounts
Running: exec signpost signpost status
Running: exec wicked wicked show all
Running: file os-release /etc/os-release
Running: glob /var/log/kdump/*
Running: settings settings.json
Running: exec kube-status systemctl status kube* -l --no-pager
Running: file ipamd.log /var/log/aws-routed-eni/ipamd.log
Running: file plugin.log /var/log/aws-routed-eni/plugin.log
logs are at: /var/log/support/bottlerocket-logs.tar.gz

	Done... your bundled logs are located in /var/log/eks_i-028cb77de07f468b9_2022-03-10_1803-UTC_0.6.2.tar.gz

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Copy link
Contributor

@jayanthvn jayanthvn left a comment

Choose a reason for hiding this comment

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

lgtm

@cartermckinnon cartermckinnon added the enhancement New feature or request label Apr 14, 2022
@jayanthvn
Copy link
Contributor

@suket22 - Can you please help review this?

Copy link
Member

@suket22 suket22 left a comment

Choose a reason for hiding this comment

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

Could you remind me why we want to add this script to this GitHub repository and not the BR repo? I think it's probably on them to provide users an easy way to collect and export logs from the instance.

Comment on lines 627 to 635
if [ -d "/.bottlerocket/" ]; then
echo "Detected Bottlerocket AMI"
is_diskfull
collect_logs_bottlerocket
finished
else
collect
pack
finished
Copy link
Member

Choose a reason for hiding this comment

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

Could we create a dedicated log collector file for BR?
We currently separate them by OperatingSystem here so it might be helpful to create a dedicated file for BR rather than add if-else statements to the script meant for the AL2 AMI.

Also helps limit the blast radius of anything going wrong.

Copy link
Author

@cgchinmay cgchinmay Apr 27, 2022

Choose a reason for hiding this comment

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

Could you remind me why we want to add this script to this GitHub repository and not the BR repo? I think it's probably on them to provide users an easy way to collect and export logs from the instance.

We provide this script for collecting cni logs to our customers. As of now, we couldn't use this script on Bottlerocket AMI, so needed this logic. Will check with @jayanthvn, if we can move this to BR repo instead

Copy link
Author

@cgchinmay cgchinmay Apr 27, 2022

Choose a reason for hiding this comment

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

Could we create a dedicated log collector file for BR? We currently separate them by OperatingSystem here so it might be helpful to create a dedicated file for BR rather than add if-else statements to the script meant for the AL2 AMI.

Also helps limit the blast radius of anything going wrong.

Thought having a same file would make the change agnostic to Customer's AMI. But agreed, having separate file would simplify the if-else logic. We will also need to update our documentation so that customers know which script to run for log collection on BR AMI. Will update this branch.

@cgchinmay cgchinmay closed this Apr 27, 2022
@cgchinmay cgchinmay reopened this Apr 27, 2022
Copy link
Member

@guessi guessi left a comment

Choose a reason for hiding this comment

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

Looking forward for Bottlerocket OS support, and I found this PR.
I've leave some comments after quick glance and I hope that help.

@@ -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


# 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

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


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 [ ! -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
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

cp ${BOTTLEROCKET_ROOTFS}/var/log/aws-routed-eni/* ${BOTTLEROCKET_ROOTFS}/tmp/ekslogs/
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.

Comment on lines +62 to +69
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
}
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.

@cgchinmay
Copy link
Author

cgchinmay commented Aug 6, 2022

Looking forward for Bottlerocket OS support, and I found this PR. I've leave some comments after quick glance and I hope that help.

@guessi thanks for your comments. Actually the changes will be included in Bottlerocket AMI itself over here: bottlerocket-os/bottlerocket#2137 and this PR will be used only as a reference. Converted it to draft to avoid confusion

@cgchinmay cgchinmay marked this pull request as draft August 6, 2022 01:30
@guessi
Copy link
Member

guessi commented Aug 6, 2022

@cgchinmay well noted~ thanks for your speedy response.

@cartermckinnon
Copy link
Member

Closing this as it's been addressed in BR's repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants