system-info.sh 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. #!/usr/bin/env sh
  2. # -------------------------------------------------------------------------------------------------
  3. # detect the kernel
  4. KERNEL_NAME="$(uname -s)"
  5. KERNEL_VERSION="$(uname -r)"
  6. ARCHITECTURE="$(uname -m)"
  7. # -------------------------------------------------------------------------------------------------
  8. # detect the virtualization and possibly the container technology
  9. # systemd-detect-virt: https://github.com/systemd/systemd/blob/df423851fcc05cf02281d11aab6aee7b476c1c3b/src/basic/virt.c#L999
  10. # lscpu: https://github.com/util-linux/util-linux/blob/581b77da7aa4a5205902857184d555bed367e3e0/sys-utils/lscpu.c#L52
  11. virtualization_normalize_name() {
  12. vname="$1"
  13. case "$vname" in
  14. "User-mode Linux") vname="uml" ;;
  15. "Windows Subsystem for Linux") vname="wsl" ;;
  16. esac
  17. echo "$vname" | tr '[:upper:]' '[:lower:]' | sed 's/ /-/g'
  18. }
  19. CONTAINER="unknown"
  20. CONT_DETECTION="none"
  21. CONTAINER_IS_OFFICIAL_IMAGE="${NETDATA_OFFICIAL_IMAGE:-false}"
  22. if [ -z "${VIRTUALIZATION}" ]; then
  23. VIRTUALIZATION="unknown"
  24. VIRT_DETECTION="none"
  25. if command -v systemd-detect-virt >/dev/null 2>&1; then
  26. VIRTUALIZATION="$(systemd-detect-virt -v)"
  27. VIRT_DETECTION="systemd-detect-virt"
  28. CONTAINER_DETECT_TMP="$(systemd-detect-virt -c)"
  29. [ -n "$CONTAINER_DETECT_TMP" ] && CONTAINER="$CONTAINER_DETECT_TMP"
  30. CONT_DETECTION="systemd-detect-virt"
  31. elif command -v lscpu >/dev/null 2>&1; then
  32. VIRTUALIZATION=$(lscpu | grep "Hypervisor vendor:" | cut -d: -f 2 | awk '{$1=$1};1')
  33. [ -n "$VIRTUALIZATION" ] && VIRT_DETECTION="lscpu"
  34. [ -z "$VIRTUALIZATION" ] && lscpu | grep -q "Virtualization:" && VIRTUALIZATION="none"
  35. elif command -v dmidecode >/dev/null 2>&1; then
  36. VIRTUALIZATION=$(dmidecode -s system-product-name 2>/dev/null | grep "VMware\|Virtual\|KVM\|Bochs")
  37. [ -n "$VIRTUALIZATION" ] && VIRT_DETECTION="dmidecode"
  38. fi
  39. if [ -z "${VIRTUALIZATION}" ] || [ "$VIRTUALIZATION" = "unknown" ]; then
  40. if [ "${KERNEL_NAME}" = "FreeBSD" ]; then
  41. VIRTUALIZATION=$(sysctl kern.vm_guest 2>/dev/null | cut -d: -f 2 | awk '{$1=$1};1')
  42. [ -n "$VIRTUALIZATION" ] && VIRT_DETECTION="sysctl"
  43. fi
  44. fi
  45. if [ -z "${VIRTUALIZATION}" ]; then
  46. # Output from the command is outside of spec
  47. VIRTUALIZATION="unknown"
  48. VIRT_DETECTION="none"
  49. elif [ "$VIRTUALIZATION" != "none" ] && [ "$VIRTUALIZATION" != "unknown" ]; then
  50. VIRTUALIZATION=$(virtualization_normalize_name $VIRTUALIZATION)
  51. fi
  52. else
  53. # Passed from outside - probably in docker run
  54. VIRT_DETECTION="provided"
  55. fi
  56. # -------------------------------------------------------------------------------------------------
  57. # detect containers with heuristics
  58. if [ "${CONTAINER}" = "unknown" ]; then
  59. if [ -f /proc/1/sched ]; then
  60. IFS='(, ' read -r process _ < /proc/1/sched
  61. if [ "${process}" = "netdata" ]; then
  62. CONTAINER="container"
  63. CONT_DETECTION="process"
  64. fi
  65. fi
  66. # ubuntu and debian supply /bin/running-in-container
  67. # https://www.apt-browse.org/browse/ubuntu/trusty/main/i386/upstart/1.12.1-0ubuntu4/file/bin/running-in-container
  68. if /bin/running-in-container > /dev/null 2>&1; then
  69. CONTAINER="container"
  70. CONT_DETECTION="/bin/running-in-container"
  71. fi
  72. # lxc sets environment variable 'container'
  73. #shellcheck disable=SC2154
  74. if [ -n "${container}" ]; then
  75. CONTAINER="lxc"
  76. CONT_DETECTION="containerenv"
  77. fi
  78. # docker creates /.dockerenv
  79. # http://stackoverflow.com/a/25518345
  80. if [ -f "/.dockerenv" ]; then
  81. CONTAINER="docker"
  82. CONT_DETECTION="dockerenv"
  83. fi
  84. fi
  85. # -------------------------------------------------------------------------------------------------
  86. # detect the operating system
  87. # Initially assume all OS detection values are for a container, these are moved later if we are bare-metal
  88. CONTAINER_OS_DETECTION="unknown"
  89. CONTAINER_NAME="unknown"
  90. CONTAINER_VERSION="unknown"
  91. CONTAINER_VERSION_ID="unknown"
  92. CONTAINER_ID="unknown"
  93. CONTAINER_ID_LIKE="unknown"
  94. if [ "${KERNEL_NAME}" = "Darwin" ]; then
  95. CONTAINER_ID=$(sw_vers -productName)
  96. CONTAINER_ID_LIKE="mac"
  97. CONTAINER_NAME="mac"
  98. CONTAINER_VERSION=$(sw_vers -productVersion)
  99. CONTAINER_OS_DETECTION="sw_vers"
  100. elif [ "${KERNEL_NAME}" = "FreeBSD" ]; then
  101. CONTAINER_ID="FreeBSD"
  102. CONTAINER_ID_LIKE="FreeBSD"
  103. CONTAINER_NAME="FreeBSD"
  104. CONTAINER_OS_DETECTION="uname"
  105. CONTAINER_VERSION=$(uname -r)
  106. KERNEL_VERSION=$(uname -K)
  107. else
  108. if [ -f "/etc/os-release" ]; then
  109. eval "$(grep -E "^(NAME|ID|ID_LIKE|VERSION|VERSION_ID)=" < /etc/os-release | sed 's/^/CONTAINER_/')"
  110. CONTAINER_OS_DETECTION="/etc/os-release"
  111. fi
  112. # shellcheck disable=SC2153
  113. if [ "${CONTAINER_NAME}" = "unknown" ] || [ "${CONTAINER_VERSION}" = "unknown" ] || [ "${CONTAINER_ID}" = "unknown" ]; then
  114. if [ -f "/etc/lsb-release" ]; then
  115. if [ "${CONTAINER_OS_DETECTION}" = "unknown" ]; then
  116. CONTAINER_OS_DETECTION="/etc/lsb-release"
  117. else
  118. CONTAINER_OS_DETECTION="Mixed"
  119. fi
  120. DISTRIB_ID="unknown"
  121. DISTRIB_RELEASE="unknown"
  122. DISTRIB_CODENAME="unknown"
  123. eval "$(grep -E "^(DISTRIB_ID|DISTRIB_RELEASE|DISTRIB_CODENAME)=" < /etc/lsb-release)"
  124. if [ "${CONTAINER_NAME}" = "unknown" ]; then CONTAINER_NAME="${DISTRIB_ID}"; fi
  125. if [ "${CONTAINER_VERSION}" = "unknown" ]; then CONTAINER_VERSION="${DISTRIB_RELEASE}"; fi
  126. if [ "${CONTAINER_ID}" = "unknown" ]; then CONTAINER_ID="${DISTRIB_CODENAME}"; fi
  127. fi
  128. if [ -n "$(command -v lsb_release 2> /dev/null)" ]; then
  129. if [ "${CONTAINER_OS_DETECTION}" = "unknown" ]; then
  130. CONTAINER_OS_DETECTION="lsb_release"
  131. else
  132. CONTAINER_OS_DETECTION="Mixed"
  133. fi
  134. if [ "${CONTAINER_NAME}" = "unknown" ]; then CONTAINER_NAME="$(lsb_release -is 2> /dev/null)"; fi
  135. if [ "${CONTAINER_VERSION}" = "unknown" ]; then CONTAINER_VERSION="$(lsb_release -rs 2> /dev/null)"; fi
  136. if [ "${CONTAINER_ID}" = "unknown" ]; then CONTAINER_ID="$(lsb_release -cs 2> /dev/null)"; fi
  137. fi
  138. fi
  139. fi
  140. # If Netdata is not running in a container then use the local detection as the host
  141. HOST_OS_DETECTION="unknown"
  142. HOST_NAME="unknown"
  143. HOST_VERSION="unknown"
  144. HOST_VERSION_ID="unknown"
  145. HOST_ID="unknown"
  146. HOST_ID_LIKE="unknown"
  147. # 'systemd-detect-virt' returns 'none' if there is no hardware/container virtualization.
  148. if [ "${CONTAINER}" = "unknown" ] || [ "${CONTAINER}" = "none" ]; then
  149. for v in NAME ID ID_LIKE VERSION VERSION_ID OS_DETECTION; do
  150. eval "HOST_$v=\$CONTAINER_$v; CONTAINER_$v=none"
  151. done
  152. else
  153. # Otherwise try and use a user-supplied bind-mount into the container to resolve the host details
  154. if [ -e "/host/etc/os-release" ]; then
  155. eval "$(grep -E "^(NAME|ID|ID_LIKE|VERSION|VERSION_ID)=" < /host/etc/os-release | sed 's/^/HOST_/')"
  156. HOST_OS_DETECTION="/host/etc/os-release"
  157. fi
  158. if [ "${HOST_NAME}" = "unknown" ] || [ "${HOST_VERSION}" = "unknown" ] || [ "${HOST_ID}" = "unknown" ]; then
  159. if [ -f "/host/etc/lsb-release" ]; then
  160. if [ "${HOST_OS_DETECTION}" = "unknown" ]; then
  161. HOST_OS_DETECTION="/etc/lsb-release"
  162. else
  163. HOST_OS_DETECTION="Mixed"
  164. fi
  165. DISTRIB_ID="unknown"
  166. DISTRIB_RELEASE="unknown"
  167. DISTRIB_CODENAME="unknown"
  168. eval "$(grep -E "^(DISTRIB_ID|DISTRIB_RELEASE|DISTRIB_CODENAME)=" < /etc/lsb-release)"
  169. if [ "${HOST_NAME}" = "unknown" ]; then HOST_NAME="${DISTRIB_ID}"; fi
  170. if [ "${HOST_VERSION}" = "unknown" ]; then HOST_VERSION="${DISTRIB_RELEASE}"; fi
  171. if [ "${HOST_ID}" = "unknown" ]; then HOST_ID="${DISTRIB_CODENAME}"; fi
  172. fi
  173. fi
  174. fi
  175. # -------------------------------------------------------------------------------------------------
  176. # Detect information about the CPU
  177. LCPU_COUNT="unknown"
  178. CPU_MODEL="unknown"
  179. CPU_VENDOR="unknown"
  180. CPU_FREQ="unknown"
  181. CPU_INFO_SOURCE="none"
  182. possible_cpu_freq=""
  183. nproc="$(command -v nproc)"
  184. lscpu="$(command -v lscpu)"
  185. lscpu_output=""
  186. dmidecode="$(command -v dmidecode)"
  187. dmidecode_output=""
  188. if [ -n "${lscpu}" ] && lscpu > /dev/null 2>&1; then
  189. lscpu_output="$(LC_NUMERIC=C ${lscpu} 2> /dev/null)"
  190. CPU_INFO_SOURCE="lscpu"
  191. LCPU_COUNT="$(echo "${lscpu_output}" | grep "^CPU(s):" | cut -f 2 -d ':' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
  192. CPU_VENDOR="$(echo "${lscpu_output}" | grep "^Vendor ID:" | cut -f 2 -d ':' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
  193. CPU_MODEL="$(echo "${lscpu_output}" | grep "^Model name:" | cut -f 2 -d ':' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
  194. possible_cpu_freq="$(echo "${lscpu_output}" | grep -F "CPU max MHz:" | cut -f 2 -d ':' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | grep -o '^[0-9]*')"
  195. if [ -z "$possible_cpu_freq" ]; then
  196. possible_cpu_freq="$(echo "${lscpu_output}" | grep -F "CPU MHz:" | cut -f 2 -d ':' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | grep -o '^[0-9]*')"
  197. fi
  198. if [ -z "$possible_cpu_freq" ]; then
  199. possible_cpu_freq="$(echo "${lscpu_output}" | grep "^Model name:" | grep -Eo "[0-9\.]+GHz" | grep -o "^[0-9\.]*" | awk '{print int($0*1000)}')"
  200. fi
  201. [ -n "$possible_cpu_freq" ] && possible_cpu_freq="${possible_cpu_freq} MHz"
  202. elif [ -n "${dmidecode}" ] && dmidecode -t processor > /dev/null 2>&1; then
  203. dmidecode_output="$(${dmidecode} -t processor 2> /dev/null)"
  204. CPU_INFO_SOURCE="dmidecode"
  205. LCPU_COUNT="$(echo "${dmidecode_output}" | grep -F "Thread Count:" | cut -f 2 -d ':' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
  206. CPU_VENDOR="$(echo "${dmidecode_output}" | grep -F "Manufacturer:" | cut -f 2 -d ':' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
  207. CPU_MODEL="$(echo "${dmidecode_output}" | grep -F "Version:" | cut -f 2 -d ':' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
  208. possible_cpu_freq="$(echo "${dmidecode_output}" | grep -F "Current Speed:" | cut -f 2 -d ':' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
  209. else
  210. if [ -n "${nproc}" ]; then
  211. CPU_INFO_SOURCE="nproc"
  212. LCPU_COUNT="$(${nproc})"
  213. elif [ "${KERNEL_NAME}" = FreeBSD ]; then
  214. CPU_INFO_SOURCE="sysctl"
  215. LCPU_COUNT="$(sysctl -n kern.smp.cpus)"
  216. if ! possible_cpu_freq=$(sysctl -n machdep.tsc_freq 2> /dev/null); then
  217. possible_cpu_freq=$(sysctl -n hw.model 2> /dev/null | grep -Eo "[0-9\.]+GHz" | grep -o "^[0-9\.]*" | awk '{print int($0*1000)}')
  218. [ -n "$possible_cpu_freq" ] && possible_cpu_freq="${possible_cpu_freq} MHz"
  219. fi
  220. elif [ "${KERNEL_NAME}" = Darwin ]; then
  221. CPU_INFO_SOURCE="sysctl"
  222. LCPU_COUNT="$(sysctl -n hw.logicalcpu)"
  223. elif [ -d /sys/devices/system/cpu ]; then
  224. CPU_INFO_SOURCE="sysfs"
  225. # This is potentially more accurate than checking `/proc/cpuinfo`.
  226. LCPU_COUNT="$(find /sys/devices/system/cpu -mindepth 1 -maxdepth 1 -type d -name 'cpu*' | grep -cEv 'idle|freq')"
  227. elif [ -r /proc/cpuinfo ]; then
  228. CPU_INFO_SOURCE="procfs"
  229. LCPU_COUNT="$(grep -c ^processor /proc/cpuinfo)"
  230. fi
  231. if [ "${KERNEL_NAME}" = Darwin ]; then
  232. CPU_MODEL="$(sysctl -n machdep.cpu.brand_string)"
  233. if [ "${ARCHITECTURE}" = "x86_64" ]; then
  234. CPU_VENDOR="$(sysctl -n machdep.cpu.vendor)"
  235. else
  236. CPU_VENDOR="Apple"
  237. fi
  238. echo "${CPU_INFO_SOURCE}" | grep -qv sysctl && CPU_INFO_SOURCE="${CPU_INFO_SOURCE} sysctl"
  239. elif uname --version 2> /dev/null | grep -qF 'GNU coreutils'; then
  240. CPU_INFO_SOURCE="${CPU_INFO_SOURCE} uname"
  241. CPU_MODEL="$(uname -p)"
  242. CPU_VENDOR="$(uname -i)"
  243. elif [ "${KERNEL_NAME}" = FreeBSD ]; then
  244. if (echo "${CPU_INFO_SOURCE}" | grep -qv sysctl); then
  245. CPU_INFO_SOURCE="${CPU_INFO_SOURCE} sysctl"
  246. fi
  247. CPU_MODEL="$(sysctl -n hw.model)"
  248. elif [ -r /proc/cpuinfo ]; then
  249. if (echo "${CPU_INFO_SOURCE}" | grep -qv procfs); then
  250. CPU_INFO_SOURCE="${CPU_INFO_SOURCE} procfs"
  251. fi
  252. CPU_MODEL="$(grep -F "model name" /proc/cpuinfo | head -n 1 | cut -f 2 -d ':' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
  253. CPU_VENDOR="$(grep -F "vendor_id" /proc/cpuinfo | head -n 1 | cut -f 2 -d ':' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
  254. fi
  255. fi
  256. if [ "${KERNEL_NAME}" = Darwin ] && [ "${ARCHITECTURE}" = "x86_64" ]; then
  257. CPU_FREQ="$(sysctl -n hw.cpufrequency)"
  258. elif [ -r /sys/devices/system/cpu/cpu0/cpufreq/base_frequency ]; then
  259. if (echo "${CPU_INFO_SOURCE}" | grep -qv sysfs); then
  260. CPU_INFO_SOURCE="${CPU_INFO_SOURCE} sysfs"
  261. fi
  262. value="$(cat /sys/devices/system/cpu/cpu0/cpufreq/base_frequency)"
  263. CPU_FREQ="$((value * 1000))"
  264. elif [ -n "${possible_cpu_freq}" ]; then
  265. CPU_FREQ="${possible_cpu_freq}"
  266. elif [ -r /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq ]; then
  267. if (echo "${CPU_INFO_SOURCE}" | grep -qv sysfs); then
  268. CPU_INFO_SOURCE="${CPU_INFO_SOURCE} sysfs"
  269. fi
  270. value="$(cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq)"
  271. CPU_FREQ="$((value * 1000))"
  272. elif [ -r /proc/cpuinfo ]; then
  273. if (echo "${CPU_INFO_SOURCE}" | grep -qv procfs); then
  274. CPU_INFO_SOURCE="${CPU_INFO_SOURCE} procfs"
  275. fi
  276. value=$(grep "cpu MHz" /proc/cpuinfo 2>/dev/null | grep -o "[0-9]*" | head -n 1 | awk '{print int($0*1000000)}')
  277. [ -n "$value" ] && CPU_FREQ="$value"
  278. fi
  279. freq_units="$(echo "${CPU_FREQ}" | cut -f 2 -d ' ')"
  280. case "${freq_units}" in
  281. GHz)
  282. value="$(echo "${CPU_FREQ}" | cut -f 1 -d ' ')"
  283. CPU_FREQ="$((value * 1000 * 1000 * 1000))"
  284. ;;
  285. MHz)
  286. value="$(echo "${CPU_FREQ}" | cut -f 1 -d ' ')"
  287. CPU_FREQ="$((value * 1000 * 1000))"
  288. ;;
  289. KHz)
  290. value="$(echo "${CPU_FREQ}" | cut -f 1 -d ' ')"
  291. CPU_FREQ="$((value * 1000))"
  292. ;;
  293. *) ;;
  294. esac
  295. # -------------------------------------------------------------------------------------------------
  296. # Detect the total system RAM
  297. TOTAL_RAM="unknown"
  298. RAM_DETECTION="none"
  299. if [ "${KERNEL_NAME}" = FreeBSD ]; then
  300. RAM_DETECTION="sysctl"
  301. TOTAL_RAM="$(sysctl -n hw.physmem)"
  302. elif [ "${KERNEL_NAME}" = Darwin ]; then
  303. RAM_DETECTION="sysctl"
  304. TOTAL_RAM="$(sysctl -n hw.memsize)"
  305. elif [ -r /proc/meminfo ]; then
  306. RAM_DETECTION="procfs"
  307. TOTAL_RAM="$(grep -F MemTotal /proc/meminfo | cut -f 2 -d ':' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | cut -f 1 -d ' ')"
  308. TOTAL_RAM="$((TOTAL_RAM * 1024))"
  309. fi
  310. # -------------------------------------------------------------------------------------------------
  311. # Detect the total system disk space
  312. DISK_SIZE="unknown"
  313. DISK_DETECTION="none"
  314. if [ "${KERNEL_NAME}" = "Darwin" ]; then
  315. if DISK_SIZE=$(diskutil info / 2>/dev/null | awk '/Disk Size/ {total += substr($5,2,length($5))} END { print total }') &&
  316. [ -n "$DISK_SIZE" ] && [ "$DISK_SIZE" != "0" ]; then
  317. DISK_DETECTION="diskutil"
  318. else
  319. types='hfs'
  320. if (lsvfs | grep -q apfs); then
  321. types="${types},apfs"
  322. fi
  323. if (lsvfs | grep -q ufs); then
  324. types="${types},ufs"
  325. fi
  326. DISK_DETECTION="df"
  327. 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))
  328. fi
  329. elif [ "${KERNEL_NAME}" = FreeBSD ]; then
  330. types='ufs'
  331. if (lsvfs | grep -q zfs); then
  332. types="${types},zfs"
  333. fi
  334. DISK_DETECTION="df"
  335. total="$(df -t ${types} -c -k | tail -n 1 | awk '{print $2}')"
  336. DISK_SIZE="$((total * 1024))"
  337. else
  338. if [ -d /sys/block ] && [ -r /proc/devices ]; then
  339. dev_major_whitelist=''
  340. # This is a list of device names used for block storage devices.
  341. # These translate to the prefixs of files in `/dev` indicating the device type.
  342. # They are sorted by lowest used device major number, with dynamically assigned ones at the end.
  343. # We use this to look up device major numbers in `/proc/devices`
  344. device_names='hd sd mfm ad ftl pd nftl dasd intfl mmcblk ub xvd rfd vbd nvme virtblk blkext'
  345. for name in ${device_names}; do
  346. if grep -qE " ${name}\$" /proc/devices; then
  347. dev_major_whitelist="${dev_major_whitelist}:$(grep -E "${name}\$" /proc/devices | sed -e 's/^[[:space:]]*//' | cut -f 1 -d ' ' | tr '\n' ':'):"
  348. fi
  349. done
  350. DISK_DETECTION="sysfs"
  351. DISK_SIZE="0"
  352. for disk in /sys/block/*; do
  353. if [ -r "${disk}/size" ] \
  354. && (echo "${dev_major_whitelist}" | grep -q ":$(cut -f 1 -d ':' "${disk}/dev"):") \
  355. && grep -qv 1 "${disk}/removable"; then
  356. size="$(($(cat "${disk}/size") * 512))"
  357. DISK_SIZE="$((DISK_SIZE + size))"
  358. fi
  359. done
  360. elif df --version 2> /dev/null | grep -qF "GNU coreutils"; then
  361. DISK_DETECTION="df"
  362. DISK_SIZE=$(($(df -x tmpfs -x devtmpfs -x squashfs -l -B1 --output=source,size | tail -n +2 | sort -u -k 1 | awk '{print $2}' | tr '\n' '+' | head -c -1)))
  363. else
  364. DISK_DETECTION="df"
  365. include_fs_types="ext*|btrfs|xfs|jfs|reiser*|zfs"
  366. DISK_SIZE=$(($(df -T -P | tail -n +2 | sort -u -k 1 | grep "${include_fs_types}" | awk '{print $3}' | tr '\n' '+' | head -c -1) * 1024))
  367. fi
  368. fi
  369. # -------------------------------------------------------------------------------------------------
  370. # Detect whether the node is kubernetes node
  371. HOST_IS_K8S_NODE="false"
  372. if [ -n "${KUBERNETES_SERVICE_HOST}" ] && [ -n "${KUBERNETES_SERVICE_PORT}" ]; then
  373. # These env vars are set for every container managed by k8s.
  374. HOST_IS_K8S_NODE="true"
  375. elif pgrep "kubelet"; then
  376. # The kubelet is the primary "node agent" that runs on each node.
  377. HOST_IS_K8S_NODE="true"
  378. fi
  379. # ------------------------------------------------------------------------------------------------
  380. # Detect instance metadata for VMs running on cloud providers
  381. CLOUD_TYPE="unknown"
  382. CLOUD_INSTANCE_TYPE="unknown"
  383. CLOUD_INSTANCE_REGION="unknown"
  384. if [ "${VIRTUALIZATION}" != "none" ] && command -v curl > /dev/null 2>&1; then
  385. # Returned HTTP status codes: GCP is 200, AWS is 200, DO is 404.
  386. curl --fail -s -m 1 --noproxy "*" http://169.254.169.254 >/dev/null 2>&1
  387. ret=$?
  388. # anything but operation timeout.
  389. if [ "$ret" != 28 ]; then
  390. # Try AWS IMDSv2
  391. if [ "${CLOUD_TYPE}" = "unknown" ]; then
  392. 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")"
  393. if [ -n "${AWS_IMDS_TOKEN}" ]; then
  394. CLOUD_TYPE="AWS"
  395. 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)"
  396. 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)"
  397. fi
  398. fi
  399. # Try GCE computeMetadata v1
  400. if [ "${CLOUD_TYPE}" = "unknown" ]; then
  401. if [ -n "$(curl --fail -s --connect-timeout 1 -m 3 --noproxy "*" -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1")" ]; then
  402. CLOUD_TYPE="GCP"
  403. 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")"
  404. [ -n "$CLOUD_INSTANCE_TYPE" ] && CLOUD_INSTANCE_TYPE=$(basename "$CLOUD_INSTANCE_TYPE")
  405. CLOUD_INSTANCE_REGION="$(curl --fail -s --connect-timeout 1 -m 3 --noproxy "*" -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/zone")"
  406. [ -n "$CLOUD_INSTANCE_REGION" ] && CLOUD_INSTANCE_REGION=$(basename "$CLOUD_INSTANCE_REGION") && CLOUD_INSTANCE_REGION=${CLOUD_INSTANCE_REGION%-*}
  407. fi
  408. fi
  409. # TODO: needs to be tested in Microsoft Azure
  410. # Try Azure IMDS
  411. # if [ "${CLOUD_TYPE}" = "unknown" ]; then
  412. # AZURE_IMDS_DATA="$(curl --fail -s -m 5 -H "Metadata: true" --noproxy "*" "http://169.254.169.254/metadata/instance?version=2021-10-01")"
  413. # if [ -n "${AZURE_IMDS_DATA}" ]; then
  414. # CLOUD_TYPE="Azure"
  415. # 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")"
  416. # 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")"
  417. # fi
  418. # fi
  419. fi
  420. fi
  421. echo "NETDATA_CONTAINER_OS_NAME=${CONTAINER_NAME}"
  422. echo "NETDATA_CONTAINER_OS_ID=${CONTAINER_ID}"
  423. echo "NETDATA_CONTAINER_OS_ID_LIKE=${CONTAINER_ID_LIKE}"
  424. echo "NETDATA_CONTAINER_OS_VERSION=${CONTAINER_VERSION}"
  425. echo "NETDATA_CONTAINER_OS_VERSION_ID=${CONTAINER_VERSION_ID}"
  426. echo "NETDATA_CONTAINER_OS_DETECTION=${CONTAINER_OS_DETECTION}"
  427. echo "NETDATA_CONTAINER_IS_OFFICIAL_IMAGE=${CONTAINER_IS_OFFICIAL_IMAGE}"
  428. echo "NETDATA_HOST_OS_NAME=${HOST_NAME}"
  429. echo "NETDATA_HOST_OS_ID=${HOST_ID}"
  430. echo "NETDATA_HOST_OS_ID_LIKE=${HOST_ID_LIKE}"
  431. echo "NETDATA_HOST_OS_VERSION=${HOST_VERSION}"
  432. echo "NETDATA_HOST_OS_VERSION_ID=${HOST_VERSION_ID}"
  433. echo "NETDATA_HOST_OS_DETECTION=${HOST_OS_DETECTION}"
  434. echo "NETDATA_HOST_IS_K8S_NODE=${HOST_IS_K8S_NODE}"
  435. echo "NETDATA_SYSTEM_KERNEL_NAME=${KERNEL_NAME}"
  436. echo "NETDATA_SYSTEM_KERNEL_VERSION=${KERNEL_VERSION}"
  437. echo "NETDATA_SYSTEM_ARCHITECTURE=${ARCHITECTURE}"
  438. echo "NETDATA_SYSTEM_VIRTUALIZATION=${VIRTUALIZATION}"
  439. echo "NETDATA_SYSTEM_VIRT_DETECTION=${VIRT_DETECTION}"
  440. echo "NETDATA_SYSTEM_CONTAINER=${CONTAINER}"
  441. echo "NETDATA_SYSTEM_CONTAINER_DETECTION=${CONT_DETECTION}"
  442. echo "NETDATA_SYSTEM_CPU_LOGICAL_CPU_COUNT=${LCPU_COUNT}"
  443. echo "NETDATA_SYSTEM_CPU_VENDOR=${CPU_VENDOR}"
  444. echo "NETDATA_SYSTEM_CPU_MODEL=${CPU_MODEL}"
  445. echo "NETDATA_SYSTEM_CPU_FREQ=${CPU_FREQ}"
  446. echo "NETDATA_SYSTEM_CPU_DETECTION=${CPU_INFO_SOURCE}"
  447. echo "NETDATA_SYSTEM_TOTAL_RAM=${TOTAL_RAM}"
  448. echo "NETDATA_SYSTEM_RAM_DETECTION=${RAM_DETECTION}"
  449. echo "NETDATA_SYSTEM_TOTAL_DISK_SIZE=${DISK_SIZE}"
  450. echo "NETDATA_SYSTEM_DISK_DETECTION=${DISK_DETECTION}"
  451. echo "NETDATA_INSTANCE_CLOUD_TYPE=${CLOUD_TYPE}"
  452. echo "NETDATA_INSTANCE_CLOUD_INSTANCE_TYPE=${CLOUD_INSTANCE_TYPE}"
  453. echo "NETDATA_INSTANCE_CLOUD_INSTANCE_REGION=${CLOUD_INSTANCE_REGION}"