mirror of
https://github.com/jangraefen/hcloud-pricing-exporter
synced 2024-11-10 05:54:15 +00:00
fca0b5f77b
* added additional tags flag * move additional labels to base fetcher * find label function instead of jump label * bugfix to labels not correctly set when -additional-labels flag not present * check for more than 0, instead of 1
64 lines
1.6 KiB
Go
64 lines
1.6 KiB
Go
package fetcher
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"github.com/hetznercloud/hcloud-go/hcloud"
|
|
)
|
|
|
|
var _ Fetcher = &serverBackup{}
|
|
|
|
// NewServerBackup creates a new fetcher that will collect pricing information on server backups.
|
|
func NewServerBackup(pricing *PriceProvider, additionalLabels ...string) Fetcher {
|
|
return &serverBackup{newBase(pricing, "server_backup", []string{"location", "type"}, additionalLabels...)}
|
|
}
|
|
|
|
type serverBackup struct {
|
|
*baseFetcher
|
|
}
|
|
|
|
func (serverBackup serverBackup) Run(client *hcloud.Client) error {
|
|
servers, _, err := client.Server.List(ctx, hcloud.ServerListOpts{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, s := range servers {
|
|
location := s.Datacenter.Location
|
|
|
|
labels := append([]string{
|
|
s.Name,
|
|
location.Name,
|
|
s.ServerType.Name,
|
|
},
|
|
parseAdditionalLabels(serverBackup.additionalLabels, s.Labels)...,
|
|
)
|
|
|
|
if s.BackupWindow != "" {
|
|
serverPrice, err := findServerPricing(location, s.ServerType.Pricings)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
hourlyPrice := serverBackup.toBackupPrice(serverPrice.Hourly.Gross)
|
|
monthlyPrice := serverBackup.toBackupPrice(serverPrice.Monthly.Gross)
|
|
|
|
serverBackup.hourly.WithLabelValues(labels...).Set(hourlyPrice)
|
|
serverBackup.monthly.WithLabelValues(labels...).Set(monthlyPrice)
|
|
} else {
|
|
serverBackup.hourly.WithLabelValues(labels...).Set(0)
|
|
serverBackup.monthly.WithLabelValues(labels...).Set(0)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (serverBackup serverBackup) toBackupPrice(rawServerPrice string) float64 {
|
|
serverPrice, err := strconv.ParseFloat(rawServerPrice, 32)
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
|
|
return serverPrice * (serverBackup.pricing.ServerBackup() / 100)
|
|
}
|