From 4fa9a9631a56cf50a527bf300f35b416b5d79428 Mon Sep 17 00:00:00 2001 From: zhangjianweibj Date: Tue, 26 Feb 2019 10:35:12 +0800 Subject: [PATCH] add userName userId projectName projectId --- README.md | 2 +- libvirt_schema/schema.go | 29 ++++++++-- libvirt_schema/tools.go | 20 +++---- prometheus-libvirt-exporter.go | 88 +++++++++++++++++------------ prometheus-libvirt-exporter_test.go | 19 +++---- 5 files changed, 95 insertions(+), 63 deletions(-) diff --git a/README.md b/README.md index d1570a0..a855fb8 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ transmit_errors_total|Number of packet transmit errors on a network interface transmit_drops_total|Number of packet transmit drops on a network interface -##Example +## Example # TYPE libvirt_domain_state_code gauge libvirt_domain_state_code{domainName="instance-00000126",instanceId="4a50e208-eb6d-4a6e-904b-f9d9ef4ec483",instanceName="test",stateDesc="the domain is shut off"} 5 libvirt_domain_state_code{domainName="instance-00000157",instanceId="cee27a5f-278f-4f1c-b6b8-3d0879834cd1",instanceName="AWvlZ9qA-DRDSSingle-FeqKU6N4",stateDesc="the domain is shut off"} 5 diff --git a/libvirt_schema/schema.go b/libvirt_schema/schema.go index eb9ab00..7fe30c0 100644 --- a/libvirt_schema/schema.go +++ b/libvirt_schema/schema.go @@ -3,9 +3,9 @@ package libvirt_schema import "encoding/xml" type Domain struct { - Devices Devices `xml:"devices"` - Name string `xml:"name"` - UUID string `xml:"uuid"` + Devices Devices `xml:"devices"` + Name string `xml:"name"` + UUID string `xml:"uuid"` Metadata Metadata `xml:"metadata"` } @@ -14,8 +14,25 @@ type Metadata struct { } type NovaInstance struct { - XMLName xml.Name `xml:"instance"` - Name string `xml:"name"` + XMLName xml.Name `xml:"instance"` + Name string `xml:"name"` + Owner NovaOwner `xml:"owner"` +} + +type NovaOwner struct { + XMLName xml.Name `xml:"owner"` + User NovaUser `xml:"user"` + Project NovaProject `xml:"project"` +} + +type NovaUser struct { + UserId string `xml:"uuid,attr"` + UserName string `xml:",chardata"` +} + +type NovaProject struct { + ProjectId string `xml:"uuid,attr"` + ProjectName string `xml:",chardata"` } type Devices struct { @@ -48,4 +65,4 @@ type InterfaceSource struct { type InterfaceTarget struct { Device string `xml:"dev,attr"` -} \ No newline at end of file +} diff --git a/libvirt_schema/tools.go b/libvirt_schema/tools.go index 396f4f1..8013809 100644 --- a/libvirt_schema/tools.go +++ b/libvirt_schema/tools.go @@ -1,26 +1,24 @@ package libvirt_schema type DomainState uint8 + const ( //no state - DOMAIN_NOSTATE = DomainState(0) + DOMAIN_NOSTATE = DomainState(0) //the domain is running - DOMAIN_RUNNING = DomainState(1) + DOMAIN_RUNNING = DomainState(1) //the domain is blocked on resource - DOMAIN_BLOCKED = DomainState(2) + DOMAIN_BLOCKED = DomainState(2) //the domain is paused by user - DOMAIN_PAUSED = DomainState(3) + DOMAIN_PAUSED = DomainState(3) //the domain is being shut down - DOMAIN_SHUTDOWN = DomainState(4) + DOMAIN_SHUTDOWN = DomainState(4) //the domain is shut off - DOMAIN_SHUTOFF = DomainState(5) + DOMAIN_SHUTOFF = DomainState(5) //the domain is crashed - DOMAIN_CRASHED = DomainState(6) + DOMAIN_CRASHED = DomainState(6) //the domain is suspended by guest power management DOMAIN_PMSUSPENDED = DomainState(7) //this enum value will increase over time as new events are added to the libvirt API. It reflects the last state supported by this version of the libvirt API. - DOMAIN_LAST = DomainState(8) + DOMAIN_LAST = DomainState(8) ) - - - diff --git a/prometheus-libvirt-exporter.go b/prometheus-libvirt-exporter.go index 6e3b5d1..aa3fdde 100644 --- a/prometheus-libvirt-exporter.go +++ b/prometheus-libvirt-exporter.go @@ -4,8 +4,8 @@ import ( "encoding/xml" "flag" "github.com/digitalocean/go-libvirt" - "github.com/zhangjianweibj/prometheus-libvirt-exporter/libvirt_schema" "github.com/prometheus/client_golang/prometheus" + "github.com/zhangjianweibj/prometheus-libvirt-exporter/libvirt_schema" "log" "net" "net/http" @@ -20,111 +20,111 @@ var ( nil) libvirtDomainNumbers = prometheus.NewDesc( - prometheus.BuildFQName("libvirt","","domains_number"), + prometheus.BuildFQName("libvirt", "", "domains_number"), "Number of the domain", []string{"host"}, nil) libvirtDomainState = prometheus.NewDesc( - prometheus.BuildFQName("libvirt","","domain_state_code"), + prometheus.BuildFQName("libvirt", "", "domain_state_code"), "Code of the domain state", - []string{"domain", "instanceName", "instanceId","stateDesc","host"}, + []string{"domain", "instanceName", "instanceId", "userName", "userId", "projectName", "projectId", "stateDesc", "host"}, nil) libvirtDomainInfoMaxMemDesc = prometheus.NewDesc( prometheus.BuildFQName("libvirt", "domain_info", "maximum_memory_bytes"), "Maximum allowed memory of the domain, in bytes.", - []string{"domain", "instanceName", "instanceId","host"}, + []string{"domain", "instanceName", "instanceId", "userName", "userId", "projectName", "projectId", "host"}, nil) libvirtDomainInfoMemoryDesc = prometheus.NewDesc( prometheus.BuildFQName("libvirt", "domain_info", "memory_usage_bytes"), "Memory usage of the domain, in bytes.", - []string{"domain", "instanceName", "instanceId","host"}, + []string{"domain", "instanceName", "instanceId", "userName", "userId", "projectName", "projectId", "host"}, nil) libvirtDomainInfoNrVirtCpuDesc = prometheus.NewDesc( prometheus.BuildFQName("libvirt", "domain_info", "virtual_cpus"), "Number of virtual CPUs for the domain.", - []string{"domain", "instanceName", "instanceId","host"}, + []string{"domain", "instanceName", "instanceId", "userName", "userId", "projectName", "projectId", "host"}, nil) libvirtDomainInfoCpuTimeDesc = prometheus.NewDesc( prometheus.BuildFQName("libvirt", "domain_info", "cpu_time_seconds_total"), "Amount of CPU time used by the domain, in seconds.", - []string{"domain", "instanceName", "instanceId","host"}, + []string{"domain", "instanceName", "instanceId", "userName", "userId", "projectName", "projectId", "host"}, nil) libvirtDomainBlockRdBytesDesc = prometheus.NewDesc( prometheus.BuildFQName("libvirt", "domain_block_stats", "read_bytes_total"), "Number of bytes read from a block device, in bytes.", - []string{"domain", "instanceName", "instanceId", "source_file", "target_device","host"}, + []string{"domain", "instanceName", "instanceId", "userName", "userId", "projectName", "projectId", "source_file", "target_device", "host"}, nil) libvirtDomainBlockRdReqDesc = prometheus.NewDesc( prometheus.BuildFQName("libvirt", "domain_block_stats", "read_requests_total"), "Number of read requests from a block device.", - []string{"domain", "instanceName", "instanceId", "source_file", "target_device","host"}, + []string{"domain", "instanceName", "instanceId", "userName", "userId", "projectName", "projectId", "source_file", "target_device", "host"}, nil) libvirtDomainBlockWrBytesDesc = prometheus.NewDesc( prometheus.BuildFQName("libvirt", "domain_block_stats", "write_bytes_total"), "Number of bytes written from a block device, in bytes.", - []string{"domain", "instanceName", "instanceId", "source_file", "target_device","host"}, + []string{"domain", "instanceName", "instanceId", "userName", "userId", "projectName", "projectId", "source_file", "target_device", "host"}, nil) libvirtDomainBlockWrReqDesc = prometheus.NewDesc( prometheus.BuildFQName("libvirt", "domain_block_stats", "write_requests_total"), "Number of write requests from a block device.", - []string{"domain", "instanceName", "instanceId", "source_file", "target_device","host"}, + []string{"domain", "instanceName", "instanceId", "userName", "userId", "projectName", "projectId", "source_file", "target_device", "host"}, nil) //DomainInterface libvirtDomainInterfaceRxBytesDesc = prometheus.NewDesc( prometheus.BuildFQName("libvirt", "domain_interface_stats", "receive_bytes_total"), "Number of bytes received on a network interface, in bytes.", - []string{"domain", "instanceName", "instanceId", "source_bridge", "target_device","host"}, + []string{"domain", "instanceName", "instanceId", "userName", "userId", "projectName", "projectId", "source_bridge", "target_device", "host"}, nil) libvirtDomainInterfaceRxPacketsDesc = prometheus.NewDesc( prometheus.BuildFQName("libvirt", "domain_interface_stats", "receive_packets_total"), "Number of packets received on a network interface.", - []string{"domain", "instanceName", "instanceId", "source_bridge", "target_device","host"}, + []string{"domain", "instanceName", "instanceId", "userName", "userId", "projectName", "projectId", "source_bridge", "target_device", "host"}, nil) libvirtDomainInterfaceRxErrsDesc = prometheus.NewDesc( prometheus.BuildFQName("libvirt", "domain_interface_stats", "receive_errors_total"), "Number of packet receive errors on a network interface.", - []string{"domain", "instanceName", "instanceId", "source_bridge", "target_device","host"}, + []string{"domain", "instanceName", "instanceId", "userName", "userId", "projectName", "projectId", "source_bridge", "target_device", "host"}, nil) libvirtDomainInterfaceRxDropDesc = prometheus.NewDesc( prometheus.BuildFQName("libvirt", "domain_interface_stats", "receive_drops_total"), "Number of packet receive drops on a network interface.", - []string{"domain", "instanceName", "instanceId", "source_bridge", "target_device","host"}, + []string{"domain", "instanceName", "instanceId", "userName", "userId", "projectName", "projectId", "source_bridge", "target_device", "host"}, nil) libvirtDomainInterfaceTxBytesDesc = prometheus.NewDesc( prometheus.BuildFQName("libvirt", "domain_interface_stats", "transmit_bytes_total"), "Number of bytes transmitted on a network interface, in bytes.", - []string{"domain", "instanceName", "instanceId", "source_bridge", "target_device","host"}, + []string{"domain", "instanceName", "instanceId", "userName", "userId", "projectName", "projectId", "source_bridge", "target_device", "host"}, nil) libvirtDomainInterfaceTxPacketsDesc = prometheus.NewDesc( prometheus.BuildFQName("libvirt", "domain_interface_stats", "transmit_packets_total"), "Number of packets transmitted on a network interface.", - []string{"domain", "instanceName", "instanceId", "source_bridge", "target_device","host"}, + []string{"domain", "instanceName", "instanceId", "userName", "userId", "projectName", "projectId", "source_bridge", "target_device", "host"}, nil) libvirtDomainInterfaceTxErrsDesc = prometheus.NewDesc( prometheus.BuildFQName("libvirt", "domain_interface_stats", "transmit_errors_total"), "Number of packet transmit errors on a network interface.", - []string{"domain", "instanceName", "instanceId", "source_bridge", "target_device","host"}, + []string{"domain", "instanceName", "instanceId", "userName", "userId", "projectName", "projectId", "source_bridge", "target_device", "host"}, nil) libvirtDomainInterfaceTxDropDesc = prometheus.NewDesc( prometheus.BuildFQName("libvirt", "domain_interface_stats", "transmit_drops_total"), "Number of packet transmit drops on a network interface.", - []string{"domain", "instanceName", "instanceId", "source_bridge", "target_device","host"}, + []string{"domain", "instanceName", "instanceId", "userName", "userId", "projectName", "projectId", "source_bridge", "target_device", "host"}, nil) domainState = map[libvirt_schema.DomainState]string{ - libvirt_schema.DOMAIN_NOSTATE:"no state", - libvirt_schema.DOMAIN_RUNNING:"the domain is running", - libvirt_schema.DOMAIN_BLOCKED:"the domain is blocked on resource", - libvirt_schema.DOMAIN_PAUSED:"the domain is paused by user", - libvirt_schema.DOMAIN_SHUTDOWN:"the domain is being shut down", - libvirt_schema.DOMAIN_SHUTOFF:"the domain is shut off", - libvirt_schema.DOMAIN_CRASHED:"the domain is crashed", - libvirt_schema.DOMAIN_PMSUSPENDED:"the domain is suspended by guest power management", - libvirt_schema.DOMAIN_LAST:"this enum value will increase over time as new events are added to the libvirt API", + libvirt_schema.DOMAIN_NOSTATE: "no state", + libvirt_schema.DOMAIN_RUNNING: "the domain is running", + libvirt_schema.DOMAIN_BLOCKED: "the domain is blocked on resource", + libvirt_schema.DOMAIN_PAUSED: "the domain is paused by user", + libvirt_schema.DOMAIN_SHUTDOWN: "the domain is being shut down", + libvirt_schema.DOMAIN_SHUTOFF: "the domain is shut off", + libvirt_schema.DOMAIN_CRASHED: "the domain is crashed", + libvirt_schema.DOMAIN_PMSUSPENDED: "the domain is suspended by guest power management", + libvirt_schema.DOMAIN_LAST: "this enum value will increase over time as new events are added to the libvirt API", } ) @@ -145,6 +145,13 @@ func CollectDomain(ch chan<- prometheus.Metric, l *libvirt.Libvirt, domain *libv domainName := domain.Name instanceName := libvirtSchema.Metadata.NovaInstance.Name instanceId := libvirtSchema.UUID + + userName := libvirtSchema.Metadata.NovaInstance.Owner.User.UserName + userId := libvirtSchema.Metadata.NovaInstance.Owner.User.UserId + + projectName := libvirtSchema.Metadata.NovaInstance.Owner.Project.ProjectName + projectId := libvirtSchema.Metadata.NovaInstance.Owner.Project.ProjectId + host, err := l.ConnectGetHostname() if err != nil { log.Fatalf("failed to get hostname: %v", err) @@ -156,7 +163,7 @@ func CollectDomain(ch chan<- prometheus.Metric, l *libvirt.Libvirt, domain *libv libvirtDomainState, prometheus.GaugeValue, float64(rState), - domainName, instanceName, instanceId,domainState[libvirt_schema.DomainState(rState)],host) + domainName, instanceName, instanceId, userName, userId, projectName, projectId, domainState[libvirt_schema.DomainState(rState)], host) if err != nil { log.Fatalf("failed to get domainInfo: %v", err) @@ -166,22 +173,22 @@ func CollectDomain(ch chan<- prometheus.Metric, l *libvirt.Libvirt, domain *libv libvirtDomainInfoMaxMemDesc, prometheus.GaugeValue, float64(rmaxmem)*1024, - domainName, instanceName, instanceId,host) + domainName, instanceName, instanceId, userName, userId, projectName, projectId, host) ch <- prometheus.MustNewConstMetric( libvirtDomainInfoMemoryDesc, prometheus.GaugeValue, float64(rmemory)*1024, - domainName, instanceName, string(instanceId[:]),host) + domainName, instanceName, string(instanceId[:]), userName, userId, projectName, projectId, host) ch <- prometheus.MustNewConstMetric( libvirtDomainInfoNrVirtCpuDesc, prometheus.GaugeValue, float64(rvirCpu), - domainName, instanceName, string(instanceId[:]),host) + domainName, instanceName, string(instanceId[:]), userName, userId, projectName, projectId, host) ch <- prometheus.MustNewConstMetric( libvirtDomainInfoCpuTimeDesc, prometheus.CounterValue, float64(rcputime)/1e9, - domainName, instanceName, string(instanceId[:]),host) + domainName, instanceName, string(instanceId[:]), userName, userId, projectName, projectId, host) // Report block device statistics. for _, disk := range libvirtSchema.Devices.Disks { @@ -195,7 +202,6 @@ func CollectDomain(ch chan<- prometheus.Metric, l *libvirt.Libvirt, domain *libv rRdReq, rRdBytes, rWrReq, rWrBytes, _, err = l.DomainBlockStats(*domain, disk.Target.Device) } - if err != nil { log.Fatalf("failed to get DomainBlockStats: %v", err) return err @@ -206,6 +212,7 @@ func CollectDomain(ch chan<- prometheus.Metric, l *libvirt.Libvirt, domain *libv prometheus.CounterValue, float64(rRdBytes), domainName, instanceName, string(instanceId[:]), + userName, userId, projectName, projectId, disk.Source.File, disk.Target.Device, host) @@ -215,6 +222,7 @@ func CollectDomain(ch chan<- prometheus.Metric, l *libvirt.Libvirt, domain *libv prometheus.CounterValue, float64(rRdReq), domainName, instanceName, string(instanceId[:]), + userName, userId, projectName, projectId, disk.Source.File, disk.Target.Device, host) @@ -224,6 +232,7 @@ func CollectDomain(ch chan<- prometheus.Metric, l *libvirt.Libvirt, domain *libv prometheus.CounterValue, float64(rWrBytes), domainName, instanceName, string(instanceId[:]), + userName, userId, projectName, projectId, disk.Source.File, disk.Target.Device, host) @@ -233,6 +242,7 @@ func CollectDomain(ch chan<- prometheus.Metric, l *libvirt.Libvirt, domain *libv prometheus.CounterValue, float64(rWrReq), domainName, instanceName, string(instanceId[:]), + userName, userId, projectName, projectId, disk.Source.File, disk.Target.Device, host) @@ -260,6 +270,7 @@ func CollectDomain(ch chan<- prometheus.Metric, l *libvirt.Libvirt, domain *libv prometheus.CounterValue, float64(rRxBytes), domainName, instanceName, string(instanceId[:]), + userName, userId, projectName, projectId, iface.Source.Bridge, iface.Target.Device, host) @@ -269,6 +280,7 @@ func CollectDomain(ch chan<- prometheus.Metric, l *libvirt.Libvirt, domain *libv prometheus.CounterValue, float64(rRxPackets), domainName, instanceName, string(instanceId[:]), + userName, userId, projectName, projectId, iface.Source.Bridge, iface.Target.Device, host) @@ -278,6 +290,7 @@ func CollectDomain(ch chan<- prometheus.Metric, l *libvirt.Libvirt, domain *libv prometheus.CounterValue, float64(rRxErrs), domainName, instanceName, string(instanceId[:]), + userName, userId, projectName, projectId, iface.Source.Bridge, iface.Target.Device, host) @@ -287,6 +300,7 @@ func CollectDomain(ch chan<- prometheus.Metric, l *libvirt.Libvirt, domain *libv prometheus.CounterValue, float64(rRxDrop), domainName, instanceName, string(instanceId[:]), + userName, userId, projectName, projectId, iface.Source.Bridge, iface.Target.Device, host) @@ -296,6 +310,7 @@ func CollectDomain(ch chan<- prometheus.Metric, l *libvirt.Libvirt, domain *libv prometheus.CounterValue, float64(rTxBytes), domainName, instanceName, string(instanceId[:]), + userName, userId, projectName, projectId, iface.Source.Bridge, iface.Target.Device, host) @@ -305,6 +320,7 @@ func CollectDomain(ch chan<- prometheus.Metric, l *libvirt.Libvirt, domain *libv prometheus.CounterValue, float64(rTxPackets), domainName, instanceName, string(instanceId[:]), + userName, userId, projectName, projectId, iface.Source.Bridge, iface.Target.Device, host) @@ -314,6 +330,7 @@ func CollectDomain(ch chan<- prometheus.Metric, l *libvirt.Libvirt, domain *libv prometheus.CounterValue, float64(rTxErrs), domainName, instanceName, string(instanceId[:]), + userName, userId, projectName, projectId, iface.Source.Bridge, iface.Target.Device, host) @@ -323,6 +340,7 @@ func CollectDomain(ch chan<- prometheus.Metric, l *libvirt.Libvirt, domain *libv prometheus.CounterValue, float64(rTxDrop), domainName, instanceName, string(instanceId[:]), + userName, userId, projectName, projectId, iface.Source.Bridge, iface.Target.Device, host) diff --git a/prometheus-libvirt-exporter_test.go b/prometheus-libvirt-exporter_test.go index b82b24f..c51804d 100644 --- a/prometheus-libvirt-exporter_test.go +++ b/prometheus-libvirt-exporter_test.go @@ -3,13 +3,13 @@ package main import ( "encoding/xml" "fmt" - "testing" "github.com/stretchr/testify/assert" libvirt_schema "github.com/zhangjianweibj/prometheus-libvirt-exporter/libvirt_schema" + "testing" ) -func init() { - +func init() { + } //go lang bug ,1.9 bug still alive @@ -19,7 +19,6 @@ https://www.oschina.net/question/203177_2213070 **/ - func TestUnmarshal(t *testing.T) { var ( str = ` @@ -151,13 +150,13 @@ func TestUnmarshal(t *testing.T) { ` ) - r := libvirt_schema.Domain {} + r := libvirt_schema.Domain{} err := xml.Unmarshal([]byte(str), &r) - if err!= nil { + if err != nil { fmt.Println(err, r) } - assert.Equal(nil,r.Metadata.NovaInstance.Name,"LqnyzNfe") - fmt.Printf("xml name=%v\n", r.Metadata.NovaInstance.XMLName) - fmt.Printf("nova name=%v\n", r.Metadata.NovaInstance.Name) - + assert.Equal(nil, r.Metadata.NovaInstance.Name, "LqnyzNfe") + fmt.Printf("xml name=%#v\n", r.Metadata.NovaInstance.XMLName) + fmt.Printf("nova name=%#v\n", r.Metadata.NovaInstance.Name) + fmt.Printf("nova =%#v\n", r.Metadata) }