certwatch.sh

Fri, 01 May 2020 15:27:45 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Fri, 01 May 2020 15:27:45 +0100
changeset 0
e6c11dadd985
permissions
-rwxr-xr-x

Initial commit

0
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 #!/bin/bash
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 . /etc/certwatch/config.sh
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 PASS=1
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 check_domain() {
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 DOMAIN="$1"
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 shift;
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 DAYS=14
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 CRT=$(echo "" | openssl s_client $@ 2>/dev/null);
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 if [[ $? != 0 ]]; then
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 PASS=0
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 send_notification "Check failed for $DOMAIN" "$CRT";
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 return;
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 fi
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 if (echo "$CRT" | openssl x509 -noout -checkend $(($DAYS*3600*24)) >/dev/null 2>&1); then
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 return;
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 fi
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 MSG=$(echo "$CRT" | openssl x509 -noout -text|egrep 'CN|DNS|Not After' 2>/dev/null)
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 PASS=0
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 send_notification "Certificate expiry: $DOMAIN" "$MSG"
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 }
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 check_https() {
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 check_domain "$1 HTTPS" -servername $1 -connect $1:443
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 }
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 check_xmpp() {
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 # need OpenSSL 1.1 for -xmpphost :(
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 check_domain "$1 XMPP" -connect $1:5222 -starttls xmpp
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 }
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 check_xmpps2s() {
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 # need OpenSSL 1.1 for -xmpphost :(
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 check_domain "$1 XMPP" -connect $1:5269 -starttls xmpp
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 }
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 check_smtp() {
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 check_domain "$1 SMTP" -connect $1:25 -starttls smtp
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 }
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 check_imap() {
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 check_domain "$1 IMAP" -connect $1:993
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 }
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 . /etc/certwatch/checks.sh
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 if [[ "$PASS" != 1 ]]; then
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 exit 2;
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 fi
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53
e6c11dadd985 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 exit 0;

mercurial