postfix.chart.sh 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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) 2016 Costa Tsaousis <costa@tsaousis.gr>
  7. #
  8. # the postqueue command
  9. # if empty, it will use the one found in the system path
  10. postfix_postqueue=
  11. # how frequently to collect queue size
  12. postfix_update_every=15
  13. postfix_priority=60000
  14. postfix_check() {
  15. # this should return:
  16. # - 0 to enable the chart
  17. # - 1 to disable the chart
  18. # try to find the postqueue executable
  19. if [ -z "$postfix_postqueue" ] || [ ! -x "$postfix_postqueue" ]
  20. then
  21. # shellcheck disable=SC2230
  22. postfix_postqueue="$(which postqueue 2>/dev/null || command -v postqueue 2>/dev/null)"
  23. fi
  24. if [ -z "$postfix_postqueue" ] || [ ! -x "$postfix_postqueue" ]
  25. then
  26. # shellcheck disable=SC2154
  27. error "cannot find postqueue. Please set 'postfix_postqueue=/path/to/postqueue' in $confd/postfix.conf"
  28. return 1
  29. fi
  30. return 0
  31. }
  32. postfix_create() {
  33. cat <<EOF
  34. CHART postfix_local.qemails '' "Postfix Queue Emails" "emails" queue postfix.queued.emails line $((postfix_priority + 1)) $postfix_update_every
  35. DIMENSION emails '' absolute 1 1
  36. CHART postfix_local.qsize '' "Postfix Queue Emails Size" "emails size in KB" queue postfix.queued.size area $((postfix_priority + 2)) $postfix_update_every
  37. DIMENSION size '' absolute 1 1
  38. EOF
  39. return 0
  40. }
  41. postfix_update() {
  42. # the first argument to this function is the microseconds since last update
  43. # pass this parameter to the BEGIN statement (see bellow).
  44. # do all the work to collect / calculate the values
  45. # for each dimension
  46. # remember: KEEP IT SIMPLE AND SHORT
  47. # 1. execute postqueue -p
  48. # 2. get the line that begins with --
  49. # 3. match the 2 numbers on the line and output 2 lines like these:
  50. # local postfix_q_size=NUMBER
  51. # local postfix_q_emails=NUMBER
  52. # 4. then execute this a script with the eval
  53. #
  54. # be very carefull with eval:
  55. # prepare the script and always egrep at the end the lines that are usefull, so that
  56. # even if something goes wrong, no other code can be executed
  57. postfix_q_emails=0
  58. postfix_q_size=0
  59. eval "$(run "$postfix_postqueue" -p |\
  60. grep "^--" |\
  61. sed -e "s/-- \([0-9]\+\) Kbytes in \([0-9]\+\) Requests.$/local postfix_q_size=\1\nlocal postfix_q_emails=\2/g" |\
  62. grep -E "^local postfix_q_(emails|size)=[0-9]+$")"
  63. # write the result of the work.
  64. cat <<VALUESEOF
  65. BEGIN postfix_local.qemails $1
  66. SET emails = $postfix_q_emails
  67. END
  68. BEGIN postfix_local.qsize $1
  69. SET size = $postfix_q_size
  70. END
  71. VALUESEOF
  72. return 0
  73. }