Skip to content

Commit

Permalink
Merge pull request #4 from natrontech/update-exporter
Browse files Browse the repository at this point in the history
feat: add vm name label to vm snapshot metrics
  • Loading branch information
janfuhrer authored Aug 23, 2023
2 parents 9a9e176 + ccda297 commit 1c28c96
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ Metrics are retrieved using the [Proxmox Backup Server API](https://pbs.proxmox.
| 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. | `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_snapshot_vm_count | The total number of backups per VM. | `datastore`, `namespace`, `vm_id`, `vm_name` |
| pbs_snapshot_vm_last_timestamp | The timestamp of the last backup of a VM. | `datastore`, `namespace`, `vm_id`, `vm_name` |
| pbs_snapshot_vm_last_verify | The verify status of the last backup of a VM. | `datastore`, `namespace`, `vm_id`, `vm_name` |
| 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. | |
Expand Down
31 changes: 17 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,17 @@ var (
snapshot_vm_count = prometheus.NewDesc(
prometheus.BuildFQName(promNamespace, "", "snapshot_vm_count"),
"The total number of backups per VM.",
[]string{"datastore", "namespace", "vm_id"}, nil,
[]string{"datastore", "namespace", "vm_id", "vm_name"}, 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,
[]string{"datastore", "namespace", "vm_id", "vm_name"}, 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,
[]string{"datastore", "namespace", "vm_id", "vm_name"}, nil,
)
host_cpu_usage = prometheus.NewDesc(
prometheus.BuildFQName(promNamespace, "", "host_cpu_usage"),
Expand Down Expand Up @@ -197,6 +197,7 @@ type SnapshotResponse struct {
Data []struct {
BackupID string `json:"backup-id"`
BackupTime int64 `json:"backup-time"`
VMName string `json:"comment"`
Verification struct {
State string `json:"state"`
} `json:"verification"`
Expand Down Expand Up @@ -293,7 +294,7 @@ func (e *Exporter) collectFromAPI(ch chan<- prometheus.Metric) error {
// debug
if *loglevel == "debug" {
log.Printf("DEBUG: Request URL: %s", req.URL)
log.Printf("DEBUG: Request Header: %s", req.Header)
//log.Printf("DEBUG: Request Header: %s", vmID)
}

// make request and show output
Expand Down Expand Up @@ -358,7 +359,7 @@ func (e *Exporter) getNodeMetrics(ch chan<- prometheus.Metric) error {
// debug
if *loglevel == "debug" {
log.Printf("DEBUG: Request URL: %s", req.URL)
log.Printf("DEBUG: Request Header: %s", req.Header)
//log.Printf("DEBUG: Request Header: %s", vmID)
}

// make request and show output
Expand Down Expand Up @@ -473,7 +474,7 @@ func (e *Exporter) getDatastoreMetric(datastore Datastore, ch chan<- prometheus.
// debug
if *loglevel == "debug" {
log.Printf("DEBUG: --Request URL: %s", req.URL)
log.Printf("DEBUG: --Request Header: %s", req.Header)
//log.Printf("DEBUG: --Request Header: %s", vmID)
}

// make request and show output
Expand Down Expand Up @@ -540,7 +541,7 @@ func (e *Exporter) getNamespaceMetric(datastore string, namespace string, ch cha
// debug
if *loglevel == "debug" {
log.Printf("DEBUG: ----Request URL: %s", req.URL)
log.Printf("DEBUG: ----Request Header: %s", req.Header)
//log.Printf("DEBUG: ----Request Header: %s", vmID)
}

// make request and show output
Expand Down Expand Up @@ -579,21 +580,23 @@ func (e *Exporter) getNamespaceMetric(datastore string, namespace string, ch cha
)

// set snapshot metrics per vm
vmNameMapping := make(map[string]string)
vmCount := make(map[string]int)
for _, snapshot := range response.Data {
// get vm name from snapshot
vmName := snapshot.BackupID
vmCount[vmName]++
vmID := snapshot.BackupID
vmNameMapping[vmID] = snapshot.VMName
vmCount[vmID]++
}

// set snapshot metrics per vm
for vmName, count := range vmCount {
for vmID, count := range vmCount {
ch <- prometheus.MustNewConstMetric(
snapshot_vm_count, prometheus.GaugeValue, float64(count), datastore, namespace, vmName,
snapshot_vm_count, prometheus.GaugeValue, float64(count), datastore, namespace, vmID, vmNameMapping[vmID],
)

// find last snapshot with backupID
lastTimeStamp, lastVerify, err := findLastSnapshotWithBackupID(response, vmName)
lastTimeStamp, lastVerify, err := findLastSnapshotWithBackupID(response, vmID)
if err != nil {
return err
}
Expand All @@ -602,10 +605,10 @@ func (e *Exporter) getNamespaceMetric(datastore string, namespace string, ch cha
lastVerifyBool = 1
}
ch <- prometheus.MustNewConstMetric(
snapshot_vm_last_timestamp, prometheus.GaugeValue, float64(lastTimeStamp), datastore, namespace, vmName,
snapshot_vm_last_timestamp, prometheus.GaugeValue, float64(lastTimeStamp), datastore, namespace, vmID, vmNameMapping[vmID],
)
ch <- prometheus.MustNewConstMetric(
snapshot_vm_last_verify, prometheus.GaugeValue, float64(lastVerifyBool), datastore, namespace, vmName,
snapshot_vm_last_verify, prometheus.GaugeValue, float64(lastVerifyBool), datastore, namespace, vmID, vmNameMapping[vmID],
)
}

Expand Down

0 comments on commit 1c28c96

Please sign in to comment.