libreswan.chart.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. # shellcheck shell=bash disable=SC1117
  2. # no need for shebang - this file is loaded from charts.d.plugin
  3. # SPDX-License-Identifier: GPL-3.0-or-later
  4. # netdata
  5. # real-time performance and health monitoring, done right!
  6. # (C) 2018 Costa Tsaousis <costa@tsaousis.gr>
  7. #
  8. # _update_every is a special variable - it holds the number of seconds
  9. # between the calls of the _update() function
  10. libreswan_update_every=1
  11. # the priority is used to sort the charts on the dashboard
  12. # 1 = the first chart
  13. libreswan_priority=90000
  14. # set to 1, to run ipsec with sudo
  15. libreswan_sudo=1
  16. # global variables to store our collected data
  17. # [TUNNELID] = TUNNELNAME
  18. # here we track the *latest* established tunnels
  19. # as detected by: ipsec whack --status
  20. declare -A libreswan_connected_tunnels=()
  21. # [TUNNELID] = VALUE
  22. # here we track values of all established tunnels (not only the latest)
  23. # as detected by: ipsec whack --trafficstatus
  24. declare -A libreswan_traffic_in=()
  25. declare -A libreswan_traffic_out=()
  26. declare -A libreswan_established_add_time=()
  27. # [TUNNELNAME] = CHARTID
  28. # here we remember CHARTIDs of all tunnels
  29. # we need this to avoid converting tunnel names to chart IDs on every iteration
  30. declare -A libreswan_tunnel_charts=()
  31. # run the ipsec command
  32. libreswan_ipsec() {
  33. if [ ${libreswan_sudo} -ne 0 ]; then
  34. sudo -n "${IPSEC_CMD}" "${@}"
  35. return $?
  36. else
  37. "${IPSEC_CMD}" "${@}"
  38. return $?
  39. fi
  40. }
  41. # fetch latest values - fill the arrays
  42. libreswan_get() {
  43. # do all the work to collect / calculate the values
  44. # for each dimension
  45. # empty the variables
  46. libreswan_traffic_in=()
  47. libreswan_traffic_out=()
  48. libreswan_established_add_time=()
  49. libreswan_connected_tunnels=()
  50. # convert the ipsec command output to a shell script
  51. # and source it to get the values
  52. # shellcheck disable=SC1090
  53. source <(
  54. {
  55. libreswan_ipsec whack --status
  56. libreswan_ipsec whack --trafficstatus
  57. } | sed -n \
  58. -e "s|[0-9]\+ #\([0-9]\+\): \"\(.*\)\".*IPsec SA established.*newest IPSEC.*|libreswan_connected_tunnels[\"\1\"]=\"\2\"|p" \
  59. -e "s|[0-9]\+ #\([0-9]\+\): \"\(.*\)\",.* add_time=\([0-9]\+\),.* inBytes=\([0-9]\+\),.* outBytes=\([0-9]\+\).*|libreswan_traffic_in[\"\1\"]=\"\4\"; libreswan_traffic_out[\"\1\"]=\"\5\"; libreswan_established_add_time[\"\1\"]=\"\3\";|p"
  60. ) || return 1
  61. # check we got some data
  62. [ ${#libreswan_connected_tunnels[@]} -eq 0 ] && return 1
  63. return 0
  64. }
  65. # _check is called once, to find out if this chart should be enabled or not
  66. libreswan_check() {
  67. # this should return:
  68. # - 0 to enable the chart
  69. # - 1 to disable the chart
  70. require_cmd ipsec || return 1
  71. # make sure it is libreswan
  72. # shellcheck disable=SC2143
  73. if [ -z "$(ipsec --version | grep -i libreswan)" ]; then
  74. error "ipsec command is not Libreswan. Disabling Libreswan plugin."
  75. return 1
  76. fi
  77. # check that we can collect data
  78. libreswan_get || return 1
  79. return 0
  80. }
  81. # create the charts for an ipsec tunnel
  82. libreswan_create_one() {
  83. local n="${1}" name
  84. name="${libreswan_connected_tunnels[${n}]}"
  85. [ ! -z "${libreswan_tunnel_charts[${name}]}" ] && return 0
  86. libreswan_tunnel_charts[${name}]="$(fixid "${name}")"
  87. cat <<EOF
  88. CHART libreswan.${libreswan_tunnel_charts[${name}]}_net '${name}_net' "LibreSWAN Tunnel ${name} Traffic" "kilobits/s" "${name}" libreswan.net area $((libreswan_priority)) $libreswan_update_every
  89. DIMENSION in '' incremental 8 1000
  90. DIMENSION out '' incremental -8 1000
  91. CHART libreswan.${libreswan_tunnel_charts[${name}]}_uptime '${name}_uptime' "LibreSWAN Tunnel ${name} Uptime" "seconds" "${name}" libreswan.uptime line $((libreswan_priority + 1)) $libreswan_update_every
  92. DIMENSION uptime '' absolute 1 1
  93. EOF
  94. return 0
  95. }
  96. # _create is called once, to create the charts
  97. libreswan_create() {
  98. local n
  99. for n in "${!libreswan_connected_tunnels[@]}"; do
  100. libreswan_create_one "${n}"
  101. done
  102. return 0
  103. }
  104. libreswan_now=$(date +%s)
  105. # send the values to netdata for an ipsec tunnel
  106. libreswan_update_one() {
  107. local n="${1}" microseconds="${2}" name id uptime
  108. name="${libreswan_connected_tunnels[${n}]}"
  109. id="${libreswan_tunnel_charts[${name}]}"
  110. [ -z "${id}" ] && libreswan_create_one "${name}"
  111. uptime=$((libreswan_now - libreswan_established_add_time[${n}]))
  112. [ ${uptime} -lt 0 ] && uptime=0
  113. # write the result of the work.
  114. cat <<VALUESEOF
  115. BEGIN libreswan.${id}_net ${microseconds}
  116. SET in = ${libreswan_traffic_in[${n}]}
  117. SET out = ${libreswan_traffic_out[${n}]}
  118. END
  119. BEGIN libreswan.${id}_uptime ${microseconds}
  120. SET uptime = ${uptime}
  121. END
  122. VALUESEOF
  123. }
  124. # _update is called continiously, to collect the values
  125. libreswan_update() {
  126. # the first argument to this function is the microseconds since last update
  127. # pass this parameter to the BEGIN statement (see bellow).
  128. libreswan_get || return 1
  129. libreswan_now=$(date +%s)
  130. local n
  131. for n in "${!libreswan_connected_tunnels[@]}"; do
  132. libreswan_update_one "${n}" "${@}"
  133. done
  134. return 0
  135. }