cgroup-network-helper.sh 8.5 KB

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