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.

356 lines
7.9 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. process_curl() {
  38. printf "<Plugin curl>\n" >> "$COLLECTD_CONF"
  39. config_foreach process_curl_page curl_page
  40. printf "</Plugin>\n\n" >> "$COLLECTD_CONF"
  41. }
  42. process_curl_page() {
  43. local cfg="$1"
  44. local name url
  45. config_get name "$cfg" name
  46. [ -z "$name" ] && {
  47. $LOG notice "No name option in config $cfg defined"
  48. return 0
  49. }
  50. config_get url "$cfg" url
  51. [ -z "$url" ] && {
  52. $LOG notice "No URL option in config $cfg defined"
  53. return 0
  54. }
  55. printf "\\t<Page \"%s\">\n" "${name}" >> "$COLLECTD_CONF"
  56. printf "\\t\\tURL \"%s\"\n" "${url}" >> "$COLLECTD_CONF"
  57. printf "\\t\\tMeasureResponseTime true\n" >> "$COLLECTD_CONF"
  58. printf "\\t</Page>\n" >> "$COLLECTD_CONF"
  59. }
  60. process_network() {
  61. local cfg="$1"
  62. local TimeToLive Forward CacheFlush
  63. printf "<Plugin network>\n" >> "$COLLECTD_CONF"
  64. config_foreach process_network_sections network_listen "listen"
  65. config_foreach process_network_sections network_server "server"
  66. config_get TimeToLive "$cfg" TimeToLive
  67. [ -z "$TimeToLive" ] || {
  68. printf "\\tTimeToLive %s\n" "${TimeToLive}" >> "$COLLECTD_CONF"
  69. }
  70. config_get CacheFlush "$cfg" CacheFlush
  71. [ -z "$CacheFlush" ] || {
  72. printf "\\tCacheFlush %s\n" "${CacheFlush}" >> "$COLLECTD_CONF"
  73. }
  74. config_get_bool Forward "$cfg" Forward
  75. if [ "$value" = "0" ]; then
  76. printf "\\tForward false\n" >> "$COLLECTD_CONF"
  77. else
  78. printf "\\tForward true\n" >> "$COLLECTD_CONF"
  79. fi
  80. printf "</Plugin>\n\n" >> "$COLLECTD_CONF"
  81. }
  82. process_network_sections() {
  83. local cfg="$1"
  84. local section="$2"
  85. local host port output
  86. config_get host "$cfg" host
  87. [ -z "$host" ] && {
  88. $LOG notice "No host option in config $cfg defined"
  89. return 0
  90. }
  91. if [ "$section" = "server" ]; then
  92. output="Server \"$host\""
  93. else
  94. output="Listen \"$host\""
  95. fi
  96. config_get port "$cfg" port
  97. if [ -z "$port" ]; then
  98. printf "\\t%s\n" "${output}" >> "$COLLECTD_CONF"
  99. else
  100. printf "\\t%s \"%s\"\n" "${output}" "${port}" >> "$COLLECTD_CONF"
  101. fi
  102. }
  103. process_iptables() {
  104. local cfg="$1"
  105. printf "<Plugin iptables>\n" >> "$COLLECTD_CONF"
  106. config_foreach process_iptables_sections iptables_match
  107. printf "</Plugin>\n\n" >> "$COLLECTD_CONF"
  108. }
  109. process_iptables_sections() {
  110. local cfg="$1"
  111. local table chain
  112. config_get table "$cfg" table
  113. [ -z "$table" ] && {
  114. $LOG notice "No table option in config $cfg defined"
  115. return 0
  116. }
  117. config_get chain "$cfg" chain
  118. [ -z "$chain" ] && {
  119. $LOG notice "No chain option in config $cfg defined"
  120. return 0
  121. }
  122. config_get index "$cfg" index
  123. [ -z "$index" ] && {
  124. $LOG notice "No index option in config $cfg defined"
  125. return 0
  126. }
  127. config_get name "$cfg" name
  128. if [ -z "$name" ]; then
  129. printf "\\tChain %s %s %s\n" "${table}" "${chain}" "${index}" >> "$COLLECTD_CONF"
  130. else
  131. printf "\\tChain %s %s %s \"%s\"\n" "${table}" "${chain}" "${index}" "${name}">> "$COLLECTD_CONF"
  132. fi
  133. }
  134. CONFIG_LIST=""
  135. add_list_option() {
  136. local value="$1"
  137. local option="$2"
  138. local indent="$3"
  139. CONFIG_LIST="${CONFIG_LIST}${indent}${option} \"$value\"\n"
  140. }
  141. process_generic() {
  142. local cfg="$1"
  143. local indent="$2"
  144. local json="$3"
  145. local config=""
  146. . /usr/share/libubox/jshn.sh
  147. json_init
  148. json_load_file "$json"
  149. json_select string 1>/dev/null 2>&1
  150. if [ $? -eq 0 ]; then
  151. json_get_keys keys
  152. for key in ${keys}; do
  153. json_get_var option "$key"
  154. config_get value "$cfg" "$option" ""
  155. [ -z "$value" ] || {
  156. config="${config}${indent}${option} \"${value}\"\n"
  157. }
  158. done
  159. json_select ..
  160. fi
  161. json_select bool 1>/dev/null 2>&1
  162. if [ $? -eq 0 ]; then
  163. json_get_keys keys
  164. for key in ${keys}; do
  165. json_get_var option "$key"
  166. config_get_bool value "$cfg" "$option"
  167. if [ "$value" = "0" ]; then
  168. config="${config}${indent}${option} false\n"
  169. fi
  170. if [ "$value" = "1" ]; then
  171. config="${config}${indent}${option} true\n"
  172. fi
  173. done
  174. json_select ..
  175. fi
  176. json_select list 1>/dev/null 2>&1
  177. if [ $? -eq 0 ]; then
  178. json_get_keys keys
  179. for key in ${keys}; do
  180. json_get_var option "$key"
  181. CONFIG_LIST=""
  182. config_list_foreach "$cfg" "$option" add_list_option "$option" "$indent"
  183. config="${config}${CONFIG_LIST}"
  184. done
  185. json_select ..
  186. fi
  187. [ -z "$config" ] || {
  188. printf "%s<Plugin %s>\n" "${CONFIG_STRING}" "$cfg" >> "$COLLECTD_CONF"
  189. echo -e "${config}" >> "$COLLECTD_CONF"
  190. printf "%s</Plugin>\n" "${CONFIG_STRING}" >> "$COLLECTD_CONF"
  191. }
  192. printf "\n" >> "$COLLECTD_CONF"
  193. }
  194. process_plugins() {
  195. local cfg="$1"
  196. local enable keys key option value
  197. config_get enable "$cfg" enable 0
  198. [ "$enable" = "1" ] || return 0
  199. [ -f "/usr/lib/collectd/$cfg.so" ] || {
  200. $LOG notice "Plugin collectd-mod-$cfg not installed"
  201. return 0
  202. }
  203. [ -f "/usr/share/collectd/plugin/$cfg.json" ] || {
  204. $LOG notice "Configuration definition file for $cfg not found"
  205. return 0
  206. }
  207. printf "LoadPlugin %s\n" "$cfg" >> "$COLLECTD_CONF"
  208. case "$cfg" in
  209. exec)
  210. CONFIG_STRING=""
  211. process_exec
  212. ;;
  213. curl)
  214. CONFIG_STRING=""
  215. process_curl
  216. ;;
  217. network)
  218. CONFIG_STRING=""
  219. process_network "$cfg"
  220. ;;
  221. iptables)
  222. CONFIG_STRING=""
  223. process_iptables
  224. ;;
  225. *)
  226. CONFIG_STRING=""
  227. process_generic "$cfg" "\\t" "/usr/share/collectd/plugin/$cfg.json"
  228. ;;
  229. esac
  230. }
  231. process_config() {
  232. local alt_config_file BaseDir Include PIDFile PluginDir TypesDB
  233. local Interval ReadThreads Hostname
  234. rm -f "$COLLECTD_CONF"
  235. [ -f /etc/config/collectd ] || {
  236. $LOG notice "UCI config not found"
  237. return 0
  238. }
  239. config_load collectd
  240. config_get alt_config_file globals alt_config_file
  241. # If "alt_config_file" specified, use that instead
  242. [ -n "$alt_config_file" ] && [ -f "$alt_config_file" ] && {
  243. rm -f "$COLLECTD_CONF"
  244. ln -s "$alt_config_file" "$COLLECTD_CONF"
  245. return 0
  246. }
  247. # GOBAL CONFIG
  248. config_get BaseDir globals BaseDir "/var/run/collectd"
  249. printf "BaseDir \"%s\"\n" "$BaseDir" >> "$COLLECTD_CONF"
  250. config_get Include globals Include "/tmp/collectd.d"
  251. printf "Include \"%s\"\n" "$Include" >> "$COLLECTD_CONF"
  252. mkdir -p "$Include"
  253. config_get PIDFile globals PIDFile "/var/run/collectd.pid"
  254. printf "PIDFile \"%s\"\n" "$PIDFile" >> "$COLLECTD_CONF"
  255. config_get PluginDir globals PluginDir "/usr/lib/collectd"
  256. printf "PluginDir \"%s\"\n" "$PluginDir" >> "$COLLECTD_CONF"
  257. config_get TypesDB globals TypesDB "/usr/share/collectd/types.db"
  258. printf "TypesDB \"%s\"\n" "$TypesDB" >> "$COLLECTD_CONF"
  259. config_get Interval globals Interval 30
  260. printf "Interval \"%s\"\n" "$Interval" >> "$COLLECTD_CONF"
  261. config_get ReadThreads globals ReadThreads 2
  262. printf "ReadThreads \"%s\"\n" "$ReadThreads" >> "$COLLECTD_CONF"
  263. config_get Hostname globals Hostname "$(uname -n)"
  264. printf "Hostname \"%s\"\n" "$Hostname" >> "$COLLECTD_CONF"
  265. printf "\n" >> "$COLLECTD_CONF"
  266. # PLUGIN CONFIG
  267. config_foreach process_plugins plugin
  268. }
  269. service_triggers()
  270. {
  271. procd_add_reload_trigger "collectd"
  272. }
  273. start_service() {
  274. process_config
  275. procd_open_instance
  276. procd_set_param command /usr/sbin/collectd
  277. procd_append_param command -C "$COLLECTD_CONF"
  278. procd_append_param command -f # don't daemonize
  279. procd_set_param nice "$NICEPRIO"
  280. procd_set_param stderr 1
  281. procd_set_param respawn
  282. procd_close_instance
  283. }
  284. reload_service() {
  285. restart "$@"
  286. }