| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package x509check |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "strconv" |
| 8 | |
| 9 | "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi" |
| 10 | ) |
| 11 | |
| 12 | var certChartsTmpl = collectorapi.Charts{ |
| 13 | certTimeUntilExpirationChartTmpl.Copy(), |
| 14 | certRevocationStatusChartTmpl.Copy(), |
| 15 | } |
| 16 | |
| 17 | var ( |
| 18 | certTimeUntilExpirationChartTmpl = collectorapi.Chart{ |
| 19 | ID: "cert_depth%d_time_until_expiration", |
| 20 | Title: "Time Until Certificate Expiration", |
| 21 | Units: "seconds", |
| 22 | Fam: "expiration time", |
| 23 | Ctx: "x509check.time_until_expiration", |
| 24 | Opts: collectorapi.Opts{StoreFirst: true}, |
| 25 | Dims: collectorapi.Dims{ |
| 26 | {ID: "cert_depth%d_expiry", Name: "expiry"}, |
| 27 | }, |
| 28 | } |
| 29 | certRevocationStatusChartTmpl = collectorapi.Chart{ |
| 30 | ID: "cert_depth%d_revocation_status", |
| 31 | Title: "Revocation Status", |
| 32 | Units: "boolean", |
| 33 | Fam: "revocation", |
| 34 | Ctx: "x509check.revocation_status", |
| 35 | Opts: collectorapi.Opts{StoreFirst: true}, |
| 36 | Dims: collectorapi.Dims{ |
| 37 | {ID: "cert_depth%d_not_revoked", Name: "not_revoked"}, |
| 38 | {ID: "cert_depth%d_revoked", Name: "revoked"}, |
| 39 | }, |
| 40 | } |
| 41 | ) |
| 42 | |
| 43 | func (c *Collector) addCertCharts(commonName string, depth int) { |
| 44 | charts := certChartsTmpl.Copy() |
| 45 | |
| 46 | if depth > 0 || !c.CheckRevocation { |
| 47 | _ = charts.Remove(certRevocationStatusChartTmpl.ID) |
| 48 | } |
| 49 | |
| 50 | for _, chart := range *charts { |
| 51 | chart.ID = fmt.Sprintf(chart.ID, depth) |
| 52 | chart.Labels = []collectorapi.Label{ |
| 53 | {Key: "source", Value: c.Source}, |
| 54 | {Key: "common_name", Value: commonName}, |
| 55 | {Key: "depth", Value: strconv.Itoa(depth)}, |
| 56 | } |
| 57 | for _, dim := range chart.Dims { |
| 58 | dim.ID = fmt.Sprintf(dim.ID, depth) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | if err := c.Charts().Add(*charts...); err != nil { |
| 63 | c.Warningf("failed to add charts for '%s': %v", commonName, err) |
| 64 | } |
| 65 | } |