cgroup-network-helper.sh 8.0 KB

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