Los certificados SSL (TLS) son la base del tráfico web seguro hoy en día, por lo que es crucial saber cuándo están a punto de caducar. No hay nada peor que recibir una alerta urgente del navegador sobre un certificado caducado, especialmente si se trata de tu propio sitio web. Por suerte, Zabbix ofrece un par de maneras de controlar esos certificados para que no te pillen desprevenido. Tanto si quieres usar sus plugins integrados como scripts personalizados, esta guía te ayudará a aclarar las cosas.
Antes, supervisar la caducidad de SSL era un poco engorroso: había que implementar scripts de consola y pasar los datos manualmente, lo cual era un fastidio si se tenían varios sitios. Ahora, con Zabbix Agent 2 y su plugin WebCertificate, todo es mucho más sencillo. Pero si te gusta usar scripts o necesitas comprobaciones personalizadas, también puedes hacerlo con OpenSSL y un poco de magia de bash. Decidir cuál usar depende de tu configuración, pero en cualquier caso, el objetivo es recibir alertas fiables y oportunas para que los certificados no se pierdan.
Cómo monitorear la expiración del certificado SSL en Zabbix
Comprobar la expiración del certificado SSL con el complemento WebCertificate en Zabbix
En primer lugar, Zabbix Agent 2 incluye un plugin de certificado web que puede extraer información SSL directamente. Esto es mucho más práctico que el antiguo método de script, especialmente si el agente ya está instalado y en ejecución. Básicamente, lo que se hace es verificar la versión del agente con:
$ zabbix_agent2 -V
Si es la versión 2.0+ e incluye el complemento, puede usar zabbix-get para recuperar los detalles del certificado directamente desde su configuración:
$ zabbix_get -s 127.0.0.1 -k web.certificate.get[woshub.com, 443]
Si todo está configurado correctamente, este comando debería generar datos JSON con información sobre la fecha de vencimiento del certificado, su origen, etc. Utilice la plantilla predefinida «Certificado de sitio web por el agente Zabbix 2» para facilitar la monitorización. A continuación, el plan de acción básico:
- Vaya a Configuración > Hosts y agregue un nuevo host para su sitio web.
- Crea un nuevo grupo anfitrión o elige uno existente: así todo estará ordenado.
- Adjunte la plantilla del certificado del sitio web mediante el agente Zabbix 2.
- Especifique la IP o DNS del host en la sección Interfaz (probablemente
127.0.0.1). - Jump over to the Macros tab; click on Inherited and host macros.
- Set
{$CERT. WEBSITE. HOSTNAME}to your domain (e.g., woshub.com). - Adjust
{$CERT. EXPIRY. WARN}macro if you want to be warned sooner than 7 days — maybe change it to 14 or 30, whatever floats your boat. - If your SSL runs on a different port than 443, specify it via
{$CERT. WEBSITE. PORT}. - Save everything, wait for Zabbix to start monitoring.
Now, Zabbix will keep an eye on that certificate and notify you when it’s about to go stale. Sort of a heads-up, not a surprise.
Monitor HTTPS Certificate Expiry with Custom Bash Script
This one’s for folks who wanna get down and dirty with scripts. Sometimes, you might not have the plugin or prefer something more customizable. The openssl command-line tool is the secret sauce here — can fetch cert expiration dates pretty reliably. So, create a little script like this:
#!/bin/bash data=`echo | openssl s_client -servername $1 -connect $1:${2:-443} 2>/dev/null | openssl x509 -noout -enddate | sed -e 's#notAfter=##'` ssldate=`date -d "${data}" '+%s'` nowdate=`date '+%s'` diff="$((${ssldate}-${nowdate}))" echo $((${diff}/24/3600))
Save it as /usr/lib/zabbix/externalscripts/sslcert_expiration.sh (yeah, you might need sudo to do that).Then, give it execution rights:
$ sudo chmod +x /usr/lib/zabbix/externalscripts/sslcert_expiration.sh
Test it out by running:
$ /usr/lib/zabbix/externalscripts/sslcert_expiration.sh woshub.com 443
If all’s good, it should show how many days are left before the cert expires. On one setup, it said 79 days — not bad. You just need to tell Zabbix to run this script via UserParameter. Edit your /etc/zabbix/zabbix_agent2.conf and add:
UserParameter=sslcertexpire[*], /usr/lib/zabbix/externalscripts/sslcert_expiration.sh $1 $2
After that, restart the agent because of course, Windows/Linux has to make it annoying:
$ sudo service zabbix-agent2 restart
Now you can test with:
$ zabbix_get -s 127.0.0.1 -p 10050 -k sslcertexpire[woshub.com, 443]
Zabbix will now receive days-left data for each monitored site. You can set up an item and trigger to warn before imminent expiration. Example trigger might be: “SSL for {$DOMAINNAME} about to expire in less than 20 days.”
- Name: Remaining SSL cert validity
{$DOMAINNAME} - Type: Zabbix Agent
- Key:
sslcertexpire[{$DOMAINNAME}, {$SSL_PORT}] - Trigger expression:
last(/CheckSSLCertExpiration/sslcertexpire[{$DOMAINNAME}, {$SSL_PORT}])<20
Don’t forget to add relevant macros to your host, like {$DOMAINNAME} (like woshub.com) and {$SSL_PORT} (usually 443).Then, assign your custom template with the trigger set up. This way, Zabbix will alert you if your cert is about to get expired, helping you avoid surprises.
Pretty much, that’s the gist — both methods work, just depends if you prefer built-in tools or scripting. Either way, you’ll get that warning before things go south.
Summary
- Use WebCertificate plugin with Zabbix Agent 2 for easier setup if the plugin's available.
- Alternatively, deploy your own bash script with OpenSSL for custom checks.
- Set macros and triggers in Zabbix to get timely alerts about expiring SSL certs.
Wrap-up
So yeah, monitoring SSL expiry with Zabbix isn’t too tough, especially if you pick the method that suits your environment. On some machines, the built-in plugin just works right out of the box once configured. With scripting, you get a little more control but might need to do some troubleshooting with paths or permissions. Either way, keeping tabs on those certs can save a lot of headache — no more last-minute panic about expired certificates. Fingers crossed, this gets one update making your life easier.