cgroup-network-helper.sh 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. #!/usr/bin/env bash
  2. # shellcheck disable=SC1117
  3. # cgroup-network-helper.sh
  4. # detect container and virtual machine interfaces
  5. #
  6. # (C) 2017 Costa Tsaousis
  7. # SPDX-License-Identifier: GPL-3.0-or-later
  8. #
  9. # This script is called as root (by cgroup-network), with either a pid, or a cgroup path.
  10. # It tries to find all the network interfaces that belong to the same cgroup.
  11. #
  12. # It supports several method for this detection:
  13. #
  14. # 1. cgroup-network (the binary father of this script) detects veth network interfaces,
  15. # by examining iflink and ifindex IDs and switching namespaces
  16. # (it also detects the interface name as it is used by the container).
  17. #
  18. # 2. this script, uses /proc/PID/fdinfo to find tun/tap network interfaces.
  19. #
  20. # 3. this script, calls virsh to find libvirt network interfaces.
  21. #
  22. # -----------------------------------------------------------------------------
  23. # the system path is cleared by cgroup-network
  24. # shellcheck source=/dev/null
  25. [ -f /etc/profile ] && source /etc/profile
  26. export LC_ALL=C
  27. PROGRAM_NAME="$(basename "${0}")"
  28. LOG_LEVEL_ERR=1
  29. LOG_LEVEL_WARN=2
  30. LOG_LEVEL_INFO=3
  31. LOG_LEVEL="$LOG_LEVEL_INFO"
  32. set_log_severity_level() {
  33. case ${NETDATA_LOG_SEVERITY_LEVEL,,} in
  34. "info") LOG_LEVEL="$LOG_LEVEL_INFO";;
  35. "warn" | "warning") LOG_LEVEL="$LOG_LEVEL_WARN";;
  36. "err" | "error") LOG_LEVEL="$LOG_LEVEL_ERR";;
  37. esac
  38. }
  39. set_log_severity_level
  40. logdate() {
  41. date "+%Y-%m-%d %H:%M:%S"
  42. }
  43. log() {
  44. local status="${1}"
  45. shift
  46. echo >&2 "$(logdate): ${PROGRAM_NAME}: ${status}: ${*}"
  47. }
  48. info() {
  49. [[ -n "$LOG_LEVEL" && "$LOG_LEVEL_INFO" -gt "$LOG_LEVEL" ]] && return
  50. log INFO "${@}"
  51. }
  52. warning() {
  53. [[ -n "$LOG_LEVEL" && "$LOG_LEVEL_WARN" -gt "$LOG_LEVEL" ]] && return
  54. log WARNING "${@}"
  55. }
  56. error() {
  57. [[ -n "$LOG_LEVEL" && "$LOG_LEVEL_ERR" -gt "$LOG_LEVEL" ]] && return
  58. log ERROR "${@}"
  59. }
  60. fatal() {
  61. log FATAL "${@}"
  62. exit 1
  63. }
  64. debug=${NETDATA_CGROUP_NETWORK_HELPER_DEBUG=0}
  65. debug() {
  66. [ "${debug}" = "1" ] && log DEBUG "${@}"
  67. }
  68. # -----------------------------------------------------------------------------
  69. # check for BASH v4+ (required for associative arrays)
  70. [ $(( BASH_VERSINFO[0] )) -lt 4 ] && \
  71. fatal "BASH version 4 or later is required (this is ${BASH_VERSION})."
  72. # -----------------------------------------------------------------------------
  73. # parse the arguments
  74. pid=
  75. cgroup=
  76. while [ -n "${1}" ]
  77. do
  78. case "${1}" in
  79. --cgroup) cgroup="${2}"; shift 1;;
  80. --pid|-p) pid="${2}"; shift 1;;
  81. --debug|debug) debug=1;;
  82. *) fatal "Cannot understand argument '${1}'";;
  83. esac
  84. shift
  85. done
  86. if [ -z "${pid}" ] && [ -z "${cgroup}" ]
  87. then
  88. fatal "Either --pid or --cgroup is required"
  89. fi
  90. # -----------------------------------------------------------------------------
  91. set_source() {
  92. [ ${debug} -eq 1 ] && echo "SRC ${*}"
  93. }
  94. # -----------------------------------------------------------------------------
  95. # veth interfaces via cgroup
  96. # cgroup-network can detect veth interfaces by itself (written in C).
  97. # If you seek for a shell version of what it does, check this:
  98. # https://github.com/netdata/netdata/issues/474#issuecomment-317866709
  99. # -----------------------------------------------------------------------------
  100. # tun/tap interfaces via /proc/PID/fdinfo
  101. # find any tun/tap devices linked to a pid
  102. proc_pid_fdinfo_iff() {
  103. local p="${1}" # the pid
  104. debug "Searching for tun/tap interfaces for pid ${p}..."
  105. set_source "fdinfo"
  106. grep "^iff:.*" "${NETDATA_HOST_PREFIX}/proc/${p}/fdinfo"/* 2>/dev/null | cut -f 2
  107. }
  108. find_tun_tap_interfaces_for_cgroup() {
  109. local c="${1}" # the cgroup path
  110. [ -d "${c}/emulator" ] && c="${c}/emulator" # check for 'emulator' subdirectory
  111. c="${c}/cgroup.procs" # make full path
  112. # for each pid of the cgroup
  113. # find any tun/tap devices linked to the pid
  114. if [ -f "${c}" ]
  115. then
  116. local p
  117. for p in $(< "${c}" )
  118. do
  119. proc_pid_fdinfo_iff "${p}"
  120. done
  121. else
  122. debug "Cannot find file '${c}', not searching for tun/tap interfaces."
  123. fi
  124. }
  125. # -----------------------------------------------------------------------------
  126. # virsh domain network interfaces
  127. virsh_cgroup_to_domain_name() {
  128. local c="${1}" # the cgroup path
  129. debug "extracting a possible virsh domain from cgroup ${c}..."
  130. # extract for the cgroup path
  131. sed -n -e "s|.*/machine-qemu\\\\x2d[0-9]\+\\\\x2d\(.*\)\.scope$|\1|p" \
  132. -e "s|.*/machine/qemu-[0-9]\+-\(.*\)\.libvirt-qemu$|\1|p" \
  133. -e "s|.*/machine/\(.*\)\.libvirt-qemu$|\1|p" \
  134. <<EOF
  135. ${c}
  136. EOF
  137. }
  138. virsh_find_all_interfaces_for_cgroup() {
  139. local c="${1}" # the cgroup path
  140. # the virsh command
  141. local virsh
  142. # shellcheck disable=SC2230
  143. virsh="$(which virsh 2>/dev/null || command -v virsh 2>/dev/null)"
  144. if [ -n "${virsh}" ]
  145. then
  146. local d
  147. d="$(virsh_cgroup_to_domain_name "${c}")"
  148. # convert hex to character
  149. # e.g.: vm01\x2dweb => vm01-web (https://github.com/netdata/netdata/issues/11088#issuecomment-832618149)
  150. d="$(printf '%b' "${d}")"
  151. if [ -n "${d}" ]
  152. then
  153. debug "running: virsh domiflist ${d}; to find the network interfaces"
  154. # 'virsh -r domiflist <domain>' example output
  155. # Interface Type Source Model MAC
  156. #--------------------------------------------------------------
  157. # vnet3 bridge br0 virtio 52:54:00:xx:xx:xx
  158. # vnet4 network default virtio 52:54:00:yy:yy:yy
  159. # match only 'network' interfaces from virsh output
  160. set_source "virsh"
  161. "${virsh}" -r domiflist "${d}" |\
  162. sed -n \
  163. -e "s|^[[:space:]]\?\([^[:space:]]\+\)[[:space:]]\+network[[:space:]]\+\([^[:space:]]\+\)[[:space:]]\+[^[:space:]]\+[[:space:]]\+[^[:space:]]\+$|\1 \1_\2|p" \
  164. -e "s|^[[:space:]]\?\([^[:space:]]\+\)[[:space:]]\+bridge[[:space:]]\+\([^[:space:]]\+\)[[:space:]]\+[^[:space:]]\+[[:space:]]\+[^[:space:]]\+$|\1 \1_\2|p"
  165. else
  166. debug "no virsh domain extracted from cgroup ${c}"
  167. fi
  168. else
  169. debug "virsh command is not available"
  170. fi
  171. }
  172. # -----------------------------------------------------------------------------
  173. # netnsid detected interfaces
  174. netnsid_find_all_interfaces_for_pid() {
  175. local pid="${1}"
  176. [ -z "${pid}" ] && return 1
  177. local nsid
  178. nsid=$(lsns -t net -p "${pid}" -o NETNSID -nr 2>/dev/null)
  179. if [ -z "${nsid}" ] || [ "${nsid}" = "unassigned" ]; then
  180. return 1
  181. fi
  182. set_source "netnsid"
  183. ip link show |\
  184. grep -B 1 -E " link-netnsid ${nsid}($| )" |\
  185. sed -n -e "s|^[[:space:]]*[0-9]\+:[[:space:]]\+\([A-Za-z0-9_]\+\)\(@[A-Za-z0-9_]\+\)*:[[:space:]].*$|\1|p"
  186. }
  187. netnsid_find_all_interfaces_for_cgroup() {
  188. local c="${1}" # the cgroup path
  189. if [ -f "${c}/cgroup.procs" ]; then
  190. netnsid_find_all_interfaces_for_pid "$(head -n 1 "${c}/cgroup.procs" 2>/dev/null)"
  191. else
  192. debug "Cannot find file '${c}/cgroup.procs', not searching for netnsid interfaces."
  193. fi
  194. }
  195. # -----------------------------------------------------------------------------
  196. find_all_interfaces_of_pid_or_cgroup() {
  197. local p="${1}" c="${2}" # the pid and the cgroup path
  198. if [ -n "${pid}" ]
  199. then
  200. # we have been called with a pid
  201. proc_pid_fdinfo_iff "${p}"
  202. netnsid_find_all_interfaces_for_pid "${p}"
  203. elif [ -n "${c}" ]
  204. then
  205. # we have been called with a cgroup
  206. info "searching for network interfaces of cgroup '${c}'"
  207. find_tun_tap_interfaces_for_cgroup "${c}"
  208. virsh_find_all_interfaces_for_cgroup "${c}"
  209. netnsid_find_all_interfaces_for_cgroup "${c}"
  210. else
  211. error "Either a pid or a cgroup path is needed"
  212. return 1
  213. fi
  214. return 0
  215. }
  216. # -----------------------------------------------------------------------------
  217. # an associative array to store the interfaces
  218. # the index is the interface name as seen by the host
  219. # the value is the interface name as seen by the guest / container
  220. declare -A devs=()
  221. # store all interfaces found in the associative array
  222. # this will also give the unique devices, as seen by the host
  223. last_src=
  224. # shellcheck disable=SC2162
  225. while read host_device guest_device
  226. do
  227. [ -z "${host_device}" ] && continue
  228. [ "${host_device}" = "SRC" ] && last_src="${guest_device}" && continue
  229. # the default guest_device is the host_device
  230. [ -z "${guest_device}" ] && guest_device="${host_device}"
  231. # when we run in debug, show the source
  232. debug "Found host device '${host_device}', guest device '${guest_device}', detected via '${last_src}'"
  233. if [ -z "${devs[${host_device}]}" ] || [ "${devs[${host_device}]}" = "${host_device}" ]; then
  234. devs[${host_device}]="${guest_device}"
  235. fi
  236. done < <( find_all_interfaces_of_pid_or_cgroup "${pid}" "${cgroup}" )
  237. # print the interfaces found, in the format netdata expects them
  238. found=0
  239. for x in "${!devs[@]}"
  240. do
  241. found=$((found + 1))
  242. echo "${x} ${devs[${x}]}"
  243. done
  244. debug "found ${found} network interfaces for pid '${pid}', cgroup '${cgroup}', run as ${USER}, ${UID}"
  245. # let netdata know if we found any
  246. [ ${found} -eq 0 ] && exit 1
  247. exit 0