1
0
mirror of https://github.com/limosek/zaf.git synced 2025-11-01 01:57:38 +01:00

Rework of functions

This commit is contained in:
Lukas Macura
2016-03-30 16:09:02 +02:00
parent bffda469c8
commit 5a811dedf6
6 changed files with 381 additions and 190 deletions

112
lib/ctrl.lib.sh Normal file
View File

@@ -0,0 +1,112 @@
# Control file related functions
# Check plugin url
# $1 plugin uri
# $2 local file to fetch
zaf_plugin_fetch_control() {
[ -z "$1" ] && return -1
local name=$(basename "$1")
zaf_fetch_url "$1/control.zaf" >"$2"
}
# Get block from stdin
# $1 option
# $2 name
zaf_ctrl_get_items() {
grep '^Item ' | cut -d ' ' -f 2 | cut -d ':' -f 1 | tr '\r\n' ' '
}
# Get item body from stdin
# $1 itemname
zaf_ctrl_get_item_block() {
grep -v '^#' | awk '/^Item '$1'/ { i=0;
while (i==0) {
getline;
if (/^\/Item/) exit;
print $0;
}}'
}
# Get global plugin block body from stdin
# $1 itemname
zaf_ctrl_get_global_block() {
grep -v '^#' | awk '{ i=0;
while (i==0) {
getline;
if (/^Item /) exit;
print $0;
}}'
}
# Get item multiline option
# $1 optionname
zaf_block_get_moption() {
awk '/^'$1'::$/ { i=0;
while (i==0) {
getline;
if (/^::$/) exit;
print $0;
}}'
}
# Get item singleline option
# $1 optionname
zaf_block_get_option() {
grep "^$1:" | cut -d ' ' -f 2- | tr -d '\r\n'
}
zaf_ctrl_check_deps() {
local deps
deps=$(zaf_ctrl_get_global_block <$1 | zaf_block_get_option "Depends-${ZAF_PKG}" )
zaf_check_deps $deps
deps=$(zaf_ctrl_get_global_block <$1 | zaf_block_get_option "Depends-bin" )
for cmd in $deps; do
if ! which $cmd >/dev/null; then
echo "Missing binary dependency $cmd. Please install it first."
return 1
fi
done
}
# Install binaries from control
# $1 control
# $2 plugindir
zaf_ctrl_install() {
local binaries
local pdir
local script
local cmd
pdir="$2"
binaries=$(zaf_ctrl_get_global_block <$1 | zaf_block_get_option "Install-bin" | zaf_far '{PLUGINDIR}' "$plugindir" )
for b in $binaries; do
zaf_fetch_url "$url/$b" >"$pdir/$b"
chmod +x "$pdir/$b"
done
script=$(zaf_ctrl_get_global_block <$1 | zaf_block_get_moption "Install-script" | zaf_far '{PLUGINDIR}' "$plugindir" )
[ -n "$script" ] && eval $script
cmd=$(zaf_ctrl_get_global_block <$1 | zaf_block_get_option "Install-cmd" | zaf_far '{PLUGINDIR}' "$plugindir" )
[ -n "$cmd" ] && $cmd
}
# Generates zabbix cfg from control file
# $1 control
# $2 pluginname
zaf_ctrl_generate_cfg() {
local items
local cmd
local ilock
items=$(zaf_ctrl_get_items <"$1")
for i in $items; do
block=$(zaf_ctrl_get_item_block <$1 $i)
ilock=$(echo $i | tr -d '[]*&;:')
cmd=$(zaf_block_get_option <$1 "Cmd")
[ -n "$cmd" ] && { echo "UserParameter=$2.${i},$cmd"; continue; }
cmd=$(zaf_block_get_option <$1 "Function")
[ -n "$cmd" ] && { echo "UserParameter=$2.${i},${ZAF_LIB_DIR}/preload.sh $cmd"; continue; }
cmd=$(zaf_block_get_moption <$1 "Script")
[ -n "$cmd" ] && { zaf_block_get_moption <$1 "Script" >${ZAF_PLUGIN_DIR}/$2/$ilock.sh; echo "UserParameter=$2.${i},${ZAF_PLUGIN_DIR}/$ilock.sh"; continue; }
done
}

