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

15 Commits
1.0 ... 1.1

Author SHA1 Message Date
Lukas Macura
45a072024c Repaired ctrl functions (from trunk) 2016-04-15 11:18:46 +02:00
Lukas Macura
c830d0e6a0 Repaired bug in install, version info 2016-04-15 08:51:50 +02:00
Lukas Macura
ed5e0501b8 Released version 1.1 2016-04-14 10:39:33 +02:00
Lukas Macura
df7c5f56dd Repaired self-upgrade 2016-04-14 10:04:45 +02:00
Lukas Macura
781f1f0bc0 Reworked parameters parsing
Repaired install libraries loading
Repaired installation error handling
2016-04-14 09:57:59 +02:00
Lukas Macura
2a0d6848a7 Repaired caching
Added testparameters and precacheparameters
2016-04-13 16:04:00 +02:00
Lukas Macura
ce4df86f46 Repaired docs, added cron 2016-04-13 12:42:12 +02:00
Lukas Macura
9ed9e990ed Missed quotes 2016-04-12 19:40:17 +02:00
Lukas Macura
a99c9b8280 Added cron support
Added precache support
2016-04-12 19:31:06 +02:00
Lukas Macura
cf90b8bc2c Added support for cached items.
Sudo setup is supported
2016-04-12 18:38:19 +02:00
Lukas Macura
6bf7c16b7d One more version repair 2016-04-12 15:42:09 +02:00
Lukas Macura
86b921dad2 Added default version strings 2016-04-12 15:37:28 +02:00
Lukas Macura
3cbd3a3702 Working on 1.1 version
Added cache support
Added Zabbix API calls
2016-04-12 15:28:29 +02:00
Lukas Macura
c2864f8d4c Added zabbix API functions 2016-04-12 10:36:39 +02:00
Lukas Macura
531b3c31f6 Switched working flow to trunk 2016-04-12 10:36:17 +02:00
12 changed files with 966 additions and 192 deletions

View File

