Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sslcheck module: (remote) SSL certificate expiry time check #5365

Merged
merged 24 commits into from Mar 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions collectors/python.d.plugin/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ include smartd_log/Makefile.inc
include spigotmc/Makefile.inc
include springboot/Makefile.inc
include squid/Makefile.inc
include sslcheck/Makefile.inc
include tomcat/Makefile.inc
include tor/Makefile.inc
include traefik/Makefile.inc
Expand Down
12 changes: 12 additions & 0 deletions collectors/python.d.plugin/sslcheck/Makefile.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# SPDX-License-Identifier: GPL-3.0-or-later

# THIS IS NOT A COMPLETE Makefile
# IT IS INCLUDED BY ITS PARENT'S Makefile.am
# IT IS REQUIRED TO REFERENCE ALL FILES RELATIVE TO THE PARENT

# install these files
dist_python_DATA += sslcheck/sslcheck.chart.py
dist_pythonconfig_DATA += sslcheck/sslcheck.conf

# do not install these files, but include them in the distribution
dist_noinst_DATA += sslcheck/README.md sslcheck/Makefile.inc
21 changes: 21 additions & 0 deletions collectors/python.d.plugin/sslcheck/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# SSL certificate expiry check

Checks the time until a remote SSL certificate expires.

## Requirements

None

### configuration

```yaml
update_every : 60

example_org:
host: 'example.org'

ilyam8 marked this conversation as resolved.
Show resolved Hide resolved
my_site_org:
host: 'my-site.org'
days_until_expiration_warning: 10
days_until_expiration_critical: 1
```
112 changes: 112 additions & 0 deletions collectors/python.d.plugin/sslcheck/sslcheck.chart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# -*- coding: utf-8 -*-
# Description: simple ssl expiration check netdata python.d module
# Original Author: Peter Thurner (github.com/p-thurner)
# SPDX-License-Identifier: GPL-3.0-or-later

import datetime
ilyam8 marked this conversation as resolved.
Show resolved Hide resolved
import socket
import ssl

from bases.FrameworkServices.SimpleService import SimpleService


update_every = 60


ORDER = [
'time_until_expiration',
]

CHARTS = {
'time_until_expiration': {
'options': [
None,
'Time Until Certificate Expiration',
'seconds',
'certificate expiration time',
'sslcheck.time_until_expiration',
'line',
],
'lines': [
['time'],
],
'variables': [
['days_until_expiration_warning'],
['days_until_expiration_critical'],
],
},
}


SSL_DATE_FMT = r'%b %d %H:%M:%S %Y %Z'

DEFAULT_PORT = 443
DEFAULT_CONN_TIMEOUT = 3
DEFAULT_DAYS_UNTIL_WARN_LIMIT = 14
DEFAULT_DAYS_UNTIL_CRIT_LIMIT = 7


class Service(SimpleService):
def __init__(self, configuration=None, name=None):
SimpleService.__init__(self, configuration=configuration, name=name)
self.order = ORDER
self.definitions = CHARTS
self.host = configuration.get('host')
self.port = configuration.get('port', DEFAULT_PORT)
self.timeout = configuration.get('timeout', DEFAULT_CONN_TIMEOUT)
self.days_warn = configuration.get('days_until_expiration_warning', DEFAULT_DAYS_UNTIL_WARN_LIMIT)
self.days_crit = configuration.get('days_until_expiration_critical', DEFAULT_DAYS_UNTIL_CRIT_LIMIT)

def check(self):
if not self.host:
self.error('host parameter is mandatory, but it is not set')
return False
ilyam8 marked this conversation as resolved.
Show resolved Hide resolved

self.debug('run check : host {host}:{port}, update every {update}s, timeout {timeout}s'.format(
host=self.host, port=self.port, update=self.update_every, timeout=self.timeout))

return bool(self.get_data())

def get_data(self):
conn = create_ssl_conn(self.host, self.timeout)

try:
conn.connect((self.host, self.port))
except Exception as error:
self.error("error on connection to {0}:{1} : {2}".format(self.host, self.port, error))
return None

peer_cert = conn.getpeercert()
conn.close()

if peer_cert is None:
self.warning("no certificate was provided by {0}:{1}".format(self.host, self.port))
return None
elif not peer_cert:
self.warning("certificate was provided by {0}:{1}, but not validated".format(self.host, self.port))
return None

