diff options
author | erdgeist <> | 2013-09-21 18:19:04 +0000 |
---|---|---|
committer | erdgeist <> | 2013-09-21 18:19:04 +0000 |
commit | c07bdabbc3a7db48fe7ca63c10377368e1aca617 (patch) | |
tree | 7b9f0c2b67908caff0c8ecb6e8b360cb30aa1fb8 | |
parent | 61e032486cd35558989421bafb9945e53718feee (diff) |
plugin config file parser added
-rwxr-xr-x | minimunin | 59 |
1 files changed, 40 insertions, 19 deletions
@@ -1,17 +1,14 @@ | |||
1 | #!/bin/sh | 1 | #!/bin/sh |
2 | 2 | ||
3 | PLUGIN_DIR=/usr/local/etc/minimunin-plugins | 3 | PLUGIN_DIR=/usr/local/etc/minimunin-plugins |
4 | CONFIG_DIR=/usr/local/etc/minimunin-configs | ||
4 | BUILTIN="cpu load swap systat iostat uptime memory open_files" | 5 | BUILTIN="cpu load swap systat iostat uptime memory open_files" |
6 | SYSCTL=/sbin/sysctl | ||
7 | [ -f ${SYSCTL} ] || SYSCTL=/usr/sbin/sysctl | ||
5 | 8 | ||
6 | # list plugins | 9 | # list plugins, read configs |
7 | PLUGINS=`/usr/bin/find ${PLUGIN_DIR} -type f -perm +111 -exec basename {} \; 2> /dev/null | /usr/bin/xargs` | 10 | PLUGINS=`/usr/bin/find ${PLUGIN_DIR} -type f -perm +111 -exec basename {} \; 2> /dev/null | /usr/bin/xargs` |
8 | 11 | CONFIGS=`/usr/bin/find ${CONFIG_DIR} -type f -exec /usr/bin/grep -v -e ^\# -e ^$ {} \; -exec /bin/echo [] \; 2> /dev/null` | |
9 | get_plugin() { | ||
10 | plug=`/usr/bin/find ${PLUGIN_DIR} -type f -perm +111 -name $1 | /usr/bin/head -n 1` | ||
11 | [ -z "${plug}" ] && return 1 | ||
12 | printf %s ${plug} | ||
13 | return 0 | ||
14 | } | ||
15 | 12 | ||
16 | main() { | 13 | main() { |
17 | # print banner | 14 | # print banner |
@@ -49,11 +46,7 @@ main() { | |||
49 | print_fetch() { | 46 | print_fetch() { |
50 | 47 | ||
51 | # see if we're serving a plugin | 48 | # see if we're serving a plugin |
52 | plug=`get_plugin $1` | 49 | call_plugin $1 fetch && return 0 |
53 | if [ $? = 0 ]; then | ||
54 | eval ${plug} | ||
55 | return | ||
56 | fi | ||
57 | 50 | ||
58 | # if not, let our builtins answer | 51 | # if not, let our builtins answer |
59 | case $1 in | 52 | case $1 in |
@@ -105,11 +98,7 @@ esac | |||
105 | print_config() { | 98 | print_config() { |
106 | 99 | ||
107 | # see if we're configuring a plugin | 100 | # see if we're configuring a plugin |
108 | plug=`get_plugin $1` | 101 | call_plugin $1 config && return 0 |
109 | if [ $? = 0 ]; then | ||
110 | eval ${plug} config | ||
111 | return | ||
112 | fi | ||
113 | 102 | ||
114 | # if not, execute built in commands | 103 | # if not, execute built in commands |
115 | case $1 in | 104 | case $1 in |
@@ -309,7 +298,39 @@ esac | |||
309 | } | 298 | } |
310 | 299 | ||
311 | get_sys() { | 300 | get_sys() { |
312 | LANG=C LC_ALL=C /sbin/sysctl -n $@ | 301 | LANG=C LC_ALL=C ${SYSCTL} -n $@ |
302 | } | ||
303 | |||
304 | env_for_plugin_from_config() { | ||
305 | unset in_sect | ||
306 | while read line; do | ||
307 | case ${line## } in | ||
308 | \[*\]) v="${line#*[}"; v="${v%%]*}" | ||
309 | case $1 in ${v}) in_sect=true;; *) unset in_sect ;; esac | ||
310 | ;; | ||
311 | env.*) [ "${in_sect}" ] || continue | ||
312 | _pref=${line#*env.}; | ||
313 | printf "%s " ${_pref%% *}=\'${_pref#* }\' | ||
314 | ;; | ||
315 | esac | ||
316 | done | ||
317 | } | ||
318 | |||
319 | call_plugin() { | ||
320 | _param=$2 | ||
321 | _plug=`/usr/bin/find ${PLUGIN_DIR} -type f -perm +111 -name $1 2>/dev/null | /usr/bin/head -n 1` | ||
322 | [ -z "${_plug}" ] && return 1 | ||
323 | |||
324 | printf %s "${CONFIGS}" | while read line; do | ||
325 | case line in | ||
326 | \[*\]) start_section;; | ||
327 | *) in_section;; | ||
328 | esac | ||
329 | done | ||
330 | |||
331 | _env=$( printf "%s\n" "${CONFIGS}" | env_for_plugin_from_config ${_plug} ) | ||
332 | eval env ${_env} ${_plug} ${_param} | ||
333 | return $? | ||
313 | } | 334 | } |
314 | 335 | ||
315 | # After function definitions, main() can use them | 336 | # After function definitions, main() can use them |