summaryrefslogtreecommitdiffstats
path: root/daemon/system-info.sh
diff options
context:
space:
mode:
Diffstat (limited to 'daemon/system-info.sh')
-rwxr-xr-xdaemon/system-info.sh123
1 files changed, 76 insertions, 47 deletions
diff --git a/daemon/system-info.sh b/daemon/system-info.sh
index 7fb2f25b..12553e3d 100755
--- a/daemon/system-info.sh
+++ b/daemon/system-info.sh
@@ -10,6 +10,18 @@ ARCHITECTURE="$(uname -m)"
# -------------------------------------------------------------------------------------------------
# detect the virtualization and possibly the container technology
+# systemd-detect-virt: https://github.com/systemd/systemd/blob/df423851fcc05cf02281d11aab6aee7b476c1c3b/src/basic/virt.c#L999
+# lscpu: https://github.com/util-linux/util-linux/blob/581b77da7aa4a5205902857184d555bed367e3e0/sys-utils/lscpu.c#L52
+virtualization_normalize_name() {
+ vname="$1"
+ case "$vname" in
+ "User-mode Linux") vname="uml" ;;
+ "Windows Subsystem for Linux") vname="wsl" ;;
+ esac
+
+ echo "$vname" | tr '[:upper:]' '[:lower:]' | sed 's/ /-/g'
+}
+
CONTAINER="unknown"
CONT_DETECTION="none"
CONTAINER_IS_OFFICIAL_IMAGE="${NETDATA_OFFICIAL_IMAGE:-false}"
@@ -18,25 +30,31 @@ if [ -z "${VIRTUALIZATION}" ]; then
VIRTUALIZATION="unknown"
VIRT_DETECTION="none"
- if [ -n "$(command -v systemd-detect-virt 2> /dev/null)" ]; then
+ if command -v systemd-detect-virt >/dev/null 2>&1; then
VIRTUALIZATION="$(systemd-detect-virt -v)"
VIRT_DETECTION="systemd-detect-virt"
CONTAINER=${CONTAINER:-$(systemd-detect-virt -c)}
CONT_DETECTION="systemd-detect-virt"
- else
- if grep -q "^flags.*hypervisor" /proc/cpuinfo 2> /dev/null; then
- VIRTUALIZATION="hypervisor"
- VIRT_DETECTION="/proc/cpuinfo"
- elif [ -n "$(command -v dmidecode)" ] && dmidecode -s system-product-name 2> /dev/null | grep -q "VMware\|Virtual\|KVM\|Bochs"; then
- VIRTUALIZATION="$(dmidecode -s system-product-name)"
- VIRT_DETECTION="dmidecode"
- else
- VIRTUALIZATION="none"
- fi
+ elif command -v lscpu >/dev/null 2>&1; then
+ VIRTUALIZATION=$(lscpu | grep "Hypervisor vendor:" | cut -d: -f 2 | awk '{$1=$1};1')
+ [ -n "$VIRTUALIZATION" ] && VIRT_DETECTION="lscpu"
+ [ -z "$VIRTUALIZATION" ] && lscpu | grep -q "Virtualization:" && VIRTUALIZATION="none"
+ elif command -v dmidecode >/dev/null 2>&1; then
+ VIRTUALIZATION=$(dmidecode -s system-product-name 2>/dev/null | grep "VMware\|Virtual\|KVM\|Bochs")
+ [ -n "$VIRTUALIZATION" ] && VIRT_DETECTION="dmidecode"
+ fi
+
+ if [ -z "${VIRTUALIZATION}" ] && [ "${KERNEL_NAME}" = "FreeBSD" ]; then
+ VIRTUALIZATION=$(sysctl kern.vm_guest 2>/dev/null | cut -d: -f 2 | awk '{$1=$1};1')
+ [ -n "$VIRTUALIZATION" ] && VIRT_DETECTION="sysctl"
fi
+
if [ -z "${VIRTUALIZATION}" ]; then
# Output from the command is outside of spec
VIRTUALIZATION="unknown"
+ VIRT_DETECTION="none"
+ elif [ "$VIRTUALIZATION" != "none" ] && [ "$VIRTUALIZATION" != "unknown" ]; then
+ VIRTUALIZATION=$(virtualization_normalize_name $VIRTUALIZATION)
fi
else
# Passed from outside - probably in docker run
@@ -332,18 +350,23 @@ DISK_SIZE="unknown"
DISK_DETECTION="none"
if [ "${KERNEL_NAME}" = "Darwin" ]; then
- types='hfs'
+ if DISK_SIZE=$(diskutil info / 2>/dev/null | awk '/Disk Size/ {total += substr($5,2,length($5))} END { print total }') &&
+ [ -n "$DISK_SIZE" ] && [ "$DISK_SIZE" != "0" ]; then
+ DISK_DETECTION="diskutil"
+ else
+ types='hfs'
- if (lsvfs | grep -q apfs); then
- types="${types},apfs"
- fi
+ if (lsvfs | grep -q apfs); then
+ types="${types},apfs"
+ fi
- if (lsvfs | grep -q ufs); then
- types="${types},ufs"
- fi
+ if (lsvfs | grep -q ufs); then
+ types="${types},ufs"
+ fi
- DISK_DETECTION="df"
- DISK_SIZE=$(($(/bin/df -k -t ${types} | tail -n +2 | sed -E 's/\/dev\/disk([[:digit:]]*)s[[:digit:]]*/\/dev\/disk\1/g' | sort -k 1 | awk -F ' ' '{s=$NF;for(i=NF-1;i>=1;i--)s=s FS $i;print s}' | uniq -f 9 | awk '{print $8}' | tr '\n' '+' | rev | cut -f 2- -d '+' | rev) * 1024))
+ DISK_DETECTION="df"
+ DISK_SIZE=$(($(/bin/df -k -t ${types} | tail -n +2 | sed -E 's/\/dev\/disk([[:digit:]]*)s[[:digit:]]*/\/dev\/disk\1/g' | sort -k 1 | awk -F ' ' '{s=$NF;for(i=NF-1;i>=1;i--)s=s FS $i;print s}' | uniq -f 9 | awk '{print $8}' | tr '\n' '+' | rev | cut -f 2- -d '+' | rev) * 1024))
+ fi
elif [ "${KERNEL_NAME}" = FreeBSD ]; then
types='ufs'
@@ -411,37 +434,43 @@ CLOUD_INSTANCE_TYPE="unknown"
CLOUD_INSTANCE_REGION="unknown"
if [ "${VIRTUALIZATION}" != "none" ] && command -v curl > /dev/null 2>&1; then
- # Try AWS IMDSv2
- if [ "${CLOUD_TYPE}" = "unknown" ]; then
- AWS_IMDS_TOKEN="$(curl --fail -s -m 5 --noproxy "*" -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")"
- if [ -n "${AWS_IMDS_TOKEN}" ]; then
- CLOUD_TYPE="AWS"
- CLOUD_INSTANCE_TYPE="$(curl --fail -s -m 5 --noproxy "*" -H "X-aws-ec2-metadata-token: $AWS_IMDS_TOKEN" -v "http://169.254.169.254/latest/meta-data/instance-type" 2> /dev/null)"
- CLOUD_INSTANCE_REGION="$(curl --fail -s -m 5 --noproxy "*" -H "X-aws-ec2-metadata-token: $AWS_IMDS_TOKEN" -v "http://169.254.169.254/latest/meta-data/placement/region" 2> /dev/null)"
+ # Returned HTTP status codes: GCP is 200, AWS is 200, DO is 404.
+ curl --fail -s -m 1 --noproxy "*" http://169.254.169.254 >/dev/null 2>&1
+ ret=$?
+ # anything but operation timeout.
+ if [ "$ret" != 28 ]; then
+ # Try AWS IMDSv2
+ if [ "${CLOUD_TYPE}" = "unknown" ]; then
+ AWS_IMDS_TOKEN="$(curl --fail -s --connect-timeout 1 -m 3 --noproxy "*" -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")"
+ if [ -n "${AWS_IMDS_TOKEN}" ]; then
+ CLOUD_TYPE="AWS"
+ CLOUD_INSTANCE_TYPE="$(curl --fail -s --connect-timeout 1 -m 3 --noproxy "*" -H "X-aws-ec2-metadata-token: $AWS_IMDS_TOKEN" -v "http://169.254.169.254/latest/meta-data/instance-type" 2>/dev/null)"
+ CLOUD_INSTANCE_REGION="$(curl --fail -s --connect-timeout 1 -m 3 --noproxy "*" -H "X-aws-ec2-metadata-token: $AWS_IMDS_TOKEN" -v "http://169.254.169.254/latest/meta-data/placement/region" 2>/dev/null)"
+ fi
fi
- fi
- # Try GCE computeMetadata v1
- if [ "${CLOUD_TYPE}" = "unknown" ]; then
- if [ -n "$(curl --fail -s -m 5 --noproxy "*" -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1")" ]; then
- CLOUD_TYPE="GCP"
- CLOUD_INSTANCE_TYPE="$(curl --fail -s -m 5 --noproxy "*" -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/machine-type")"
- [ -n "$CLOUD_INSTANCE_TYPE" ] && CLOUD_INSTANCE_TYPE=$(basename "$CLOUD_INSTANCE_TYPE")
- CLOUD_INSTANCE_REGION="$(curl --fail -s -m 5 --noproxy "*" -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/zone")"
- [ -n "$CLOUD_INSTANCE_REGION" ] && CLOUD_INSTANCE_REGION=$(basename "$CLOUD_INSTANCE_REGION") && CLOUD_INSTANCE_REGION=${CLOUD_INSTANCE_REGION%-*}
+ # Try GCE computeMetadata v1
+ if [ "${CLOUD_TYPE}" = "unknown" ]; then
+ if [ -n "$(curl --fail -s --connect-timeout 1 -m 3 --noproxy "*" -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1")" ]; then
+ CLOUD_TYPE="GCP"
+ CLOUD_INSTANCE_TYPE="$(curl --fail -s --connect-timeout 1 -m 3 --noproxy "*" -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/machine-type")"
+ [ -n "$CLOUD_INSTANCE_TYPE" ] && CLOUD_INSTANCE_TYPE=$(basename "$CLOUD_INSTANCE_TYPE")
+ CLOUD_INSTANCE_REGION="$(curl --fail -s --connect-timeout 1 -m 3 --noproxy "*" -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/zone")"
+ [ -n "$CLOUD_INSTANCE_REGION" ] && CLOUD_INSTANCE_REGION=$(basename "$CLOUD_INSTANCE_REGION") && CLOUD_INSTANCE_REGION=${CLOUD_INSTANCE_REGION%-*}
+ fi
fi
- fi
- # TODO: needs to be tested in Microsoft Azure
- # Try Azure IMDS
- # if [ "${CLOUD_TYPE}" = "unknown" ]; then
- # AZURE_IMDS_DATA="$(curl --fail -s -m 5 -H "Metadata: true" --noproxy "*" "http://169.254.169.254/metadata/instance?version=2021-10-01")"
- # if [ -n "${AZURE_IMDS_DATA}" ]; then
- # CLOUD_TYPE="Azure"
- # CLOUD_INSTANCE_TYPE="$(curl --fail -s -m 5 -H "Metadata: true" --noproxy "*" "http://169.254.169.254/metadata/instance/compute/vmSize?version=2021-10-01&format=text")"
- # CLOUD_INSTANCE_REGION="$(curl --fail -s -m 5 -H "Metadata: true" --noproxy "*" "http://169.254.169.254/metadata/instance/compute/location?version=2021-10-01&format=text")"
- # fi
- # fi
+ # TODO: needs to be tested in Microsoft Azure
+ # Try Azure IMDS
+ # if [ "${CLOUD_TYPE}" = "unknown" ]; then
+ # AZURE_IMDS_DATA="$(curl --fail -s -m 5 -H "Metadata: true" --noproxy "*" "http://169.254.169.254/metadata/instance?version=2021-10-01")"
+ # if [ -n "${AZURE_IMDS_DATA}" ]; then
+ # CLOUD_TYPE="Azure"
+ # CLOUD_INSTANCE_TYPE="$(curl --fail -s -m 5 -H "Metadata: true" --noproxy "*" "http://169.254.169.254/metadata/instance/compute/vmSize?version=2021-10-01&format=text")"
+ # CLOUD_INSTANCE_REGION="$(curl --fail -s -m 5 -H "Metadata: true" --noproxy "*" "http://169.254.169.254/metadata/instance/compute/location?version=2021-10-01&format=text")"
+ # fi
+ # fi
+ fi
fi
echo "NETDATA_CONTAINER_OS_NAME=${CONTAINER_NAME}"