Skip to content

Instantly share code, notes, and snippets.

@se7enack
Created January 3, 2025 18:36
Show Gist options
  • Save se7enack/87173fac1e8d2897ca99fc9e6d3c2c96 to your computer and use it in GitHub Desktop.
Save se7enack/87173fac1e8d2897ca99fc9e6d3c2c96 to your computer and use it in GitHub Desktop.
k8s: check ssl expiration cronjob
apiVersion: batch/v1
kind: CronJob
metadata:
name: check-ssl-expiration
namespace: default
spec:
concurrencyPolicy: Allow
failedJobsHistoryLimit: 1
jobTemplate:
spec:
template:
spec:
containers:
- command:
- /bin/sh
- -c
- echo "today=\$(date +%Y-%m-%d);months=\"Jan Feb Mar Apr May Jun Jul
Aug Sep Oct Nov Dec\";x=\$(echo -n \$1' ';curl --insecure -vvI https://\${1}
2>&1 | awk 'BEGIN { cert=0 } /^\* SSL connection/ { cert=1 } /^\*/ {
if (cert) print }' | grep expire|awk -F \":\" '{print \$2 \"\" \$4}'
| awk '{print \$4\"-\"\$1\"-\"\$2}');month=\$(echo \$x | awk '{print
\$2}' | awk -F '-' '{print \$2}');nummonth=\$(string=\"\${months%\$month*}\"
&& echo -n \"\$((\${#string}/4 + 1))\");entry=\$(echo -n \$x | awk -v
ORS=\" \" '{print \$1}'; echo -n \$x | awk '{print \$2}' | awk -v ORS=\"\"
-F '-' '{print \$1\"-\"}'; echo -n \$nummonth;echo -n \$x | awk '{print
\$2}' | awk -F '-' '{print \"-\"\$3}');entrydate=\$(echo \$entry | awk
'{print \$2}');echo \"\$x\";daysleft=\$(echo -n \$(date -d \"\$entrydate\"
\+\%s 2>/dev/null) \$(date -d \"\$today\" \+\%s 2>/dev/null) | awk -v
ORS=\"\" '{print (\$1 - \$2) / 86400}');if [ \"\$daysleft\" -le \"$daythreshold\"
]; then curl -X POST --data '{\"text\":\"'\"The SSL certificate for
https://\${1} will expire in \${daysleft} days!\"'\"}' $slackurl; fi"
> /tmp/expires;chmod +x /tmp/expires;for DOMAIN in [ $check ]; do /tmp/expires
$DOMAIN | grep -vE '\]|\[' >> list.txt; done;cat list.txt | awk '{print
$2 " " $1}' | sort;rm -rf list.txt
env:
- name: check
value: google.com github.com ipv4ip.com
- name: daythreshold
value: "31"
- name: slackurl
value: https://hooks.slack.com/services/xxxxxxxxx/xxxxxxxxxxx/xxxxxxxxxxxxxxxxxxxxxxxx
image: curlimages/curl:8.2.0
imagePullPolicy: IfNotPresent
name: check-ssl-expiration
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: OnFailure
schedulerName: default-scheduler
terminationGracePeriodSeconds: 30
schedule: 00 13 * * 1-5
successfulJobsHistoryLimit: 1
suspend: false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment