forked from eldada/kubernetes-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getPodsLoad.sh
executable file
·105 lines (83 loc) · 2.42 KB
/
getPodsLoad.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
#!/bin/bash
# UNCOMMENT this line to enable debugging
# set -xv
# Get formatted results of the pods underlying node's load average (using cat /proc/loadavg)
NAMESPACE=default
INCLUDE_FILTER=.*
SCRIPT_NAME=$0
######### Functions #########
errorExit () {
echo -e "\nERROR: $1\n"
exit 1
}
usage () {
cat << END_USAGE
${SCRIPT_NAME} - Get formatted results of the pods underlying node's load average (using cat /proc/loadavg)
Usage: ${SCRIPT_NAME} <options>
-n | --namespace <name> : Namespace to use. Default: default
-i | --include <string> : Include only pods with this string in their name
-h | --help : Show this usage
Examples:
========
# Get load form pods in namespace bar:
${SCRIPT_NAME} --namespace bar
# Get only specific pods in namespace
${SCRIPT_NAME} --namespace bar --include artifactory
END_USAGE
exit 1
}
# Process command line options. See usage above for supported options
processOptions () {
while [[ $# -gt 0 ]]; do
case "$1" in
-n | --namespace)
NAMESPACE="$2"
shift 2
;;
-i | --include)
INCLUDE_FILTER="$2"
shift 2
;;
-h | --help)
usage
exit 0
;;
*)
usage
;;
esac
done
if [[ -z "${INCLUDE_FILTER}" ]]; then
INCLUDE_FILTER=.*
fi
}
# Test connection and that there are pods in the namespace
testConnection () {
kubectl get ns "${NAMESPACE}" > /dev/null || errorExit "Namespace ${NAMESPACE} does not exist"
[[ $(kubectl get pods -n "${NAMESPACE}" 2> /dev/null| wc -l | tr -d ' ') == '0' ]] && errorExit "Namespace ${NAMESPACE} has no running pods"
}
getPodsLoad () {
local load1
local load5
local load15
local dummy
local pods
# Print header
echo "Pod, Load 1, Load 5, Load 15"
# Get list of pods
pods=$(kubectl get pods -n "${NAMESPACE}" --no-headers -o=custom-columns=NAME:.metadata.name)
# Go over the pods and extract data
for p in $pods; do
if [[ ${p} =~ ${INCLUDE_FILTER} ]]; then
read -r load1 load5 load15 dummy <<< $(kubectl exec -n "${NAMESPACE}" "$p" -- sh -c "cat /proc/loadavg" 2> /dev/null)
echo "$p, $load1, $load5, $load15"
fi
done
}
main () {
processOptions "$@"
testConnection
getPodsLoad
}
######### Main #########
main "$@"