| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- #!/usr/bin/env python3
- import datetime
- import socket
- import ssl
- import requests
- def get_cert(domain):
- context = ssl.create_default_context()
- conn = context.wrap_socket(
- socket.socket(socket.AF_INET),
- server_hostname=domain,
- )
- # 3 second timeout because Lambda has runtime limitations
- conn.settimeout(3.0)
- conn.connect((domain, 443))
- return conn.getpeercert()
- def ssl_alt_names(ssl_info):
- return [item[1] for item in ssl_info['subjectAltName']]
- def ssl_valid_time_remaining(ssl_info):
- ssl_date_fmt = r'%b %d %H:%M:%S %Y %Z'
- expires = datetime.datetime.strptime(ssl_info['notAfter'], ssl_date_fmt)
- return expires - datetime.datetime.utcnow()
- def check_domain(domain):
- cert = get_cert(domain)
- t_remaining = ssl_valid_time_remaining(cert)
- return t_remaining.days + t_remaining.seconds / 86400
- def post_data(domain, time):
- db = 'scripts_data'
- INFLUXDB_HOST = "http://grafana.labs:8086/write?db=%s" % db
- data = 'days_remaining,hostname=%s value=%.2f' % (domain, time)
- headers = {'Content-Type': 'application/octet-stream'}
- r = requests.post(INFLUXDB_HOST, data=data, headers=headers)
- if r.status_code < 200 or r.status_code > 300:
- print("When posting %s I got status_code %d" % (domain, r.status_code))
- hosts = [ 'labs.davidventura.com.ar', 'especificosba.com.ar', 'casa.especificosba.com.ar' ]
- data = []
- for h in hosts:
- try:
- t = check_domain(h)
- data.append((h,t))
- except Exception as e:
- print("Exception while contacting %s" % h)
- print(e)
- for d in data:
- post_data(*d)
|