Skip to content

Commit ba7e574

Browse files
author
Federico Ceratto
committed
Add certificate monitor
1 parent 8137d5b commit ba7e574

File tree

4 files changed

+85
-2
lines changed

4 files changed

+85
-2
lines changed

ansible/roles/letsencrypt/tasks/main.yml

+31-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
---
2-
- name: Install Letsencrypt certbot
2+
- name: Install certbot and Python library
33
apt:
44
name:
55
- certbot
6-
- python-certbot-nginx
6+
- python3-prometheus-client
77
state: latest
88
update_cache: yes
99
cache_valid_time: '{{ apt_cache_valid_time }}'
@@ -51,4 +51,33 @@
5151
name: certbot.timer
5252
state: started
5353
enabled: yes
54+
55+
- name: Add certificate monitoring monitor_certs.py
56+
template:
57+
src: templates/monitor_certs.py
58+
dest: /etc/ooni/monitor_certs.py
59+
mode: 0755
60+
owner: root
61+
62+
- name: Install monitor_certs.service
63+
template:
64+
src: templates/monitor_certs.service
65+
dest: /etc/systemd/system/monitor_certs.service
66+
mode: 0755
67+
owner: root
68+
69+
- name: Add certificate monitoring monitor_certs.py
70+
template:
71+
src: templates/monitor_certs.timer
72+
dest: /etc/systemd/system/monitor_certs.timer
73+
mode: 0755
74+
owner: root
75+
76+
- name: enable timer for monitor_certs
77+
systemd:
78+
daemon_reload: yes
79+
name: monitor_certs.timer
80+
state: started
81+
enabled: yes
82+
5483
...
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
"""Monitor certbot certificates
5+
"""
6+
7+
# Compatible with Python3.7 - linted with Black
8+
9+
import subprocess
10+
from datetime import datetime, timezone
11+
12+
import prometheus_client as prom
13+
14+
NODEEXP_FN = "/run/nodeexp/monitor_certs.prom"
15+
16+
17+
def main():
18+
prom_reg = prom.CollectorRegistry()
19+
gauge = prom.Gauge("certificate_age", "", ["domain"], registry=prom_reg)
20+
out = subprocess.check_output(["certbot", "certificates"])
21+
out = out.decode("utf-8")
22+
for line in out.splitlines():
23+
if line.startswith(" Certificate Name: "):
24+
domain_name = line.split()[-1]
25+
26+
elif line.startswith(" Expiry Date: "):
27+
# Expiry Date: 2020-09-12 08:16:47+00:00 (VALID: 79 days)
28+
exp = line[17:42]
29+
exp = datetime.strptime(exp, "%Y-%m-%d %H:%M:%S%z")
30+
delta = exp - datetime.now(timezone.utc)
31+
s = int(delta.total_seconds())
32+
gauge.labels(domain_name).set(s)
33+
34+
prom.write_to_textfile(NODEEXP_FN, prom_reg)
35+
36+
37+
if __name__ == "__main__":
38+
main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[Unit]
2+
Description=Run monitor_certs.py, activated by monitor_certs.timer
3+
4+
[Service]
5+
Type=oneshot
6+
ExecStart=/etc/ooni/monitor_certs.py
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[Unit]
2+
Description=Run monitor_certs.py at the beginning of every hour
3+
4+
[Timer]
5+
OnCalendar=*-*-* *:00:00
6+
7+
[Install]
8+
WantedBy=timers.target
9+

0 commit comments

Comments
 (0)