Skip to content

Instantly share code, notes, and snippets.

@Richard-Barrett
Last active September 13, 2023 17:28
Show Gist options
  • Save Richard-Barrett/0253940a8b7510044b57dc5c6ff8505c to your computer and use it in GitHub Desktop.
Save Richard-Barrett/0253940a8b7510044b57dc5c6ff8505c to your computer and use it in GitHub Desktop.
Kubernetes Information Collection Script
#!/bin/bash
# =============================================
# Created by: Richard Barrett
# Date Created: 02/27/2020
# Purpose: Kube Auto Collector for Support Dump
# Company: Mirantis
# =============================================
# Documentation:
# ========================================================================================
# https://docs.mirantis.com/docker-enterprise/v3.1/dockeree-products/mke/user-access.html
# ========================================================================================
#set -e
# keep track of the last executed command
trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG
# echo an error message before exiting
trap 'echo "\"${last_command}\" command filed with exit code $?."' EXIT
# System Variables
# ================
DATE="$(date +'%Y%-m%d')"
AUTHTOKEN=$(curl -sk -d '{"username":"$USERNAME","password":"$PASSWORD"}' ${REQUEST_URL}/auth/login | jq -r .auth_token)
KUBECTL_PRESENT=$(kubctl version)
OS_VERSION=$(cat /etc/os-release)
MKE_CLUSTER_DIR="/tmp/mke_cluster/"
KUBERNETES_INFO_DIR="/tmp/mke_cluster/kubernetes_info/"
KUBERNETES_NODES_DIR="/tmp/mke_cluster/kubernetes_info/nodes"
KUBERNETES_NAMESPACES_DIR="/tmp/mke_cluster/kubernetes_info/namespaces"
KUBERNETES_DEPLOYMENTS_DIR="/tmp/mke_cluster/kubernetes_info/deployments"
KUBERNETES_PODS_DIR="/tmp/mke_cluster/kubernetes_info/pods"
KUBERNETES_INGRESS_DIR="/tmp/mke_cluster/kubernetes_info/ingress"
KUBERNETES_DAEMONSETS_DIR="/tmp/mke_cluster/kubernetes_info/daemonsets"
KUBERNETES_REPLICASETS_DIR="/tmp/mke_cluster/kubernetes_info/replicasets"
KUBERNETES_STATEFULSETS_DIR="/tmp/mke_cluster/kubernetes_info/statefulsets"
KUBERNETES_JOBS_DIR="/tmp/mke_cluster/kubernetes_info/jobs"
KUBERNETES_CRONJOBS_DIR="/tmp/mke_cluster/kubernetes_info/cronjobs"
KUBERNETES_INGRESS_DIR="/tmp/mke_cluster/kubernetes_info/ingress"
KUBERNETES_NETWORKPOLICY_DIR="/tmp/mke_cluster/kubernetes_info/network_policies"
KUBERNETES_PERSISTENTVOLUMES_DIR="/tmp/mke_cluster/kubernetes_info/persistent_volumes"
KUBERNETES_PERSISTENTVOLUME_CLAIMS_DIR="/tmp/mke_cluster/kubernetes_info/persistent_volumes_claims"
KUBERNETES_SERVICES_DIR="/tmp/mke_cluster/kubernetes_info/services"
KUBERNETES_SERVICEACCOUNTS_DIR="/tmp/mke_cluster/kubernetes_info/service_accounts"
KUBERNETES_ROLES_DIR="/tmp/mke_cluster/kubernetes_info/roles"
KUBERNETES_CLUSTERROLES_DIR="/tmp/mke_cluster/kubernetes_info/clusterroles"
KUBERNETES_CLUSTERROLE_BINDINGS_DIR="/tmp/mke_cluster/kubernetes_info/clusterrole_bindings"
# Make Cluster Kubernetes Info Directory
# ======================================
echo "MKE Version is $MKE_VERSION..."
# Make Directories in /tmp/mcpinfo
if [ -d "$MKE_CLUSTER_DIR" ]; then
# Take action if $MKE_CLUSTER_DIR exists
echo "The Direcotry $MKE_CLUSTER_DIR exists..."
echo "Check for Pre-Existing Kubernetes Info..."
if [ -d "$KUBERNETES_INFO_DIR" ]; then
echo "Pre-Existing Kubernetes Info $KUBERNETES_INFO_DIR Exists..."
else
echo "Pre-Existing Kubernetes Info $KUBERNETES_INFO_DIR Does Not Exist..."
echo "Making Kubernetes Info $KUBERNETES_INFO_DIR Sub-Directories..."
mkdir $KUBERNETES_NODES_DIR $KUBERNETES_NAMESPACES_DIR $KUBERNETES_DEPLOYMENTS_DIR $KUBERNETES_PODS_DIR
mkdir $KUBERNETES_INGRESS_DIR $KUBERNETES_DAEMONSETS_DIR $KUBERNETES_REPLICASETS_DIR $KUBERNETES_JOBS_DIR $KUBERNETES_STATEFULSETS_DIR
mkdir $KUBERNETES_CRONJOBS_DIR $KUBERNETES_INGRESS_DIR $KUBERNETES_NETWORKPOLICY_DIR $KUBERNETES_PERSISTENTVOLUMES_DIR
mkdir $KUBERNETES_PERSISTENTVOLUME_CLAIMS_DIR $KUBERNETES_SERVICES_DIR $KUBERNETES_SERVICEACCOUNTS_DIR
mkdir $KUBERNETES_ROLES_DIR $KUBERNETES_CLUSTERROLES_DIR $KUBERNETES_CLUSTERROLE_BINDINGS_DIR
fi
# Check for SOSReport Availability
#if [
# Prompt for Install and Congifuration of SOSReport Availability
# Run SOSReport
else
echo "Directory $MKE_CLUSTER_DIR does not exist..."
echo "Making $MKE_CLUSTER_DIR for Kubernetes Info..."
mkdir $MKE_CLUSTER_DIR
if [ -d "$KUBERNETES_INFO_DIR" ]; then
echo "Pre-Existing Kubernetes Info $KUBERNETES_INFO_DIR Exists..."
else
echo "Pre-Existing Kubernetes Info $KUBERNETES_INFO_DIR Does Not Exist..."
echo "Making Kubernetes Info $KUBERNETES_INFO_DIR Sub-Directories......"
mkdir $KUBERNETES_NODES_DIR $KUBERNETES_NAMESPACES_DIR $KUBERNETES_DEPLOYMENTS_DIR $KUBERNETES_PODS_DIR
mkdir $KUBERNETES_INGRESS_DIR $KUBERNETES_DAEMONSETS_DIR $KUBERNETES_REPLICASETS_DIR $KUBERNETES_JOBS_DIR $KUBERNETES_STATEFULSETS_DIR
mkdir $KUBERNETES_CRONJOBS_DIR $KUBERNETES_INGRESS_DIR $KUBERNETES_NETWORKPOLICY_DIR $KUBERNETES_PERSISTENTVOLUMES_DIR
mkdir $KUBERNETES_PERSISTENTVOLUME_CLAIMS_DIR $KUBERNETES_SERVICES_DIR $KUBERNETES_SERVICEACCOUNTS_DIR
mkdir $KUBERNETES_ROLES_DIR $KUBERNETES_CLUSTERROLES_DIR $KUBERNETES_CLUSTERROLE_BINDINGS_DIR
fi
# Check for SOSReport Availability
#if [
# Prompt for Install and Congifuration of SOSReport Availability
# Run SOSReport
# echo "Process complete Support Dump Collected and Stored in `$MCP_INFO_DIR`"
fi
# Grab Token
# =====================
echo "Would you like to collect kubernetes cluster information (yes/no)?
read ANS
echo "What is your MKE/UCP username?"
read USERNAME
echo "What is your MKE/UCP password?"
read PASSWORD
echo "What is the MKE/UCP IP Address?"
read MKE_IP
while true; do
read -p "Create Client Bundle Inside of Current Working Directory (yes/no)?" yn
case $yn in
[Yy]* ) echo "==================================="; \
echo " Collecting Info..."; \
echo "==================================="; \
# Get MKE Client Bundle and Create Bundle in Working Directory as hostname_bundle_date
# Make Bundle Directory
mkdir $(pwd)/$hostname_bundle_$DATE;
cd $(pwd)/$hostname_bundle_$DATE;
AUTHTOKEN=$(curl -sk -d '{"username":"$USERNAME","password":"$PASSWORD"}' https://$MKE_IP/auth/login | jq -r .auth_token);
curl -k -H "Authorization: Bearer $AUTHTOKEN" https://$MKE_IP/api/clientbundle -o bundle.zip;
unzip bundle.zip;
eval "$(<env.sh)";
echo "Confirming Docker MKE Containers are Running..."
docker ps -af state=running;
echo "Checking Kubernetes Cluster...";
echo "Checking Kubectl Client is installed...";
#if [ KUBECTL;
#fi;
echo "Checking Kuberentes Nodes...";
kubectl get nodes -o wide > $KUBERNETES_INFO_DIR/$KUBERNETES_NODES_DIR;
echo "Checking API Resources in kube-system...";
kubectl api-resources --verbs=list --namespaced -o name \
| xargs -n 1 kubectl get --show-kind --ignore-not-found -n kube-system -o=json > $KUBERNETES_INFO_DIR/kubernetes_api_resources.json;
echo "Checking All Kubernetes Resources in kube-system...";
kubectl get all -n kube-system -o=json > $KUBERNETES_INFO_DIR/all_kube_system.json;
echo "Checking All Available Namespaces...";
kubectl get ns -o=json > $KUBERNETES_NAMESPACES_DIR/namespaces.json;
echo "Checking All Kubernetes Deployments...";
kubectl get deployments --all-namespaces -o=json > $KUBERNETES_DEPLOYMENTS_DIR/deployments.json;
echo "Checking All Kubernetes Pods...";
kubectl get pods --all-namespaces -o=json > $KUBERNETES_PODS_DIR/pods.json;
echo "Checking Ingress Resources...";
kubectl get ingress --all-namespaces -o=json > $KUBERNETES_INGRESS_DIR/ingress.json
echo "Checking Daemonsets...";
kubectl get ds --all-namesapces -o=json > $KUBERNETES_DAEMONSETS_DIR/daemonsets.json;
echo "Checking ReplicaSets...";
kubectl get rs --all-namespaces -o=json > $KUBERNETES_REPLICASETS_DIR/replicasets.json;
echo "Checking Statefulsets..." ;
kubectl get ss --all-namespaces -o=json > $KUBERNETES_STATEFULSETS_DIR/statefulsets.json;
echo "Checking Jobs...";
kubectl get jobs --all-namespaces -o=json > $KUBERNETES_JOBS_DIR/jobs.json;
echo "Checking Cronjobs...";
kubectl get cronjobs --all-namespaces -o=json > $KUBERNETES_CRONJOBS/cronjobs.json;
echo "Checking Kubernetes Ingress...";
kubectl get ingress --all-namespaces -o=json > $KUBERNETES_INGRESS_DIR/ingress.json;
#echo "Checking Networkpolicy...";
#kubectl get networkpolicy
echo "Checking Persistent Volumes...";
kubectl get pv --all-namespaces -o=json > $KUBERNETES_PERSISTENTVOLUMES_DIR/persistentvolumes.json;
echo "Checking Persistent Volume Claims...";
kubectl get pvc --all-namespaces -o=josn > $KUBERNETES_PERSISTENTVOLUME_CLAIMS_DIR/persistentvolumeclaims.json;
echo "Checking Kubernetes Services...";
kubectl get svc --all-namespaces -o=json > $KUBERNETES_SERVICES_DIR/services.json;
echo "Checking Kubernetes Service Accounts..";
kubectl get serviceaccounts --all-namespaces > $KUBERNETES_SERVICEACCOUNTS_DIR/serviceaccounts.json;
echo "Checking Kubernetes Roles...";
kubectl get roles --all-namespaces -o=json > $KUBERNETES_ROLES_DIR/roles.json;
echo "Checking Kubernetes Clusterroles...";
kubectl get clusterroles --all-namespaces -o=json > $KUBERNETES_CLUSTERROLES_DIR/clusterroles.json;
echo "Checking Kubernetes Clusterrole Bindings...";
kubectl get clusterrolebindings --all-namespaces -o=json > $KUBERNETES_CLUSTERROLE_BINDINGS_DIR/clusterrolebindings.json;
break;; \
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment