Skip to content

Commit

Permalink
Merge pull request #3 from natrontech/update-exporter
Browse files Browse the repository at this point in the history
feat: add last snapshot metrics
  • Loading branch information
janfuhrer committed Aug 23, 2023
2 parents d0b65be + 9af6e83 commit 9a9e176
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 9 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@ Metrics are retrieved using the [Proxmox Backup Server API](https://pbs.proxmox.
| pbs_available | The available bytes of the underlying storage. | `datastore` |
| pbs_size | The size of the underlying storage in bytes. | `datastore` |
| pbs_used | The used bytes of the underlying storage. | `datastore` |
| pbs_snapshot_count | The total number of backups. | `namespace` |
| pbs_snapshot_vm_count | The total number of backups per VM. | `namespace`, `vm_id` |
| pbs_snapshot_count | The total number of backups. | `datastore`, `namespace` |
| pbs_snapshot_vm_count | The total number of backups per VM. | `datastore`, `namespace`, `vm_id` |
| pbs_snapshot_vm_last_timestamp | The timestamp of the last backup of a VM. | `datastore`, `namespace`, `vm_id` |
| pbs_snapshot_vm_last_verify | The verify status of the last backup of a VM. | `datastore`, `namespace`, `vm_id` |
| pbs_host_cpu_usage | The CPU usage of the host. | |
| pbs_host_memory_free | The free memory of the host. | |
| pbs_host_memory_total | The total memory of the host. | |
| pbs_host_memory_used | The used memory of the host. | |
| pbs_host_swap_free | The free swap of the host. | |
| pbs_host_swap_total | The total swap of the host. | |
| pbs_host_swap_used | The used swap of the host. | |
| pbs_host_available_free | The available disk of the local root disk in bytes. | |
| pbs_host_disk_available | The available disk of the local root disk in bytes. | |
| pbs_host_disk_total | The total disk of the local root disk in bytes. | |
| pbs_host_disk_used | The used disk of the local root disk in bytes. | |
| pbs_host_uptime | The uptime of the host. | |
Expand Down
65 changes: 59 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,22 @@ var (
snapshot_count = prometheus.NewDesc(
prometheus.BuildFQName(promNamespace, "", "snapshot_count"),
"The total number of backups.",
[]string{"namespace"}, nil,
[]string{"datastore", "namespace"}, nil,
)
snapshot_vm_count = prometheus.NewDesc(
prometheus.BuildFQName(promNamespace, "", "snapshot_vm_count"),
"The total number of backups per VM.",
[]string{"namespace", "vm_id"}, nil,
[]string{"datastore", "namespace", "vm_id"}, nil,
)
snapshot_vm_last_timestamp = prometheus.NewDesc(
prometheus.BuildFQName(promNamespace, "", "snapshot_vm_last_timestamp"),
"The timestamp of the last backup of a VM.",
[]string{"datastore", "namespace", "vm_id"}, nil,
)
snapshot_vm_last_verify = prometheus.NewDesc(
prometheus.BuildFQName(promNamespace, "", "snapshot_vm_last_verify"),
"The verify status of the last backup of a VM.",
[]string{"datastore", "namespace", "vm_id"}, nil,
)
host_cpu_usage = prometheus.NewDesc(
prometheus.BuildFQName(promNamespace, "", "host_cpu_usage"),
Expand Down Expand Up @@ -118,7 +128,7 @@ var (
nil, nil,
)
host_disk_available = prometheus.NewDesc(
prometheus.BuildFQName(promNamespace, "", "host_available_free"),
prometheus.BuildFQName(promNamespace, "", "host_disk_available"),
"The available disk of the local root disk in bytes.",
nil, nil,
)
Expand Down Expand Up @@ -185,7 +195,11 @@ type NamespaceResponse struct {

type SnapshotResponse struct {
Data []struct {
BackupID string `json:"backup-id"`
BackupID string `json:"backup-id"`
BackupTime int64 `json:"backup-time"`
Verification struct {
State string `json:"state"`
} `json:"verification"`
} `json:"data"`
}

Expand Down Expand Up @@ -232,6 +246,8 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- used
ch <- snapshot_count
ch <- snapshot_vm_count
ch <- snapshot_vm_last_timestamp
ch <- snapshot_vm_last_verify
ch <- host_cpu_usage
ch <- host_memory_free
ch <- host_memory_total
Expand Down Expand Up @@ -559,7 +575,7 @@ func (e *Exporter) getNamespaceMetric(datastore string, namespace string, ch cha

// set total snapshot metrics
ch <- prometheus.MustNewConstMetric(
snapshot_count, prometheus.GaugeValue, float64(len(response.Data)), namespace,
snapshot_count, prometheus.GaugeValue, float64(len(response.Data)), datastore, namespace,
)

// set snapshot metrics per vm
Expand All @@ -573,13 +589,50 @@ func (e *Exporter) getNamespaceMetric(datastore string, namespace string, ch cha
// set snapshot metrics per vm
for vmName, count := range vmCount {
ch <- prometheus.MustNewConstMetric(
snapshot_vm_count, prometheus.GaugeValue, float64(count), namespace, vmName,
snapshot_vm_count, prometheus.GaugeValue, float64(count), datastore, namespace, vmName,
)

// find last snapshot with backupID
lastTimeStamp, lastVerify, err := findLastSnapshotWithBackupID(response, vmName)
if err != nil {
return err
}
lastVerifyBool := 0
if lastVerify == "ok" {
lastVerifyBool = 1
}
ch <- prometheus.MustNewConstMetric(
snapshot_vm_last_timestamp, prometheus.GaugeValue, float64(lastTimeStamp), datastore, namespace, vmName,
)
ch <- prometheus.MustNewConstMetric(
snapshot_vm_last_verify, prometheus.GaugeValue, float64(lastVerifyBool), datastore, namespace, vmName,
)
}

return nil
}

func findLastSnapshotWithBackupID(response SnapshotResponse, backupID string) (int64, string, error) {
// find biggest value of backupTime of backupID in response array
var lastTimeStamp int64
var lastVerify string
for _, snapshot := range response.Data {
if snapshot.BackupID == backupID {
if snapshot.BackupTime > lastTimeStamp {
lastTimeStamp = snapshot.BackupTime
lastVerify = snapshot.Verification.State
}
}
}

// if lastTimeStamp is still 0, no snapshot was found
if lastTimeStamp != 0 {
return lastTimeStamp, lastVerify, nil
}

return 0, "", fmt.Errorf("ERROR: No snapshot found with backupID %s", backupID)
}

func main() {
flag.Parse()

Expand Down

0 comments on commit 9a9e176

Please sign in to comment.