74
lib/os.lib.sh Normal file
View File

@@ -0,0 +1,74 @@
# Os related functions
zaf_detect_system() {
if which dpkg >/dev/null; then
ZAF_PKG="dpkg"
ZAF_OS=$(lsb_release -is)
ZAF_OS_CODENAME=$(lsb_release -cs)
ZAF_CURL_INSECURE=0
ZAF_AGENT_PKG="zabbix-agent"
return
else if which rpm >/dev/null; then
ZAF_PKG="rpm"
ZAF_OS=$(lsb_release -is)
ZAF_OS_CODENAME=$(lsb_release -cs)
ZAF_CURL_INSECURE=0
ZAF_AGENT_PKG="zabbix-agent"
return
else if which opkg >/dev/null; then
ZAF_PKG="opkg"
. /etc/openwrt_release
ZAF_OS="$DISTRIB_ID"
ZAF_OS_CODENAME="$DISTRIB_CODENAME"
ZAF_AGENT_RESTART="/etc/init.d/zabbix_agentd restart"
ZAF_AGENT_CONFIGD="/var/run/zabbix_agentd.conf.d/"
ZAF_AGENT_CONFIG="/etc/zabbix_agentd.conf"
ZAF_AGENT_PKG="zabbix-agentd"
ZAF_CURL_INSECURE=1
return
else
ZAF_PKG="unknown"
ZAF_OS="unknown"
ZAF_OS_CODENAME="unknown"
ZAF_AGENT_PKG=""
fi
fi
fi
}
# Check if dpkg dependency is met
# $* - packages
zaf_check_deps_dpkg() {
dpkg-query -f '${Package}\n' -W $* >/dev/null
}
# Check if dpkg dependency is met
# $* - packages
zaf_check_deps_rpm() {
rpm --quiet -qi $*
}
# Check if dpkg dependency is met
# $* - packages
zaf_check_deps_opkg() {
local p
for p in $*; do
opkg info $p | grep -q 'Package:' || { echo "Missing package $p" >&2; return 1; }
done
}
# Check dependency based on system
zaf_check_deps() {
case $ZAF_PKG in
dpkg) zaf_check_deps_dpkg $*
;;
opkg) zaf_check_deps_opkg $*
;;
rpm) zaf_check_deps_rpm $*
;;
*) return
;;
esac
}

View File

