You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 lines
5.0 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2006-2016 OpenWrt.org
  3. START=80
  4. STOP=10
  5. USE_PROCD=1
  6. COLLECTD_CONF="/tmp/collectd.conf"
  7. LOG="logger -t collectd[$$] -p"
  8. NICEPRIO=5
  9. CONFIG_STRING=""
  10. process_exec() {
  11. printf "<Plugin exec>\n" >> "$COLLECTD_CONF"
  12. config_foreach process_exec_sections exec_input "Exec"
  13. config_foreach process_exec_sections exec_notify "NotificationExec"
  14. printf "</Plugin>\n\n" >> "$COLLECTD_CONF"
  15. }
  16. process_exec_sections() {
  17. local cfg="$1"
  18. local section="$2"
  19. local cmdline cmduser cmdgroup
  20. config_get cmdline "$cfg" cmdline
  21. [ -z "$cmdline" ] && {
  22. $LOG notice "No cmdline option in config $cfg defined"
  23. return 0
  24. }
  25. config_get cmduser "$cfg" cmduser
  26. [ -z "$cmduser" ] && {
  27. $LOG notice "No cmduser option in config $cfg defined"
  28. return 0
  29. }
  30. config_get cmdgroup "$cfg" cmdgroup
  31. if [ -z "$cmdgroup" ]; then
  32. printf "\\t%s \"%s\" \"%s\"\n" "${section}" "${cmduser}" "${cmdline}" >> "$COLLECTD_CONF"
  33. else
  34. printf "\\t%s \"%s:%s\" \"%s\"\n" "${section}" "${cmduser}" "${cmdgroup}" "${cmdline}" >> "$COLLECTD_CONF"
  35. fi
  36. }
  37. CONFIG_LIST=""
  38. add_list_option() {
  39. local value="$1"
  40. local option="$2"
  41. local indent="$3"
  42. CONFIG_LIST="${CONFIG_LIST}${indent}${option} \"$value\"\n"
  43. }
  44. process_generic() {
  45. local cfg="$1"
  46. local indent="$2"
  47. local json="$3"
  48. local config=""
  49. . /usr/share/libubox/jshn.sh
  50. json_init
  51. json_load_file "$json"
  52. json_select string 1>/dev/null 2>&1
  53. if [ $? -eq 0 ]; then
  54. json_get_keys keys
  55. for key in ${keys}; do
  56. json_get_var option "$key"
  57. config_get value "$cfg" "$option" ""
  58. [ -z "$value" ] || {
  59. config="${config}${indent}${option} \"${value}\"\n"
  60. }
  61. done
  62. json_select ..
  63. fi
  64. json_select bool 1>/dev/null 2>&1
  65. if [ $? -eq 0 ]; then
  66. json_get_keys keys
  67. for key in ${keys}; do
  68. json_get_var option "$key"
  69. config_get_bool value "$cfg" "$option"
  70. if [ "$value" = "0" ]; then
  71. config="${config}${indent}${option} false\n"
  72. fi
  73. if [ "$value" = "1" ]; then
  74. config="${config}${indent}${option} true\n"
  75. fi
  76. done
  77. json_select ..
  78. fi
  79. json_select list 1>/dev/null 2>&1
  80. if [ $? -eq 0 ]; then
  81. json_get_keys keys
  82. for key in ${keys}; do
  83. json_get_var option "$key"
  84. CONFIG_LIST=""
  85. config_list_foreach "$cfg" "$option" add_list_option "$option" "$indent"
  86. config="${config}${CONFIG_LIST}"
  87. done
  88. json_select ..
  89. fi
  90. [ -z "$config" ] || {
  91. printf "%s<Plugin %s>\n" "${CONFIG_STRING}" "$cfg" >> "$COLLECTD_CONF"
  92. echo -e "${config}" >> "$COLLECTD_CONF"
  93. printf "%s</Plugin>\n" "${CONFIG_STRING}" >> "$COLLECTD_CONF"
  94. }
  95. printf "\n" >> "$COLLECTD_CONF"
  96. }
  97. process_plugins() {
  98. local cfg="$1"
  99. local enable keys key option value
  100. config_get enable "$cfg" enable 0
  101. [ "$enable" = "1" ] || return 0
  102. [ -f "/usr/lib/collectd/$cfg.so" ] || {
  103. $LOG notice "Plugin collectd-mod-$cfg not installed"
  104. return 0
  105. }
  106. [ -f "/usr/share/collectd/plugin/$cfg.json" ] || {
  107. $LOG notice "Configuration definition file for $cfg not found"
  108. return 0
  109. }
  110. printf "LoadPlugin %s\n" "$cfg" >> "$COLLECTD_CONF"
  111. case "$cfg" in
  112. exec)
  113. CONFIG_STRING=""
  114. process_exec
  115. ;;
  116. *)
  117. CONFIG_STRING=""
  118. process_generic "$cfg" "\\t" "/usr/share/collectd/plugin/$cfg.json"
  119. ;;
  120. esac
  121. }
  122. process_config() {
  123. local alt_config_file BaseDir Include PIDFile PluginDir TypesDB
  124. local Interval ReadThreads Hostname
  125. rm -f "$COLLECTD_CONF"
  126. [ -f /etc/config/collectd ] || {
  127. $LOG notice "UCI config not found"
  128. return 0
  129. }
  130. config_load collectd
  131. config_get alt_config_file globals alt_config_file
  132. # If "alt_config_file" specified, use that instead
  133. [ -n "$alt_config_file" ] && [ -f "$alt_config_file" ] && {
  134. rm -f "$COLLECTD_CONF"
  135. ln -s "$alt_config_file" "$COLLECTD_CONF"
  136. return 0
  137. }
  138. # GOBAL CONFIG
  139. config_get BaseDir globals BaseDir "/var/run/collectd"
  140. printf "BaseDir \"%s\"\n" "$BaseDir" >> "$COLLECTD_CONF"
  141. config_get Include globals Include "/tmp/collectd.d"
  142. printf "Include \"%s\"\n" "$Include" >> "$COLLECTD_CONF"
  143. mkdir -p "$Include"
  144. config_get PIDFile globals PIDFile "/var/run/collectd.pid"
  145. printf "PIDFile \"%s\"\n" "$PIDFile" >> "$COLLECTD_CONF"
  146. config_get PluginDir globals PluginDir "/usr/lib/collectd"
  147. printf "PluginDir \"%s\"\n" "$PluginDir" >> "$COLLECTD_CONF"
  148. config_get TypesDB globals TypesDB "/usr/share/collectd/types.db"
  149. printf "TypesDB \"%s\"\n" "$TypesDB" >> "$COLLECTD_CONF"
  150. config_get Interval globals Interval 30
  151. printf "Interval \"%s\"\n" "$Interval" >> "$COLLECTD_CONF"
  152. config_get ReadThreads globals ReadThreads 2
  153. printf "ReadThreads \"%s\"\n" "$ReadThreads" >> "$COLLECTD_CONF"
  154. config_get Hostname globals Hostname "$(hostname)"
  155. printf "Hostname \"%s\"\n" "$Hostname" >> "$COLLECTD_CONF"
  156. printf "\n" >> "$COLLECTD_CONF"
  157. # PLUGIN CONFIG
  158. config_foreach process_plugins plugin
  159. }
  160. start_service() {
  161. procd_open_instance
  162. procd_set_param command /usr/sbin/collectd
  163. procd_append_param command -C "$COLLECTD_CONF"
  164. procd_append_param command -f # don't daemonize, procd will handle that for us
  165. procd_set_param nice "$NICEPRIO"
  166. process_config
  167. # set auto respawn behavior
  168. procd_set_param respawn
  169. procd_close_instance
  170. }