|
5 | 5 | "flag" |
6 | 6 | "fmt" |
7 | 7 | "log/slog" |
| 8 | + "strings" |
8 | 9 | "sync" |
9 | 10 | "time" |
10 | 11 |
|
@@ -59,7 +60,7 @@ func (c *vmCollector) Update(ch chan<- prometheus.Metric, namespace string, clie |
59 | 60 |
|
60 | 61 | err := fetchProperties( |
61 | 62 | 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, |
63 | 64 | ) |
64 | 65 | if err != nil { |
65 | 66 | return err |
@@ -111,6 +112,33 @@ func (c *vmCollector) Update(ch chan<- prometheus.Metric, namespace string, clie |
111 | 112 | ), prometheus.GaugeValue, float64(datastore.Committed), |
112 | 113 | ) |
113 | 114 | } |
| 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 | + } |
114 | 142 | } |
115 | 143 |
|
116 | 144 | } |
|
0 commit comments