return {
'time': cert_expiration_seconds(peer_cert),
'days_until_expiration_warning': self.days_warn,
'days_until_expiration_critical': self.days_crit,
}


def create_ssl_conn(hostname, timeout):
context = ssl.create_default_context()
conn = context.wrap_socket(
socket.socket(socket.AF_INET),
server_hostname=hostname,
)
conn.settimeout(timeout)

return conn


def cert_expiration_seconds(cert):
expiration_date = datetime.datetime.strptime(cert['notAfter'], SSL_DATE_FMT)
current_date = datetime.datetime.utcnow()
delta = expiration_date - current_date

return ((delta.days * 86400 + delta.seconds) * 10 ** 6 + delta.microseconds) / 10 ** 6
76 changes: 76 additions & 0 deletions collectors/python.d.plugin/sslcheck/sslcheck.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# netdata python.d.plugin configuration for sslcheck
#
# This file is in YaML format. Generally the format is:
#
# name: value
#
# There are 2 sections:
# - global variables
# - one or more JOBS
#
# JOBS allow you to collect values from multiple sources.
# Each source will have its own set of charts.
#
# JOB parameters have to be indented (using spaces only, example below).

# ----------------------------------------------------------------------
# Global Variables
# These variables set the defaults for all JOBs, however each JOB
# may define its own, overriding the defaults.

# update_every sets the default data collection frequency.
# If unset, the python.d.plugin default is used.
# update_every: 1

# priority controls the order of charts at the netdata dashboard.
# Lower numbers move the charts towards the top of the page.
# If unset, the default for python.d.plugin is used.
# priority: 60000

# penalty indicates whether to apply penalty to update_every in case of failures.
# Penalty will increase every 5 failed updates in a row. Maximum penalty is 10 minutes.
# penalty: yes

# autodetection_retry sets the job re-check interval in seconds.
# The job is not deleted if check fails.
# Attempts to start the job are made once every autodetection_retry.
# This feature is disabled by default.
# autodetection_retry: 0

# ----------------------------------------------------------------------
# JOBS (data collection sources)
#
# The default JOBS share the same *name*. JOBS with the same name
# are mutually exclusive. Only one of them will be allowed running at
# any time. This allows autodetection to try several alternatives and
# pick the one that works.
#
# Any number of jobs is supported.
#
# All python.d.plugin JOBS (for all its modules) support a set of
# predefined parameters. These are:
#
# job_name:
# name: myname # the JOB's name as it will appear at the
# # dashboard (by default is the job_name)
# # JOBs sharing a name are mutually exclusive
# update_every: 1 # the JOB's data collection frequency
# priority: 60000 # the JOB's order on the dashboard
# penalty: yes # the JOB's penalty
# autodetection_retry: 0 # the JOB's re-check interval in seconds
#
# Additionally to the above, sslcheck also supports the following:
#
# host: 'host' # [required] the remote host address in either IPv4, IPv6 or as DNS name
# port: 443 # [optional] the port number to check. Specify an integer, not service name. Default is 443.
# timeout: 3 # [optional] the socket timeout when connecting
# days_until_expiration_warning: 14 # [optional] days before the alarm status is warning. Default is 14.
# days_until_expiration_critical: 7 # [optional] days before the alarm status is critical. Default is 7.
#
# ----------------------------------------------------------------------
# AUTO-DETECTION JOBS
# only one of them will run (they have the same name)

ilyam8 marked this conversation as resolved.
Show resolved Hide resolved
# example_org:
# host : 'example.org'
# days_until_expiration_warning: 10
1 change: 1 addition & 0 deletions health/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ dist_healthconfig_DATA = \
health.d/retroshare.conf \
health.d/softnet.conf \
health.d/squid.conf \
health.d/sslcheck.conf \
health.d/stiebeleltron.conf \
health.d/swap.conf \
health.d/tcp_conn.conf \
Expand Down
10 changes: 10 additions & 0 deletions health/health.d/sslcheck.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

template: sslcheck_days_until_expiration
on: sslcheck.time_until_expiration
calc: $time
units: seconds
every: 60s
warn: $this < $days_until_expiration_warning*24*60*60
ilyam8 marked this conversation as resolved.
Show resolved Hide resolved
crit: $this < $days_until_expiration_critical*24*60*60
info: certificate time until expiration
to: webmaster