{"id":9357,"date":"2024-03-21T10:07:55","date_gmt":"2024-03-21T10:07:55","guid":{"rendered":"https:\/\/cheapwindowsvps.com\/blog\/how-to-monitor-website-ssl-tls-certificate-expiration-using-zabbix\/"},"modified":"2025-01-20T10:15:36","modified_gmt":"2025-01-20T10:15:36","slug":"how-to-monitor-website-ssl-tls-certificate-expiration-using-zabbix","status":"publish","type":"post","link":"https:\/\/cheapwindowsvps.com\/blog\/how-to-monitor-website-ssl-tls-certificate-expiration-using-zabbix\/","title":{"rendered":"How to Monitor Website SSL\/TLS Certificate Expiration using Zabbix"},"content":{"rendered":"<p><p>SSL (TLS) certificates play a pivotal role in today&#8217;s infrastructure. Hence, it&#8217;s incumbent upon administrators to make sure they don&#8217;t expire and are timely renewed. This blog post will guide you on how to employ Zabbix for monitoring your websites against expiring SSL certificates.<\/p>\n<\/p>\n<p><p>No longer required in the latest versions of Zabbix are console scripts to monitor SSL certificate expiration, which used to be passed to Zabbix through the <strong>UserParameter<\/strong>. Zabbix Agent 2 now enables inspection of certificate details using the built-in <strong>WebCertificate plugin<\/strong>. This post will delve into the two methods for monitoring SSL certificate expiration on Zabbix.<\/p>\n<\/p>\n<p><h2>Monitoring SSL Certificate Expiry Using WebCertificate Plugin on Zabbix<\/h2>\n<\/p>\n<p><p>Zabbix Agent 2 includes an in-built WebCertificate plugin that enables you to verify the information of a website certificate. It is advisable to confirm that this agent version is installed on the host:<\/p>\n<\/p>\n<p><p><code>$ zabbix_agent2 -V<\/code><\/p>\n<\/p>\n<p><p>The WebCertificate plugin comes in handy when retrieving information about the certificate of a site directly from the console using the zabbix-get command:<\/p>\n<\/p>\n<p><p><code>$ zabbix_get -s 127.0.0.1 -k web.certificate.get[woshub.com,443]<\/code><\/p>\n<\/p>\n<p><p>The command should return a JSON object that contains the attributes of the certificate for the specified Web site.<\/p>\n<\/p>\n<p><p>A built-in template \u201cWebsite certificate by Zabbix agent 2\u201d is available in Zabbix. Use it to check the TLS\/SSL certificate expiration date.<\/p>\n<\/p>\n<ol>\n<li>Go to Configuration -&gt; Hosts and add a new host with the website name;<\/li>\n<li>Create a new host group or select an existing one;<\/li>\n<li>Assign the template <strong>Website certificate by Zabbix agent 2<\/strong>;<\/li>\n<li>Specify the IP address or name of the Zabbix agent in the <strong>Interface<\/strong> section (in our example, <code>127.0.0.1<\/code>);<\/li>\n<li>Then go to the <strong>Macros<\/strong> tab and click <strong>Inherited and host macros<\/strong>;<\/li>\n<li>In the <code>{$CERT.WEBSITE.HOSTNAME}<\/code> macro, specify the DNS name of the Web site whose certificate you want to check;<\/li>\n<li>By default, a warning that an SSL Certificate is about to expire will be displayed 7 days before the expiration date. You can increase this value with a macro <code>{$CERT.EXPIRY.WARN}<\/code> ;<\/li>\n<li>If the website uses a TLS\/SSL port other than 443, you can specify it using a macro <code>{$CERT.WEBSITE.PORT}<\/code> ;<\/li>\n<li>Save settings.<\/li>\n<\/ol>\n<p><p>Zabbix now warns you when a site\u2019s certificate is about to expire.<\/p>\n<\/p>\n<p><h2>Monitor HTTPS Certificate Expiry with Script in Zabbix<\/h2>\n<\/p>\n<p><p>You can use external scripts to retrieve certificate expiry information in previous versions of Zabbix. The openssl command line tool allows you to extract website certificate information.<\/p>\n<\/p>\n<p><p>Create a bash script file <strong>\/usr\/lib\/zabbix\/externalscripts\/sslcert_expiration.sh<\/strong> with the following code:<\/p>\n<\/p>\n<p><pre><\/p><p>#!\/bin\/bash<\/p><p>data=`echo | openssl s_client -servername $1 -connect $1:${2:-443} 2&gt;\/dev\/null | openssl x509 -noout -enddate | sed -e 's#notAfter=##'`<\/p><p>ssldate=`date -d \"${data}\" '+%s'`<\/p><p>nowdate=`date '+%s'`<\/p><p>diff=\"$((${ssldate}-${nowdate}))\"<\/p><p>echo $((${diff}\/24\/3600))<\/p><p><\/pre>\n<\/p>\n<p><p>Allow the script to run:<\/p>\n<\/p>\n<p><p><code>$ sudo chmod +x \/usr\/lib\/zabbix\/externalscripts\/sslcert_expiration.sh<\/code><\/p>\n<\/p>\n<p><p>This script returns the number of days left until the certificate expires. Check that the script works correctly.<\/p>\n<\/p>\n<p><p><code>$ \/usr\/lib\/zabbix\/externalscripts\/sslcert_expiration.sh woshub.com 443<\/code><\/p>\n<\/p>\n<p><p>The script returned that the site\u2019s certificate is valid for the next 79 days.<\/p>\n<\/p>\n<p><p>It&#8217;s necessary to grant permission to the Zabbix Agent to execute this bespoke bash script via the <strong>UserParameter<\/strong> parameter.<\/p>\n<p><code>rel=\"nofollow\" target=\"_blank\"&gt;$ sudo mcedit \/etc\/zabbix\/zabbix_agent2.conf<\/code><\/p>\n<p>Include:<\/p>\n<pre>UserParameter=sslcertexpire[*],\/usr\/lib\/zabbix\/externalscripts\/sslcert_expiration.sh $1 $2<\/pre>\n<\/p>\n<p><div>Using a script in UserParameter, you can also <a href=\"https:\/\/woshub.com\/monitor-domain-name-expiration-zabbix\/\" rel=\"nofollow noopener\" target=\"_blank\">check the expiration date of a domain in Zabbix<\/a>.<\/div>\n<p>Restart the agent:<\/p>\n<p><code>$ sudo service zabbix-agent2 restart<\/code><\/p>\n<p>Check that the Zabbix agent can receive data via the new parameter. You can use the <strong>zabbix-get<\/strong> command to test the agent\u2019s response:<\/p>\n<\/p>\n<p><div>  <ins><\/ins> <\/div>\n<p><code>$ zabbix_get -s 127.0.0.1 -p 10050 -k sslcertexpire[woshub.com,443]<\/code><\/p>\n<p>Now add a new template <strong>CheckSSLCertExpiration<\/strong> to monitor the expiration of HTTPS certificates in Zabbix.<\/p>\n<p>In the Items tab, add a parameter<\/p>\n<\/p>\n<p><p>Name: Remaining SSL cert validity <kbd>{$DOMAINNAME}<\/kbd><\/p>\n<\/p>\n<p><p>Type: Zabbix Agent<\/p>\n<\/p>\n<p><p>Key: <kbd>sslcertexpire[{$DOMAINNAME},{$SSL_PORT}]<\/kbd><\/p>\n<\/p>\n<p><p>Type of information: Numeric (unsigned)<\/p>\n<\/p>\n<p><p>Update Interval: 1d<\/p>\n<\/p>\n<p><p>History: 90d<\/p>\n<\/p>\n<p><p>Trend: 365d<\/p>\n<\/p>\n<p><p>Then add a new trigger:<\/p>\n<\/p>\n<p><p>Name: The SSL certificate for <kbd>{$DOMAINNAME}<\/kbd> is about to expire<\/p>\n<\/p>\n<p><p>Expression: <kbd>last(\/CheckSSLCertExpiration\/sslcertexpire[{$DOMAINNAME},{$SSL_PORT}])&lt;20<\/kbd><\/p>\n<\/p>\n<p><p>Severity: High<\/p>\n<\/p>\n<p><p>This trigger will generate an alert if there are less than 20 days left before the SSL certificate expires.<\/p>\n<\/p>\n<p><p>You can include a recovery trigger parameter:<\/p>\n<\/p>\n<p><p><code>Recovery expression: last(\/CheckSSLCertExpiration\/sslcertexpire[{$DOMAINNAME},{$SSL_PORT}])&gt;20<\/code><\/p>\n<\/p>\n<p><p>Then, introduce a new host to Zabbix for your website featuring an <strong>Agent<\/strong> interface type.<\/p>\n<\/p>\n<p><p>Under the <strong>Macros<\/strong> section, integrate two parameters: the server name as well as the port at which the SSL certificate inspection is required:<\/p>\n<\/p>\n<ul>\n<li>Macro: <code>{$DOMAINNAME}<\/code><\/li>\n<li>Value: <code>woshub.com<\/code><\/li>\n<\/ul>\n<p>and<\/p>\n<ul>\n<li>Macro: <code>{$SSL_PORT}<\/code><\/li>\n<li>Value: <code>443<\/code><\/li>\n<\/ul>\n<p>Assign the previously created CheckSSLCertExpiration template to the host.<\/p>\n<\/p>\n<p><p>Now check the <strong>Latest Data<\/strong>. Zabbix shows that the site\u2019s SSL certificate will expire in 79 days.<\/p>\n<\/p>\n<p><p>To notify the administrator that the SSL Certificate needs to be renewed soon, you can enable email or messenger notifications.<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>SSL (TLS) certificates play a pivotal role in today&#8217;s infrastructure. Hence, it&#8217;s incumbent upon administrators to make sure they don&#8217;t expire and are timely renewed. This blog post\u2026<\/p>\n","protected":false},"author":1,"featured_media":9358,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[116],"tags":[],"class_list":["post-9357","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts\/9357","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/comments?post=9357"}],"version-history":[{"count":2,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts\/9357\/revisions"}],"predecessor-version":[{"id":10347,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts\/9357\/revisions\/10347"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/media\/9358"}],"wp:attachment":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/media?parent=9357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/categories?post=9357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/tags?post=9357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}