1
0
mirror of https://github.com/limosek/zaf.git synced 2024-11-21 18:09:06 +01:00
limosek-zaf/zaf

339 lines
8.4 KiB
Plaintext
Raw Normal View History

2016-03-23 14:37:51 +01:00
#!/bin/sh
if [ -z "$secondstage" ]; then
ZAF_CFG_FILE="/etc/zaf.conf"
if [ -f $ZAF_CFG_FILE ]; then
. $ZAF_CFG_FILE
else
echo "Missing config file $ZAF_CFG_FILE! Exiting."
exit 2
fi
2016-03-23 14:37:51 +01:00
# Read options as config for ZAF
for pair in "$@"; do
if echo $pair | grep -qE '^ZAF\_(.*)='; then
option=$(echo $pair|cut -d '=' -f 1)
value=$(echo $pair|cut -d '=' -f 2-)
eval "${option}='$value'"
export secondstage=1
else
params="$params $pair"
fi
done
export $(set |grep ^ZAF_ | cut -d '=' -f 1)
# If some variables in cmd were stripped, rerun only with right arguments and exported variables
[ -n "$secondstage" ] && exec $0 $params
fi
2016-04-07 13:14:53 +02:00
2016-03-30 16:09:02 +02:00
[ -z "$ZAF_TMP_BASE" ] && ZAF_TMP_BASE=/tmp/zaf
2016-04-01 12:20:23 +02:00
ZAF_TMP_DIR="${ZAF_TMP_BASE}-${USER}"
[ -z "$ZAF_CACHE_DIR" ] && ZAF_CACHE_DIR=${ZAF_TMP_BASE}c
2016-04-01 12:20:23 +02:00
[ -z "$ZAF_DEBUG" ] && ZAF_DEBUG=1
2016-04-12 10:36:17 +02:00
export ZAF_DEBUG
# If debug is on, do not remove tmp dir
if [ "$ZAF_DEBUG" -le 3 ]; then
trap "rm -rif ${ZAF_TMP_DIR}" EXIT
fi
! [ -d "${ZAF_TMP_DIR}" ] && mkdir "${ZAF_TMP_DIR}"
if ! [ -d "${ZAF_CACHE_DIR}" ]; then
mkdir "${ZAF_CACHE_DIR}"
fi
2016-03-30 16:09:02 +02:00
2016-04-01 15:51:45 +02:00
if [ -f ./lib/zaf.lib.sh ]; then
. ./lib/zaf.lib.sh
. ./lib/os.lib.sh
. ./lib/ctrl.lib.sh
. ./lib/cache.lib.sh
. ./lib/zbxapi.lib.sh
2016-04-01 15:51:45 +02:00
[ -f ./lib/zaf.${ZAF_OS}.sh ] && . ./lib/zaf.${ZAF_OS}.sh
else
2016-03-23 14:37:51 +01:00
. ${ZAF_LIB_DIR}/zaf.lib.sh
2016-03-30 16:09:02 +02:00
. ${ZAF_LIB_DIR}/os.lib.sh
. ${ZAF_LIB_DIR}/ctrl.lib.sh
. ${ZAF_LIB_DIR}/cache.lib.sh
. ${ZAF_LIB_DIR}/zbxapi.lib.sh
[ -f ${ZAF_LIB_DIR}/zaf.${ZAF_OS}.sh ] && . ${ZAF_LIB_DIR}/zaf.${ZAF_OS}.sh
2016-04-01 15:51:45 +02:00
fi
2016-03-23 14:37:51 +01:00
if zaf_is_root; then
chgrp zabbix "${ZAF_CACHE_DIR}"
chmod g+w "${ZAF_CACHE_DIR}"
fi
2016-03-23 14:37:51 +01:00
case $1 in
2016-03-23 17:25:18 +01:00
check-agent-config)
zaf_check_agent_config
;;
cache-clean)
zaf_cache_clean
;;
userparms)
for plugin in $(zaf_list_plugins); do
plugindir=$ZAF_PLUGINS_DIR/$plugin
control=$plugindir/control.zaf
zaf_ctrl_generate_cfg "${control}" "${plugin}" \
| zaf_far '{PLUGINDIR}' "${plugindir}"
done
;;
agent-config)
for plugin in $(zaf_list_plugins); do
plugindir=$ZAF_PLUGINS_DIR/$plugin
control=$plugindir/control.zaf
zaf_ctrl_generate_cfg "${control}" "${plugin}" \
| zaf_far '{PLUGINDIR}' "${plugindir}" >${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf
done
;;
2016-03-23 14:37:51 +01:00
update)
2016-04-12 10:36:17 +02:00
zaf_wrn "Updating repository ${ZAF_REPO_GITURL}..."
2016-03-23 14:37:51 +01:00
zaf_update_repo
;;
2016-04-12 10:36:17 +02:00
upgrade)
ZAF_DEBUG=$ZAF_DEBUG $0 reinstall $(zaf_list_plugins)
2016-04-12 10:36:17 +02:00
;;
2016-03-24 15:46:42 +01:00
show)
2016-04-12 10:36:17 +02:00
shift;
if [ -z "$1" ]; then
2016-04-01 12:20:23 +02:00
zaf_list_plugins | while read plugin; do
zaf_plugin_info $ZAF_PLUGINS_DIR/$plugin/control.zaf
done
2016-04-12 10:36:17 +02:00
else
if zaf_list_plugins | grep -q "^$1"; then
[ -f $ZAF_PLUGINS_DIR/$1/control.zaf ] && zaf_plugin_info $ZAF_PLUGINS_DIR/$1/control.zaf || zaf_err "Plugin $1 not installed."
2016-04-01 12:20:23 +02:00
else
2016-04-12 10:36:17 +02:00
if echo $1 |grep -q ^http; then
zaf_prepare_plugin "$1" "$ZAF_TMP_DIR/plugin"
2016-04-01 15:51:45 +02:00
zaf_plugin_info "$ZAF_TMP_DIR/plugin/control.zaf"
else
2016-04-12 10:36:17 +02:00
zaf_err "Plugin $1 not installed."
2016-04-01 15:51:45 +02:00
fi
2016-04-01 12:20:23 +02:00
fi
2016-03-24 15:46:42 +01:00
fi
;;
2016-04-01 15:51:45 +02:00
plugins)
2016-03-24 15:46:42 +01:00
zaf_list_plugins
2016-03-23 14:37:51 +01:00
;;
2016-04-01 15:51:45 +02:00
items)
2016-04-12 10:36:17 +02:00
shift
if [ -z "$1" ]; then
2016-03-24 15:46:42 +01:00
zaf_list_items
else
2016-04-12 10:36:17 +02:00
zaf_list_plugin_items "$1"
2016-03-24 15:46:42 +01:00
fi
;;
2016-04-01 15:51:45 +02:00
test)
[ "$USER" != "zabbix" ] && zaf_wrn "You are not zabbix user. Test will be run with your privileges and sudo access!"
2016-04-12 10:36:17 +02:00
shift
if echo $1|grep -q '\.'; then
zaf_test_item "$1"
2016-04-04 14:49:38 +02:00
exit
fi
2016-04-12 10:36:17 +02:00
if [ -n "$1" ]; then
2016-04-01 15:51:45 +02:00
plugins="$@"
else
plugins="$(zaf_list_plugins)"
fi
for p in $plugins; do
2016-04-12 10:36:17 +02:00
! zaf_is_plugin $p && zaf_err "Unknown plugin $p"
for i in $(zaf_list_plugin_items $p test); do
2016-04-01 15:51:45 +02:00
echo $i: $(zaf_test_item $i)
echo
2016-04-01 15:51:45 +02:00
done
done
2016-03-23 17:25:18 +01:00
;;
2016-04-12 10:36:17 +02:00
get)
shift
if echo $1|grep -q '\.'; then
zaf_get_item "$1"
exit
fi
if [ -n "$1" ]; then
plugins="$@"
else
plugins="$(zaf_list_plugins)"
fi
for p in $plugins; do
for i in $(zaf_list_plugin_items $p test); do
2016-04-12 10:36:17 +02:00
echo $i: $(zaf_get_item $i)
echo
2016-04-12 10:36:17 +02:00
done
done
;;
precache)
shift
for i in $*; do
if zaf_is_plugin $i; then
for j in $(zaf_list_plugin_items $i precache); do
item=$(echo $j | cut -d '[' -f 1)
params=$(echo $j | cut -d '[' -f 2 | cut -d ']' -f 1 | tr ',' ' ')
zaf_precache_item $params >/dev/null
done
else
item=$(echo $i | cut -d '[' -f 1)
params=$(echo $i | cut -d '[' -f 2 | cut -d ']' -f 1 | tr ',' ' ')
zaf_precache_item $params >/dev/null
fi
done
;;
2016-03-23 14:37:51 +01:00
install)
2016-04-12 10:36:17 +02:00
shift
2016-04-04 14:00:44 +02:00
[ -z "$1" ] && echo "$0 install plugin [plugin]..."
2016-03-30 16:09:02 +02:00
for p in $@; do
if zaf_is_plugin "$(basename $p)"; then
zaf_wrn "Plugin $(basename $p) already installed. Skipping installation."
2016-04-01 12:20:23 +02:00
continue
fi
2016-03-30 16:09:02 +02:00
zaf_install_plugin "$p"
2016-04-01 12:20:23 +02:00
installed=1
2016-03-30 16:09:02 +02:00
done
2016-04-01 12:20:23 +02:00
[ -n "$installed" ] && zaf_is_root && zaf_restart_agent
;;
reinstall)
2016-04-12 10:36:17 +02:00
shift
2016-04-04 14:00:44 +02:00
[ -z "$1" ] && echo "$0 reinstall plugin [plugin]..."
2016-04-01 12:20:23 +02:00
for p in $@; do
if zaf_is_plugin "$p"; then
2016-04-01 15:51:45 +02:00
zaf_remove_plugin "$(basename $p)"
2016-04-01 12:20:23 +02:00
reinstalled=1
fi
zaf_install_plugin "$p"
reinstalled=1
done
[ -n "$reinstalled" ] && zaf_is_root && zaf_restart_agent
2016-03-23 14:37:51 +01:00
;;
2016-03-23 17:25:18 +01:00
remove)
2016-04-12 10:36:17 +02:00
shift
2016-04-04 14:00:44 +02:00
[ -z "$1" ] && echo "$0 remove plugin [plugin]..."
2016-04-01 12:20:23 +02:00
for p in $@; do
if zaf_is_plugin "$p"; then
zaf_remove_plugin "$p"
removed=1
fi
done
[ -n "$removed" ] && zaf_is_root && zaf_restart_agent
2016-03-23 17:25:18 +01:00
;;
2016-03-24 15:46:42 +01:00
self-upgrade)
2016-04-12 10:36:17 +02:00
shift
[ -z "$1" ] && auto=auto
2016-04-07 13:14:53 +02:00
zaf_os_specific zaf_check_deps zaf && zaf_err "Zaf is installed as system package. Cannot self-upgrade."
2016-03-30 16:09:02 +02:00
rm -rf /tmp/zaf-installer && mkdir /tmp/zaf-installer
2016-04-04 15:10:38 +02:00
if ! which curl >/dev/null;
then
zaf_err "Curl not found. Cannot continue. Please install it."
2016-03-30 16:09:02 +02:00
fi
2016-04-14 10:04:45 +02:00
zaf_fetch_url $ZAF_RAW_URL/$ZAF_GITBRANCH/install.sh | exec sh -s $auto "$@"
2016-04-04 15:10:38 +02:00
exit
2016-03-24 15:46:42 +01:00
;;
2016-03-24 20:37:03 +01:00
self-remove)
2016-04-12 10:36:17 +02:00
shift
2016-04-07 13:14:53 +02:00
zaf_os_specific zaf_check_deps zaf && zaf_err "Zaf is installed as system package. Cannot self-remove."
2016-04-01 15:51:45 +02:00
. /etc/zaf.conf
2016-04-12 10:36:17 +02:00
if [ "$1" = "force" ]; then
2016-04-01 15:51:45 +02:00
rm -rf /etc/zaf.conf ${ZAF_PLUGINS_DIR} ${ZAF_REPO_DIR} ${ZAF_LIB_DIR} \
${ZAF_BIN_DIR}/zaf ${ZAF_AGENT_CONFIGD}/zaf_*
2016-03-24 20:37:03 +01:00
else
echo "This will remove zaf from this computer and erase all configuration."
2016-04-01 15:51:45 +02:00
echo "This command will be executed:"
echo "rm -rf /etc/zaf.conf ${ZAF_PLUGINS_DIR} ${ZAF_REPO_DIR} ${ZAF_LIB_DIR} \
${ZAF_BIN_DIR}/zaf ${ZAF_AGENT_CONFIGD}/zaf_*"
echo
2016-03-24 20:37:03 +01:00
echo "To continue, please do $0 self-remove force"
fi
;;
api)
zaf_zbxapi_login
case $2 in
hostid)
zaf_zbxapi_gethostid "$3"
;;
host)
zaf_zbxapi_gethost "$3"
;;
hostgroupid)
zaf_zbxapi_gethostgroupid "$3"
;;
hosts)
gid=$(zaf_zbxapi_gethostgroupid "$3")
zaf_zbxapi_gethostsingroup $gid
;;
backup-group)
shift; shift
gid=$(zaf_zbxapi_gethostgroupid "$1")
shift
hosts=$(zaf_zbxapi_gethostsingroup $gid)
dir="."
[ -n "$1" ] && dir="$1"
zaf_wrn "Will backup this hosts: $hosts"
zaf_wrn "Output dir: $dir"
for h in $hosts; do
zaf_bglimit 5
(hn=$(zaf_zbxapi_gethost $h)
zaf_wrn "Exporting host $hn($h)..."
zaf_zbxapi_backuphost $h >$dir/$hn.xml )
done
wait
;;
backup-host)
shift; shift
hostid=$(zaf_zbxapi_gethostid "$1")
zaf_wrn "Exporting host $3($hostid)..."
zaf_zbxapi_backuphost $hostid
;;
*)
echo "$0 api command [parameters]"
echo "hostid 'host' Get hostid from hostname"
echo "host 'hostid' Get hostname from hostid"
echo "hostgroupid 'hostgroup' Get hostgroup id from hostgroup"
echo "hosts 'hostgroup' Get hosts in group"
echo "backup-group 'hostgroup' [dir] Backup all hosts in group (get their config from zabbix and save to dir/hostname.xml)"
echo "backup-host 'host' Backup host (get config from zabbix to stdout)"
echo
exit
;;
esac
;;
2016-03-24 15:46:42 +01:00
2016-03-23 14:37:51 +01:00
*)
2016-04-04 14:00:44 +02:00
echo "$0 Version ${ZAF_VERSION}. Please use some of this commands:"
2016-04-14 10:44:50 +02:00
echo "$0 Cmd [ZAF_OPTION=value] [ZAF_CTRL_Option=value] [ZAF_CTRLI_Item_Option=value] ..."
2016-04-12 10:36:17 +02:00
echo "Commands:"
echo "$0 update To update repo (not plugins, similar to apt-get update)"
echo "$0 upgrade To upgrade installed plugins from repo"
2016-04-01 15:51:45 +02:00
echo "$0 plugins To list installed plugins"
2016-03-24 15:46:42 +01:00
echo "$0 show [plugin] To show installed plugins or plugin info"
2016-04-01 15:51:45 +02:00
echo "$0 items [plugin] To list all suported items [for plugin]"
2016-04-12 10:36:17 +02:00
echo "$0 test [plugin[.item]] To test [all] suported items by zabbix_agentd [for plugin]"
echo "$0 get [plugin[.item]] To test [all] suported items by zabbix_get [for plugin]"
echo "$0 precache [plugin[.item]] To precache [all] suported items"
2016-03-24 15:46:42 +01:00
echo "$0 install plugin To install plugin"
echo "$0 remove plugin To remove plugin"
echo "$0 api To zabbix API functions. See $0 api for more info."
echo "$0 userparms See userparms generated from zaf on stdout"
echo "$0 agent-config Reconfigure zabbix userparms in $ZAF_AGENT_CONFIGD"
2016-03-24 15:46:42 +01:00
echo "$0 self-upgrade To self-upgrade zaf"
2016-03-30 16:09:02 +02:00
echo "$0 self-remove To self-remove zaf and its config"
echo "$0 cache-clean To remove all entries from cache"
2016-04-12 10:36:17 +02:00
echo
2016-03-23 14:37:51 +01:00
;;
2016-03-23 17:25:18 +01:00
2016-03-23 14:37:51 +01:00
esac