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.

361 lines
8.1 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 [ "$value" = "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 -e "${config}" >> "$COLLECTD_CONF"
  194. printf "%s</Plugin>\n" "${CONFIG_STRING}" >> "$COLLECTD_CONF"
  195. }
  196. printf "\n" >> "$COLLECTD_CONF"
  197. }
  198. process_plugins() {
  199. local cfg="$1"
  200. local enable keys key option value
  201. config_get enable "$cfg" enable 0
  202. [ "$enable" = "1" ] || return 0
  203. [ -f "/usr/lib/collectd/$cfg.so" ] || {
  204. $LOG notice "Plugin collectd-mod-$cfg not installed"
  205. return 0
  206. }
  207. [ -f "/usr/share/collectd/plugin/$cfg.json" ] || {
  208. $LOG notice "Configuration definition file for $cfg not found"
  209. return 0
  210. }
  211. printf "LoadPlugin %s\n" "$cfg" >> "$COLLECTD_CONF"
  212. case "$cfg" in
  213. exec)
  214. CONFIG_STRING=""
  215. process_exec
  216. ;;
  217. curl)
  218. CONFIG_STRING=""
  219. process_curl
  220. ;;
  221. network)
  222. CONFIG_STRING=""
  223. process_network "$cfg"
  224. ;;
  225. iptables)
  226. CONFIG_STRING=""
  227. process_iptables
  228. ;;
  229. *)
  230. CONFIG_STRING=""
  231. process_generic "$cfg" "\\t" "/usr/share/collectd/plugin/$cfg.json"
  232. ;;
  233. esac
  234. }
  235. process_config() {
  236. local alt_config_file BaseDir Include PIDFile PluginDir TypesDB
  237. local Interval ReadThreads Hostname
  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 Hostname globals Hostname "$(uname -n)"
  265. printf "Hostname \"%s\"\n" "$Hostname" >> "$COLLECTD_CONF"
  266. config_get Include globals Include "/tmp/collectd.d"
  267. printf "Include \"%s\"\n" "$Include" >> "$COLLECTD_CONF"
  268. mkdir -p "$Include"
  269. printf "\n" >> "$COLLECTD_CONF"
  270. # PLUGIN CONFIG
  271. config_foreach process_plugins plugin
  272. }
  273. service_triggers()
  274. {
  275. procd_add_reload_trigger "collectd"
  276. }
  277. start_service() {
  278. process_config
  279. procd_open_instance
  280. procd_set_param command /usr/sbin/collectd
  281. procd_append_param command -C "$COLLECTD_CONF"
  282. procd_append_param command -f # don't daemonize
  283. procd_set_param nice "$NICEPRIO"
  284. procd_set_param stderr 1
  285. procd_set_param respawn
  286. procd_close_instance
  287. }
  288. reload_service() {
  289. restart "$@"
  290. }