Skip to content

Commit

Permalink
Libvirt 6 7 (#16)
Browse files Browse the repository at this point in the history
* Add Vcpu Wait metric

* Bump libvirt-go to libvirt-go 7

* gitignore: add libvirt-exporter

* Get vcpu.Wait only if instance is active

* Update docker builder and build_static to use new libvirt

* Fix buildindocker to build dynamically linked with current libvirt

* Fix Dockerfile to build dynamically linked with alpine's libvirt

* Remove static builds and old building scripts

* Add new build scripts

* Remove libvirt-patches as we don't need them anymore

* Make readme more informative

* Add Dockerfile for ubuntu1604
AlexZzz authored Mar 26, 2021
1 parent d8f3f45 commit dbb86d4
Showing 124 changed files with 16,309 additions and 3,726 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea
/libvirt_exporter
libvirt-exporter
22 changes: 0 additions & 22 deletions Dockerbuildimage

This file was deleted.

41 changes: 9 additions & 32 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,41 +1,18 @@
# Stage 1: Build libvirt exporter
FROM golang:alpine

# Install dependencies
RUN apk add --update git gcc g++ make libc-dev portablexdr-dev linux-headers libnl-dev perl libtirpc-dev pkgconfig wget libtirpc libtirpc-static patch
RUN wget ftp://xmlsoft.org/libxml2/libxml2-2.9.4.tar.gz -P /tmp && \
tar -xf /tmp/libxml2-2.9.4.tar.gz -C /tmp
WORKDIR /tmp/libxml2-2.9.4
RUN ./configure --disable-shared --enable-static && \
make -j2 && \
make install
RUN wget https://libvirt.org/sources/libvirt-3.2.0.tar.xz -P /tmp && \
tar -xf /tmp/libvirt-3.2.0.tar.xz -C /tmp
WORKDIR /tmp/libvirt-3.2.0
COPY libvirt-patches libvirt-patches
ENV LIBVIRT_EXPORTER_PATH=/libvirt-exporter

RUN patch -i libvirt-patches/0001-musl.patch -p1 && \
./configure --disable-shared --enable-static --localstatedir=/var --without-storage-mpath && \
make -j2 && \
RUN apk add ca-certificates g++ git go libnl-dev linux-headers make libvirt-dev libvirt && \
wget ftp://xmlsoft.org/libxml2/libxml2-2.9.8.tar.gz -P /tmp && \
tar -xf /tmp/libxml2-2.9.8.tar.gz -C /tmp/ && \
cd /tmp/libxml2-2.9.8 && \
./configure && \
make -j$(nproc) && \
make install && \
sed -i 's/^Libs:.*/& -lnl -ltirpc -lxml2/' /usr/local/lib/pkgconfig/libvirt.pc

# Prepare working directory
ENV LIBVIRT_EXPORTER_PATH=/go/src/github.com/rumanzo/libvirt_exporter_improved
RUN mkdir -p $LIBVIRT_EXPORTER_PATH
mkdir -p $LIBVIRT_EXPORTER_PATH
WORKDIR $LIBVIRT_EXPORTER_PATH
COPY . .

# Build and strip exporter
RUN go get -d ./... && \
go build --ldflags '-extldflags "-static"' && \
strip libvirt-exporter

# Stage 2: Prepare final image
FROM scratch

# Copy binary from Stage 1
COPY --from=0 /go/src/github.com/rumanzo/libvirt_exporter_improved/libvirt-exporter .
RUN go build

# Entrypoint for starting exporter
ENTRYPOINT [ "./libvirt-exporter" ]
41 changes: 22 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
# Prometheus libvirt exporter
Project forked from https://github.com/kumina/libvirt_exporter and substantially rewritten.
Implemented support for several additional metrics, ceph rbd (and network block devices), ovs.
Implemented statistics collection using GetAllDomainStats

And then forked again from https://github.com/rumanzo/libvirt_exporter_improved and rewritten.
Implemented meta metrics and more info about disks, interfaces and domain.

This repository provides code for a Prometheus metrics exporter
for [libvirt](https://libvirt.org/). This exporter connects to any
libvirt daemon and exports per-domain metrics related to CPU, memory,
disk and network usage. By default, this exporter listens on TCP port
9177.

This exporter makes use of
[libvirt-go](https://github.com/libvirt/libvirt-go), the official Go
bindings for libvirt. This exporter make use of the
`GetAllDomainStats()`
- `Dockerfile` - creates a docker container with dynamically linked libvirt-exporter. Make an image and run with `docker container run -p9177:9177 -v /var/run/libvirt:/var/run/libvirt yourcontainername`. Based on the latest golang:alpine.
- `build-with` - builds dynamically linked libvirt-exporter in the container based on Dockerfile specified as an argument. Ex.: `build-with ./build_container/Dockerfile.ubuntu2004` will build libvirt-exporter for Ubuntu 20.04.

# Metrics
The following metrics/labels are being exported:

```
@@ -63,6 +50,22 @@ libvirt_domain_memory_stats_used_percent{domain="..."}
libvirt_up
```

Repository contains a shell script, `build_static.sh`, that builds a
statically linked copy of this exporter in an Alpine Linux based
container.
# Historical
Project forked from https://github.com/kumina/libvirt_exporter and substantially rewritten.
Implemented support for several additional metrics, ceph rbd (and network block devices), ovs.
Implemented statistics collection using GetAllDomainStats

And then forked again from https://github.com/rumanzo/libvirt_exporter_improved and rewritten.
Implemented meta metrics and more info about disks, interfaces and domain.

This repository provides code for a Prometheus metrics exporter
for [libvirt](https://libvirt.org/). This exporter connects to any
libvirt daemon and exports per-domain metrics related to CPU, memory,
disk and network usage. By default, this exporter listens on TCP port
9177.

This exporter makes use of
[libvirt-go](https://github.com/libvirt/libvirt-go), the official Go
bindings for libvirt. This exporter make use of the
`GetAllDomainStats()`

27 changes: 27 additions & 0 deletions build-with
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

if [[ "$#" -ne 1 ]]; then
printf "Need one arg. One of:\n"
ls -d ./build_containers/*
exit 1
fi

if [ -f "${1}" ]; then
DOCKERFILE=$1
else
echo "Wrong argument, must be one of:"
ls -d ./build_containers/*
exit 1
fi

set -xeu

DOCKERFILE=$1
BINARY_NAME=libvirt-exporter

docker build -t libvirtexporterbuild -f ${DOCKERFILE} .
docker run --rm \
-v "$PWD":/libvirt-exporter -w /libvirt-exporter \
-e GOOS=linux \
-e GOARCH=amd64 \
libvirtexporterbuild:latest go build -o ${BINARY_NAME}
10 changes: 10 additions & 0 deletions build_containers/Dockerfile.ubuntu1604
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM ubuntu:xenial

ENV PATH=$PATH:/usr/local/go/bin

RUN set -ex && \
apt update && \
DEBIAN_FRONTEND=noninteractive apt install -yq g++ libvirt0 libvirt-dev wget && \
wget https://golang.org/dl/go1.16.2.linux-amd64.tar.gz && \
rm -rf /usr/local/go && tar -C /usr/local -xzf go1.16.2.linux-amd64.tar.gz && \
export PATH=$PATH:/usr/local/go/bin
10 changes: 10 additions & 0 deletions build_containers/Dockerfile.ubuntu1804
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM ubuntu:bionic

ENV PATH=$PATH:/usr/local/go/bin

RUN set -ex && \
apt update && \
DEBIAN_FRONTEND=noninteractive apt install -yq g++ libvirt0 libvirt-dev wget && \
wget https://golang.org/dl/go1.16.2.linux-amd64.tar.gz && \
rm -rf /usr/local/go && tar -C /usr/local -xzf go1.16.2.linux-amd64.tar.gz && \
export PATH=$PATH:/usr/local/go/bin
10 changes: 10 additions & 0 deletions build_containers/Dockerfile.ubuntu2004
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM ubuntu:focal

ENV PATH=$PATH:/usr/local/go/bin

RUN set -ex && \
apt update && \
DEBIAN_FRONTEND=noninteractive apt install -yq g++ libvirt0 libvirt-dev wget && \
wget https://golang.org/dl/go1.16.2.linux-amd64.tar.gz && \
rm -rf /usr/local/go && tar -C /usr/local -xzf go1.16.2.linux-amd64.tar.gz && \
export PATH=$PATH:/usr/local/go/bin
41 changes: 0 additions & 41 deletions build_static.sh

This file was deleted.

18 changes: 0 additions & 18 deletions buildindocker.sh

This file was deleted.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ go 1.12
require (
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4 // indirect
github.com/libvirt/libvirt-go v4.0.0+incompatible
github.com/libvirt/libvirt-go v7.0.0+incompatible
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/prometheus/client_golang v1.1.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -29,6 +29,8 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxv
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/libvirt/libvirt-go v4.0.0+incompatible h1:dtUFWq34ja56W/I3V/OvqAYxp38jaG1KqvcXHeZDNEw=
github.com/libvirt/libvirt-go v4.0.0+incompatible/go.mod h1:34zsnB4iGeOv7Byj6qotuW8Ya4v4Tr43ttjz/F0wjLE=
github.com/libvirt/libvirt-go v7.0.0+incompatible h1:twXBsJe7klsz2Zogxm4GJF5aBRPmdY72RX8nDumB86A=
github.com/libvirt/libvirt-go v7.0.0+incompatible/go.mod h1:34zsnB4iGeOv7Byj6qotuW8Ya4v4Tr43ttjz/F0wjLE=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
28 changes: 0 additions & 28 deletions libvirt-patches/0001-musl.patch

This file was deleted.

23 changes: 22 additions & 1 deletion libvirt_exporter.go
Original file line number Diff line number Diff line change
@@ -84,6 +84,11 @@ var (
"Real CPU number, or one of the values from virVcpuHostCpuState",
[]string{"domain", "vcpu"},
nil)
libvirtDomainVcpuWaitDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_vcpu", "wait"),
"Vcpu's wait_sum metric. CONFIG_SCHEDSTATS has to be enabled",
[]string{"domain", "vcpu"},
nil)

libvirtDomainMetaBlockDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_block", "meta"),
@@ -317,7 +322,7 @@ func CollectDomain(ch chan<- prometheus.Metric, stat libvirt.DomainStats) error
domainStatsVcpu, err := stat.Domain.GetVcpus()
if err != nil {
lverr, ok := err.(libvirt.Error)
if ! ok || lverr.Code != libvirt.ERR_OPERATION_INVALID {
if !ok || lverr.Code != libvirt.ERR_OPERATION_INVALID {
return err
}
} else {
@@ -343,6 +348,21 @@ func CollectDomain(ch chan<- prometheus.Metric, stat libvirt.DomainStats) error
domainName,
strconv.FormatInt(int64(vcpu.Number), 10))
}
/* There's no Wait in GetVcpus()
* But there's no cpu number in libvirt.DomainStats
* Time and State are present in both structs
* So, let's take Wait here
*/
for cpuNum, vcpu := range stat.Vcpu {
if vcpu.WaitSet {
ch <- prometheus.MustNewConstMetric(
libvirtDomainVcpuWaitDesc,
prometheus.CounterValue,
float64(vcpu.Wait)/1000/1000/1000,
domainName,
strconv.FormatInt(int64(cpuNum), 10))
}
}
}

// Report block device statistics.
@@ -707,6 +727,7 @@ func (e *LibvirtExporter) Describe(ch chan<- *prometheus.Desc) {
ch <- libvirtDomainVcpuStateDesc
ch <- libvirtDomainVcpuTimeDesc
ch <- libvirtDomainVcpuCPUDesc
ch <- libvirtDomainVcpuWaitDesc

// Domain block stats
ch <- libvirtDomainMetaBlockDesc
Loading

0 comments on commit dbb86d4

Please sign in to comment.