Grafana dashboard: Better SSL Monitoring (x509_cert)
Description: A very simple and visual Dashboard to monitor SSL Certificates (x.509) using the native inputs.x509_cert from Telegraf.
It just works automatically once the sources are added to telegraf.

Just place the configuration to your telegraf instance and import the Dashboard template. 

To just have a Box with the next due certificate on your main Situation Room Dashboard you can use the following query:

`SELECT (bottom(expiry,common_name,1)/60/60/24) as exp,common_name FROM "x509_cert" WHERE time >= now() - 1h`

It's easy to maintain the Config in a Git repo and pull it ever few hours to your Telegraf Server. Such a Cron could look like:

`0 0,3,6,9,12,15,18,21 * * * /usr/bin/git archive --remote=ssh://git@your.gitrepo:7999/sslchec/domains.git HEAD certs.conf|tar -xO > /etc/telegraf/telegraf.d/certs.conf && killall -1 telegraf`


{
  "__inputs": [
    {
      "name": "DS_INFLUXDB",
      "label": "InfluxDB",
      "description": "",
      "type": "datasource",
      "pluginId": "influxdb",
      "pluginName": "InfluxDB"
    }
  ],
  "__requires": [
    {
      "type": "grafana",
      "id": "grafana",
      "name": "Grafana",
      "version": "7.3.7"
    },
    {
      "type": "panel",
      "id": "graph",
      "name": "Graph",
      "version": ""
    },
    {
      "type": "datasource",
      "id": "influxdb",
      "name": "InfluxDB",
      "version": "1.0.0"
    },
    {
      "type": "panel",
      "id": "stat",
      "name": "Stat",
      "version": ""
    },
    {
      "type": "panel",
      "id": "table-old",
      "name": "Table (old)",
      "version": ""
    }
  ],
  "annotations": {
    "list": [
      {
        "builtIn": 1,
        "datasource": "-- Grafana --",
        "enable": true,
        "hide": true,
        "iconColor": "rgba(0, 211, 255, 1)",
        "name": "Annotations & Alerts",
        "type": "dashboard"
      }
    ]
  },
  "description": "A very simple and visual Dashboard to monitor SSL Certificates (x.509) using the native inputs.x509_cert from Telegraf.\r\nIt just works automatically once the sources are added to telegraf.",
  "editable": true,
  "gnetId": 13683,
  "graphTooltip": 0,
  "id": null,
  "iteration": 1611228034426,
  "links": [],
  "panels": [
    {
      "cacheTimeout": null,
      "datasource": "${DS_INFLUXDB}",
      "description": "SSL Certificate Monitoring Stat. On the Title of the widget you can find the SSL Certificate name, and on the value the current TTL/Expiry date in days.\n\nThresholds are:\n* From 0 days to 14, Red\n* From 15 days to 30, Orange\n* From 31 days onward, Green",
      "fieldConfig": {
        "defaults": {
          "custom": {},
          "decimals": 1,
          "mappings": [
            {
              "id": 0,
              "op": "=",
              "text": "N/A",
              "type": 1,
              "value": "null"
            }
          ],
          "max": 0,
          "min": 0,
          "nullValueMode": "connected",
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "semi-dark-red",
                "value": null
              },
              {
                "color": "semi-dark-orange",
                "value": 14
              },
              {
                "color": "semi-dark-green",
                "value": 30
              }
            ]
          },
          "unit": "days"
        },
        "overrides": [
          {
            "matcher": {
              "id": "byRegexp",
              "options": "/^x509_cert.last.*$/"
            },
            "properties": [
              {
                "id": "displayName",
                "value": "EXP"
              },
              {
                "id": "color",
                "value": {
                  "mode": "thresholds"
                }
              }
            ]
          },
          {
            "matcher": {
              "id": "byRegexp",
              "options": "/^x509_cert.issuer_common_name.*$/"
            },
            "properties": [
              {
                "id": "displayName",
                "value": "CA"
              },
              {
                "id": "color",
                "value": {
                  "fixedColor": "dark-blue",
                  "mode": "fixed"
                }
              }
            ]
          },
          {
            "matcher": {
              "id": "byRegexp",
              "options": "/^x509_cert.verification_code.*$/"
            },
            "properties": [
              {
                "id": "color",
                "value": {
                  "mode": "thresholds"
                }
              },
              {
                "id": "mappings",
                "value": [
                  {
                    "from": "",
                    "id": 1,
                    "text": "Chain OK",
                    "to": "",
                    "type": 1,
                    "value": "0"
                  },
                  {
                    "from": "1",
                    "id": 2,
                    "text": "Chain ERR",
                    "to": "999999",
                    "type": 2,
                    "value": "1"
                  }
                ]
              },
              {
                "id": "thresholds",
                "value": {
                  "mode": "absolute",
                  "steps": [
                    {
                      "color": "semi-dark-green",
                      "value": null
                    },
                    {
                      "color": "dark-red",
                      "value": 1
                    }
                  ]
                }
              }
            ]
          }
        ]
      },
      "gridPos": {
        "h": 4,
        "w": 3,
        "x": 0,
        "y": 0
      },
      "id": 2,
      "links": [],
      "maxPerRow": 8,
      "options": {
        "colorMode": "background",
        "graphMode": "none",
        "justifyMode": "auto",
        "orientation": "horizontal",
        "reduceOptions": {
          "calcs": [
            "last"
          ],
          "fields": "",
          "values": false
        },
        "textMode": "value"
      },
      "pluginVersion": "7.3.7",
      "repeat": "ssl_name",
      "repeatDirection": "h",
      "targets": [
        {
          "groupBy": [
            {
              "params": [
                "$__interval"
              ],
              "type": "time"
            },
            {
              "params": [
                "san"
              ],
              "type": "tag"
            },
            {
              "params": [
                "none"
              ],
              "type": "fill"
            }
          ],
          "measurement": "x509_cert",
          "orderByTime": "ASC",
          "policy": "default",
          "query": "SELECT last(\"expiry\") /60/60/24,issuer_common_name,verification_code FROM \"x509_cert\" WHERE (\"source\" =~ /^$ssl_name$/ AND \"san\" !~ /^$/) AND $timeFilter GROUP BY time($__interval), \"common_name\" fill(none)",
          "rawQuery": true,
          "refId": "A",
          "resultFormat": "time_series",
          "select": [
            [
              {
                "params": [
                  "expiry"
                ],
                "type": "field"
              },
              {
                "params": [],
                "type": "last"
              },
              {
                "params": [
                  "/60/60/24"
                ],
                "type": "math"
              }
            ]
          ],
          "tags": [
            {
              "key": "source",
              "operator": "=~",
              "value": "/^$ssl_name$/"
            },
            {
              "condition": "AND",
              "key": "san",
              "operator": "!=",
              "value": "\"\""
            }
          ]
        }
      ],
      "timeFrom": null,
      "timeShift": null,
      "title": "$ssl_name",
      "type": "stat"
    },
    {
      "columns": [
        {
          "$$hashKey": "object:93",
          "text": "Current",
          "value": "current"
        }
      ],
      "datasource": "${DS_INFLUXDB}",
      "description": "SSL Certificate Monitoring Table. On the first column you can find the SSL x509 Certificate name, and on the second column the TTL/Expirty Date in days.\n\nThresholds are:\n* From 0 days to 14, Red\n* From 15 days to 30, Orange\n* From 31 days onward, Green",
      "fieldConfig": {
        "defaults": {
          "custom": {}
        },
        "overrides": []
      },
      "fontSize": "100%",
      "gridPos": {
        "h": 10,
        "w": 24,
        "x": 0,
        "y": 32
      },
      "id": 9,
      "pageSize": null,
      "showHeader": true,
      "sort": {
        "col": 0,
        "desc": true
      },
      "styles": [
        {
          "$$hashKey": "object:105",
          "alias": "",
          "align": "auto",
          "colorMode": null,
          "colors": [
            "rgba(245, 54, 54, 0.9)",
            "rgba(237, 129, 40, 0.89)",
            "rgba(50, 172, 45, 0.97)"
          ],
          "dateFormat": "YYYY-MM-DD HH:mm:ss",
          "decimals": 2,
          "link": false,
          "mappingType": 1,
          "pattern": "Time",
          "thresholds": [],
          "type": "hidden",
          "unit": "short"
        },
        {
          "$$hashKey": "object:106",
          "alias": "SSL Expiry in",
          "align": "left",
          "colorMode": "row",
          "colors": [
            "#E02F44",
            "#FF780A",
            "rgba(50, 172, 45, 0.97)"
          ],
          "decimals": 2,
          "pattern": "Current",
          "thresholds": [
            "14",
            "30"
          ],
          "type": "number",
          "unit": "d"
        },
        {
          "$$hashKey": "object:107",
          "alias": "SSL Certificate",
          "align": "auto",
          "colorMode": null,
          "colors": [
            "rgba(245, 54, 54, 0.9)",
            "rgba(237, 129, 40, 0.89)",
            "rgba(50, 172, 45, 0.97)"
          ],
          "dateFormat": "YYYY-MM-DD HH:mm:ss",
          "decimals": 2,
          "link": false,
          "mappingType": 1,
          "pattern": "Metric",
          "preserveFormat": true,
          "sanitize": true,
          "thresholds": [
            ""
          ],
          "type": "string",
          "unit": "short",
          "valueMaps": []
        }
      ],
      "targets": [
        {
          "alias": "$tag_source",
          "groupBy": [
            {
              "params": [
                "$__interval"
              ],
              "type": "time"
            },
            {
              "params": [
                "null"
              ],
              "type": "fill"
            }
          ],
          "orderByTime": "ASC",
          "policy": "default",
          "query": "SELECT (expiry / 60 / 60 / 24) as \"expiry\" FROM \"x509_cert\" WHERE (\"source\" =~ /^$ssl_name$/ and \"san\" !~ /^$/) AND $timeFilter GROUP BY  \"source\"  ",
          "rawQuery": true,
          "refId": "A",
          "resultFormat": "time_series",
          "select": [
            [
              {
                "params": [
                  "value"
                ],
                "type": "field"
              },
              {
                "params": [],
                "type": "mean"
              }
            ]
          ],
          "tags": []
        }
      ],
      "timeFrom": null,
      "timeShift": null,
      "title": "",
      "transform": "timeseries_aggregations",
      "transparent": true,
      "type": "table-old"
    },
    {
      "alert": {
        "alertRuleTags": {},
        "conditions": [
          {
            "evaluator": {
              "params": [
                10
              ],
              "type": "lt"
            },
            "operator": {
              "type": "and"
            },
            "query": {
              "params": [
                "A",
                "1h",
                "now"
              ]
            },
            "reducer": {
              "params": [],
              "type": "last"
            },
            "type": "query"
          }
        ],
        "executionErrorState": "alerting",
        "for": "5m",
        "frequency": "1m",
        "handler": 1,
        "message": "SSL Certificate is about to expire in <10d",
        "name": "Cert Aging",
        "noDataState": "no_data",
        "notifications": [
          {
            "uid": "000000002"
          }
        ]
      },
      "aliasColors": {},
      "bars": false,
      "dashLength": 10,
      "dashes": false,
      "datasource": "${DS_INFLUXDB}",
      "description": "This Graph is primarily for implementing the Alerting.",
      "fieldConfig": {
        "defaults": {
          "custom": {}
        },
        "overrides": []
      },
      "fill": 0,
      "fillGradient": 0,
      "gridPos": {
        "h": 8,
        "w": 24,
        "x": 0,
        "y": 42
      },
      "hiddenSeries": false,
      "id": 43,
      "legend": {
        "avg": false,
        "current": false,
        "max": false,
        "min": false,
        "show": true,
        "total": false,
        "values": false
      },
      "lines": true,
      "linewidth": 1,
      "nullPointMode": "null",
      "options": {
        "alertThreshold": true
      },
      "percentage": false,
      "pluginVersion": "7.3.7",
      "pointradius": 2,
      "points": false,
      "renderer": "flot",
      "seriesOverrides": [],
      "spaceLength": 10,
      "stack": false,
      "steppedLine": false,
      "targets": [
        {
          "alias": "$tag_common_name SAN $tag_san",
          "groupBy": [
            {
              "params": [
                "$__interval"
              ],
              "type": "time"
            },
            {
              "params": [
                "null"
              ],
              "type": "fill"
            }
          ],
          "orderByTime": "ASC",
          "policy": "default",
          "query": "SELECT (expiry / 60 / 60 / 24) as \"expiry\" FROM \"x509_cert\" WHERE $timeFilter GROUP BY common_name,san",
          "rawQuery": true,
          "refId": "A",
          "resultFormat": "time_series",
          "select": [
            [
              {
                "params": [
                  "value"
                ],
                "type": "field"
              },
              {
                "params": [],
                "type": "mean"
              }
            ]
          ],
          "tags": []
        }
      ],
      "thresholds": [
        {
          "colorMode": "critical",
          "fill": true,
          "line": true,
          "op": "lt",
          "value": 10
        }
      ],
      "timeFrom": null,
      "timeRegions": [],
      "timeShift": null,
      "title": "Cert Age",
      "tooltip": {
        "shared": true,
        "sort": 0,
        "value_type": "individual"
      },
      "type": "graph",
      "xaxis": {
        "buckets": null,
        "mode": "time",
        "name": null,
        "show": true,
        "values": []
      },
      "yaxes": [
        {
          "$$hashKey": "object:75",
          "format": "short",
          "label": null,
          "logBase": 2,
          "max": null,
          "min": "0",
          "show": true
        },
        {
          "$$hashKey": "object:76",
          "format": "short",
          "label": null,
          "logBase": 1,
          "max": null,
          "min": null,
          "show": true
        }
      ],
      "yaxis": {
        "align": false,
        "alignLevel": null
      }
    }
  ],
  "refresh": false,
  "schemaVersion": 26,
  "style": "dark",
  "tags": [],
  "templating": {
    "list": [
      {
        "allValue": null,
        "current": {},
        "datasource": "${DS_INFLUXDB}",
        "definition": "SHOW TAG VALUES FROM x509_cert WITH KEY=source",
        "error": null,
        "hide": 0,
        "includeAll": true,
        "label": "SSL Certificate",
        "multi": true,
        "name": "ssl_name",
        "options": [],
        "query": "SHOW TAG VALUES FROM x509_cert WITH KEY=source",
        "refresh": 1,
        "regex": "",
        "skipUrlSync": false,
        "sort": 0,
        "tagValuesQuery": "",
        "tags": [],
        "tagsQuery": "",
        "type": "query",
        "useTags": false
      }
    ]
  },
  "time": {
    "from": "now-1h",
    "to": "now"
  },
  "timepicker": {
    "refresh_intervals": [
      "5s",
      "10s",
      "30s",
      "1m",
      "5m",
      "15m",
      "30m",
      "1h",
      "2h",
      "1d"
    ]
  },
  "timezone": "",
  "title": "Better SSL Monitoring (x509_cert)",
  "uid": "0QnblY8Wz",
  "version": 46
}