| 1 | // Copyright 2017 Kumina, https://kumina.nl/ |
| 2 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | // you may not use this file except in compliance with the License. |
| 4 | // You may obtain a copy of the License at |
| 5 | // |
| 6 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | // |
| 8 | // Unless required by applicable law or agreed to in writing, software |
| 9 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | // See the License for the specific language governing permissions and |
| 12 | // limitations under the License. |
| 13 | |
| 14 | package main |
| 15 | |
| 16 | import ( |
| 17 | "encoding/xml" |
| 18 | "log" |
| 19 | "net/http" |
| 20 | "os" |
| 21 | |
| 22 | "github.com/kumina/libvirt_exporter/libvirt_schema" |
| 23 | "github.com/libvirt/libvirt-go" |
| 24 | "github.com/prometheus/client_golang/prometheus" |
| 25 | "github.com/prometheus/client_golang/prometheus/promhttp" |
| 26 | "gopkg.in/alecthomas/kingpin.v2" |
| 27 | ) |
| 28 | |
| 29 | // LibvirtExporter implements a Prometheus exporter for libvirt state. |
| 30 | type LibvirtExporter struct { |
| 31 | uri string |
| 32 | exportNovaMetadata bool |
| 33 | |
| 34 | libvirtUpDesc *prometheus.Desc |
| 35 | libvirtDomainsNumberDesc *prometheus.Desc |
| 36 | |
| 37 | libvirtDomainStateCode *prometheus.Desc |
| 38 | libvirtDomainInfoMaxMemDesc *prometheus.Desc |
| 39 | libvirtDomainInfoMemoryDesc *prometheus.Desc |
| 40 | libvirtDomainInfoNrVirtCpuDesc *prometheus.Desc |
| 41 | libvirtDomainInfoCpuTimeDesc *prometheus.Desc |
| 42 | |
| 43 | libvirtDomainBlockRdBytesDesc *prometheus.Desc |
| 44 | libvirtDomainBlockRdReqDesc *prometheus.Desc |
| 45 | libvirtDomainBlockRdTotalTimesDesc *prometheus.Desc |
| 46 | libvirtDomainBlockWrBytesDesc *prometheus.Desc |
| 47 | libvirtDomainBlockWrReqDesc *prometheus.Desc |
| 48 | libvirtDomainBlockWrTotalTimesDesc *prometheus.Desc |
| 49 | libvirtDomainBlockFlushReqDesc *prometheus.Desc |
| 50 | libvirtDomainBlockFlushTotalTimesDesc *prometheus.Desc |
| 51 | |
| 52 | libvirtDomainInterfaceRxBytesDesc *prometheus.Desc |
| 53 | libvirtDomainInterfaceRxPacketsDesc *prometheus.Desc |
| 54 | libvirtDomainInterfaceRxErrsDesc *prometheus.Desc |
| 55 | libvirtDomainInterfaceRxDropDesc *prometheus.Desc |
| 56 | libvirtDomainInterfaceTxBytesDesc *prometheus.Desc |
| 57 | libvirtDomainInterfaceTxPacketsDesc *prometheus.Desc |
| 58 | libvirtDomainInterfaceTxErrsDesc *prometheus.Desc |
| 59 | libvirtDomainInterfaceTxDropDesc *prometheus.Desc |
| 60 | } |
| 61 | |
| 62 | // NewLibvirtExporter creates a new Prometheus exporter for libvirt. |
| 63 | func NewLibvirtExporter(uri string, exportNovaMetadata bool) (*LibvirtExporter, error) { |
| 64 | var domainLabels []string |
| 65 | if exportNovaMetadata { |
| 66 | domainLabels = []string{"domain", "uuid", "name", "flavor", "project_name"} |
| 67 | } else { |
| 68 | domainLabels = []string{"domain", "uuid"} |
| 69 | } |
| 70 | return &LibvirtExporter{ |
| 71 | uri: uri, |
| 72 | exportNovaMetadata: exportNovaMetadata, |
| 73 | libvirtUpDesc: prometheus.NewDesc( |
| 74 | prometheus.BuildFQName("libvirt", "", "up"), |
| 75 | "Whether scraping libvirt's metrics was successful.", |
| 76 | nil, |
| 77 | nil), |
| 78 | libvirtDomainsNumberDesc: prometheus.NewDesc( |
| 79 | prometheus.BuildFQName("libvirt", "", "domains_number"), |
| 80 | "Number of domains.", |
| 81 | nil, |
| 82 | nil), |
| 83 | libvirtDomainStateCode: prometheus.NewDesc( |
| 84 | prometheus.BuildFQName("libvirt", "", "domain_state_code"), |
| 85 | "State of the domain.", |
| 86 | domainLabels, |
| 87 | nil), |
| 88 | libvirtDomainInfoMaxMemDesc: prometheus.NewDesc( |
| 89 | prometheus.BuildFQName("libvirt", "domain_info", "maximum_memory_bytes"), |
| 90 | "Maximum allowed memory of the domain, in bytes.", |
| 91 | domainLabels, |
| 92 | nil), |
| 93 | libvirtDomainInfoMemoryDesc: prometheus.NewDesc( |
| 94 | prometheus.BuildFQName("libvirt", "domain_info", "memory_usage_bytes"), |
| 95 | "Memory usage of the domain, in bytes.", |
| 96 | domainLabels, |
| 97 | nil), |
| 98 | libvirtDomainInfoNrVirtCpuDesc: prometheus.NewDesc( |
| 99 | prometheus.BuildFQName("libvirt", "domain_info", "virtual_cpus"), |
| 100 | "Number of virtual CPUs for the domain.", |
| 101 | domainLabels, |
| 102 | nil), |
| 103 | libvirtDomainInfoCpuTimeDesc: prometheus.NewDesc( |
| 104 | prometheus.BuildFQName("libvirt", "domain_info", "cpu_time_seconds_total"), |
| 105 | "Amount of CPU time used by the domain, in seconds.", |
| 106 | domainLabels, |
| 107 | nil), |
| 108 | libvirtDomainBlockRdBytesDesc: prometheus.NewDesc( |
| 109 | prometheus.BuildFQName("libvirt", "domain_block_stats", "read_bytes_total"), |
| 110 | "Number of bytes read from a block device, in bytes.", |
| 111 | append(domainLabels, "source_file", "target_device"), |
| 112 | nil), |
| 113 | libvirtDomainBlockRdReqDesc: prometheus.NewDesc( |
| 114 | prometheus.BuildFQName("libvirt", "domain_block_stats", "read_requests_total"), |
| 115 | "Number of read requests from a block device.", |
| 116 | append(domainLabels, "source_file", "target_device"), |
| 117 | nil), |
| 118 | libvirtDomainBlockRdTotalTimesDesc: prometheus.NewDesc( |
| 119 | prometheus.BuildFQName("libvirt", "domain_block_stats", "read_seconds_total"), |
| 120 | "Amount of time spent reading from a block device, in seconds.", |
| 121 | append(domainLabels, "source_file", "target_device"), |
| 122 | nil), |
| 123 | libvirtDomainBlockWrBytesDesc: prometheus.NewDesc( |
| 124 | prometheus.BuildFQName("libvirt", "domain_block_stats", "write_bytes_total"), |
| 125 | "Number of bytes written from a block device, in bytes.", |
| 126 | append(domainLabels, "source_file", "target_device"), |
| 127 | nil), |
| 128 | |
| 129 | libvirtDomainBlockWrReqDesc: prometheus.NewDesc( |
| 130 | prometheus.BuildFQName("libvirt", "domain_block_stats", "write_requests_total"), |
| 131 | "Number of write requests from a block device.", |
| 132 | append(domainLabels, "source_file", "target_device"), |
| 133 | nil), |
| 134 | libvirtDomainBlockWrTotalTimesDesc: prometheus.NewDesc( |
| 135 | prometheus.BuildFQName("libvirt", "domain_block_stats", "write_seconds_total"), |
| 136 | "Amount of time spent writing from a block device, in seconds.", |
| 137 | append(domainLabels, "source_file", "target_device"), |
| 138 | nil), |
| 139 | libvirtDomainBlockFlushReqDesc: prometheus.NewDesc( |
| 140 | prometheus.BuildFQName("libvirt", "domain_block_stats", "flush_requests_total"), |
| 141 | "Number of flush requests from a block device.", |
| 142 | append(domainLabels, "source_file", "target_device"), |
| 143 | nil), |
| 144 | libvirtDomainBlockFlushTotalTimesDesc: prometheus.NewDesc( |
| 145 | prometheus.BuildFQName("libvirt", "domain_block_stats", "flush_seconds_total"), |
| 146 | "Amount of time spent flushing of a block device, in seconds.", |
| 147 | append(domainLabels, "source_file", "target_device"), |
| 148 | nil), |
| 149 | |
| 150 | libvirtDomainInterfaceRxBytesDesc: prometheus.NewDesc( |
| 151 | prometheus.BuildFQName("libvirt", "domain_interface_stats", "receive_bytes_total"), |
| 152 | "Number of bytes received on a network interface, in bytes.", |
| 153 | append(domainLabels, "source_bridge", "target_device"), |
| 154 | nil), |
| 155 | libvirtDomainInterfaceRxPacketsDesc: prometheus.NewDesc( |
| 156 | prometheus.BuildFQName("libvirt", "domain_interface_stats", "receive_packets_total"), |
| 157 | "Number of packets received on a network interface.", |
| 158 | append(domainLabels, "source_bridge", "target_device"), |
| 159 | nil), |
| 160 | libvirtDomainInterfaceRxErrsDesc: prometheus.NewDesc( |
| 161 | prometheus.BuildFQName("libvirt", "domain_interface_stats", "receive_errors_total"), |
| 162 | "Number of packet receive errors on a network interface.", |
| 163 | append(domainLabels, "source_bridge", "target_device"), |
| 164 | nil), |
| 165 | libvirtDomainInterfaceRxDropDesc: prometheus.NewDesc( |
| 166 | prometheus.BuildFQName("libvirt", "domain_interface_stats", "receive_drops_total"), |
| 167 | "Number of packet receive drops on a network interface.", |
| 168 | append(domainLabels, "source_bridge", "target_device"), |
| 169 | nil), |
| 170 | libvirtDomainInterfaceTxBytesDesc: prometheus.NewDesc( |
| 171 | prometheus.BuildFQName("libvirt", "domain_interface_stats", "transmit_bytes_total"), |
| 172 | "Number of bytes transmitted on a network interface, in bytes.", |
| 173 | append(domainLabels, "source_bridge", "target_device"), |
| 174 | nil), |
| 175 | libvirtDomainInterfaceTxPacketsDesc: prometheus.NewDesc( |
| 176 | prometheus.BuildFQName("libvirt", "domain_interface_stats", "transmit_packets_total"), |
| 177 | "Number of packets transmitted on a network interface.", |
| 178 | append(domainLabels, "source_bridge", "target_device"), |
| 179 | nil), |
| 180 | libvirtDomainInterfaceTxErrsDesc: prometheus.NewDesc( |
| 181 | prometheus.BuildFQName("libvirt", "domain_interface_stats", "transmit_errors_total"), |
| 182 | "Number of packet transmit errors on a network interface.", |
| 183 | append(domainLabels, "source_bridge", "target_device"), |
| 184 | nil), |
| 185 | libvirtDomainInterfaceTxDropDesc: prometheus.NewDesc( |
| 186 | prometheus.BuildFQName("libvirt", "domain_interface_stats", "transmit_drops_total"), |
| 187 | "Number of packet transmit drops on a network interface.", |
| 188 | append(domainLabels, "source_bridge", "target_device"), |
| 189 | nil), |
| 190 | }, nil |
| 191 | } |
| 192 | |
| 193 | // Describe returns metadata for all Prometheus metrics that may be exported. |
| 194 | func (e *LibvirtExporter) Describe(ch chan<- *prometheus.Desc) { |
| 195 | ch <- e.libvirtUpDesc |
| 196 | ch <- e.libvirtDomainsNumberDesc |
| 197 | |
| 198 | ch <- e.libvirtDomainStateCode |
| 199 | ch <- e.libvirtDomainInfoMaxMemDesc |
| 200 | ch <- e.libvirtDomainInfoMemoryDesc |
| 201 | ch <- e.libvirtDomainInfoNrVirtCpuDesc |
| 202 | ch <- e.libvirtDomainInfoCpuTimeDesc |
| 203 | |
| 204 | ch <- e.libvirtDomainBlockRdBytesDesc |
| 205 | ch <- e.libvirtDomainBlockRdReqDesc |
| 206 | ch <- e.libvirtDomainBlockRdTotalTimesDesc |
| 207 | ch <- e.libvirtDomainBlockWrBytesDesc |
| 208 | ch <- e.libvirtDomainBlockWrReqDesc |
| 209 | ch <- e.libvirtDomainBlockWrTotalTimesDesc |
| 210 | ch <- e.libvirtDomainBlockFlushReqDesc |
| 211 | ch <- e.libvirtDomainBlockFlushTotalTimesDesc |
| 212 | } |
| 213 | |
| 214 | // Collect scrapes Prometheus metrics from libvirt. |
| 215 | func (e *LibvirtExporter) Collect(ch chan<- prometheus.Metric) { |
| 216 | err := e.CollectFromLibvirt(ch) |
| 217 | if err == nil { |
| 218 | ch <- prometheus.MustNewConstMetric( |
| 219 | e.libvirtUpDesc, |
| 220 | prometheus.GaugeValue, |
| 221 | 1.0) |
| 222 | } else { |
| 223 | log.Printf("Failed to scrape metrics: %s", err) |
| 224 | ch <- prometheus.MustNewConstMetric( |
| 225 | e.libvirtUpDesc, |
| 226 | prometheus.GaugeValue, |
| 227 | 0.0) |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | // CollectFromLibvirt obtains Prometheus metrics from all domains in a |
| 232 | // libvirt setup. |
| 233 | func (e *LibvirtExporter) CollectFromLibvirt(ch chan<- prometheus.Metric) error { |
| 234 | conn, err := libvirt.NewConnect(e.uri) |
| 235 | if err != nil { |
| 236 | return err |
| 237 | } |
| 238 | defer conn.Close() |
| 239 | |
| 240 | // Use ListDomains() as opposed to using ListAllDomains(), as |
| 241 | // the latter is unsupported when talking to a system using |
| 242 | // libvirt 0.9.12 or older. |
| 243 | domainIds, err := conn.ListDomains() |
| 244 | if err != nil { |
| 245 | return err |
| 246 | } |
| 247 | // number of domains |
| 248 | ch <- prometheus.MustNewConstMetric( |
| 249 | e.libvirtDomainsNumberDesc, |
| 250 | prometheus.GaugeValue, |
| 251 | float64(len(domainIds))) |
| 252 | |
| 253 | for _, id := range domainIds { |
| 254 | domain, err := conn.LookupDomainById(id) |
| 255 | if err == nil { |
| 256 | err = e.CollectDomain(ch, domain) |
| 257 | domain.Free() |
| 258 | if err != nil { |
| 259 | return err |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | return nil |
| 265 | } |
| 266 | |
| 267 | // CollectDomain extracts Prometheus metrics from a libvirt domain. |
| 268 | func (e *LibvirtExporter) CollectDomain(ch chan<- prometheus.Metric, domain *libvirt.Domain) error { |
| 269 | // Decode XML description of domain to get block device names, etc. |
| 270 | xmlDesc, err := domain.GetXMLDesc(0) |
| 271 | if err != nil { |
| 272 | return err |
| 273 | } |
| 274 | var desc libvirt_schema.Domain |
| 275 | err = xml.Unmarshal([]byte(xmlDesc), &desc) |
| 276 | if err != nil { |
| 277 | return err |
| 278 | } |
| 279 | |
| 280 | domainName, err := domain.GetName() |
| 281 | if err != nil { |
| 282 | return err |
| 283 | } |
| 284 | var domainUUID = desc.UUID |
| 285 | |
| 286 | // Extract domain label valuies |
| 287 | var domainLabelValues []string |
| 288 | if e.exportNovaMetadata { |
| 289 | var ( |
| 290 | novaName = desc.Metadata.NovaInstance.Name |
| 291 | novaFlavor = desc.Metadata.NovaInstance.Flavor.Name |
| 292 | novaProjectName = desc.Metadata.NovaInstance.Owner.ProjectName |
| 293 | ) |
| 294 | domainLabelValues = []string{domainName, domainUUID, novaName, novaFlavor, novaProjectName} |
| 295 | } else { |
| 296 | domainLabelValues = []string{domainName, domainUUID} |
| 297 | } |
| 298 | |
| 299 | // Report domain info. |
| 300 | info, err := domain.GetInfo() |
| 301 | if err != nil { |
| 302 | return err |
| 303 | } |
| 304 | ch <- prometheus.MustNewConstMetric( |
| 305 | e.libvirtDomainStateCode, |
| 306 | prometheus.GaugeValue, |
| 307 | float64(info.State), |
| 308 | domainLabelValues...) |
| 309 | |
| 310 | ch <- prometheus.MustNewConstMetric( |
| 311 | e.libvirtDomainInfoMaxMemDesc, |
| 312 | prometheus.GaugeValue, |
| 313 | float64(info.MaxMem)*1024, |
| 314 | domainLabelValues...) |
| 315 | ch <- prometheus.MustNewConstMetric( |
| 316 | e.libvirtDomainInfoMemoryDesc, |
| 317 | prometheus.GaugeValue, |
| 318 | float64(info.Memory)*1024, |
| 319 | domainLabelValues...) |
| 320 | ch <- prometheus.MustNewConstMetric( |
| 321 | e.libvirtDomainInfoNrVirtCpuDesc, |
| 322 | prometheus.GaugeValue, |
| 323 | float64(info.NrVirtCpu), |
| 324 | domainLabelValues...) |
| 325 | ch <- prometheus.MustNewConstMetric( |
| 326 | e.libvirtDomainInfoCpuTimeDesc, |
| 327 | prometheus.CounterValue, |
| 328 | float64(info.CpuTime)/1e9, |
| 329 | domainLabelValues...) |
| 330 | |
| 331 | // Report block device statistics. |
| 332 | for _, disk := range desc.Devices.Disks { |
| 333 | if disk.Device == "cdrom" || disk.Device == "fd" { |
| 334 | continue |
| 335 | } |
| 336 | |
| 337 | blockStats, err := domain.BlockStats(disk.Target.Device) |
| 338 | if err != nil { |
| 339 | return err |
| 340 | } |
| 341 | |
| 342 | if blockStats.RdBytesSet { |
| 343 | ch <- prometheus.MustNewConstMetric( |
| 344 | e.libvirtDomainBlockRdBytesDesc, |
| 345 | prometheus.CounterValue, |
| 346 | float64(blockStats.RdBytes), |
| 347 | append(domainLabelValues, disk.Source.File, disk.Target.Device)...) |
| 348 | } |
| 349 | if blockStats.RdReqSet { |
| 350 | ch <- prometheus.MustNewConstMetric( |
| 351 | e.libvirtDomainBlockRdReqDesc, |
| 352 | prometheus.CounterValue, |
| 353 | float64(blockStats.RdReq), |
| 354 | append(domainLabelValues, disk.Source.File, disk.Target.Device)...) |
| 355 | } |
| 356 | if blockStats.RdTotalTimesSet { |
| 357 | ch <- prometheus.MustNewConstMetric( |
| 358 | e.libvirtDomainBlockRdTotalTimesDesc, |
| 359 | prometheus.CounterValue, |
| 360 | float64(blockStats.RdTotalTimes)/1e9, |
| 361 | append(domainLabelValues, disk.Source.File, disk.Target.Device)...) |
| 362 | } |
| 363 | if blockStats.WrBytesSet { |
| 364 | ch <- prometheus.MustNewConstMetric( |
| 365 | e.libvirtDomainBlockWrBytesDesc, |
| 366 | prometheus.CounterValue, |
| 367 | float64(blockStats.WrBytes), |
| 368 | append(domainLabelValues, disk.Source.File, disk.Target.Device)...) |
| 369 | } |
| 370 | if blockStats.WrReqSet { |
| 371 | ch <- prometheus.MustNewConstMetric( |
| 372 | e.libvirtDomainBlockWrReqDesc, |
| 373 | prometheus.CounterValue, |
| 374 | float64(blockStats.WrReq), |
| 375 | append(domainLabelValues, disk.Source.File, disk.Target.Device)...) |
| 376 | } |
| 377 | if blockStats.WrTotalTimesSet { |
| 378 | ch <- prometheus.MustNewConstMetric( |
| 379 | e.libvirtDomainBlockWrTotalTimesDesc, |
| 380 | prometheus.CounterValue, |
| 381 | float64(blockStats.WrTotalTimes)/1e9, |
| 382 | append(domainLabelValues, disk.Source.File, disk.Target.Device)...) |
| 383 | } |
| 384 | if blockStats.FlushReqSet { |
| 385 | ch <- prometheus.MustNewConstMetric( |
| 386 | e.libvirtDomainBlockFlushReqDesc, |
| 387 | prometheus.CounterValue, |
| 388 | float64(blockStats.FlushReq), |
| 389 | append(domainLabelValues, disk.Source.File, disk.Target.Device)...) |
| 390 | } |
| 391 | if blockStats.FlushTotalTimesSet { |
| 392 | ch <- prometheus.MustNewConstMetric( |
| 393 | e.libvirtDomainBlockFlushTotalTimesDesc, |
| 394 | prometheus.CounterValue, |
| 395 | float64(blockStats.FlushTotalTimes)/1e9, |
| 396 | append(domainLabelValues, disk.Source.File, disk.Target.Device)...) |
| 397 | } |
| 398 | // Skip "Errs", as the documentation does not clearly |
| 399 | // explain what this means. |
| 400 | } |
| 401 | |
| 402 | // Report network interface statistics. |
| 403 | for _, iface := range desc.Devices.Interfaces { |
| 404 | if iface.Target.Device == "" { |
| 405 | continue |
| 406 | } |
| 407 | interfaceStats, err := domain.InterfaceStats(iface.Target.Device) |
| 408 | if err != nil { |
| 409 | return err |
| 410 | } |
| 411 | |
| 412 | if interfaceStats.RxBytesSet { |
| 413 | ch <- prometheus.MustNewConstMetric( |
| 414 | e.libvirtDomainInterfaceRxBytesDesc, |
| 415 | prometheus.CounterValue, |
| 416 | float64(interfaceStats.RxBytes), |
| 417 | append(domainLabelValues, iface.Source.Bridge, iface.Target.Device)...) |
| 418 | } |
| 419 | if interfaceStats.RxPacketsSet { |
| 420 | ch <- prometheus.MustNewConstMetric( |
| 421 | e.libvirtDomainInterfaceRxPacketsDesc, |
| 422 | prometheus.CounterValue, |
| 423 | float64(interfaceStats.RxPackets), |
| 424 | append(domainLabelValues, iface.Source.Bridge, iface.Target.Device)...) |
| 425 | } |
| 426 | if interfaceStats.RxErrsSet { |
| 427 | ch <- prometheus.MustNewConstMetric( |
| 428 | e.libvirtDomainInterfaceRxErrsDesc, |
| 429 | prometheus.CounterValue, |
| 430 | float64(interfaceStats.RxErrs), |
| 431 | append(domainLabelValues, iface.Source.Bridge, iface.Target.Device)...) |
| 432 | } |
| 433 | if interfaceStats.RxDropSet { |
| 434 | ch <- prometheus.MustNewConstMetric( |
| 435 | e.libvirtDomainInterfaceRxDropDesc, |
| 436 | prometheus.CounterValue, |
| 437 | float64(interfaceStats.RxDrop), |
| 438 | append(domainLabelValues, iface.Source.Bridge, iface.Target.Device)...) |
| 439 | } |
| 440 | if interfaceStats.TxBytesSet { |
| 441 | ch <- prometheus.MustNewConstMetric( |
| 442 | e.libvirtDomainInterfaceTxBytesDesc, |
| 443 | prometheus.CounterValue, |
| 444 | float64(interfaceStats.TxBytes), |
| 445 | append(domainLabelValues, iface.Source.Bridge, iface.Target.Device)...) |
| 446 | } |
| 447 | if interfaceStats.TxPacketsSet { |
| 448 | ch <- prometheus.MustNewConstMetric( |
| 449 | e.libvirtDomainInterfaceTxPacketsDesc, |
| 450 | prometheus.CounterValue, |
| 451 | float64(interfaceStats.TxPackets), |
| 452 | append(domainLabelValues, iface.Source.Bridge, iface.Target.Device)...) |
| 453 | } |
| 454 | if interfaceStats.TxErrsSet { |
| 455 | ch <- prometheus.MustNewConstMetric( |
| 456 | e.libvirtDomainInterfaceTxErrsDesc, |
| 457 | prometheus.CounterValue, |
| 458 | float64(interfaceStats.TxErrs), |
| 459 | append(domainLabelValues, iface.Source.Bridge, iface.Target.Device)...) |
| 460 | } |
| 461 | if interfaceStats.TxDropSet { |
| 462 | ch <- prometheus.MustNewConstMetric( |
| 463 | e.libvirtDomainInterfaceTxDropDesc, |
| 464 | prometheus.CounterValue, |
| 465 | float64(interfaceStats.TxDrop), |
| 466 | append(domainLabelValues, iface.Source.Bridge, iface.Target.Device)...) |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | return nil |
| 471 | } |
| 472 | |
| 473 | func main() { |
| 474 | var ( |
| 475 | app = kingpin.New("libvirt_exporter", "Prometheus metrics exporter for libvirt") |
| 476 | listenAddress = app.Flag("web.listen-address", "Address to listen on for web interface and telemetry.").Default(":9177").String() |
| 477 | metricsPath = app.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").String() |
| 478 | libvirtURI = app.Flag("libvirt.uri", "Libvirt URI from which to extract metrics.").Default("qemu:///system").String() |
| 479 | libvirtExportNovaMetadata = app.Flag("libvirt.export-nova-metadata", "Export OpenStack Nova specific labels from libvirt domain xml").Default("false").Bool() |
| 480 | ) |
| 481 | kingpin.MustParse(app.Parse(os.Args[1:])) |
| 482 | |
| 483 | exporter, err := NewLibvirtExporter(*libvirtURI, *libvirtExportNovaMetadata) |
| 484 | if err != nil { |
| 485 | panic(err) |
| 486 | } |
| 487 | prometheus.MustRegister(exporter) |
| 488 | |
| 489 | http.Handle(*metricsPath, promhttp.Handler()) |
| 490 | http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 491 | w.Write([]byte(` |
| 492 | <html> |
| 493 | <head><title>Libvirt Exporter</title></head> |
| 494 | <body> |
| 495 | <h1>Libvirt Exporter</h1> |
| 496 | <p><a href='` + *metricsPath + `'>Metrics</a></p> |
| 497 | </body> |
| 498 | </html>`)) |
| 499 | }) |
| 500 | log.Fatal(http.ListenAndServe(*listenAddress, nil)) |
| 501 | } |