Skip to content

Commit 16c58dc

Browse files
authored
Merge pull request #18 from kennhardy/snapshot-support
Adding support for snapshot metrics
2 parents c36a021 + ca37806 commit 16c58dc

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

vmware/collectors/vm.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"flag"
66
"fmt"
77
"log/slog"
8+
"strings"
89
"sync"
910
"time"
1011

@@ -59,7 +60,7 @@ func (c *vmCollector) Update(ch chan<- prometheus.Metric, namespace string, clie
5960

6061
err := fetchProperties(
6162
loginData["ctx"].(context.Context), loginData["view"].(*view.Manager), loginData["client"].(*vim25.Client),
62-
[]string{"VirtualMachine"}, []string{"summary", "runtime", "storage"}, &vms, c.logger,
63+
[]string{"VirtualMachine"}, []string{"summary", "runtime", "storage", "snapshot", "snapshot.rootSnapshotList", "snapshot.currentSnapshot"}, &vms, c.logger,
6364
)
6465
if err != nil {
6566
return err
@@ -111,6 +112,33 @@ func (c *vmCollector) Update(ch chan<- prometheus.Metric, namespace string, clie
111112
), prometheus.GaugeValue, float64(datastore.Committed),
112113
)
113114
}
115+
// Check if the VM has any snapshots, set value of metric to unix timestamp of snapshot creation time
116+
if vm.Snapshot != nil {
117+
c.logger.Debug("msg", fmt.Sprintf("VM %s has snapshots", vm.Summary.Config.Name), nil)
118+
for _, rootSnap := range vm.Snapshot.RootSnapshotList {
119+
snapDate := rootSnap.CreateTime.Format(time.RFC3339)
120+
// Check snapshot name and description if it contains the string "[keep]". If yes, set keepSnap to true.
121+
keepSnap := false
122+
if rootSnap.Name != "" {
123+
if strings.Contains(rootSnap.Name, "[keep]") {
124+
keepSnap = true
125+
}
126+
}
127+
if rootSnap.Description != "" {
128+
if strings.Contains(rootSnap.Description, "[keep]") {
129+
keepSnap = true
130+
}
131+
}
132+
ch <- prometheus.MustNewConstMetric(
133+
prometheus.NewDesc(
134+
prometheus.BuildFQName(namespace, vmSubsystem, "snapshot_info"),
135+
"Unix timestamp since snapshot creation", nil,
136+
map[string]string{"vmmo": vm.Self.Value, "vm": vm.Summary.Config.Name,
137+
"vcenter": loginData["target"].(string), "snapshot_create_time": snapDate, "snapshot_keep": fmt.Sprintf("%t", keepSnap)},
138+
), prometheus.GaugeValue, float64(rootSnap.CreateTime.Unix()),
139+
)
140+
}
141+
}
114142
}
115143

116144
}

0 commit comments

Comments
 (0)