DEV Community

Cover image for cert-monitor: TLS certificate alerts on Telegram and email, once per stage
Abdeldjalil Chougui
Abdeldjalil Chougui

Posted on Originally published at abdeldjalilchougui.biz

cert-monitor: TLS certificate alerts on Telegram and email, once per stage

An expired TLS certificate is an outage with a date on it. Everyone knows months ahead when it will happen, and it still happens: the renewal job failed quietly, a certificate was installed by hand on one server and forgotten, or the mail server on port 993 was never part of the automation.

Expiry is also not the only way a certificate breaks. A server can be configured without its intermediate certificate and work in some clients while failing in others. A certificate can be valid but issued for a different hostname. An old endpoint can still negotiate TLS 1.0. From the outside, each of these looks like the same vague "handshake failed".

cert-monitor watches the TLS certificates of your hosts and tells you, once per stage, before something breaks. It is a single Go binary with a status page and alerts on Telegram and by email. The README describes the audience as small teams and self-hosters with a handful to a few hundred endpoints, such as web servers, mail servers on 993/465 or internal APIs, who want a heads-up rather than a full observability stack.

What it does

It runs four kinds of checks on every target.

Expiry. It alerts at 30, 14, 7 and 1 days left, and again when the certificate has expired. The thresholds are configurable. Each alert is sent once per threshold per certificate. Renewing the certificate gives it a new fingerprint, and the sequence starts over.

Chain of trust. An unknown issuer, a self-signed certificate, a missing intermediate, or an expired intermediate.

Hostname. The certificate must cover the name you connect with (SNI).

Weak crypto. SHA-1 or MD5 signatures on the leaf or the intermediates, RSA keys under 2048 bits, ECDSA keys under 256 bits, and servers that negotiate TLS 1.0 or 1.1.

Flexible targets. A target is host, host:port or sni@host:port. The last form connects to one address while asking for another name, as in api.example.com@10.0.0.5:8443, which is useful for checking one backend behind a load balancer.

Detailed diagnosis. The tool dials with InsecureSkipVerify and then verifies by hand with x509.Verify, VerifyHostname and its own algorithm checks. That is why a broken certificate is reported in detail, such as "missing intermediate" or the list of names the certificate does cover, instead of failing with one handshake error. Eight hosts are checked in parallel.

Alerts that do not repeat. The state per target is the leaf fingerprint, the most urgent threshold already alerted, the set of non-expiry problems, and a streak of connection errors. An expiry alert fires when the days left reach a stage that has not been alerted for that certificate. Problems alert when their set changes, and a "resolved" message follows when it empties. Connection errors must happen twice in a row before you hear about them.

Alerts that are not lost. State only advances when at least one notifier delivered the message. A Telegram outage therefore means a retry on the next round, not a lost alert. The state file is saved atomically, with a temp file and a rename.

Status page. A server-rendered table sorted by urgency, plus JSON at /api/status, a POST /api/check endpoint, and /healthz.

How to use it

Install the binary with Go:

go install github.com/AbdeldjalilChougui/cert-monitor/cmd/cert-monitor@latest
Enter fullscreen mode Exit fullscreen mode

Or build it from a checkout and run a one-off check:

go build -o cert-monitor ./cmd/cert-monitor

# One-off check (exit code 0 ok, 1 warning, 2 critical):
./cert-monitor -check example.com mail.example.com:993 api.example.com@10.0.0.5:8443
Enter fullscreen mode Exit fullscreen mode

The exit code makes the one-off mode usable from a script or a cron job. The README shows this example output:

TARGET                        STATUS    DAYS   EXPIRES     DETAILS
github.com                    ok        43     2026-11-29
expired.badssl.com            critical  -4206  2015-04-12  certificate expired on 2015-04-12; chain: …
wrong.host.badssl.com         critical  72     2026-12-28  hostname: certificate is not valid for wrong.host.badssl.com (it covers *.badssl.com, badssl.com)
sha1-intermediate.badssl.com  critical  -2331  2020-05-30  …; weak signature: intermediate COMODO SSL CA is signed with SHA1-RSA
Enter fullscreen mode Exit fullscreen mode

