Skip to content

Commit

Permalink
added network package
Browse files Browse the repository at this point in the history
  • Loading branch information
czerwonk committed Jan 12, 2018
1 parent c6e28f2 commit de1d966
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
49 changes: 49 additions & 0 deletions network/helper.go
@@ -0,0 +1,49 @@
package network

import (
"fmt"
"sync"

"github.com/czerwonk/ovirt_exporter/statistic"

"github.com/czerwonk/ovirt_api/api"
"github.com/prometheus/client_golang/prometheus"
)

func CollectMetricsForHost(path, prefix string, labelNames, labelValues []string, client *api.Client, ch chan<- prometheus.Metric) error {
nics := &HostNics{}
err := client.GetAndParse(path, nics)
if err != nil {
return err
}

return collectForNics(nics.Nics, path, prefix, labelNames, labelValues, client, ch)
}

func CollectMetricsForVM(path, prefix string, labelNames, labelValues []string, client *api.Client, ch chan<- prometheus.Metric) error {
nics := &VMNics{}
err := client.GetAndParse(path, nics)
if err != nil {
return err
}

return collectForNics(nics.Nics, path, prefix, labelNames, labelValues, client, ch)
}

func collectForNics(nics []Nic, path, prefix string, labelNames, labelValues []string, client *api.Client, ch chan<- prometheus.Metric) error {
wg := sync.WaitGroup{}
wg.Add(len(nics))
for _, n := range nics {
p := fmt.Sprintf("%s/%s/statistics", path, n.ID)
ln := append(labelNames, "nic", "mac")
l := append(labelValues, n.Name, n.Mac.Address)

go func() {
statistic.CollectMetrics(p, prefix+"network_", ln, l, client, ch)
wg.Done()
}()
}

wg.Wait()
return nil
}
17 changes: 17 additions & 0 deletions network/nic.go
@@ -0,0 +1,17 @@
package network

type HostNics struct {
Nics []Nic `xml:"host_nic"`
}

type VMNics struct {
Nics []Nic `xml:"nic"`
}

type Nic struct {
ID string `xml:"id,attr"`
Name string `xml:"name"`
Mac struct {
Address string `xml:"address"`
} `xml:"mac"`
}

0 comments on commit de1d966

Please sign in to comment.