@@ -4,14 +4,32 @@ CONTROLFILES=$(foreach p,$(PLUGINS),$(p)/control.zaf)
ZAF_EXPORT_OPTS=$(foreach o,$(ZAF_OPTIONS),$(shell echo $(o)|cut -d '=' -f 1))
DEBIAN_DIR=tmp/deb
DEBIAN_CTRL=$(DEBIAN_DIR)/DEBIAN
DEBIAN_PKG=out/zaf.deb
DEBIAN_PKG=$(shell . lib/zaf.lib.sh; echo out/zaf-$$ZAF_VERSION.deb)
ifeq ($(ZAF_DEBUG),)
ZAF_DEBUG=0
endif
ifeq ($(ZAF_OPTIONS),)
ZAF_OPTIONS = ZAF_GIT=0
endif
ifeq ($(IPLUGINS),)
IPLUGINS = zaf
endif
all: help
help:
@echo make '{deb|ipk|rpm}' '[PLUGINS="/dir/plg1 /dir/plg2] [ZAF_OPTIONS="ZAF_cfg=val ..."] [AGENT_OPTIONS="Z_Server=host ..."]'
@echo make '{deb|ipk|rpm}' '[PLUGINS="/dir/plg1 [/dir2]...] [IPLUGINS="plg1 [plg2]..."] [ZAF_OPTIONS="ZAF_cfg=val ..."] [AGENT_OPTIONS="Z_Server=host ..."]'
@echo PLUGINS are embedded into package. Has to be local directories accessible during build.
@echo IPLUGINS will be downloaded and installed after package is installed. Can be name or url accessible after package installation.
@echo
deb: deb-clean deb-init deb-deps deb-control deb-scripts deb-cp deb-package
deb: $(DEBIAN_PKG)
$(DEBIAN_PKG): deb-clean deb-init deb-deps deb-control deb-scripts deb-cp deb-package
clean:
@rm -rf tmp/* out/*
deb-clean:
@rm -rf $(DEBIAN_DIR) $(DEBIAN_PKG)
@@ -29,26 +47,22 @@ deb-control:
for p in $(PLUGINS); do \
DEPENDS="$$DEPENDS,$$(zaf_ctrl_get_global_option $$p/control.zaf Depends-dpkg | tr ' ' ',')"; \
done; \
zaf_far '{ZAF_VERSION}' "0.1" <files/control.template | zaf_far '{ZAF_DEPENDS}' "$$DEPENDS" >$(DEBIAN_CTRL)/control
[ "$$ZAF_GITBRANCH" = "master" ] && master=master; \
zaf_far '{ZAF_VERSION}' "$${ZAF_VERSION}$$master" <files/control.template | zaf_far '{ZAF_DEPENDS}' "$$DEPENDS" >$(DEBIAN_CTRL)/control
deb-scripts:
ifneq ($(PLUGINS),)
@. lib/zaf.lib.sh; \
. lib/ctrl.lib.sh; \
for p in $(PLUGINS); do \
plugins="$$plugins "$$(zaf_ctrl_get_global_option $$p/control.zaf Plugin) ; \
done; \
cat files/postinst.template | zaf_far '{PLUGINS}' "$$plugins" | zaf_far '{ZAF_LIB_DIR}' "/usr/lib/zaf" >$(DEBIAN_CTRL)/postinst
cat files/postinst.template | zaf_far '{PLUGINS}' "$(PLUGINS)" | zaf_far "{IPLUGINS}" "$(IPLUGINS)" | zaf_far '{ZAF_LIB_DIR}' "/usr/lib/zaf" >$(DEBIAN_CTRL)/postinst
@chmod +x $(DEBIAN_CTRL)/postinst
@cp files/preinst.template $(DEBIAN_CTRL)/preinst
@chmod +x $(DEBIAN_CTRL)/preinst
@cp files/prerm.template $(DEBIAN_CTRL)/prerm
@chmod +x $(DEBIAN_CTRL)/prerm
endif
deb-cp:
@mkdir -p $(DEBIAN_DIR)
@set -e; INSTALL_PREFIX=$(DEBIAN_DIR) ZAF_DEBUG=0 ./install.sh auto $(ZAF_OPTIONS) $(AGENT_OPTIONS)
@set -e; INSTALL_PREFIX=$(DEBIAN_DIR) ZAF_DEBUG=$(ZAF_DEBUG) ./install.sh auto $(ZAF_OPTIONS) $(AGENT_OPTIONS)
@. lib/zaf.lib.sh; \
. lib/ctrl.lib.sh; \
for p in $(PLUGINS); do \
@@ -66,13 +80,12 @@ ifneq ($(AGENT_OPTIONS),)
@echo "ZAF_AGENT_OPTIONS=\"$(AGENT_OPTIONS)\"" >>$(DEBIAN_DIR)/etc/zaf.conf
endif
deb-changelog:
@cp files/changelog.template $(DEBIAN_CTRL)/changelog
deb-package:
@dpkg-deb -b $(DEBIAN_DIR) $(DEBIAN_PKG)
@echo "\nCheck configuration:"
@cat $(DEBIAN_DIR)/etc/zaf.conf
@echo PLUGINS embedded: $(PLUGINS)
@echo PLUGINS in postinst: $(IPLUGINS)
@echo

101
README.md
View File

@@ -7,7 +7,7 @@ If you are maintainer of some external check, it is enough to create zaf file in
## Motivation
Did you install lot of zabbix agents? Do you want to setup them all? Do you want to change some zabbix agent options on lot of system? Do you want to write your own simple check or discovery rule for zabbix and it is nightmare to deploy same script on more zabbix agents? Are you tired searching some system specific agent check and setup them individualy?
Did you install lot of zabbix agents and try to setup similar common user parameters? Do you want to setup them all? Do you want to change some zabbix agent options on lot of system? Do you want to write your own simple check or discovery rule for zabbix and it is nightmare to deploy same script on more zabbix agents? Are you tired searching some system specific agent check and setup them individualy?
So zaf is here for you :)
## Features
@@ -19,62 +19,122 @@ So zaf is here for you :)
* Sharing code. Many zabbix external checks share common shell functions.
* Simplification of userparameter functions (does not have to be one-line long code)
* Zabbix discovery simplification. Creating zabbix item for discovery is not so easy in shell based system and result is not nice. But you can use framework function to do so.
* OS packaging support (in future).
* Zabbix agent autoinstallation and autoconfiguration suitable to use in puppet or another tool
* OS packaging support
## Installing Zaf
You need to be root and you must have curl installed on your system. Depending on your system, github certificates may not be available so you have to use *-k* option for curl (insecure). Default installation type is silent. So there will be no questions and everything will be autodetected. This simple command should be used on most systems:
```
curl -k https://raw.githubusercontent.com/limosek/zaf/master/install.sh | sh
curl -k https://raw.githubusercontent.com/limosek/zaf/1.1/install.sh | sh
```
### Install options and autoconfiguration
General parameters for install.sh on any system (simplest way how to install)
```
curl -k https://raw.githubusercontent.com/limosek/zaf/master/install.sh | \
curl -k https://raw.githubusercontent.com/limosek/zaf/1.1/install.sh | \
sh -s {auto|interactive|debug-auto|debug-interactive} [Agent-Options] [Zaf-Options]
```
or use git version:
```
git clone https://github.com/limosek/zaf.git
git clone https://github.com/limosek/zaf.git; cd zaf; git checkout 1.1
./install.sh {auto|interactive|debug-auto|debug-interactive} [Agent-Options] [Zaf-Options]
Agent-Options: A_Option=value [...]
Agent-Options: Z_Option=value [...]
Zaf-Options: ZAF_OPT=value [...]
To unset Agent-Option use Z_Option=''
```
So you can pass ANY configuration of your zabbix agent directly to installer prefixing it with *Z_*. Please note that options are *Case Sensitive*!
Next to this, you can pass ANY zaf config options by *ZAF_* prefix. Yes, we need some more documentation of ZAF options. Please look at least here: https://github.com/limosek/zaf/blob/master/install.sh#L160
Next to this, you can pass ANY zaf config options by *ZAF_* prefix. Interresting ZAF options:
```
# If we want to use GIT and local GIT repository
ZAF_GIT='1'# Default
# Git repository. Can be your local version
ZAF_REPO_GITURL='https://github.com/limosek/zaf-plugins.git'
# Where to install plugins
ZAF_PLUGINS_DIR='/usr/lib/zaf/plugins'
# Plugins can be downloaded from http[s] too
ZAF_REPO_URL='https://raw.githubusercontent.com/limosek/zaf-plugins/master/'
```
Installer will try to autoguess suitable config options for your system.
Now everything was tested on Debian and OpenWrt. If somebody is interrested in, you can help and test with some rpm specific functions.
Now everything was tested on Debian and OpenWrt. If somebody is interrested in, you can help and test with some rpm specific functions. Remember that on some systems, default zabbix agent config is empty so you *need to* enter essential config options as parameters.
### Example
Suppose you want to autoinstall agent on clean system. You need only curl installed. Everything else is one-cmd process.
This command will install zaf, install zabbix-agent if necessary and sets zabbix variables on agent to reach server. This command can be automatized by puppet or another deploying system.
```
curl -k https://raw.githubusercontent.com/limosek/zaf/master/install.sh | sh -s auto \
curl -k https://raw.githubusercontent.com/limosek/zaf/1.1/install.sh | sh -s auto \
Z_Server=zabbix.server.local \
Z_ServerActive=zabbix.server.local \
Z_HostnameItem=system.hostname Z_RefreshActiveChecks=60 \
ZAF_PLUGINS_GITURL="git://gitserver.local"
ZAF_REPO_GITURL="git://gitserver.local"
```
### Packaged version
You can make your own deb package with preconfigured option. It is up to you to put it to right APT repository and install.
```
git clone https://github.com/limosek/zaf.git \
&& cd zaf \
&& git checkout 1.1 \
&& git clone https://github.com/limosek/zaf-plugins.git \
&& make deb PLUGINS="./zaf-plugins/fsx" IPLUGINS="zaf" ZAF_OPTIONS="ZAF_GIT=0" AGENT_OPTIONS="Z_Server=zabbix.server Z_ServerActive=zabbix.server Z_StartAgents=8"
sudo dpkg -i out/zaf-1.1.deb
```
General usage:
```
make {deb|ipk|rpm} [PLUGINS="/dir/plg1 [/dir2]...] [IPLUGINS="plg1 [plg2]..."] [ZAF_OPTIONS="ZAF_cfg=val ..."] [AGENT_OPTIONS="Z_Server=host ..."]
PLUGINS are embedded into package. Has to be local directories accessible during build.
IPLUGINS will be downloaded and installed after package is installed. Can be name or url accessible after package installation.
```
## Zaf plugin
Zaf plugin is set of configuration options and binaries which are needed for specific checks. For example, to monitor postfix, we need some cron job which is automaticaly run and next ti this, some external items which has to be configured. Do not mix zaf plugin and zabbix plugin. While zaf plugin is set of scripts or binaries external to zabbix agent, zabbix plugin is internal zabbix lodadable module.
### Control file
Control file is main part of zaf plugin. It describes how to install plugin and defines all checks. In fact, simple control file can be enough to create zaf plugin because scripts can be embeded within. There are two kind of options: global and per item. Each option can be singleline:
```
Plugin: pluginname
```
or multiline:
```
Description::
Zaf plugin for monitoring fail2ban with LLD
Credits
2014 dron, jiri.slezka@slu.cz
2016 limo, lukas.macura@slu.cz
::
```
Items are enclosed like this:
```
Item some_item:
Description::
Returns number of currently banned IPs for jail
::
Parameters: jail
Cmd: sudo fail2ban-client status $1 | grep "Currently banned:" | grep -o -E "[0-9]*"
/Item
```
During plugin installation, zaf will check all dependencies, do install binaries and generates apropriate zabbix.conf.d entries. Look into https://github.com/limosek/zaf-plugins repository for more examples.
## Zaf utility
Zaf binary can be installed on any system from openwrt to big system. It has minimal dependencies and is shell based. Is has minimal size (up to 50kb of code). It can be used for installing, removing and testing zaf plugin items. Zaf should be run as root.
```
zaf
/usr/bin/zaf Version trunk. Please use some of this commands:
/usr/bin/zaf update To update repo
/usr/bin/zaf plugins To list installed plugins
/usr/bin/zaf show [plugin] To show installed plugins or plugin info
/usr/bin/zaf items [plugin] To list all suported items [for plugin]
/usr/bin/zaf test [plugin[.item]] To test all suported items [for plugin]
/usr/bin/zaf install plugin To install plugin
/usr/bin/zaf remove plugin To remove plugin
/usr/bin/zaf self-upgrade To self-upgrade zaf
/usr/bin/zaf self-remove To self-remove zaf and its config
zaf Version 1.1. Please use some of this commands:
zaf update To update repo
zaf plugins To list installed plugins
zaf show [plugin] To show installed plugins or plugin info
zaf items [plugin] To list all suported items [for plugin]
zaf test [plugin[.item]] To test [all] suported items by zabbix_agentd [for plugin]
zaf get [plugin[.item]] To test [all] suported items by zabbix_get [for plugin]
zaf install plugin To install plugin
zaf remove plugin To remove plugin
zaf self-upgrade To self-upgrade zaf
zaf self-remove To self-remove zaf and its config
```
@@ -96,7 +156,6 @@ Installer will look into control file, run setup task defined there, fetch binar
## How it works
Zaf installer will do most of actions needed to monitor some specific plugin items. Configuration of plugin is very simple and text readable. Anybody can write its own plugin or make its plugin "zafable". It is enough to create *control.zaf" file. For example, look into https://github.com/limosek/zaf-plugins repository. This is default repository for zaf.
## I want to make my own plugin!
Great! Look into https://github.com/limosek/zaf-plugins repository, look to control files and try to create your own. It is easy! You can contact me for help.

View File

@@ -1,4 +1,4 @@
PACKAGE (0.1) zaf; urgency=medium
PACKAGE (1.0) zaf; urgency=medium
* Initial release.

View File

@@ -8,9 +8,10 @@ configure)
. /usr/lib/zaf/os.lib.sh
. /usr/lib/zaf/ctrl.lib.sh
cd /usr/lib/zaf && /usr/lib/zaf/install.sh reconf
zaf reinstall {PLUGINS}
[ -n "{PLUGINS}" ] && zaf reinstall {PLUGINS} {IPLUGINS}
fi
;;
esac
true

View File

@@ -1,11 +1,13 @@
#!/bin/sh
[ -z "$ZAF_DEBUG" ] && ZAF_DEBUG=1
if [ -z "$ZAF_URL" ]; then
# Runing as standalone install.sh. We have to download rest of files first.
[ -z "$ZAF_VERSION" ] && ZAF_VERSION=master
ZAF_URL="https://github.com/limosek/zaf/"
fi
[ -z "$ZAF_GITBRANCH" ] && ZAF_GITBRANCH=1.1
# Lite version of zaf_fetch_url, full version will be loaded later
zaf_fetch_url(){
if [ -z "$ZAF_OFFLINE" ]; then
@@ -19,7 +21,7 @@ zaf_fetch_url(){
# Download tgz and extract to /tmp/zaf-installer
zaf_download_files() {
rm -rf /tmp/zaf-installer
zaf_fetch_url $ZAF_URL/archive/$ZAF_VERSION.tar.gz | tar -C /tmp -zx && mv /tmp/zaf-$ZAF_VERSION /tmp/zaf-installer
zaf_fetch_url $ZAF_URL/archive/$ZAF_GITBRANCH.tar.gz | tar -f - -C /tmp -zx && mv /tmp/zaf-$ZAF_GITBRANCH /tmp/zaf-installer
}
if ! [ -f README.md ]; then
@@ -39,6 +41,8 @@ if ! type zaf_version >/dev/null; then
. lib/zaf.lib.sh
. lib/os.lib.sh
. lib/ctrl.lib.sh
. lib/cache.lib.sh
. lib/zbxapi.lib.sh
fi
# Read options as config for ZAF
@@ -52,7 +56,6 @@ done
[ -z "$ZAF_CFG_FILE" ] && ZAF_CFG_FILE=$INSTALL_PREFIX/etc/zaf.conf
[ -n "$C_ZAF_DEBUG" ] && ZAF_DEBUG=$C_ZAF_DEBUG
[ -z "$ZAF_DEBUG" ] && ZAF_DEBUG=1
# Read option. If it is already set in zaf.conf, it is skipped. If env variable is set, it is used instead of default
# It sets global variable name on result.
@@ -99,9 +102,10 @@ zaf_set_option(){
local description
if ! grep -q "^$1=" ${ZAF_CFG_FILE}; then
echo "$1='$2'" >>${ZAF_CFG_FILE}
zaf_dbg "Saving $1 to $2 in ${ZAF_CFG_FILE}" >&2
zaf_dbg "Saving $1 to $2 in ${ZAF_CFG_FILE}"
else
zaf_wrn "Preserving $1 to $2 in ${ZAF_CFG_FILE}" >&2
sed -i "s#^$1=\(.*\)#$1='$2'#" ${ZAF_CFG_FILE}
zaf_dbg "Changing $1 to $2 in ${ZAF_CFG_FILE}"
fi
}
@@ -113,7 +117,20 @@ zaf_set_agent_option() {
local value="$2"
if grep -q ^$option\= $ZAF_AGENT_CONFIG; then
zaf_dbg "Setting option $option in $ZAF_AGENT_CONFIG."
sed -i "s/$option=(.*)/$option=$2/" $ZAF_AGENT_CONFIG
sed -i "s/$option=\(.*\)/$option=$2/" $ZAF_AGENT_CONFIG
else
zaf_move_agent_option "$1" "$2"
fi
}
# Unset config option in zabbix agent config file
# $1 option
zaf_unset_agent_option() {
local option="$1"
local value="$2"
if grep -q ^$option\= $ZAF_AGENT_CONFIG; then
zaf_dbg "Unsetting option $option in $ZAF_AGENT_CONFIG."
sed -i "s/$option=\(.*\)/#$option=$2/" $ZAF_AGENT_CONFIG
fi
}
@@ -157,27 +174,36 @@ zaf_configure_agent() {
echo $pair | grep -q '^Z\_' || continue # Skip non Z_ vars
option=$(echo $pair|cut -d '=' -f 1|cut -d '_' -f 2)
value=$(echo $pair|cut -d '=' -f 2-)
if [ -n "$value" ]; then
zaf_set_agent_option "$option" "$value"
options="$options Z_$option='$value'"
else
zaf_unset_agent_option "$option"
fi
options="$options Z_$option=$value"
done
zaf_set_option ZAF_AGENT_OPTIONS "${options}"
}
zaf_configure(){
zaf_preconfigure(){
zaf_detect_system
zaf_os_specific zaf_configure_os
if ! zaf_is_root; then
[ -z "$INSTALL_PREFIX" ] && zaf_err "We are not root. Use INSTALL_PREFIX or become root."
else
[ "$1" != "reconf" ] && zaf_os_specific zaf_check_deps zaf && zaf_err "Zaf is installed as system package. Cannot install."
fi
zaf_get_option ZAF_PKG "Packaging system to use" "$ZAF_PKG" "$1"
zaf_get_option ZAF_OS "Operating system to use" "$ZAF_OS" "$1"
zaf_get_option ZAF_OS_CODENAME "Operating system codename" "$ZAF_OS_CODENAME" "$1"
zaf_get_option ZAF_AGENT_PKG "Zabbix agent package" "$ZAF_AGENT_PKG" "$1"
zaf_get_option ZAF_AGENT_OPTIONS "Zabbix options to set in cfg" "$ZAF_AGENT_OPTIONS" "$1"
}
zaf_configure(){
zaf_get_option ZAF_PKG "Packaging system to use" "$ZAF_PKG" "$INSTALL_MODE"
zaf_get_option ZAF_OS "Operating system to use" "$ZAF_OS" "$INSTALL_MODE"
zaf_get_option ZAF_OS_CODENAME "Operating system codename" "$ZAF_OS_CODENAME" "$INSTALL_MODE"
zaf_get_option ZAF_AGENT_PKG "Zabbix agent package" "$ZAF_AGENT_PKG" "$INSTALL_MODE"
zaf_get_option ZAF_AGENT_OPTIONS "Zabbix options to set in cfg" "$ZAF_AGENT_OPTIONS" "$INSTALL_MODE"
if zaf_is_root && [ -n "$ZAF_AGENT_PKG" ]; then
if ! zaf_os_specific zaf_check_deps "$ZAF_AGENT_PKG"; then
if [ "$1" = "auto" ]; then
if [ "$INSTALL_MODE" = "auto" ]; then
zaf_os_specific zaf_install_agent
fi
fi
@@ -187,20 +213,26 @@ zaf_configure(){
else
ZAF_GIT=0
fi
zaf_get_option ZAF_GIT "Git is installed" "$ZAF_GIT" "$1"
zaf_get_option ZAF_CURL_INSECURE "Insecure curl (accept all certificates)" "1" "$1"
zaf_get_option ZAF_TMP_BASE "Tmp directory prefix (\$USER will be added)" "/tmp/zaf" "$1"
zaf_get_option ZAF_LIB_DIR "Libraries directory" "/usr/lib/zaf" "$1"
zaf_get_option ZAF_BIN_DIR "Directory to put binaries" "/usr/bin" "$1"
zaf_get_option ZAF_PLUGINS_DIR "Plugins directory" "${ZAF_LIB_DIR}/plugins" "$1"
[ "${ZAF_GIT}" = 1 ] && zaf_get_option ZAF_PLUGINS_GITURL "Git plugins repository" "https://github.com/limosek/zaf-plugins.git" "$1"
zaf_get_option ZAF_PLUGINS_URL "Plugins http[s] repository" "https://raw.githubusercontent.com/limosek/zaf-plugins/master/" "$1"
zaf_get_option ZAF_REPO_DIR "Plugins directory" "${ZAF_LIB_DIR}/repo" "$1"
zaf_get_option ZAF_AGENT_CONFIG "Zabbix agent config" "/etc/zabbix/zabbix_agentd.conf" "$1"
zaf_get_option ZAF_GIT "Git is installed" "$ZAF_GIT" "$INSTALL_MODE"
zaf_get_option ZAF_CURL_INSECURE "Insecure curl (accept all certificates)" "1" "$INSTALL_MODE"
zaf_get_option ZAF_TMP_BASE "Tmp directory prefix (\$USER will be added)" "/tmp/zaf" "$INSTALL_MODE"
zaf_get_option ZAF_LIB_DIR "Libraries directory" "/usr/lib/zaf" "$INSTALL_MODE"
zaf_get_option ZAF_BIN_DIR "Directory to put binaries" "/usr/bin" "$INSTALL_MODE"
zaf_get_option ZAF_PLUGINS_DIR "Plugins directory" "${ZAF_LIB_DIR}/plugins" "$INSTALL_MODE"
[ "${ZAF_GIT}" = 1 ] && zaf_get_option ZAF_REPO_GITURL "Git plugins repository" "https://github.com/limosek/zaf-plugins.git" "$INSTALL_MODE"
zaf_get_option ZAF_REPO_URL "Plugins http[s] repository" "https://raw.githubusercontent.com/limosek/zaf-plugins/master/" "$INSTALL_MODE"
zaf_get_option ZAF_REPO_DIR "Plugins directory" "${ZAF_LIB_DIR}/repo" "$INSTALL_MODE"
zaf_get_option ZAF_AGENT_CONFIG "Zabbix agent config" "/etc/zabbix/zabbix_agentd.conf" "$INSTALL_MODE"
! [ -d "${ZAF_AGENT_CONFIGD}" ] && [ -d "/etc/zabbix/zabbix_agentd.d" ] && ZAF_AGENT_CONFIGD="/etc/zabbix/zabbix_agentd.d"
zaf_get_option ZAF_AGENT_CONFIGD "Zabbix agent config.d" "/etc/zabbix/zabbix_agentd.conf.d/" "$1"
zaf_get_option ZAF_AGENT_BIN "Zabbix agent binary" "/usr/sbin/zabbix_agentd" "$1"
zaf_get_option ZAF_AGENT_RESTART "Zabbix agent restart cmd" "service zabbix-agent restart" "$1"
zaf_get_option ZAF_AGENT_CONFIGD "Zabbix agent config.d" "/etc/zabbix/zabbix_agentd.conf.d/" "$INSTALL_MODE"
zaf_get_option ZAF_AGENT_BIN "Zabbix agent binary" "/usr/sbin/zabbix_agentd" "$INSTALL_MODE"
zaf_get_option ZAF_AGENT_RESTART "Zabbix agent restart cmd" "service zabbix-agent restart" "$INSTALL_MODE"
zaf_get_option ZAF_SUDOERSD "Sudo sudoers.d directory" "/etc/sudoers.d" "$INSTALL_MODE"
zaf_get_option ZAF_CROND "Cron.d directory" "/etc/cron.d" "$INSTALL_MODE"
zaf_get_option ZAF_ZBXAPI_URL "Zabbix API url" "http://localhost/zabbix/api_jsonrpc.php" "$INSTALL_MODE"
zaf_get_option ZAF_ZBXAPI_USER "Zabbix API user" "zaf" "$INSTALL_MODE"
zaf_get_option ZAF_ZBXAPI_PASS "Zabbix API password" "" "$INSTALL_MODE"
zaf_get_option ZAF_ZBXAPI_AUTHTYPE "Zabbix API authentication type" "internal" "$INSTALL_MODE"
if zaf_is_root && ! [ -x $ZAF_AGENT_BIN ]; then
zaf_err "Zabbix agent ($ZAF_AGENT_BIN) not installed? Use ZAF_AGENT_BIN env variable to specify location. Exiting."
@@ -221,15 +253,20 @@ zaf_configure(){
zaf_set_option ZAF_LIB_DIR "$ZAF_LIB_DIR"
zaf_set_option ZAF_BIN_DIR "$ZAF_BIN_DIR"
zaf_set_option ZAF_PLUGINS_DIR "$ZAF_PLUGINS_DIR"
zaf_set_option ZAF_PLUGINS_URL "$ZAF_PLUGINS_URL"
[ "${ZAF_GIT}" = 1 ] && zaf_set_option ZAF_PLUGINS_GITURL "$ZAF_PLUGINS_GITURL"
zaf_set_option ZAF_REPO_URL "$ZAF_REPO_URL"
[ "${ZAF_GIT}" = 1 ] && zaf_set_option ZAF_REPO_GITURL "$ZAF_REPO_GITURL"
zaf_set_option ZAF_REPO_DIR "$ZAF_REPO_DIR"
zaf_set_option ZAF_AGENT_CONFIG "$ZAF_AGENT_CONFIG"
zaf_set_option ZAF_AGENT_CONFIGD "$ZAF_AGENT_CONFIGD"
zaf_set_option ZAF_AGENT_BIN "$ZAF_AGENT_BIN"
zaf_set_option ZAF_AGENT_RESTART "$ZAF_AGENT_RESTART"
zaf_set_option ZAF_SUDOERSD "$ZAF_SUDOERSD"
zaf_set_option ZAF_CROND "$ZAF_CROND"
zaf_set_option ZAF_ZBXAPI_URL "$ZAF_ZBXAPI_URL"
zaf_set_option ZAF_ZBXAPI_USER "$ZAF_ZBXAPI_USER"
zaf_set_option ZAF_ZBXAPI_PASS "$ZAF_ZBXAPI_PASS"
zaf_set_option ZAF_ZBXAPI_AUTHTYPE "$ZAF_ZBXAPI_AUTHTYPE"
[ -n "$ZAF_PREPACKAGED_DIR" ] && zaf_set_option ZAF_PREPACKAGED_DIR "$ZAF_PREPACKAGED_DIR"
ZAF_TMP_DIR="${ZAF_TMP_BASE}-${USER}-$$"
if zaf_is_root; then
zaf_configure_agent $ZAF_AGENT_OPTIONS "$@"
@@ -241,21 +278,22 @@ zaf_install_all() {
rm -rif ${ZAF_TMP_DIR}
mkdir -p ${ZAF_TMP_DIR}
zaf_install_dir ${ZAF_LIB_DIR}
for i in lib/zaf.lib.sh lib/os.lib.sh lib/ctrl.lib.sh README.md; do
zaf_install $i ${ZAF_LIB_DIR}/
for i in lib/zaf.lib.sh lib/os.lib.sh lib/ctrl.lib.sh lib/cache.lib.sh lib/zbxapi.lib.sh README.md; do
zaf_install $i ${ZAF_LIB_DIR}/ || zaf_err "Error installing $i"
done
for i in lib/zaflock lib/preload.sh; do
zaf_install_bin $i ${ZAF_LIB_DIR}/
zaf_install_bin $i ${ZAF_LIB_DIR}/ || zaf_err "Error installing $i"
done
zaf_install_dir ${ZAF_BIN_DIR}
for i in zaf; do
zaf_install_bin $i ${ZAF_BIN_DIR}/
zaf_install_bin $i ${ZAF_BIN_DIR}/ || zaf_err "Error installing $i"
done
zaf_install_dir ${ZAF_PLUGINS_DIR}
zaf_install_dir ${ZAF_TMP_DIR}/p/zaf
zaf_install_dir ${ZAF_PLUGINS_DIR}
zaf_install_dir ${ZAF_BIN_DIR}
export INSTALL_PREFIX ZAF_CFG_FILE ZAF_DEBUG
}
zaf_postconfigure() {
if zaf_is_root; then
[ "${ZAF_GIT}" = 1 ] && ${INSTALL_PREFIX}/${ZAF_BIN_DIR}/zaf update
${INSTALL_PREFIX}/${ZAF_BIN_DIR}/zaf reinstall zaf || zaf_err "Error installing zaf plugin."
@@ -267,62 +305,91 @@ zaf_install_all() {
exit 1
fi
else
[ "${ZAF_GIT}" = 1 ] && [ -n "${INSTALL_PREFIX}" ] && git clone "${ZAF_PLUGINS_GITURL}" "${INSTALL_PREFIX}/${ZAF_REPO_DIR}"
[ "${ZAF_GIT}" = 1 ] && [ -n "${INSTALL_PREFIX}" ] && git clone "${ZAF_REPO_GITURL}" "${INSTALL_PREFIX}/${ZAF_REPO_DIR}"
fi
rm -rif ${ZAF_TMP_DIR}
echo "Install OK. Use 'zaf' without parameters to continue."
zaf_wrn "Install done. Use 'zaf' to get started."
true
}
if [ -f "${ZAF_CFG_FILE}" ]; then
. "${ZAF_CFG_FILE}"
fi
ZAF_TMP_DIR="${ZAF_TMP_BASE-/tmp/zaf}-${USER}-$$"
ZAF_TMP_DIR="/tmp/zaf-installer-tmp/"
# If debug is on, do not remove tmp dir
if [ "$ZAF_DEBUG" -le 3 ]; then
trap "rm -rif ${ZAF_TMP_DIR}" EXIT
trap "rm -rif /tmp/zaf-installer" EXIT
fi
! [ -d "${ZAF_TMP_DIR}" ] && mkdir "${ZAF_TMP_DIR}"
case $1 in
interactive)
shift
zaf_configure interactive
INSTALL_MODE=interactive
zaf_preconfigure
zaf_configure "$@"
zaf_install_all
zaf_postconfigure
;;
auto)
shift
zaf_configure auto
INSTALL_MODE=auto
zaf_preconfigure
zaf_configure "$@"
zaf_install_all
zaf_postconfigure
;;
debug-auto)
shift;
ZAF_DEBUG=4
zaf_configure auto
INSTALL_MODE=auto
zaf_preconfigure
zaf_configure "$@"
zaf_install_all
zaf_postconfigure
;;
debug-interactive)
shift;
ZAF_DEBUG=4
zaf_configure interactive
INSTALL_MODE=interactive
zaf_preconfigure
zaf_configure "$@"
zaf_install_all
zaf_postconfigure
;;
debug)
shift;
ZAF_DEBUG=4
zaf_configure auto
INSTALL_MODE=auto
zaf_preconfigure
zaf_configure "$@"
zaf_install_all
zaf_postconfigure
;;
reconf)
shift;
rm -f $ZAF_CFG_FILE
zaf_configure auto
INSTALL_MODE=auto
zaf_preconfigure reconf
zaf_configure "$@"
zaf_postconfigure
;;
install)
zaf_configure auto
INSTALL_MODE=auto
zaf_preconfigure nor
zaf_configure "$@"
zaf_install_all
zaf_postconfigure
;;
*)
echo
echo "Please specify how to install."
echo "install.sh {auto|interactive|debug-auto|debug-interactive|reconf} [Agent-Options] [Zaf-Options]"
echo "scratch means that config file will be created from scratch"
echo " Agent-Options: A_Option=value [...]"
echo " Agent-Options: Z_Option=value [...]"
echo " Zaf-Options: ZAF_OPT=value [...]"
echo " To unset Agent-Option use Z_Option=''"
echo
echo "Example 1 (default install): install.sh auto"
echo 'Example 2 (preconfigure agent options): install.sh auto A_Server=zabbix.server A_ServerActive=zabbix.server A_Hostname=$(hostname)'

84
lib/cache.lib.sh Normal file
View File

@@ -0,0 +1,84 @@
# Zaf cache related functions
zaf_cache_clean(){
if [ -n "$ZAF_CACHE_DIR" ]; then
zaf_wrn "Removing cache entries"
rm -rf "$ZAF_CACHE_DIR"
else
zaf_err "Cache dir not set."
fi
mkdir -p "$ZAF_CACHE_DIR"
}
# Get cache key from requested param
zaf_cache_key(){
echo "$1" | md5sum - | cut -d ' ' -f 1
}
# Put object into cache
# $1 key
# $2 value
# $3 lifetime in seconds
zaf_tocache(){
! [ -w $ZAF_CACHE_DIR ] && return 1
local key
local value
key=$(zaf_cache_key "$1")
echo "$2" >$ZAF_CACHE_DIR/$key
echo "$1" >$ZAF_CACHE_DIR/$key.info
touch -m -d "$3 seconds" $ZAF_CACHE_DIR/$key.info
zaf_trc "Cache: Saving entry $1($key)"
}
# Put object into cache from stdin and copy to stdout
# $1 key
# $2 lifetime in seconds
zaf_tocache_stdin(){
! [ -w $ZAF_CACHE_DIR ] && return 1
local key
key=$(zaf_cache_key "$1")
cat >$ZAF_CACHE_DIR/$key
if [ -s $ZAF_CACHE_DIR/$key ]; then
zaf_trc "Cache: Saving entry $1($key)"
echo "$1" >$ZAF_CACHE_DIR/$key.info
touch -m -d "$2 seconds" $ZAF_CACHE_DIR/$key.info
cat $ZAF_CACHE_DIR/$key
else
rm $ZAF_CACHE_DIR/$key
fi
}
# Remove entry from cache
# $1 key
zaf_cache_delentry(){
! [ -w $ZAF_CACHE_DIR ] && return 1
local key
key=$(zaf_cache_key "$1")
zaf_trc "Cache: removing $1($key) from cache"
rm -f "$ZAF_CACHE_DIR/$key*"
}
# Get object from cache
# $1 key
zaf_fromcache(){
! [ -r $ZAF_CACHE_DIR ] || [ -n "$ZAF_NOCACHE" ] && return 1
local key
local value
key=$(zaf_cache_key "$1")
if [ -f $ZAF_CACHE_DIR/$key ]; then
if [ "$ZAF_CACHE_DIR/$key.info" -nt "$ZAF_CACHE_DIR/$key" ]; then
zaf_trc "Cache: serving $1($key) from cache"
cat $ZAF_CACHE_DIR/$key
else
zaf_trc "Cache: removing old entry $1"
rm -f "$ZAF_CACHE_DIR/$key*"
return 2
fi
else
zaf_trc "Cache: missing entry $1($key)"
return 1
fi
}

View File

@@ -22,7 +22,7 @@ zaf_ctrl_get_item_block() {
# Get global plugin block body from stdin
# $1 itemname
zaf_ctrl_get_global_block() {
grep -v '^#' | awk '{ i=0;
grep -v '^#' | awk '{ i=0; print $0;
while (i==0) {
getline;
if (/^Item /) exit;
@@ -33,7 +33,7 @@ zaf_ctrl_get_global_block() {
# Get item multiline option
# $1 optionname
zaf_block_get_moption() {
awk '/^'$1'::$/ { i=0;
awk '/^'$1'::$/ { i=0; print $0;
while (i==0) {
getline;
if (/^::$/) {i=1; continue;};
@@ -55,33 +55,100 @@ zaf_block_get_option() {
# $1 - control file
# $2 - option name
zaf_ctrl_get_global_option() {
local ctrlvar
local ctrlopt
ctrlopt="ZAF_CTRL_$(zaf_stripctrl $2)"
eval ctrlvar=\$$ctrlopt
if [ -n "$ctrlvar" ]; then
zaf_dbg "Overriding control field $2 from env $ctrlopt($ctrlvar)"
echo $ctrlopt
else
zaf_ctrl_get_global_block <$1 | zaf_block_get_moption "$2" \
|| zaf_ctrl_get_global_block <$1 | zaf_block_get_option "$2"
fi
}
# Get item specific option (single or multiline)
# $1 - control file
# $2 - item name
# $3 - option name
zaf_ctrl_get_item_option() {
local ctrlvar
local ctrlopt
ctrlopt="ZAF_CTRLI_$(zaf_stripctrl $2)_$(zaf_stripctrl $3)"
eval ctrlvar=\$$ctrlopt
if [ -n "$ctrlvar" ]; then
zaf_dbg "Overriding item control field $2/$3 from env $ctrlopt($ctrlvar)"
echo $ctrlopt
else
zaf_ctrl_get_item_block <$1 "$2" | zaf_block_get_moption "$3" \
|| zaf_ctrl_get_item_block <$1 "$2" | zaf_block_get_option "$3"
fi
}
# Check dependencies based on control file
zaf_ctrl_check_deps() {
local deps
deps=$(zaf_ctrl_get_global_block <$1 | zaf_block_get_option "Depends-${ZAF_PKG}" )
zaf_os_specific zaf_check_deps $deps
if ! zaf_os_specific zaf_check_deps $deps; then
zaf_err "Missing one of dependend system packages: $deps"
fi
deps=$(zaf_ctrl_get_global_block <$1 | zaf_block_get_option "Depends-bin" )
for cmd in $deps; do
if ! which $cmd >/dev/null; then
zaf_wrn "Missing binary dependency $cmd. Please install it first."
return 1
zaf_err "Missing binary dependency $cmd. Please install it first."
fi
done
}
# Install binaries from control
# Install sudo config from control
# $1 plugin
# $2 control
# $3 plugindir
zaf_ctrl_sudo() {
local pdir
local plugin
local sudo
local cmd
local parms
pdir="$3"
plugin=$1
zaf_dbg "Installing sudoers entry $ZAF_SUDOERSD/zaf_$plugin"
sudo=$(zaf_ctrl_get_global_option $2 "Sudo" | zaf_far '{PLUGINDIR}' "${plugindir}")
[ -z "$sudo" ] && return # Nothing to install
if ! which sudo >/dev/null; then
zaf_wrn "Sudo needed bud not installed?"
fi
cmd=$(echo $sudo | cut -d ' ' -f 1)
parms=$(echo $sudo | cut -d ' ' -f 2-)
if which $cmd >/dev/null ; then
(echo "zabbix ALL=NOPASSWD:SETENV: $(which $cmd) $(echo $parms | tr '%' '*')";echo) >$ZAF_SUDOERSD/zaf_$plugin || zaf_err "Error during zaf_ctrl_sudo"
chmod 0440 $ZAF_SUDOERSD/zaf_$plugin
else
zaf_err "Cannot find binary '$cmd' to put into sudoers."
fi
}
# Install crontab config from control
# $1 plugin
# $2 control
# $3 plugindir
zaf_ctrl_cron() {
local pdir
local plugin
local cron
pdir="$3"
plugin=$1
zaf_dbg "Installing cron entry $ZAF_CROND/zaf_$plugin"
cron=$(zaf_ctrl_get_global_option $2 "Cron")
[ -z "$cron" ] && return # Nothing to install
zaf_ctrl_get_global_option $2 "Cron" | zaf_far '{PLUGINDIR}' "${plugindir}" >$ZAF_CROND/zaf_$plugin || zaf_err "Error during zaf_ctrl_cron"
}
# Install files defined to be installed in control to plugun directory
# $1 pluginurl
# $2 control
# $3 plugindir
@@ -89,18 +156,26 @@ zaf_ctrl_install() {
local binaries
local pdir
local script
local cmd
local files
local f
local b
pdir="$3"
(set -e
binaries=$(zaf_ctrl_get_global_option $2 "Install-bin")
for b in $binaries; do
zaf_fetch_url "$1/$b" >"${ZAF_TMP_DIR}/$b"
zaf_install_bin "${ZAF_TMP_DIR}/$b" "$pdir"
done
files=$(zaf_ctrl_get_global_option $2 "Install-files")
for f in $files; do
zaf_fetch_url "$1/$b" >"${ZAF_TMP_DIR}/$b"
zaf_install "${ZAF_TMP_DIR}/$b" "$pdir"
done
script=$(zaf_ctrl_get_global_option $2 "Install-script")
[ -n "$script" ] && eval "$script"
cmd=$(zaf_ctrl_get_global_option $2 "Install-cmd")
[ -n "$cmd" ] && $cmd
true
) || zaf_err "Error during zaf_ctrl_install"
}
# Generates zabbix cfg from control file
@@ -112,10 +187,12 @@ zaf_ctrl_generate_cfg() {
local iscript
local ikey
local lock
local cache
items=$(zaf_ctrl_get_items <"$1")
(set -e
for i in $items; do
iscript=$(echo $i | tr -d '[]*&;:')
iscript=$(zaf_stripctrl $i)
params=$(zaf_ctrl_get_item_option $1 $i "Parameters")
if [ -n "$params" ]; then
ikey="$2.$i[*]"
@@ -132,25 +209,27 @@ zaf_ctrl_generate_cfg() {
if [ -n "$lock" ]; then
lock="${ZAF_LIB_DIR}/zaflock $lock "
fi
cache=$(zaf_ctrl_get_item_option $1 $i "Cache")
if [ -n "$cache" ]; then
cache="_cache '$cache' "
fi
cmd=$(zaf_ctrl_get_item_option $1 $i "Cmd")
if [ -n "$cmd" ]; then
$(which echo) "UserParameter=$ikey,${ZAF_LIB_DIR}/preload.sh $lock$cmd";
$(which echo) "UserParameter=$ikey,${ZAF_LIB_DIR}/preload.sh $cache $lock$cmd";
continue
fi
cmd=$(zaf_ctrl_get_item_option $1 $i "Function")
if [ -n "$cmd" ]; then
$(which echo) -E "UserParameter=$ikey,${ZAF_LIB_DIR}/preload.sh $lock$cmd";
continue;
fi
cmd=$(zaf_ctrl_get_item_option $1 $i "Script")
if [ -n "$cmd" ]; then
zaf_ctrl_get_item_option $1 $i "Script" >${ZAF_TMP_DIR}/${iscript}.sh;
zaf_ctrl_get_item_option $1 $i "Script" | \
zaf_far '{INCLUDES}' '. /etc/zaf.conf; . ${ZAF_LIB_DIR}/zaf.lib.sh; . ${ZAF_LIB_DIR}/ctrl.lib.sh; . ${ZAF_LIB_DIR}/zbxapi.lib.sh; . ${ZAF_LIB_DIR}/cache.lib.sh; ' \
>${ZAF_TMP_DIR}/${iscript}.sh;
zaf_install_bin ${ZAF_TMP_DIR}/${iscript}.sh ${ZAF_PLUGINS_DIR}/$2/
$(which echo) "UserParameter=$ikey,${ZAF_LIB_DIR}/preload.sh $lock${ZAF_PLUGINS_DIR}/$2/${iscript}.sh $args";
$(which echo) "UserParameter=$ikey,$cache $lock${ZAF_PLUGINS_DIR}/$2/${iscript}.sh $args";
continue;
fi
zaf_err "Item $i declared in control file but has no Cmd, Function or Script!"
done
) || zaf_err "Error during zaf_ctrl_generate_cfg"
}

View File

@@ -12,28 +12,30 @@ zaf_configure_os_freebsd() {
ZAF_AGENT_PKG="zabbix3-agent"
ZAF_AGENT_CONFIG="/usr/local/etc/zabbix3/zabbix_agentd.conf"
ZAF_AGENT_CONFIGD="/usr/local/etc/zabbix3/zabbix_agentd.conf.d/"
ZAF_AGENT_BIN="/usr/local/sbin/zabbix_agentd"
ZAF_AGENT_RESTART="service zabbix_agentd restart"
}
zaf_detect_system() {
if which dpkg >/dev/null; then
ZAF_PKG=dpkg
ZAF_OS=$(lsb_release -is|tr '[:upper:]' '[:lower:]')
ZAF_OS_CODENAME=$(lsb_release -cs|tr '[:upper:]' '[:lower:]')
ZAF_OS=$(lsb_release -is|zaf_tolower)
ZAF_OS_CODENAME=$(lsb_release -cs|zaf_tolower)
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|tr '[:upper:]' '[:lower:]')
ZAF_OS_CODENAME=$(lsb_release -cs|tr '[:upper:]' '[:lower:]')
ZAF_OS=$(lsb_release -is|zaf_tolower)
ZAF_OS_CODENAME=$(lsb_release -cs|zaf_tolower)
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="$(echo $DISTRIB_ID|tr '[:upper:]' '[:lower:]')"
ZAF_OS_CODENAME="$(echo $DISTRIB_CODENAME|tr '[:upper:]' '[:lower:]')"
ZAF_OS="$(echo $DISTRIB_ID|zaf_tolower)"
ZAF_OS_CODENAME="$(echo $DISTRIB_CODENAME|zaf_tolower)"
return
else if which pkg >/dev/null; then
ZAF_PKG="pkg"
@@ -145,7 +147,7 @@ zaf_check_deps_rpm() {
zaf_check_deps_opkg() {
local p
for p in $*; do
opkg info $p | grep -q 'Package:' || { echo "Missing package $p" >&2; return 1; }
opkg info $p | grep -q 'Package:' || { return 1; }
done
}
@@ -154,7 +156,7 @@ zaf_check_deps_opkg() {
zaf_check_deps_pkg() {
local p
for p in $*; do
pkg query -x "Package: %n" $p| grep -q 'Package:' || { echo "Missing package $p" >&2; return 1; }
pkg query -x "Package: %n" $p| grep -q 'Package:' || { return 1; }
done
}

View File

@@ -4,19 +4,34 @@
[ -z "$ZAF_TMP_BASE" ] && ZAF_TMP_BASE=/tmp/zaf
ZAF_TMP_DIR="${ZAF_TMP_BASE}-${USER}"
[ -z "$ZAF_CACHE_DIR" ] && ZAF_CACHE_DIR=${ZAF_TMP_BASE}c
trap "rm -rif ${ZAF_TMP_DIR}" EXIT
! [ -d "${ZAF_TMP_DIR}" ] && mkdir "${ZAF_TMP_DIR}"
! [ -d "${ZAF_CACHE_DIR}" ] && mkdir "${ZAF_CACHE_DIR}"
[ -z "$ZAF_DEBUG" ] && ZAF_DEBUG=1
. ${ZAF_LIB_DIR}/zaf.lib.sh
. ${ZAF_LIB_DIR}/ctrl.lib.sh
. ${ZAF_LIB_DIR}/os.lib.sh
. ${ZAF_LIB_DIR}/zbxapi.lib.sh
. ${ZAF_LIB_DIR}/cache.lib.sh
export ZAF_LIB_DIR
export ZAF_TMP_DIR
export ZAF_PLUGINS_DIR
if [ "$1" = "_cache" ] || [ "$1" = "_nocache" ] ; then
[ "$1" = "_nocache" ] && export ZAF_NOCACHE=1
shift
seconds=$1
shift
parms=$(echo $*|tr -d ' ')
if ! zaf_fromcache $parms; then
([ "$(basename $0)" = "preload.sh" ] && [ -n "$*" ] && $@ ) | zaf_tocache_stdin $parms $seconds
fi
else
[ "$(basename $0)" = "preload.sh" ] && [ -n "$*" ] && $@
fi

View File

@@ -1,13 +1,18 @@
# Hardcoded variables
ZAF_VERSION="master"
ZAF_VERSION="1.1"
ZAF_GITBRANCH="1.1"
ZAF_URL="https://github.com/limosek/zaf"
ZAF_RAW_URL="https://raw.githubusercontent.com/limosek/zaf"
############################################ Common routines
zaf_msg() {
echo $@
}
zaf_trc() {
[ "$ZAF_DEBUG" -ge "3" ] && logger -s -t zaf -- $@
}
zaf_dbg() {
[ "$ZAF_DEBUG" -ge "2" ] && logger -s -t zaf -- $@
}
@@ -31,7 +36,11 @@ zaf_fetch_url() {
local scheme
local uri
local insecure
local out
if zaf_fromcache "$1"; then
return
fi
scheme=$(echo $1|cut -d ':' -f 1)
uri=$(echo $1|cut -d '/' -f 3-)
if [ "$1" = "$scheme" ]; then
@@ -42,7 +51,7 @@ zaf_fetch_url() {
[ "$scheme" != "file" ] && [ -n "$ZAF_OFFLINE" ] && zaf_err "Cannot download $1 in offline mode!"
[ "${ZAF_CURL_INSECURE}" = "1" ] && insecure="-k"
zaf_dbg curl $insecure -f -s -L -o - $1
curl $insecure -f -s -L -o - "$1"
curl $insecure -f -s -L -o - "$1" | zaf_tocache_stdin "$1" 120
;;
esac
}
@@ -64,6 +73,27 @@ zaf_far(){
eval $sedcmd
}
# Limit concurrent processes or continue
zaf_bglimit(){
local maxbg
local maxnumber
local cnumber
if [ $# -eq 0 ] ; then
maxbg=5
else
maxbg=$1
fi
maxnumber=$((0 + ${1:-0}))
while true; do
cnumber=$(jobs | wc -l)
if [ $cnumber -lt $maxnumber ]; then
break
fi
zaf_dbg "Limiting next job due to $maxbg limit of bg jobs"
sleep 1
done
}
# Initialises discovery function
zaf_discovery_begin(){
cat <<EOF
@@ -143,11 +173,11 @@ zaf_check_agent_config() {
# Update repo
zaf_update_repo() {
[ "$ZAF_GIT" != 1 ] && { zaf_err "Git is not installed. Exiting."; }
if [ -z "${ZAF_PLUGINS_GITURL}" ] || [ -z "${ZAF_REPO_DIR}" ]; then
[ "$ZAF_GIT" != 1 ] && { zaf_err "Git is disabled or is not installed. Exiting."; }
if [ -z "${ZAF_REPO_GITURL}" ] || [ -z "${ZAF_REPO_DIR}" ]; then
zaf_err "This system is not configured for git repository."
else
[ ! -d "${ZAF_REPO_DIR}" ] && git clone "${ZAF_PLUGINS_GITURL}" "${ZAF_REPO_DIR}"
[ ! -d "${ZAF_REPO_DIR}" ] && git clone "${ZAF_REPO_GITURL}" "${ZAF_REPO_DIR}"
(cd ${ZAF_REPO_DIR} && git pull)
fi
}
@@ -171,7 +201,11 @@ zaf_get_plugin_url() {
if [ -n "${ZAF_PREPACKAGED_DIR}" ] && [ -d "${ZAF_PREPACKAGED_DIR}/$1" ]; then
url="${ZAF_PREPACKAGED_DIR}/$1"
else
zaf_err "Plugin $1 not found."
if [ -n "${ZAF_REPO_URL}" ]; then
url="${ZAF_REPO_URL}/$1"
else
zaf_err "Cannot find plugin $1"
fi
fi
fi
fi
@@ -183,6 +217,7 @@ zaf_get_plugin_url() {
zaf_plugin_info() {
local control="$1"
! [ -f "$control" ] && zaf_err "Control file $control not found."
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)
@@ -197,7 +232,13 @@ zaf_plugin_info() {
[ -n "$purl" ] && echo "Url: $purl"
[ -n "$phome" ] && echo "Home: $phome"
echo
if zaf_is_plugin "$(basename $plugin)"; then
echo -n "Defined items: "; zaf_list_plugin_items $plugin
echo -n "Test items: "; zaf_list_plugin_items $plugin test
echo -n "Precache items: "; zaf_list_plugin_items $plugin precache
else
echo "Items: $pitems"
fi
echo
}
@@ -209,7 +250,7 @@ zaf_prepare_plugin() {
local plugindir
local control
url=$(zaf_get_plugin_url "$1")/control.zaf
url=$(zaf_get_plugin_url "$1")/control.zaf || exit $?
plugindir="$2"
control=${plugindir}/control.zaf
zaf_install_dir "$plugindir"
@@ -217,7 +258,7 @@ zaf_prepare_plugin() {
if zaf_fetch_url "$url" >"${control}"; then
zaf_ctrl_check_deps "${control}"
else
zaf_err "Cannot fetch or write control file!"
zaf_err "prepare_plugin: Cannot fetch or write control file $control from url $url!"
fi
}
@@ -226,21 +267,28 @@ zaf_install_plugin() {
local plugin
local plugindir
local control
local version
if zaf_prepare_plugin "$1" "${ZAF_TMP_DIR}/plugin"; then
url=$(zaf_get_plugin_url "$1")
plugin=$(zaf_ctrl_get_global_block <"${ZAF_TMP_DIR}/plugin/control.zaf" | zaf_block_get_option Plugin)
control="${ZAF_TMP_DIR}/plugin/control.zaf"
plugin=$(zaf_ctrl_get_global_option $control Plugin)
version=$(zaf_ctrl_get_global_option $control Version)
plugindir="${ZAF_PLUGINS_DIR}"/$plugin
if zaf_prepare_plugin "$1" $plugindir; then
if [ -n "$plugin" ] && zaf_prepare_plugin "$1" $plugindir; then
zaf_wrn "Installing plugin $plugin version $version"
zaf_dbg "Source url: $url, Destination dir: $plugindir"
control=${plugindir}/control.zaf
[ "$ZAF_DEBUG" -gt 0 ] && zaf_plugin_info "${control}"
[ "$ZAF_DEBUG" -gt 1 ] && zaf_plugin_info "${control}"
zaf_ctrl_check_deps "${control}"
zaf_ctrl_install "$url" "${control}" "${plugindir}"
zaf_ctrl_sudo "$plugin" "${control}" "${plugindir}"
zaf_ctrl_cron "$plugin" "${control}" "${plugindir}"
zaf_ctrl_generate_cfg "${control}" "${plugin}" \
| zaf_far '{PLUGINDIR}' "${plugindir}" >${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf
zaf_dbg "Generated ${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf"
else
zaf_err "Cannot install plugin $plugin to $plugindir!"
zaf_err "Cannot install plugin '$plugin' to $plugindir!"
fi
else
zaf_err "Cannot prepare plugin $1"
@@ -266,7 +314,9 @@ zaf_discovery_plugins() {
zaf_list_plugins | zaf_discovery '{#PLUGIN}'
}
zaf_plugin_version() {
# $1 plugin
# $2 ctrl_option
zaf_plugin_option() {
local plugindir
local cfile
@@ -276,17 +326,37 @@ zaf_plugin_version() {
if zaf_is_plugin "$1"; then
plugindir="${ZAF_PLUGINS_DIR}/$1"
cfile="$plugindir/control.zaf"
zaf_ctrl_get_global_option $cfile Version
zaf_ctrl_get_global_option $cfile $2
else
zaf_err "Plugin $1 not installed."
fi
}
zaf_plugin_version() {
zaf_plugin_option "$1" Version
}
zaf_plugin_maintainer() {
zaf_plugin_option "$1" Maintainer
}
zaf_plugin_url() {
zaf_plugin_option "$1" Url
}
zaf_plugin_web() {
zaf_plugin_option "$1" Web
}
zaf_plugin_template_url() {
echo $(zaf_plugin_option "$1" Url)/template.xml
}
# $1 plugin
# $2 test to get test items, precache to get items to precache
zaf_list_plugin_items() {
local items
local i
local p
local key
local testparms
local precache
if ! zaf_is_plugin "$1"; then
zaf_err "Missing plugin name or plugin $1 unknown. ";
@@ -296,12 +366,25 @@ zaf_list_plugin_items() {
items=$(zaf_ctrl_get_items <$cfile)
for i in $items; do
p=$(zaf_ctrl_get_item_option $cfile $i "Parameters")
testparms=$(zaf_ctrl_get_item_option $cfile $i "Testparameters")
precache=$(zaf_ctrl_get_item_option $cfile $i "Precache")
if [ -n "$p" ]; then
key="$1.$i[]"
if [ -n "$testparms" ] && [ "$2" = "test" ]; then
for tp in $testparms; do
echo -n "$1.$i[$tp] "
done
else
if [ -n "$precache" ] && [ "$2" = "precache" ]; then
for tp in $precache; do
echo -n "$1.$i[$tp] "
done
fi
[ "$2" != "test" ] && key="$1.$i[]"
fi
else
key="$1.$i"
fi
echo -n "$key "
[ "$2" != "precache" ] && echo -n "$key "
done
echo
}
@@ -312,15 +395,44 @@ zaf_list_items() {
done
}
zaf_get_item() {
if which zabbix_get >/dev/null; then
zabbix_get -s localhost -k "$1" || zaf_wrn "Cannot reach agent on localhost. Please localhost to Server list."
return 11
else
zaf_wrn "Please install zabbix_get binary to check items over network."
return 11
fi
}
zaf_test_item() {
$ZAF_AGENT_BIN -t "$1"
}
zaf_precache_item() {
cmd=$(grep "^UserParameter=$item" $ZAF_AGENT_CONFIGD/zaf*.conf | cut -d ',' -f 2- | sed -e "s/_cache/_nocache/")
zaf_wrn "Precaching item $item[$(echo $*| tr ' ' ',')] ($cmd)"
eval $cmd
}
zaf_remove_plugin() {
! [ -d ${ZAF_PLUGINS_DIR}/$1 ] && { zaf_err "Plugin $1 not installed!"; }
zaf_wrn "Removing plugin $1"
! zaf_is_plugin $1 && { zaf_err "Plugin $1 not installed!"; }
zaf_wrn "Removing plugin $1 (version $(zaf_plugin_version $1))"
rm -rf ${ZAF_PLUGINS_DIR}/$1
rm -f ${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf
rm -f ${ZAF_AGENT_CONFIGD}/zaf_$1.conf ${ZAF_CROND}/zaf_$1 ${ZAF_SUDOERSD}/zaf_$1
}
zaf_tolower() {
tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'
}
zaf_toupper() {
tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
}
# Return simplified key with discarded special chars.
zaf_stripctrl() {
echo $1 | tr '[]*&;:.-' '________'
}

188
lib/zbxapi.lib.sh Normal file
View File

@@ -0,0 +1,188 @@
# Call api function
# $1 - query string
zaf_zbxapi_do() {
local result
if ! zaf_fromcache "$1"; then
zaf_trc "Zabbix API: $1"
result=$(curl -s -f -L -X POST -H 'Content-Type: application/json-rpc' -d "$1" "$ZAF_ZBXAPI_URL")
if [ $? = 0 ] && echo $result | grep -q '"result":'; then
zaf_tocache "$1" "$result" 60
echo $result
else
zaf_err "Error processing API request. ($?,$result)"
fi
fi
}
# Extract result from JSON response
zaf_zbxapi_getresult() {
sed -e 's/\({"jsonrpc":"2.0","result":\)\(.*\),\("id":.*\)/\2/g' | sed -e 's/^\[\]$//'
}
# Extract XML result from JSON response
zaf_zbxapi_getxml() {
zaf_zbxapi_getresult | sed -e 's/{"jsonrpc":"2.0","result":"//' | sed -e 's/","id"\:1}//' | sed -e 's#\\\([<">/]\)#\1#g'
}
# Extract string from JSON response result
zaf_zbxapi_getstring() {
sed -e 's/^"//' -e 's/"$//' -e 's/\\n/'\\n'/g'
}
# Extract value from JSON response result
# $1 key
zaf_zbxapi_getvalue() {
tr ',' '\n' | grep "\"$1\":" | cut -d '"' -f 4
}
# Zabbix API related functions
# Parameters in global variables ZAF_ZBX_API_*
# returns auth on stdout or false
zaf_zbxapi_login(){
local authstr
local user
local pass
local result
[ -z "$ZAF_ZBXAPI_URL" ] || [ -z "$ZAF_ZBXAPI_USER" ] || [ -z "$ZAF_ZBXAPI_PASS" ] && zaf_err "Zabbix Api parameters not set! Set ZAF_ZBXAPI_URL, ZAF_ZBXAPI_USER and ZAF_ZBXAPI_PASS and try again."
authstr='{
"method" : "user.login",
"params" : {
"password" : "'$ZAF_ZBXAPI_PASS'",
"user" : "'$ZAF_ZBXAPI_USER'"
},
"id" : 0,
"jsonrpc" : "2.0"
}'
if [ "$ZAF_ZBXAPI_AUTHTYPE" = "http" ] ; then
ZAF_ZBXAPI_URL=$(echo $ZAF_ZBXAPI_URL | cut -d '/' -f 1)//$ZAF_ZBXAPI_USER:$ZAF_ZBXAPI_PASS@$(echo $ZAF_ZBXAPI_URL | cut -d '/' -f 3-)
fi
result=$(zaf_zbxapi_do "$authstr")
ZAF_ZBXAPI_AUTH=$(echo $result |zaf_zbxapi_getresult| zaf_zbxapi_getstring)
zaf_dbg "Logged into zabbix API ($ZAF_ZBXAPI_AUTH)"
}
# $1 hostgroup name
zaf_zbxapi_gethostgroupid() {
local hstr
local filter
local gfilter
hstr='{
"method": "hostgroup.get",
"jsonrpc": "2.0",
"auth": "'$ZAF_ZBXAPI_AUTH'",
"params": {
"filter": {
"name": ["'$1'"]
},
"output": "shorten"
},
"id": 1
}'
zaf_zbxapi_do "$hstr" | zaf_zbxapi_getresult | tr ',' '\n' | cut -d '"' -f 4
}
# $1 hostname
zaf_zbxapi_gethostid() {
local hstr
local host
local groupid
local filter
local gfilter
host="$1"
if [ -n "$host" ] ; then
filter='"filter": { "host": [ "'$host'" ] },'
fi
hstr='{
"method": "host.get",
"jsonrpc": "2.0",
"auth": "'$ZAF_ZBXAPI_AUTH'",
"params": {
'$filter'
"output": "shorten"
},
"id": 1
}'
zaf_zbxapi_do "$hstr" | zaf_zbxapi_getresult | tr ',' '\n' | cut -d '"' -f 4
}
# $1 hostid
zaf_zbxapi_gethost() {
local hstr
local host
local groupid
local filter
local gfilter
hostid="$1"
if [ -n "$hostid" ] ; then
filter='"hostids": [ "'$hostid'" ],'
fi
hstr='{
"method": "host.get",
"jsonrpc": "2.0",
"auth": "'$ZAF_ZBXAPI_AUTH'",
"params": {
'$filter'
"output": "extend"
},
"id": 1
}'
zaf_zbxapi_do "$hstr" | zaf_zbxapi_getresult | zaf_zbxapi_getvalue host
}
# $1 hostgroupid
zaf_zbxapi_gethostsingroup() {
local hstr
local host
local groupid
local filter
local gfilter
groupid="$1"
if [ -n "$groupid" ]; then
gfilter='"groupids": [ "'$groupid'" ],'
fi
hstr='{
"method": "host.get",
"jsonrpc": "2.0",
"auth": "'$ZAF_ZBXAPI_AUTH'",
"params": {
'$gfilter'
'$filter'
"output": "shorten"
},
"id": 1
}'
zaf_zbxapi_do "$hstr" | zaf_zbxapi_getresult | tr ',' '\n' | cut -d '"' -f 4
}
# Host backup
# $1 hostid
zaf_zbxapi_backuphost(){
local bkpstr
host="$1"
bkpstr='
{
"method": "configuration.export",
"jsonrpc": "2.0",
"params": {
"options": {
"hosts": [
"'$host'"
]
},
"format": "xml"
},
"auth": "'$ZAF_ZBXAPI_AUTH'",
"id": 1
}'
zaf_zbxapi_do "$bkpstr" | zaf_zbxapi_getxml
}

222
zaf
View File

@@ -1,5 +1,6 @@
#!/bin/sh
if [ -z "$secondstage" ]; then
ZAF_CFG_FILE="/etc/zaf.conf"
if [ -f $ZAF_CFG_FILE ]; then
. $ZAF_CFG_FILE
@@ -10,55 +11,108 @@ fi
# Read options as config for ZAF
for pair in "$@"; do
echo $pair | grep -q '^ZAF\_' || continue
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
[ -z "$ZAF_TMP_BASE" ] && ZAF_TMP_BASE=/tmp/zaf
ZAF_TMP_DIR="${ZAF_TMP_BASE}-${USER}"
trap "rm -rif ${ZAF_TMP_DIR}" EXIT
! [ -d "${ZAF_TMP_DIR}" ] && mkdir "${ZAF_TMP_DIR}"
[ -z "$ZAF_CACHE_DIR" ] && ZAF_CACHE_DIR=${ZAF_TMP_BASE}c
[ -z "$ZAF_DEBUG" ] && ZAF_DEBUG=1
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
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
[ -f ./lib/zaf.${ZAF_OS}.sh ] && . ./lib/zaf.${ZAF_OS}.sh
else
. ${ZAF_LIB_DIR}/zaf.lib.sh
. ${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
fi
if zaf_is_root; then
chgrp zabbix "${ZAF_CACHE_DIR}"
chmod g+w "${ZAF_CACHE_DIR}"
fi
case $1 in
check-agent-config)
zaf_check_agent_config
;;
update)
zaf_wrn "Updating repository ${ZAF_PLUGINS_GITURL}..."
zaf_update_repo
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
;;
update)
zaf_wrn "Updating repository ${ZAF_REPO_GITURL}..."
zaf_update_repo
;;
upgrade)
ZAF_DEBUG=$ZAF_DEBUG $0 reinstall $(zaf_list_plugins)
;;
show)
if [ -z "$2" ]; then
shift;
if [ -z "$1" ]; then
zaf_list_plugins | while read plugin; do
zaf_plugin_info $ZAF_PLUGINS_DIR/$plugin/control.zaf
done
else
if zaf_list_plugins | grep -q "^$2"; then
zaf_plugin_info $ZAF_PLUGINS_DIR/$2/control.zaf
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."
else
if echo $2 |grep -q ^http; then
zaf_prepare_plugin "$2" "$ZAF_TMP_DIR/plugin"
if echo $1 |grep -q ^http; then
zaf_prepare_plugin "$1" "$ZAF_TMP_DIR/plugin"
zaf_plugin_info "$ZAF_TMP_DIR/plugin/control.zaf"
else
zaf_err "Plugin $2 not installed."
zaf_err "Plugin $1 not installed."
fi
fi
fi
@@ -69,37 +123,76 @@ plugins)
;;
items)
if [ -z "$2" ]; then
shift
if [ -z "$1" ]; then
zaf_list_items
else
zaf_list_plugin_items "$2"
zaf_list_plugin_items "$1"
fi
;;
test)
if echo $2|grep -q '\.'; then
zaf_test_item "$2"
[ "$USER" != "zabbix" ] && zaf_wrn "You are not zabbix user. Test will be run with your privileges and sudo access!"
shift
if echo $1|grep -q '\.'; then
zaf_test_item "$1"
exit
fi
if [ -n "$2" ]; then
shift
if [ -n "$1" ]; then
plugins="$@"
else
plugins="$(zaf_list_plugins)"
fi
for p in $plugins; do
for i in $(zaf_list_plugin_items $p); do
! zaf_is_plugin $p && zaf_err "Unknown plugin $p"
for i in $(zaf_list_plugin_items $p test); do
echo $i: $(zaf_test_item $i)
echo
done
done
;;
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
echo $i: $(zaf_get_item $i)
echo
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
;;
install)
shift;
shift
[ -z "$1" ] && echo "$0 install plugin [plugin]..."
for p in $@; do
if zaf_is_plugin "$p"; then
zaf_wrn "Plugin $p already installed. Skipping installation."
if zaf_is_plugin "$(basename $p)"; then
zaf_wrn "Plugin $(basename $p) already installed. Skipping installation."
continue
fi
zaf_install_plugin "$p"
@@ -109,7 +202,7 @@ install)
;;
reinstall)
shift;
shift
[ -z "$1" ] && echo "$0 reinstall plugin [plugin]..."
for p in $@; do
if zaf_is_plugin "$p"; then
@@ -123,7 +216,7 @@ reinstall)
;;
remove)
shift;
shift
[ -z "$1" ] && echo "$0 remove plugin [plugin]..."
for p in $@; do
if zaf_is_plugin "$p"; then
@@ -135,26 +228,23 @@ remove)
;;
self-upgrade)
shift
[ -z "$1" ] && auto=auto
zaf_os_specific zaf_check_deps zaf && zaf_err "Zaf is installed as system package. Cannot self-upgrade."
rm -rf /tmp/zaf-installer && mkdir /tmp/zaf-installer
if ! which curl >/dev/null;
then
zaf_err "Curl not found. Cannot continue. Please install it."
fi
[ -z "$2" ] && auto=auto
set -e
mkdir -p /tmp/zaf-installer \
&& cd /tmp/zaf-installer \
&& (for i in lib/zaf.lib.sh lib/os.lib.sh lib/ctrl.lib.sh install.sh ; do echo curl -f -k -s -L -o - "$ZAF_URL/$i" >&2; curl -f -k -s -L -o - "$ZAF_URL/$i"; done) >install.sh \
&& chmod +x install.sh \
&& exec ./install.sh $auto "$@"
zaf_fetch_url $ZAF_RAW_URL/$ZAF_GITBRANCH/install.sh | exec sh -s $auto "$@"
exit
;;
self-remove)
shift
zaf_os_specific zaf_check_deps zaf && zaf_err "Zaf is installed as system package. Cannot self-remove."
. /etc/zaf.conf
if [ "$2" = "force" ]; then
if [ "$1" = "force" ]; then
rm -rf /etc/zaf.conf ${ZAF_PLUGINS_DIR} ${ZAF_REPO_DIR} ${ZAF_LIB_DIR} \
${ZAF_BIN_DIR}/zaf ${ZAF_AGENT_CONFIGD}/zaf_*
else
@@ -167,17 +257,81 @@ self-remove)
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
;;
*)
echo "$0 Version ${ZAF_VERSION}. Please use some of this commands:"
echo "$0 update To update repo"
echo "$0 Cmd [ZAF_OPTION=value] [ZAF_CTRL_Option=value] [ZAF_CTRLI_Item_Option=value] ..."
echo "Commands:"
echo "$0 update To update repo (not plugins, similar to apt-get update)"
echo "$0 upgrade To upgrade installed plugins from repo"
echo "$0 plugins To list installed plugins"
echo "$0 show [plugin] To show installed plugins or plugin info"
echo "$0 items [plugin] To list all suported items [for plugin]"
echo "$0 test [plugin[.item]] To test all suported items [for plugin]"
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"
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"
echo "$0 self-upgrade To self-upgrade zaf"
echo "$0 self-remove To self-remove zaf and its config"
echo "$0 cache-clean To remove all entries from cache"
echo
;;
esac