forked from prometheus/node_exporter
/
virt.sh
executable file
·58 lines (54 loc) · 2.17 KB
/
virt.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/bin/bash
#
#
# Description: Expose metrics for detected platform virtualization
# Dependencies: virt-what (packages)
#
# The script creates one metric series for each detected virtualization platform
# reported by virt-what.
#
# Author: Clayton Coleman <smarterclayton@gmail.com>
set -o errexit
set -o nounset
set -o pipefail
v="${TMPDIR:-/tmp}/virt.working"
rm -f "${v}" || true
touch "${v}"
if [ -x /usr/sbin/virt-what ]
then
platforms=$(echo $( virt-what ) | tr '\n' ' ')
echo '# HELP virt_platform reports one series per detected virtualization type. If no type is detected, the type is "none".' >>"${v}"
echo '# TYPE virt_platform gauge' >>"${v}"
count=0
for platform in ${platforms}; do
if [[ -z "${platform}" ]]; then
continue
fi
count=$(( count + 1 ))
echo "virt_platform{type=\"${platform}\"} 1" >>"${v}"
done
# Attempt AWS detection on older (m4) and newer (m5) instance types, taken from https://serverfault.com/a/903599
if ([ -f /sys/hypervisor/uuid ] && [ `head -c 3 /sys/hypervisor/uuid` == "ec2" ]) ||
([ -r /sys/devices/virtual/dmi/id/product_uuid ] && [ `head -c 3 /sys/devices/virtual/dmi/id/product_uuid` == "EC2" ]); then
count=$(( count + 1 ))
echo "virt_platform{type=\"aws\"} 1" >>"${v}"
fi
# Attempt GCP detection
if dmidecode -s bios-vendor | grep Google; then
count=$(( count + 1 ))
echo "virt_platform{type=\"gcp\"} 1" >>"${v}"
fi
# Attempt OpenStack detection
if dmidecode | grep "Product Name: OpenStack Compute"; then
count=$(( count + 1 ))
echo "virt_platform{type=\"openstack\"} 1" >>"${v}"
fi
if [[ "${count}" -eq 0 ]]; then
line="$( printf 'virt_platform{type="none",bios_vendor="%q",bios_version="%q",system_manufacturer="%q",system_product_name="%q",system_version="%q",baseboard_manufacturer="%q",baseboard_product_name="%q"} 1' \
"$( dmidecode -s bios-vendor )" "$( dmidecode -s bios-version )" \
"$( dmidecode -s system-manufacturer )" "$( dmidecode -s system-product-name )" "$( dmidecode -s system-version )" \
"$( dmidecode -s baseboard-manufacturer )" "$( dmidecode -s baseboard-product-name )" )"
echo "${line//\\ / }" >>"${v}"
fi
fi
mv "${v}" virt.prom