-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathswarm_core_info.sh
126 lines (106 loc) · 3.96 KB
/
swarm_core_info.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
#!/bin/bash
set -e
# check to see DOCKER_HOST is set & set curl command to be able to re-use it and help readability of the script
if [ -n "${UCP_URL}" ]
then
CURL_CMD="curl -s -m 15 --cacert /data/ca.pem --key /data/key.pem --cert /data/cert.pem https://${UCP_URL}"
# check to see if the UCP endpoint is available
if [ "$(curl -s --cacert /data/ca.pem "https://${UCP_URL}/_ping")" != "OK" ]
then
# try without the cacert
if [ "$(curl -s "https://${UCP_URL}/_ping")" != "OK" ]
then
echo "ERROR: UCP unavailable/unhealthy at https://${UCP_URL}/_ping"
exit 1
else
# if this works, we need to not include the CA cert
CURL_CMD="curl -s -m 15 --key /data/key.pem --cert /data/cert.pem https://${UCP_URL}"
fi
fi
else
CURL_CMD="curl -s -m 5 --unix-socket /var/run/docker.sock http://v1.30"
# retrieve container runtime platform
PLATFORM_NAME="$(${CURL_CMD}/version | jq -r .Platform.Name)"
# check to see if the docker socket is available
if [ ! -S /var/run/docker.sock ]
then
echo "ERROR: Docker socket not found at /var/run/docker.sock"
exit 1
fi
# check to see if runtime is MCR
if [[ "${PLATFORM_NAME}" != *"Mirantis Container Runtime"* ]]
then
echo "ERROR: nodes is not an MCR node. Platform = ${PLATFORM_NAME}"
exit 1
else
echo "Node is an MCR node"
fi
fi
# shellcheck disable=SC2086
# check to see if the engine is Swarm/is a manager by trying to get a token
if [ "$(${CURL_CMD}/swarm | jq -r .message)" != "null" ]
then
echo "ERROR: Docker engine is not a Swarm manager"
exit 1
fi
pull_node_data() {
# output for type
if [ "${2}" = "linux" ] || [ "${2}" = "windows" ]
then
echo -e "Data for ${1} nodes running ${2}:"
else
echo -e "Data for ${1} nodes:"
fi
# add filter for managers or workers
if [ "${1}" = "manager" ] || [ "${1}" = "worker" ]
then
NODE_FILTER="?filters=%7B%22role%22%3A%7B%22${1}%22%3Atrue%7D%7D"
else
NODE_FILTER=""
fi
# see if we are looking for just linux or windows nodes
if [ "${2}" = "linux" ] || [ "${2}" = "windows" ]
then
# shellcheck disable=SC2086
# get the number of nano CPUs reported from Swarm on each node for the specified OS
nanoCPUs="$(for NODE in $(${CURL_CMD}/nodes${NODE_FILTER} | jq -r '.[]|select(.Description.Platform.OS|contains("'"${2}"'"))|.ID'); do ${CURL_CMD}/nodes/"${NODE}" | jq -r .Description.Resources.NanoCPUs; done)"
else
# shellcheck disable=SC2086
# get the number of nano CPUs reported from Swarm on each node
nanoCPUs="$(for NODE in $(${CURL_CMD}/nodes${NODE_FILTER} | jq -r '.[]|.ID'); do ${CURL_CMD}/nodes/"${NODE}" | jq -r .Description.Resources.NanoCPUs; done)"
fi
# convery nano CPUs to CPUs
CPUs=$(for i in ${nanoCPUs}; do echo "$((i/1000000000))"; done)
# get the sum of all CPU counts
ttlCPU="$(COUNT=0; TOTAL=0; for i in ${CPUs};do TOTAL=$(echo "${TOTAL}+${i}" | bc ); ((COUNT++)); done; echo "${TOTAL}")"
# count the number of nodes reported
NODE_COUNT=$(echo "${CPUs}" | wc -l)
# find the average CPU count
avgCPU="$(echo "scale=2; ${ttlCPU} / ${NODE_COUNT}" | bc)"
# determine the smallest # CPUs per node
minCPU="$(echo "${CPUs}" | sort -n | head -1)"
# determine the largest # CPUs per node
maxCPU="$(echo "${CPUs}" | sort -n | tail -n 1)"
# find the unique CPU node sizes
CPU_sizes="$(echo "${CPUs}" | sort -n | uniq)"
# report the quantity of nodes that match a given number of CPUs
for SIZE in ${CPU_sizes}; do echo -n "${SIZE} Core x "; echo "${CPUs}" | grep "${SIZE}" | sort -n | wc -l; done
# report the rest of the data
echo "
# Nodes - ${NODE_COUNT}
Ttl Core - ${ttlCPU}
Min Core - ${minCPU}
Max Core - ${maxCPU}
Avg Core - ${avgCPU}"
}
echo "========================"
pull_node_data all
echo "========================"
pull_node_data manager
echo "========================"
pull_node_data worker
echo "========================"
pull_node_data all linux
echo "========================"
pull_node_data all windows
echo "========================"