@@ -1,14 +1,4 @@
############################################ Init part
# Get all config variables and initialise TMP
! [ -f /etc/zaf.conf ] && { echo "Config file /etc/zaf.conf does not exists! Exiting."; exit 2; }
. /etc/zaf.conf
. ${ZAF_LIB_DIR}/jshn.sh
ZAF_TMP_DIR="${ZAF_TMP_BASE}-${USER}-$$"
trap "rm -rif ${ZAF_TMP_DIR}" EXIT
! [ -d "${ZAF_TMP_DIR}" ] && mkdir "${ZAF_TMP_DIR}"
############################################ Common routines
zaf_msg() {
@@ -32,7 +22,7 @@ zaf_fetch_url() {
http|https|ftp|file)
[ "${ZAF_CURL_INSECURE}" = "1" ] && insecure="-k"
zaf_msg curl $insecure -f -s -L -o - "$1"
curl $insecure -f -s -L -o - "$1";
curl $insecure -f -s -L -o - "$1"
;;
esac
}
@@ -113,81 +103,13 @@ zaf_update_repo() {
[ -n "${ZAF_PLUGINS_REPO}" ] && cd ${ZAF_REPO_DIR} && git pull
}
# Check plugin url
# $1 plugin uri
# $2 local file to fetch
zaf_plugin_fetch_control() {
[ -z "$1" ] && return -1
local name=$(basename "$1")
zaf_fetch_url "$1/control" >"$2"
}
# Get option from control file
# $1 control file
# $2 option
zaf_ctrl_get_option() {
awk 'BEGIN { FS=": "; }; /^'$2': / { printf $2$3$4$5"\n"; }' <$1
}
# Get description from control file
# $1 control file
# $2 option
zaf_ctrl_get_description() {
awk \
"/^$2/"' { i=1;
while (1) {
getline; if (substr($0,0,1) != " ") exit;
printf $0"\n";
}
}' <$1
}
zaf_ctrl_binary_deps() {
local deps
deps=$(zaf_ctrl_get_option "$1" Binary-Depends)
for cmd in $deps; do
if ! which $cmd >/dev/null; then
echo "Missing binary dependency $cmd. Please install it first."
exit 5
fi
done
}
zaf_ctrl_install_bin() {
local binaries
local pdir
binaries=$(zaf_ctrl_get_option "$1" Install-bin)
pdir="${ZAF_PLUGINS_DIR}/${2}/"
for b in $binaries; do
zaf_fetch_url "$url/$b" >"$pdir/$b"
chmod +x "$pdir/$b"
done
}
# Generates zabbix cfg from control file
# $1 control
# $2 pluginname
zaf_ctrl_generate_cfg() {
local items
local cmd
local ilock
items=$(zaf_ctrl_get_option "$1" Item)
for i in $items; do
ilock=$(echo $i | tr -d '[]*&;:')
cmd=$(zaf_ctrl_get_option "$1" "Item-cmd-$i")
echo "UserParameter=$2.${i},$cmd"
done
}
# Install plugin.
# Parameter is url, directory or plugin name (will be searched in default plugin dir)
zaf_install_plugin() {
# Construct url from plugin name
# It can be http[s]://url
# /path (from file)
# name (to try from repo)
zaf_get_plugin_url() {
local url
local control
local plugin
local plugindir
if echo "$1" | grep -q '/'; then
url="$1" # plugin with path - installing from directory
else
@@ -201,51 +123,73 @@ zaf_install_plugin() {
fi
fi
fi
plugin="plug$$"
rm -rf ${ZAF_TMP_DIR}/${plugin}
control=${ZAF_TMP_DIR}/${plugin}/control
mkdir -p "${ZAF_TMP_DIR}/${plugin}"
echo $url
}
# $1 - control
# $2 - if nonempty, show informarions instead of setting env
zaf_plugin_info() {
local control="$1"
plugin=$(zaf_ctrl_get_global_block <"${control}" | zaf_block_get_option Plugin)
pdescription=$(zaf_ctrl_get_global_block <"${control}" | zaf_block_get_moption Description)
pmaintainer=$(zaf_ctrl_get_global_block <"${control}" | zaf_block_get_option Maintainer)
pversion=$(zaf_ctrl_get_global_block <"${control}" | zaf_block_get_option Version)
purl=$(zaf_ctrl_get_global_block <"${control}" | zaf_block_get_option Url)
phome=$(zaf_ctrl_get_global_block <"${control}" | zaf_block_get_option Home)
pitems=$(zaf_ctrl_get_items <"${control}")
[ -z "$2" ] && return
echo
echo -n "Plugin $plugin "; [ -n "$version" ] && echo -n "version ${pversion}"; echo ":"
echo "$pdescription"; echo
[ -n "$pmaintainer" ] && echo "Maintainer: $pmaintainer"
[ -n "$purl" ] && echo "Url: $purl"
[ -n "$phome" ] && echo "Home: $phome"
echo
echo "Items: $pitems"
echo
}
# Prepare plugin into tmp dir
# $1 is url, directory or plugin name (will be searched in default plugin dir).
# $2 is directory to prepare.
zaf_prepare_plugin() {
url=$(zaf_get_plugin_url "$1")
plugindir="$2"
control=${plugindir}/control.zaf
echo "Fetching control file from $url ..."
if zaf_plugin_fetch_control "$url" "${control}"; then
plugin=$(zaf_ctrl_get_option "${control}" Plugin)
if [ -n "$plugin" ]; then
echo Installing plugin $plugin from $url...
plugindir="${ZAF_PLUGINS_DIR}/${plugin}"
zaf_ctrl_binary_deps "${control}"
mkdir -p $plugindir
zaf_ctrl_install_bin "${control}" "${plugin}"
zaf_ctrl_generate_cfg "${control}" "${plugin}" | \
zaf_plugin_info "${control}"
zaf_ctrl_check_deps "${control}"
else
echo "Cannot fetch control file!"
return 1
fi
}
zaf_install_plugin() {
mkdir "${ZAF_TMP_DIR}/plugin"
if zaf_prepare_plugin "$1" "${ZAF_TMP_DIR}/plugin"; then
plugindir="${ZAF_PLUGINS_DIR}"/$plugin
echo $plugindir;exit;
if zaf_prepare_plugin "$1" $plugindir; then
zaf_ctrl_check_deps "${control}"
zaf_ctrl_install "${control}" "${plugin}"
zaf_ctrl_generate_cfg "${control}" "${plugin}"
exit;
#| \
zaf_far '{PLUGINDIR}' "$plugindir" | \
zaf_far '{ZAFLIB}' ". ${ZAF_LIB_DIR}/zaf.lib.sh; . " | \
zaf_far '{ZAFFUNC}' ". ${ZAF_LIB_DIR}/zaf.lib.sh; " | \
zaf_far '{ZAFLIBDIR}' "${ZAF_LIB_DIR}" | \
zaf_far '{ZAFLOCK}' "${ZAF_LIB_DIR}/zaflock '$plugin' " \
>${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf
zaf_restart_agent
cp $control "$plugindir"/
zaf_fetch_url $url/template.xml >"$plugindir"/template.xml
>$plugindir/zabbix.conf
else
echo "Bad control file $control ($url)!"
cat $control
exit 4
fi
else
echo "Cannot fetch control file!"
exit 4
fi
}
# Show installed plugins (human readable)
# $1 - plugin
zaf_show_installed_plugins() {
local cfile
local plugin
cd ${ZAF_PLUGINS_DIR}; ls --hide '.' -1 | while read plugin; do
cfile=${ZAF_PLUGINS_DIR}/$plugin/control
echo Plugin $plugin:
zaf_ctrl_get_description $cfile Plugin:
echo " Homepage:" $(zaf_ctrl_get_option $cfile Web)
echo " Maintainer:" $(zaf_ctrl_get_option $cfile Maintainer)
echo
done
}
# List installed plugins
@@ -256,41 +200,6 @@ zaf_list_plugins() {
cd ${ZAF_PLUGINS_DIR}; ls -1
}
zaf_show_plugin() {
local items
local plugindir
local cfile
local tst
if [ -z "$1" ]; then
echo "Missing plugin name";
exit 1
fi
[ -n "$2" ] && tst=1
plugindir="${ZAF_PLUGINS_DIR}/$1"
cfile="$plugindir/control"
if [ -d "$plugindir" ] ; then
echo "Plugin $1:"
zaf_ctrl_get_description "$cfile" "Plugin:"
echo " Homepage:" $(zaf_ctrl_get_option $cfile Web)
echo " Maintainer:" $(zaf_ctrl_get_option $cfile Maintainer)
items=$(zaf_list_plugin_items $1)
echo " Supported items:"
for i in $items; do
if [ -n "$tst" ]; then
echo -n " "; ${ZAF_AGENT_BIN} -t "$1.$i"
else
echo -n " $1.$i: "
fi
echo
zaf_ctrl_get_description "$cfile" "Item: $i";
echo
done
else
echo "Plugin $1 not installed"
fi
}
zaf_discovery_plugins() {
zaf_list_plugins | zaf_discovery '{#PLUGIN}'
}