cgroup-network-helper.sh 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #!/usr/bin/env bash
  2. # cgroup-network-helper.sh
  3. # detect container and virtual machine interfaces
  4. #
  5. # (C) 2017 Costa Tsaousis
  6. # GPL v3+
  7. #
  8. # This script is called as root (by cgroup-network), with either a pid, or a cgroup path.
  9. # It tries to find all the network interfaces that belong to the same cgroup.
  10. #
  11. # It supports several method for this detection:
  12. #
  13. # 1. cgroup-network (the binary father of this script) detects veth network interfaces,
  14. # by examining iflink and ifindex IDs and switching namespaces
  15. # (it also detects the interface name as it is used by the container).
  16. #
  17. # 2. this script, uses /proc/PID/fdinfo to find tun/tap network interfaces.
  18. #
  19. # 3. this script, calls virsh to find libvirt network interfaces.
  20. #
  21. # -----------------------------------------------------------------------------
  22. export PATH="${PATH}:/sbin:/usr/sbin:/usr/local/sbin"
  23. export LC_ALL=C
  24. PROGRAM_NAME="$(basename "${0}")"
  25. logdate() {
  26. date "+%Y-%m-%d %H:%M:%S"
  27. }
  28. log() {
  29. local status="${1}"
  30. shift
  31. echo >&2 "$(logdate): ${PROGRAM_NAME}: ${status}: ${*}"
  32. }
  33. warning() {
  34. log WARNING "${@}"
  35. }
  36. error() {
  37. log ERROR "${@}"
  38. }
  39. info() {
  40. log INFO "${@}"
  41. }
  42. fatal() {
  43. log FATAL "${@}"
  44. exit 1
  45. }
  46. debug=0
  47. debug() {
  48. [ "${debug}" = "1" ] && log DEBUG "${@}"
  49. }
  50. # -----------------------------------------------------------------------------
  51. # check for BASH v4+ (required for associative arrays)
  52. [ $(( ${BASH_VERSINFO[0]} )) -lt 4 ] && \
  53. fatal "BASH version 4 or later is required (this is ${BASH_VERSION})."
  54. # -----------------------------------------------------------------------------
  55. # defaults to allow running this script by hand
  56. [ -z "${NETDATA_PLUGINS_DIR}" ] && NETDATA_PLUGINS_DIR="$(dirname "${0}")"
  57. [ -z "${NETDATA_CONFIG_DIR}" ] && NETDATA_CONFIG_DIR="$(dirname "${0}")/../../../../etc/netdata"
  58. [ -z "${NETDATA_CACHE_DIR}" ] && NETDATA_CACHE_DIR="$(dirname "${0}")/../../../../var/cache/netdata"
  59. # -----------------------------------------------------------------------------
  60. # parse the arguments
  61. pid=
  62. cgroup=
  63. while [ ! -z "${1}" ]
  64. do
  65. case "${1}" in
  66. --cgroup) cgroup="${2}"; shift 1;;
  67. --pid|-p) pid="${2}"; shift 1;;
  68. --debug|debug) debug=1;;
  69. *) fatal "Cannot understand argument '${1}'";;
  70. esac
  71. shift
  72. done
  73. if [ -z "${pid}" -a -z "${cgroup}" ]
  74. then
  75. fatal "Either --pid or --cgroup is required"
  76. fi
  77. # -----------------------------------------------------------------------------
  78. set_source() {
  79. [ ${debug} -eq 1 ] && echo "SRC ${*}"
  80. }
  81. # -----------------------------------------------------------------------------
  82. # veth interfaces via cgroup
  83. # cgroup-network can detect veth interfaces by itself (written in C).
  84. # If you seek for a shell version of what it does, check this:
  85. # https://github.com/firehol/netdata/issues/474#issuecomment-317866709
  86. # -----------------------------------------------------------------------------
  87. # tun/tap interfaces via /proc/PID/fdinfo
  88. # find any tun/tap devices linked to a pid
  89. proc_pid_fdinfo_iff() {
  90. local p="${1}" # the pid
  91. debug "Searching for tun/tap interfaces for pid ${p}..."
  92. set_source "fdinfo"
  93. grep ^iff:.* "${NETDATA_HOST_PREFIX}/proc/${p}/fdinfo"/* 2>/dev/null | cut -f 2
  94. }
  95. find_tun_tap_interfaces_for_cgroup() {
  96. local c="${1}" # the cgroup path
  97. # for each pid of the cgroup
  98. # find any tun/tap devices linked to the pid
  99. if [ -f "${c}/emulator/cgroup.procs" ]
  100. then
  101. local p
  102. for p in $(< "${c}/emulator/cgroup.procs" )
  103. do
  104. proc_pid_fdinfo_iff ${p}
  105. done
  106. fi
  107. }
  108. # -----------------------------------------------------------------------------
  109. # virsh domain network interfaces
  110. virsh_cgroup_to_domain_name() {
  111. local c="${1}" # the cgroup path
  112. debug "extracting a possible virsh domain from cgroup ${c}..."
  113. # extract for the cgroup path
  114. sed -n -e "s|.*/machine-qemu\\\\x2d[0-9]\+\\\\x2d\(.*\)\.scope$|\1|p" \
  115. -e "s|.*/machine/\(.*\)\.libvirt-qemu$|\1|p" \
  116. <<EOF
  117. ${c}
  118. EOF
  119. }
  120. virsh_find_all_interfaces_for_cgroup() {
  121. local c="${1}" # the cgroup path
  122. # the virsh command
  123. local virsh="$(which virsh 2>/dev/null || command -v virsh 2>/dev/null)"
  124. if [ ! -z "${virsh}" ]
  125. then
  126. local d="$(virsh_cgroup_to_domain_name "${c}")"
  127. if [ ! -z "${d}" ]
  128. then
  129. debug "running: virsh domiflist ${d}; to find the network interfaces"
  130. # match only 'network' interfaces from virsh output
  131. set_source "virsh"
  132. "${virsh}" -r domiflist ${d} |\
  133. sed -n \
  134. -e "s|^\([^[:space:]]\+\)[[:space:]]\+network[[:space:]]\+\([^[:space:]]\+\)[[:space:]]\+[^[:space:]]\+[[:space:]]\+[^[:space:]]\+$|\1 \1_\2|p" \
  135. -e "s|^\([^[:space:]]\+\)[[:space:]]\+bridge[[:space:]]\+\([^[:space:]]\+\)[[:space:]]\+[^[:space:]]\+[[:space:]]\+[^[:space:]]\+$|\1 \1_\2|p"
  136. else
  137. debug "no virsh domain extracted from cgroup ${c}"
  138. fi
  139. else
  140. debug "virsh command is not available"
  141. fi
  142. }
  143. # -----------------------------------------------------------------------------
  144. find_all_interfaces_of_pid_or_cgroup() {
  145. local p="${1}" c="${2}" # the pid and the cgroup path
  146. if [ ! -z "${pid}" ]
  147. then
  148. # we have been called with a pid
  149. proc_pid_fdinfo_iff ${p}
  150. elif [ ! -z "${c}" ]
  151. then
  152. # we have been called with a cgroup
  153. info "searching for network interfaces of cgroup '${c}'"
  154. find_tun_tap_interfaces_for_cgroup "${c}"
  155. virsh_find_all_interfaces_for_cgroup "${c}"
  156. else
  157. error "Either a pid or a cgroup path is needed"
  158. return 1
  159. fi
  160. return 0
  161. }
  162. # -----------------------------------------------------------------------------
  163. # an associative array to store the interfaces
  164. # the index is the interface name as seen by the host
  165. # the value is the interface name as seen by the guest / container
  166. declare -A devs=()
  167. # store all interfaces found in the associative array
  168. # this will also give the unique devices, as seen by the host
  169. last_src=
  170. while read host_device guest_device
  171. do
  172. [ -z "${host_device}" ] && continue
  173. [ "${host_device}" = "SRC" ] && last_src="${guest_device}" && continue
  174. # the default guest_device is the host_device
  175. [ -z "${guest_device}" ] && guest_device="${host_device}"
  176. # when we run in debug, show the source
  177. debug "Found host device '${host_device}', guest device '${guest_device}', detected via '${last_src}'"
  178. [ -z "${devs[${host_device}]}" -o "${devs[${host_device}]}" = "${host_device}" ] && \
  179. devs[${host_device}]="${guest_device}"
  180. done < <( find_all_interfaces_of_pid_or_cgroup "${pid}" "${cgroup}" )
  181. # print the interfaces found, in the format netdata expects them
  182. found=0
  183. for x in "${!devs[@]}"
  184. do
  185. found=$((found + 1))
  186. echo "${x} ${devs[${x}]}"
  187. done
  188. debug "found ${found} network interfaces for pid '${pid}', cgroup '${cgroup}', run as ${USER}, ${UID}"
  189. # let netdata know if we found any
  190. [ ${found} -eq 0 ] && exit 1
  191. exit 0