]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph-cm-ansible.git/commitdiff
checkcerts.py: actually fix "send email" 767/head
authorDan Mick <dan.mick@redhat.com>
Tue, 18 Feb 2025 23:21:00 +0000 (15:21 -0800)
committerDan Mick <dan.mick@redhat.com>
Tue, 18 Feb 2025 23:21:00 +0000 (15:21 -0800)
argparse can't do a nargs="*" optional arg *and* check for its
presence; add a separate arg -E to send the email, and keep -e as
an optional list of addressees.

Also add the full path and host where checkcerts.py is running.

Signed-off-by: Dan Mick <dan.mick@redhat.com>
tools/checkcerts.py

index 6d923e8270e24ecbca1936413ed0f34c2d076b20..b2b360ad0fc038a8767de9ba4fa74ecbc8ee14ba 100755 (executable)
@@ -52,7 +52,8 @@ DEFAULT_EMAIL = [
 def parse_args():
     ap = argparse.ArgumentParser()
     ap.add_argument('-q', '--quiet', action='store_true')
-    ap.add_argument('-e', '--email', nargs='*')
+    ap.add_argument('-E', '--send-email', action='store_true', help="send email with warnings")
+    ap.add_argument('-e', '--email', nargs='*', default=DEFAULT_EMAIL, help=f'list of addresses to send to (default: {DEFAULT_EMAIL})')
     ap.add_argument('-d', '--domains', nargs='*', default=DEFAULT_DOMAINS)
     return ap.parse_args()
 
@@ -70,7 +71,9 @@ To: %s
 Subject: %s
 
 %s
-""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
+
+Report from %s running on %s
+""" % (FROM, ", ".join(TO), SUBJECT, TEXT, os.path.realpath(sys.argv[0]), socket.gethostname())
 
     # send it 
     server = smtplib.SMTP('localhost')
@@ -97,7 +100,7 @@ def main():
             errstr = f'{domain} cert error: {e}'
 
         if not certerr:
-            expire = datetime.datetime.strptime(cert['notAfter'], 
+            expire = datetime.datetime.strptime(cert['notAfter'],
                 '%b %d %H:%M:%S %Y %Z')
             now = datetime.datetime.utcnow()
             left = expire - now
@@ -105,7 +108,7 @@ def main():
             errstr = f'{domain:30s} cert: {str(left).rsplit(".",1)[0]} left until it expires'
         if not args.quiet:
             print(errstr, file=sys.stderr)
-        if (certerr or (left < warn)) and args.email:
+        if (certerr or (left < warn)) and (args.send_email):
             subject = f'Certificate problem with {domain}'
             body = errstr
             email = args.email