Skip to content

Commit

Permalink
add userName userId projectName projectId
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangjianweibj committed Feb 26, 2019
1 parent 809f129 commit 4fa9a96
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 63 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 23 additions & 6 deletions libvirt_schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand All @@ -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 {
Expand Down Expand Up @@ -48,4 +65,4 @@ type InterfaceSource struct {

type InterfaceTarget struct {
Device string `xml:"dev,attr"`
}
}
20 changes: 9 additions & 11 deletions libvirt_schema/tools.go
Original file line number Diff line number Diff line change
@@ -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)
)



88 changes: 53 additions & 35 deletions prometheus-libvirt-exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
}
)

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
Loading

0 comments on commit 4fa9a96

Please sign in to comment.