Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Remove duplicates from substribed metrics (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
sandlbn authored Mar 8, 2017
1 parent 674c33a commit 68f6665
Show file tree
Hide file tree
Showing 9 changed files with 315 additions and 50 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ virt-install --name=test_vm --arch=x86_64 --vcpus=1 --ram=512 --os-type=linux --
```
4. Load Snap libvirt collector plugin and create task:
```
wget https://raw.githubusercontent.com/intelsdi-x/snap-plugin-collector-libvirt/master/example/task-example.yaml
wget https://raw.githubusercontent.com/intelsdi-x/snap-plugin-collector-libvirt/master/example/tasks/task-example.yaml
snaptel plugin load snap-plugin-collector-libvirt
snaptel task create -t task-example.yaml
```
Expand Down
File renamed without changes.
22 changes: 22 additions & 0 deletions example/tasks/task-static-example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

---
version: 1
schedule:
type: "simple"
interval: "1s"
max-failures: 10
workflow:
collect:
metrics:
/intel/libvirt/test/cpu/cputime: {}
/intel/libvirt/test/disk/hd0/rdbytes: {}
/intel/libvirt/test/disk/hd0/rdreq: {}
/intel/libvirt/test/disk/hd0/wrbytes: {}
/intel/libvirt/test/disk/hd0/wrreq: {}
/intel/libvirt/test/network/vir0/rxbytes: {}
/intel/libvirt/test/network/vir0/txbytes: {}
publish:
- plugin_name: "file"
config:
file: "/tmp/libvirt_metrics.log"

21 changes: 21 additions & 0 deletions libvirt/libvirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type Libvirt interface {
GetDomainInterfaces(domain libvirt.VirDomain) ([]string, error)
GetDomainDisks(domain libvirt.VirDomain) ([]string, error)
GetVCPUStatistics(domain libvirt.VirDomain) (map[string]int64, error)
GetRequestedInstances(conn libvirt.VirConnection, domainNames []string) ([]libvirt.VirDomain, error)
GetInstanceByDomainName(conn libvirt.VirConnection, domainName string) (libvirt.VirDomain, error)
}

// GetInstanceIds return all names of active VirDomains
Expand Down Expand Up @@ -67,3 +69,22 @@ func GetInstances(conn libvirt.VirConnection) ([]libvirt.VirDomain, error) {
}
return libvirtDomains, nil
}

// GetRequestedInstances return all instances from domainNames slice
func GetRequestedInstances(conn libvirt.VirConnection, domainNames []string) ([]libvirt.VirDomain, error) {
var libvirtDomains []libvirt.VirDomain
for _, domainName := range domainNames {
domain, err := GetInstanceByDomainName(conn, domainName)
if err != nil {
return libvirtDomains, err
}
libvirtDomains = append(libvirtDomains, domain)
}
return libvirtDomains, nil

}

// GetInstanceByDomainName return instance by DomainName
func GetInstanceByDomainName(conn libvirt.VirConnection, domainName string) (libvirt.VirDomain, error) {
return conn.LookupDomainByName(domainName)
}
23 changes: 23 additions & 0 deletions libvirt/libvirt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ func TestLibirt(t *testing.T) {
So(err, ShouldNotBeNil)
So(len(interf), ShouldResemble, 0)
})
Convey("Get DomainNames", t, func() {
conn, err := libvirt.NewVirConnection("test:///default")
So(err, ShouldBeNil)
domains, err := GetInstanceIds(conn)
So(err, ShouldBeNil)
So(len(domains), ShouldResemble, 1)
})
Convey("Get Interface Name when Interface exist", t, func() {
buf, err := ioutil.ReadFile("./test_domain.xml")
if err != nil {
Expand Down Expand Up @@ -133,4 +140,20 @@ func TestLibirt(t *testing.T) {
expectedData := map[string]string{"nova_uuid": "5a26891c-efb0-4c6a-8bef-b54c45296136"}
So(data, ShouldResemble, expectedData)
})
Convey("Get Instances from a slice", t, func() {
conn, err := libvirt.NewVirConnection("test:///default")
instances := []string{"test"}
So(err, ShouldBeNil)
domains, err := GetRequestedInstances(conn, instances)
So(err, ShouldBeNil)
So(len(domains), ShouldResemble, 1)
})
Convey("Get Instances from a slice, when instance doesn't exist", t, func() {
conn, err := libvirt.NewVirConnection("test:///default")
instances := []string{"testi1"}
So(err, ShouldBeNil)
domains, err := GetRequestedInstances(conn, instances)
So(err, ShouldNotBeNil)
So(len(domains), ShouldResemble, 0)
})
}
74 changes: 74 additions & 0 deletions libvirtcollector/common.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
package libvirtcollector

import (
"bytes"
"strings"
"time"

wrapper "github.com/intelsdi-x/snap-plugin-collector-libvirt/libvirt"
"github.com/intelsdi-x/snap-plugin-lib-go/v1/plugin"
libvirt "github.com/sandlbn/libvirt-go"
)

func getLibvirtURI(cfg plugin.Config) string {
uri, err := cfg.GetString("uri")
if err != nil {
return defaultURI
}
return uri
}

func createMetric(ns plugin.Namespace) plugin.Metric {
metricType := plugin.Metric{

Expand All @@ -22,6 +34,7 @@ func filterNamespace(metricType string, mts []plugin.Metric) (int, []plugin.Metr
filteredMetrics = append(filteredMetrics, m)
}
}
filteredMetrics = removeDuplicates(filteredMetrics)
return len(filteredMetrics), filteredMetrics
}

Expand Down Expand Up @@ -65,3 +78,64 @@ func copyNamespaceElements(ns []plugin.NamespaceElement) []plugin.NamespaceEleme
copy(newNs, ns)
return newNs
}

func removeDuplicates(elements []plugin.Metric) []plugin.Metric {
// Use map to record duplicates as we find them.
encountered := map[string]bool{}
result := []plugin.Metric{}

for _, v := range elements {
ns := strings.Join(v.Namespace.Strings(), "/")
if !encountered[ns] {
encountered[ns] = true
result = append(result, v)
}
}
// Return the new slice.
return result
}

func metricStored(elements []plugin.Metric, newNamespace []plugin.NamespaceElement) bool {
for _, v := range elements {
ns := strings.Join(v.Namespace.Strings(), "")
if ns == joinNamespaceElements(newNamespace) {
return true
}
}
return false
}

func joinNamespaceElements(ns []plugin.NamespaceElement) string {
var buffer bytes.Buffer
for _, v := range ns {
buffer.WriteString(v.Value)
}
return buffer.String()
}

func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}

func getInstances(conn libvirt.VirConnection, elements []plugin.Metric) ([]libvirt.VirDomain, error) {

instances := []string{}

for _, v := range elements {
domain := v.Namespace.Strings()[nsDomainPosition]

if domain == "*" {
return wrapper.GetInstances(conn)
}
if !contains(instances, domain) {
instances = append(instances, domain)
}
}
return wrapper.GetRequestedInstances(conn, instances)

}
Loading

0 comments on commit 68f6665

Please sign in to comment.