Skip to content

Commit f43262e

Browse files
committed
grpc: TLS certificate metrics
Signed-off-by: Christian Svensson <bluecmd@google.com>
1 parent fc3959a commit f43262e

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

pkg/bmc/grpc.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ package bmc
77
import (
88
"context"
99
"crypto/tls"
10+
"crypto/x509"
1011
"fmt"
1112
"io"
1213
"net"
1314

1415
"github.com/grpc-ecosystem/go-grpc-prometheus"
16+
"github.com/prometheus/client_golang/prometheus"
1517
"github.com/u-root/u-bmc/config"
1618
pb "github.com/u-root/u-bmc/proto"
1719
"google.golang.org/grpc"
@@ -41,6 +43,26 @@ type mgmtServer struct {
4143
v *config.Version
4244
}
4345

46+
var (
47+
tlsCertificateExpiry = prometheus.NewGauge(prometheus.GaugeOpts{
48+
Namespace: "ubmc",
49+
Subsystem: "grpc",
50+
Name: "certificate_expiry",
51+
Help: "UNIX timestamp when the currently loaded TLS certificate expires for the gRPC server",
52+
})
53+
tlsCertificateLoaded = prometheus.NewGauge(prometheus.GaugeOpts{
54+
Namespace: "ubmc",
55+
Subsystem: "grpc",
56+
Name: "certificate_loaded",
57+
Help: "Whether the gRPC server has loaded a certificate",
58+
})
59+
)
60+
61+
func init() {
62+
prometheus.MustRegister(tlsCertificateExpiry)
63+
prometheus.MustRegister(tlsCertificateLoaded)
64+
}
65+
4466
func (m *mgmtServer) PressButton(ctx context.Context, r *pb.ButtonPressRequest) (*pb.ButtonPressResponse, error) {
4567
c, err := m.gpio.PressButton(ctx, r.Button, r.DurationMs)
4668
if err != nil {
@@ -119,6 +141,11 @@ func (m *mgmtServer) newServer(l net.Listener, c *tls.Certificate) {
119141
if c != nil {
120142
creds := credentials.NewServerTLSFromCert(c)
121143
opts = []grpc.ServerOption{grpc.Creds(creds)}
144+
c, err := x509.ParseCertificate(c.Certificate[0])
145+
if err == nil {
146+
tlsCertificateLoaded.Set(float64(1))
147+
tlsCertificateExpiry.Set(float64(c.NotAfter.Unix()))
148+
}
122149
}
123150

124151
g := grpc.NewServer(opts...)

0 commit comments

Comments
 (0)