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.

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