To run it continuously, copy the example config and start it:

cp config.example.json config.json   # edit targets and notifiers
TELEGRAM_BOT_TOKEN=… TELEGRAM_CHAT_ID=… SMTP_PASSWORD=… ./cert-monitor -config config.json
open http://localhost:8090/
Enter fullscreen mode Exit fullscreen mode

The config is one JSON file:

{
  "listen": ":8090",
  "interval": "6h",
  "timeout": "10s",
  "state_file": "state.json",
  "thresholds": [30, 14, 7, 1],
  "targets": [
    "example.com",
    "mail.example.com:993",
    "api.example.com@203.0.113.10:8443"
  ],
  "telegram": {"bot_token": "${TELEGRAM_BOT_TOKEN}", "chat_id": "${TELEGRAM_CHAT_ID}"},
  "smtp": {
    "addr": "smtp.example.com:587",
    "username": "alerts@example.com",
    "password": "${SMTP_PASSWORD}",
    "from": "alerts@example.com",
    "to": ["ops@example.com"]
  }
}
Enter fullscreen mode Exit fullscreen mode

${VAR} references are expanded from the environment, so secrets stay out of the file. interval is the time between check rounds (6 hours by default), and timeout is the per-host dial and handshake timeout. Without state_file, the dedupe state lives in memory only, so set it if you do not want alerts to repeat after a restart. SMTP uses STARTTLS when the server offers it.

Things to know

  • No revocation checks. There are no OCSP or CRL checks, no Certificate Transparency lookups and no CAA checks.
  • Implicit TLS only. STARTTLS protocols, such as SMTP on 25/587 or IMAP on 143, are not supported as targets. Only implicit TLS ports (443, 465, 993, 8443 and so on) are.
  • No routing. Alerts go to every configured notifier. There is no per-target routing and there are no quiet hours.
  • The status page has no authentication. Bind it to localhost or put it behind a proxy if your host list is sensitive.
  • Weak-crypto checks have a scope. They cover signatures, key sizes and the protocol version, not individual cipher suites.
  • What was tested. go test -race ./... generates its own PKI at runtime, with a root, intermediates and leaves, and serves it with httptest TLS servers. The check tests cover a healthy certificate, warning and critical expiry, an expired certificate, a hostname mismatch, SNI picking the right certificate among several, self-signed and missing-intermediate chains, a 1024-bit RSA key, a SHA-1-signed leaf, a server capped at TLS 1.1, and a connection failure. The alert tests cover the full threshold sequence including renewal, problem sets and resolution, the two-strikes rule for errors, state persistence, the Telegram Bot API against a fake server, and SMTP against a minimal in-process SMTP server. The monitor tests cover one alert per stage across runs, retry after a failed delivery, and the status page and JSON.

Why I built it

For a small team, certificate monitoring usually goes wrong in one of two ways. Either nothing checks, and the first alert is a user saying the site shows a warning. Or a script checks every day and sends "expires in 12 days" every morning, until the message becomes background noise and nobody reads it on the day it matters.

The design answers the second problem as much as the first. One alert per stage per certificate means a message is rare enough to be read. Requiring two connection errors in a row avoids waking someone up for one dropped packet. Advancing state only after a successful delivery means a notifier outage does not swallow the one alert you needed.

The detailed diagnosis exists for the same reason. An alert that says "missing intermediate" or lists the names the certificate covers tells you what to fix. An alert that says "handshake failed" only tells you to start investigating.

And it is one binary with a JSON file, because the people it is for do not want to run a monitoring platform to watch a few dozen certificates.

Get the code

The source, the example config and the tests are on GitHub: https://github.com/AbdeldjalilChougui/cert-monitor. It is MIT licensed. Issues and pull requests are welcome.


Originally published at abdeldjalilchougui.biz.

Top comments (0)