Skip to content

Commit 71b074a

Browse files
authored
Merge pull request #35 from MoBlockbuster/APICRT-CHECK_OPS-2971
add MODE: apicert to check the expiration date
2 parents 49b5dc2 + 3fa0837 commit 71b074a

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

check_kubernetes.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,19 @@ usage() {
2828
- Pod restart count in pods mode; default is 30
2929
- Job failed count in jobs mode; default is 1
3030
- Pvc storage utilization; default is 80%
31+
- APICERT expiration days for apicert mode; default is 30
3132
-c CRIT Critical threshold for
3233
- Pod restart count (in pods mode); default is 150
3334
- Unbound Persistent Volumes in unboundpvs mode; default is 5
3435
- Job failed count in jobs mode; default is 2
3536
- Pvc storage utilization; default is 90%
37+
- APICERT expiration days for apicert mode; default is 15
3638
-M EXIT_CODE Exit code when resource is missing; default is 2 (CRITICAL)
3739
-h Show this help and exit
3840
3941
Modes are:
4042
apiserver Not for kubectl, should be used for each apiserver independently
43+
apicert Check the apicert expiration date
4144
nodes Check for active nodes
4245
daemonsets Check for daemonsets readiness
4346
deployments Check for deployments availability
@@ -150,6 +153,37 @@ mode_apiserver() {
150153
fi
151154
}
152155

156+
mode_apicert() {
157+
if [ -z "$APISERVER" ]; then
158+
die "Apiserver URL should be defined in this mode"
159+
fi
160+
WARN=${WARN:-30}
161+
CRIT=${CRIT:-15}
162+
APICERT=$(echo "$APISERVER" | awk -F "//" '{ print $2 }' | awk -F ":" '{ print $1 }')
163+
APIPORT=$(echo "$APISERVER" | awk -F "//" '{ print $2 }' | awk -F ":" '{ print $2 }')
164+
APIPORT=${APIPORT:=443}
165+
timeout "$TIMEOUT" bash -c "</dev/tcp/$APICERT/$APIPORT" &>/dev/null
166+
if [ $? -ne 0 ]; then
167+
echo "APICERT is in UNKNOWN"
168+
exit 3
169+
fi
170+
APICERTDATE=$(echo | openssl s_client -connect "$APICERT":"$APIPORT" 2>/dev/null | openssl x509 -noout -dates | grep notAfter | sed -e 's#notAfter=##')
171+
a=$(date -d "$APICERTDATE" +%s)
172+
b=$(date +%s)
173+
c=$((a-b))
174+
d=$((c/3600/24))
175+
echo "APICERT expires in $d days"
176+
if [ "$d" -gt "$WARN" ] && [ "$d" -gt "$CRIT" ]; then
177+
echo "APICERT is OK"
178+
elif [ "$d" -le "$WARN" ] && [ $d -gt "$CRIT" ]; then
179+
echo "APICERT is in WARN"
180+
EXITCODE=1
181+
elif [ "$d" -le "$CRIT" ]; then
182+
echo "APICERT is in CRIT"
183+
EXITCODE=2
184+
fi
185+
}
186+
153187
mode_nodes() {
154188
data="$(getJSON "api/v1/nodes")"
155189
[ $? -gt 0 ] && die "$data"
@@ -723,6 +757,7 @@ mode_jobs() {
723757

724758
case "$MODE" in
725759
(apiserver) mode_apiserver ;;
760+
(apicert) mode_apicert ;;
726761
(daemonsets) mode_daemonsets ;;
727762
(deployments) mode_deployments ;;
728763
(nodes) mode_nodes ;;

0 commit comments

Comments
 (0)