cgroup-network-helper.sh 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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/qemu-[0-9]\+-\(.*\)\.libvirt-qemu$|\1|p" \
  118. -e "s|.*/machine/\(.*\)\.libvirt-qemu$|\1|p" \
  119. <<EOF
  120. ${c}
  121. EOF
  122. }
  123. virsh_find_all_interfaces_for_cgroup() {
  124. local c="${1}" # the cgroup path
  125. # the virsh command
  126. local virsh
  127. # shellcheck disable=SC2230
  128. virsh="$(which virsh 2>/dev/null || command -v virsh 2>/dev/null)"
  129. if [ -n "${virsh}" ]
  130. then
  131. local d
  132. d="$(virsh_cgroup_to_domain_name "${c}")"
  133. # convert hex to character
  134. # e.g.: vm01\x2dweb => vm01-web (https://github.com/netdata/netdata/issues/11088#issuecomment-832618149)
  135. d="$(printf '%b' "${d}")"
  136. if [ -n "${d}" ]
  137. then
  138. debug "running: virsh domiflist ${d}; to find the network interfaces"
  139. # 'virsh -r domiflist <domain>' example output
  140. # Interface Type Source Model MAC
  141. #--------------------------------------------------------------
  142. # vnet3 bridge br0 virtio 52:54:00:xx:xx:xx
  143. # vnet4 network default virtio 52:54:00:yy:yy:yy
  144. # match only 'network' interfaces from virsh output
  145. set_source "virsh"
  146. "${virsh}" -r domiflist "${d}" |\
  147. sed -n \
  148. -e "s|^[[:space:]]\?\([^[:space:]]\+\)[[:space:]]\+network[[:space:]]\+\([^[:space:]]\+\)[[:space:]]\+[^[:space:]]\+[[:space:]]\+[^[:space:]]\+$|\1 \1_\2|p" \
  149. -e "s|^[[:space:]]\?\([^[:space:]]\+\)[[:space:]]\+bridge[[:space:]]\+\([^[:space:]]\+\)[[:space:]]\+[^[:space:]]\+[[:space:]]\+[^[:space:]]\+$|\1 \1_\2|p"
  150. else
  151. debug "no virsh domain extracted from cgroup ${c}"
  152. fi
  153. else
  154. debug "virsh command is not available"
  155. fi
  156. }
  157. # -----------------------------------------------------------------------------
  158. # netnsid detected interfaces
  159. netnsid_find_all_interfaces_for_pid() {
  160. local pid="${1}"
  161. [ -z "${pid}" ] && return 1
  162. local nsid
  163. nsid=$(lsns -t net -p "${pid}" -o NETNSID -nr 2>/dev/null)
  164. if [ -z "${nsid}" ] || [ "${nsid}" = "unassigned" ]; then
  165. return 1
  166. fi
  167. set_source "netnsid"
  168. ip link show |\
  169. grep -B 1 -E " link-netnsid ${nsid}($| )" |\
  170. sed -n -e "s|^[[:space:]]*[0-9]\+:[[:space:]]\+\([A-Za-z0-9_]\+\)\(@[A-Za-z0-9_]\+\)*:[[:space:]].*$|\1|p"
  171. }
  172. netnsid_find_all_interfaces_for_cgroup() {
  173. local c="${1}" # the cgroup path
  174. if [ -f "${c}/cgroup.procs" ]; then
  175. netnsid_find_all_interfaces_for_pid "$(head -n 1 "${c}/cgroup.procs" 2>/dev/null)"
  176. else
  177. debug "Cannot find file '${c}/cgroup.procs', not searching for netnsid interfaces."
  178. fi
  179. }
  180. # -----------------------------------------------------------------------------
  181. find_all_interfaces_of_pid_or_cgroup() {
  182. local p="${1}" c="${2}" # the pid and the cgroup path
  183. if [ -n "${pid}" ]
  184. then
  185. # we have been called with a pid
  186. proc_pid_fdinfo_iff "${p}"
  187. netnsid_find_all_interfaces_for_pid "${p}"
  188. elif [ -n "${c}" ]
  189. then
  190. # we have been called with a cgroup
  191. info "searching for network interfaces of cgroup '${c}'"
  192. find_tun_tap_interfaces_for_cgroup "${c}"
  193. virsh_find_all_interfaces_for_cgroup "${c}"
  194. netnsid_find_all_interfaces_for_cgroup "${c}"
  195. else
  196. error "Either a pid or a cgroup path is needed"
  197. return 1
  198. fi
  199. return 0
  200. }
  201. # -----------------------------------------------------------------------------
  202. # an associative array to store the interfaces
  203. # the index is the interface name as seen by the host
  204. # the value is the interface name as seen by the guest / container
  205. declare -A devs=()
  206. # store all interfaces found in the associative array
  207. # this will also give the unique devices, as seen by the host
  208. last_src=
  209. # shellcheck disable=SC2162
  210. while read host_device guest_device
  211. do
  212. [ -z "${host_device}" ] && continue
  213. [ "${host_device}" = "SRC" ] && last_src="${guest_device}" && continue
  214. # the default guest_device is the host_device
  215. [ -z "${guest_device}" ] && guest_device="${host_device}"
  216. # when we run in debug, show the source
  217. debug "Found host device '${host_device}', guest device '${guest_device}', detected via '${last_src}'"
  218. if [ -z "${devs[${host_device}]}" ] || [ "${devs[${host_device}]}" = "${host_device}" ]; then
  219. devs[${host_device}]="${guest_device}"
  220. fi
  221. done < <( find_all_interfaces_of_pid_or_cgroup "${pid}" "${cgroup}" )
  222. # print the interfaces found, in the format netdata expects them
  223. found=0
  224. for x in "${!devs[@]}"
  225. do
  226. found=$((found + 1))
  227. echo "${x} ${devs[${x}]}"
  228. done
  229. debug "found ${found} network interfaces for pid '${pid}', cgroup '${cgroup}', run as ${USER}, ${UID}"
  230. # let netdata know if we found any
  231. [ ${found} -eq 0 ] && exit 1
  232. exit 0