1
0
mirror of https://github.com/limosek/zaf.git synced 2024-09-20 21:01:38 +02:00
limosek-zaf/lib/zaf.lib.sh

248 lines
5.7 KiB
Bash
Raw Normal View History

2016-03-23 17:25:18 +01:00
2016-03-24 16:25:58 +01:00
############################################ Common routines
2016-03-24 20:37:03 +01:00
zaf_msg() {
[ "$ZAF_DEBUG" = "1" ] && echo $@
}
2016-03-24 09:38:34 +01:00
# Fetch url to stdout
# $1 url
# It supports real file, file:// and other schemes known by curl
2016-03-23 17:25:18 +01:00
zaf_fetch_url() {
local scheme
local uri
local insecure
2016-03-23 17:25:18 +01:00
scheme=$(echo $1|cut -d ':' -f 1)
uri=$(echo $1|cut -d '/' -f 3-)
if [ "$1" = "$scheme" ]; then
cat "$1"
fi
case $scheme in
http|https|ftp|file)
[ "${ZAF_CURL_INSECURE}" = "1" ] && insecure="-k"
2016-03-24 20:37:03 +01:00
zaf_msg curl $insecure -f -s -L -o - "$1"
2016-03-30 16:09:02 +02:00
curl $insecure -f -s -L -o - "$1"
2016-03-23 17:25:18 +01:00
;;
esac
}
2016-03-24 09:38:34 +01:00
# Find and replace string
2016-03-23 17:25:18 +01:00
zaf_far(){
local f
local t
local sedcmd="sed"
i=1
while [ "$i" -lt "$#" ];
do
eval f=\$${i}
i=$(expr $i + 1)
eval t=\$${i}
i=$(expr $i + 1)
sedcmd="$sedcmd -e 's~$f~$t~g'"
done
eval $sedcmd
}
2016-03-24 16:25:58 +01:00
# Initialises discovery function
zaf_discovery_init(){
json_init
json_add_array data
}
# Add row(s) to discovery data
zaf_discovery_add_row(){
json_add_object "obj"
while [ -n "$1" ]; do
json_add_string "$1" "$2"
shift
shift
done
json_close_object
}
# Dumps json object
zaf_discovery_dump(){
json_close_array
json_dump
}
# Read standard input as discovery data. Columns are divided by space.
# Arguments are name of variables to discovery.
# Dumps json to stdout
zaf_discovery(){
local a b c d e f g h i j;
zaf_discovery_init
while read a b c d e f g h i j; do
zaf_discovery_add_row "$1" "${1:+${a}}" "$2" "${2:+${b}}" "$3" "${3:+${c}}" "$4" "${4:+${d}}" "$5" "${5:+${e}}" "$6" "${6:+${f}}" "$7" "${7:+${g}}" "$8" "${8:+${h}}" "$9" "${9:+${i}}"
done
zaf_discovery_dump
}
############################################ Zaf internal routines
2016-03-24 17:26:34 +01:00
zaf_version() {
echo master
}
2016-03-24 09:38:34 +01:00
# Restart zabbix agent
2016-03-23 17:25:18 +01:00
zaf_restart_agent() {
${ZAF_AGENT_RESTART}
}
2016-03-24 09:38:34 +01:00
# Check if zaf.version item is populated
2016-03-23 17:25:18 +01:00
zaf_check_agent_config() {
zaf_restart_agent
zabbix_agentd -t zaf.version
}
2016-03-23 14:37:51 +01:00
# Update repo
zaf_update_repo() {
2016-03-24 15:46:42 +01:00
[ "$ZAF_GIT" != 1 ] && { echo "Git is not installed."; return 1; }
! [ -d ${ZAF_REPO_DIR} ] && git clone "${ZAF_PLUGINS_REPO}" "${ZAF_REPO_DIR}"
2016-03-23 17:25:18 +01:00
[ -n "${ZAF_PLUGINS_REPO}" ] && cd ${ZAF_REPO_DIR} && git pull
2016-03-23 14:37:51 +01:00
}
2016-03-24 15:46:42 +01:00
2016-03-30 16:09:02 +02:00
# Construct url from plugin name
# It can be http[s]://url
# /path (from file)
# name (to try from repo)
zaf_get_plugin_url() {
2016-03-23 17:25:18 +01:00
local url
if echo "$1" | grep -q '/'; then
url="$1" # plugin with path - installing from directory
2016-03-23 17:25:18 +01:00
else
if echo "$1" | grep -q ^http; then
url="$1" # plugin with http[s] url
else
if [ -d "${ZAF_REPO_DIR}/$1" ]; then
url="${ZAF_REPO_DIR}/$1"
else
url="${ZAF_PLUGINS_REPO}/$1";
fi
fi
2016-03-23 17:25:18 +01:00
fi
2016-03-30 16:09:02 +02:00
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 ..."
2016-03-23 17:25:18 +01:00
if zaf_plugin_fetch_control "$url" "${control}"; then
2016-03-30 16:09:02 +02:00
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;
#| \
2016-03-24 15:46:42 +01:00
zaf_far '{PLUGINDIR}' "$plugindir" | \
2016-03-30 16:09:02 +02:00
zaf_far '{ZAFLIBDIR}' "${ZAF_LIB_DIR}" | \
2016-03-24 15:46:42 +01:00
zaf_far '{ZAFLOCK}' "${ZAF_LIB_DIR}/zaflock '$plugin' " \
2016-03-30 16:09:02 +02:00
>$plugindir/zabbix.conf
2016-03-24 15:46:42 +01:00
else
2016-03-24 20:37:03 +01:00
echo "Bad control file $control ($url)!"
2016-03-24 15:46:42 +01:00
cat $control
exit 4
fi
2016-03-23 17:25:18 +01:00
fi
2016-03-24 15:46:42 +01:00
}
# List installed plugins
# $1 - plugin
zaf_list_plugins() {
local cfile
local plugin
2016-03-24 17:16:46 +01:00
cd ${ZAF_PLUGINS_DIR}; ls -1
2016-03-24 15:46:42 +01:00
}
2016-03-24 17:26:34 +01:00
zaf_discovery_plugins() {
zaf_list_plugins | zaf_discovery '{#PLUGIN}'
}
zaf_plugin_version() {
if [ -z "$1" ]; then
echo "Missing plugin name";
exit 1
fi
plugindir="${ZAF_PLUGINS_DIR}/$1"
cfile="$plugindir/control"
if [ -d "$plugindir" ] ; then
zaf_ctrl_get_option "$cfile" Version
else
echo "Plugin $1 not installed"
fi
}
2016-03-24 15:46:42 +01:00
zaf_list_plugin_items() {
if [ -z "$1" ]; then
echo "Missing plugin name";
exit 1
fi
plugindir="${ZAF_PLUGINS_DIR}/$1"
cfile="$plugindir/control"
if [ -d "$plugindir" ] ; then
zaf_ctrl_get_option "$cfile" Item
else
echo "Plugin $1 not installed"
fi
}
zaf_list_items() {
for p in $(zaf_list_plugins); do
zaf_list_plugin_items $p
done
}
2016-03-23 17:25:18 +01:00
zaf_remove_plugin() {
2016-03-24 15:46:42 +01:00
! [ -d ${ZAF_PLUGINS_DIR}/$1 ] && { echo "Plugin $1 not installed!"; exit 2; }
2016-03-23 17:25:18 +01:00
rm -rf ${ZAF_PLUGINS_DIR}/$1
rm -f ${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf
2016-03-23 14:37:51 +01:00
}
2016-03-23 17:25:18 +01:00