1
0
mirror of https://github.com/limosek/zaf.git synced 2025-10-31 17:47:37 +01:00

1 Commits
1.3 ... 1.1

Author SHA1 Message Date
Lukas Macura
45a072024c Repaired ctrl functions (from trunk) 2016-04-15 11:18:46 +02:00
28 changed files with 815 additions and 2090 deletions

View File

@@ -1,21 +1,10 @@
# Zabbix agent framework makefile # Zabbix agent framework makefile
all: help
help:
@echo make '{deb|arch|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
include deb.mk
include arch.mk
include ipk.mk
include rpm.mk
CONTROLFILES=$(foreach p,$(PLUGINS),$(p)/control.zaf) CONTROLFILES=$(foreach p,$(PLUGINS),$(p)/control.zaf)
ZAF_EXPORT_OPTS=$(foreach o,$(ZAF_OPTIONS),$(shell echo $(o)|cut -d '=' -f 1)) 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=$(shell . lib/zaf.lib.sh; echo out/zaf-$$ZAF_VERSION.deb)
ifeq ($(ZAF_DEBUG),) ifeq ($(ZAF_DEBUG),)
ZAF_DEBUG=0 ZAF_DEBUG=0
endif endif
@@ -27,15 +16,76 @@ ifeq ($(IPLUGINS),)
IPLUGINS = zaf IPLUGINS = zaf
endif endif
all: help
help:
@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: $(DEBIAN_PKG) deb: $(DEBIAN_PKG)
arch: $(ARCH_PKG) $(DEBIAN_PKG): deb-clean deb-init deb-deps deb-control deb-scripts deb-cp deb-package
rpm: $(RPM_PKG)
ipk: $(IPK_PKG)
clean: clean:
@rm -rf tmp/* out/* @rm -rf tmp/* out/*
deb-clean:
@rm -rf $(DEBIAN_DIR) $(DEBIAN_PKG)
deb-init:
@mkdir -p tmp out $(DEBIAN_DIR)
deb-deps: $(CONTROLFILES)
@which dpkg >/dev/null && which dpkg-buildpackage >/dev/null && which dch >/dev/null || { echo "You need essential debian developer tools. Please install them:\nsudo apt-get install build-essential devscripts debhelper"; exit 2; }
deb-control:
@mkdir -p $(DEBIAN_CTRL)
@. lib/zaf.lib.sh; \
. lib/ctrl.lib.sh; \
for p in $(PLUGINS); do \
DEPENDS="$$DEPENDS,$$(zaf_ctrl_get_global_option $$p/control.zaf Depends-dpkg | tr ' ' ',')"; \
done; \
[ "$$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:
@. lib/zaf.lib.sh; \
. lib/ctrl.lib.sh; \
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
deb-cp:
@mkdir -p $(DEBIAN_DIR)
@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 \
plugin=$$(zaf_ctrl_get_global_option $$p/control.zaf Plugin) ; \
mkdir -p $(DEBIAN_DIR)/usr/lib/zaf/prepackaged/$$plugin/; \
cp -R $$p/* $(DEBIAN_DIR)/usr/lib/zaf/prepackaged/$$plugin/; \
done
@cat lib/*lib.sh install.sh >$(DEBIAN_DIR)/usr/lib/zaf/install.sh
@chmod +x $(DEBIAN_DIR)/usr/lib/zaf/install.sh
@rm -rf $(DEBIAN_DIR)/tmp
@cp $(DEBIAN_DIR)/etc/zaf.conf tmp/zaf.conf
@grep -E "$$(echo $(ZAF_EXPORT_OPTS) | tr ' ' '|')=" tmp/zaf.conf >$(DEBIAN_DIR)/etc/zaf.conf
@echo "ZAF_PREPACKAGED_DIR=\"/usr/lib/zaf/prepackaged\"" >>$(DEBIAN_DIR)/etc/zaf.conf
ifneq ($(AGENT_OPTIONS),)
@echo "ZAF_AGENT_OPTIONS=\"$(AGENT_OPTIONS)\"" >>$(DEBIAN_DIR)/etc/zaf.conf
endif
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

146
README.md
View File

@@ -1,15 +1,13 @@
# Zabbix (Agent) Framework # Zabbix Agent Framework
This tool is used to maintain external zabbix checks in *one place*. There are lot of places where it is possible to download many external checks. This tool is used to maintain external zabbix checks in *one place*. There are lot of places where it is possible to download many external checks.
But there is problem with installation, update and centralised management. This tool should do all of this in easy steps. In future it can be *starting point* to But there is problem with installation, update and centralised management. This tool should do all of this in easy steps. In future it can be *starting point* to
install and configure zabbix agent on systems with one step. Primary goal is not to make all plugins available here but to be able to use any plugin and decentralized development. install and configure zabbix agent on systems with one step. Primary goal is not to make all plugins available here but to be able to use any plugin and decentralized development.
If you are maintainer of some external check, it is enough to create zaf file in your repo and use zaf installer everywhere. If you are maintainer of some external check, it is enough to create zaf file in your repo and use zaf installer everywhere.
Next to this, this tool can even communicate with Zabbix API with *NO dependencies* to high level languages. Shell, sed and awk only.
## Motivation ## Motivation
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? Do you want to auto simple backup all hosts in your zabbix to xml files? Or do you want to do some scripting on host depending on Zabbix server configuration? 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 :) So zaf is here for you :)
## Features ## Features
@@ -23,73 +21,44 @@ So zaf is here for you :)
* 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. * 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.
* Zabbix agent autoinstallation and autoconfiguration suitable to use in puppet or another tool * Zabbix agent autoinstallation and autoconfiguration suitable to use in puppet or another tool
* OS packaging support * OS packaging support
* Zabbix API support
## Installing Zaf ## 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: 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/1.3/install.sh | sh curl -k https://raw.githubusercontent.com/limosek/zaf/1.1/install.sh | sh
```
If you have only wget installed:
```
wget --no-check-certificate -O - https://raw.githubusercontent.com/limosek/zaf/1.3/install.sh | sh
```
If you do not have https support (like openwrt), I made small http mirror of github files which is synchronized each day:
```
wget -O - http://github.macura.cz/zaf/1.3/install.sh| ZAF_URL=http://github.macura.cz/zaf/ ZAF_REPO_URL=http://github.macura.cz/zaf-plugins/1.3/ sh
``` ```
### Install options and autoconfiguration ### Install options and autoconfiguration
General parameters for install.sh on any system (simplest way how to install) General parameters for install.sh on any system (simplest way how to install)
``` ```
curl -k https://raw.githubusercontent.com/limosek/zaf/1.3/install.sh | \ curl -k https://raw.githubusercontent.com/limosek/zaf/1.1/install.sh | \
sh -s {auto|interactive|debug-auto|debug-interactive} [Agent-Options] [Server-Options] [Zaf-Options] sh -s {auto|interactive|debug-auto|debug-interactive} [Agent-Options] [Zaf-Options]
``` ```
or use git version: or use git version:
``` ```
git clone https://github.com/limosek/zaf.git; cd zaf; git checkout 1.3 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]
./install.sh {auto|interactive|debug-auto|debug-interactive} [Agent-Options] [Server-Options] [Zaf-Options]
Agent-Options: Z_Option=value [...] Agent-Options: Z_Option=value [...]
Server-Options: S_Option=value [...]
Zaf-Options: ZAF_OPT=value [...] Zaf-Options: ZAF_OPT=value [...]
To unset Agent-Option use Z_Option='' 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*! 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. You can see all interesting ZAF options if you use install interactive command. Next to this, you can pass ANY zaf config options by *ZAF_* prefix. Interresting ZAF options:
Interresting ZAF options: ```
# If we want to use GIT and local GIT repository
ZAF_GIT='1'# Default
Option|Info|Default # Git repository. Can be your local version
------|----|------- ZAF_REPO_GITURL='https://github.com/limosek/zaf-plugins.git'
ZAF_PKG|Packaging system to use|detect
ZAF_OS|Operating system to use|detect # Where to install plugins
ZAF_OS_CODENAME|Operating system codename|detect ZAF_PLUGINS_DIR='/usr/lib/zaf/plugins'
ZAF_AGENT_PKG|Zabbix agent package|detect
ZAF_AGENT_OPTIONS|Zabbix options to set in cfg|empty # Plugins can be downloaded from http[s] too
ZAF_GIT|Git is installed|detect ZAF_REPO_URL='https://raw.githubusercontent.com/limosek/zaf-plugins/master/'
ZAF_CURL_INSECURE|Insecure curl (accept all certificates)|1
ZAF_TMP_DIR|Tmp directory|/tmp/ ```
ZAF_CACHE_DIR|Cache directory|/tmp/zafc
ZAF_LIB_DIR|Libraries directory|/usr/lib/zaf
ZAF_PLUGINS_DIR|Plugins directory|${ZAF_LIB_DIR}/plugins
ZAF_PROXY|http[s] and ftp proxy used by zaf|empty
ZAF_REPO_URL|Plugins http[s] repository|https://raw.githubusercontent.com/limosek/zaf-plugins/1.3/
ZAF_REPO_DIR|Plugins directory|${ZAF_LIB_DIR}/repo
ZAF_AGENT_CONFIG|Zabbix agent config|/etc/zabbix/zabbix_agentd.conf
ZAF_AGENT_CONFIGD|Zabbix agent config.d|/etc/zabbix/zabbix_agentd.conf.d/
ZAF_AGENT_BIN|Zabbix agent binary|/usr/sbin/zabbix_agentd
ZAF_AGENT_RESTART|Zabbix agent restart cmd|detect
ZAF_SERVER_CONFIG|Zabbix server config|/etc/zabbix/zabbix_server.conf
ZAF_SERVER_CONFIGD|Zabbix server config.d|/etc/zabbix/zabbix_server.conf.d/
ZAF_SERVER_BIN|Zabbix server binary|/usr/sbin/zabbix_server
ZAF_SUDOERSD|Sudo sudoers.d directory|/etc/sudoers.d
ZAF_CROND|Cron.d directory|/etc/cron.d
ZAF_ZBXAPI_URL|Zabbix API url|http://localhost/zabbix/api_jsonrpc.php
ZAF_ZBXAPI_USER|Zabbix API user|zaf
ZAF_ZBXAPI_PASS|Zabbix API password|empty
ZAF_ZBXAPI_AUTHTYPE|Zabbix API authentication type|internal
ZAF_PLUGINS|Plugins to autoinstall|empty
Installer will try to autoguess suitable config options for your system. 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. Remember that on some systems, default zabbix agent config is empty so you *need to* enter essential config options as parameters. 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.
@@ -98,7 +67,7 @@ Now everything was tested on Debian and OpenWrt. If somebody is interrested in,
Suppose you want to autoinstall agent on clean system. You need only curl installed. Everything else is one-cmd process. 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. 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/1.3/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_Server=zabbix.server.local \
Z_ServerActive=zabbix.server.local \ Z_ServerActive=zabbix.server.local \
Z_HostnameItem=system.hostname Z_RefreshActiveChecks=60 \ Z_HostnameItem=system.hostname Z_RefreshActiveChecks=60 \
@@ -110,15 +79,16 @@ You can make your own deb package with preconfigured option. It is up to you to
``` ```
git clone https://github.com/limosek/zaf.git \ git clone https://github.com/limosek/zaf.git \
&& cd zaf \ && cd zaf \
&& git checkout 1.3 \ && git checkout 1.1 \
&& git clone https://github.com/limosek/zaf-plugins.git \ && git clone https://github.com/limosek/zaf-plugins.git \
&& make deb PLUGINS="./zaf-plugins/fsx" ZAF_PLUGINS="zaf" ZAF_OPTIONS="ZAF_GIT=0" AGENT_OPTIONS="Z_Server=zabbix.server Z_ServerActive=zabbix.server Z_StartAgents=8" && 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.3.deb sudo dpkg -i out/zaf-1.1.deb
``` ```
General usage: General usage:
``` ```
make {deb|ipk|rpm} [ZAF_PLUGINS="plg1 [plg2]" [ZAF_OPTIONS="ZAF_cfg=val ..."] [AGENT_OPTIONS="Z_Server=host ..."] make {deb|ipk|rpm} [PLUGINS="/dir/plg1 [/dir2]...] [IPLUGINS="plg1 [plg2]..."] [ZAF_OPTIONS="ZAF_cfg=val ..."] [AGENT_OPTIONS="Z_Server=host ..."]
ZAF_PLUGINS are embedded into package. Has to be local directories accessible during build. 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
@@ -153,53 +123,19 @@ During plugin installation, zaf will check all dependencies, do install binaries
## Zaf utility ## 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 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 zaf
./zaf Version 1.3. Please use some of this commands: zaf Version 1.1. Please use some of this commands:
./zaf Cmd [ZAF_OPTION=value] [ZAF_CTRL_Option=value] [ZAF_CTRLI_Item_Option=value] ... zaf update To update repo
Plugin manipulation commands: zaf plugins To list installed plugins
./zaf update To update repo (not plugins, similar to apt-get update) zaf show [plugin] To show installed plugins or plugin info
./zaf upgrade To upgrade installed plugins from repo zaf items [plugin] To list all suported items [for plugin]
./zaf install plugin To install plugin zaf test [plugin[.item]] To test [all] suported items by zabbix_agentd [for plugin]
./zaf remove plugin To remove 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
Plugin info commands:
./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]
Plugin diagnostic commands:
./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 precache [plugin[.item]] To precache [all] suported items
Zabbix API commands:
./zaf api To zabbix API functions. See ./zaf api for more info.
Agent config info commands:
./zaf userparms See userparms generated from zaf on stdout
./zaf agent-config Reconfigure zabbix userparms in /etc/zabbix/zabbix_agentd.d
Zaf related commands:
./zaf self-upgrade To self-upgrade zaf
./zaf self-remove To self-remove zaf and its config
./zaf cache-clean To remove all entries from cache
```
Zaf can even communicate with zabbix server using its API. If you set ZAF_ZBXAPI_URL, ZAF_ZBXAPI_USER and ZAF_ZBXAPI_PASS in /etc/zaf.conf, you can use it:
```
./zaf api command [parameters]
get-host-id host Get host id
get-byid-host id [property] Get host property from id. Leave empty property for JSON
get-template-id template Get template id
get-byid-template id [property] Get template property from id. Leave empty property for JSON
get-map-id map Get map id
get-byid-map id [property] Get map property from id. Leave empty property for JSON
get-inventory host [fields] Get inventory fields [or all fields]
export-hosts dir [hg] Backup all hosts [in group hg] (get their config from zabbix and save to dir/hostname.xml)
export-host host Backup host (get config from zabbix to stdout)
import-template {plugin|file} Import template for plugin or from file
export-template name Export template to stdout
export-templates dir Export all templates to dir
``` ```
### Installing plugin ### Installing plugin

22
arch.mk
View File

@@ -1,22 +0,0 @@
# Arch linux specific definitions
ARCH_DIR=tmp/archlinux
ifeq ($(ARCH_PKG),)
ARCH_PKG=$(shell . lib/zaf.lib.sh; echo out/zaf-$$ZAF_VERSION.arch)
endif
$(ARCH_PKG): clean arch-init arch-build
arch-init:
@mkdir -p tmp out $(ARCH_DIR)
@. lib/zaf.lib.sh; \
. lib/ctrl.lib.sh; \
cat files/archlinux/PKGBUILD | zaf_far '{PLUGINS}' "$(PLUGINS)" | zaf_far "{IPLUGINS}" "$(IPLUGINS)" | \
zaf_far "{ZAF_OPTIONS}" "$(ZAF_OPTIONS)" | zaf_far "{AGENT_OPTIONS}" "$(AGENT_OPTIONS)" \
>$(ARCH_DIR)/PKGBUILD
arch-build:
@cd $(ARCH_DIR) && makepkg -f

View File

@@ -1,15 +0,0 @@
#!/bin/sh
. lib/zaf.lib.sh
! [ -d plugins ] && git clone https://github.com/limosek/zaf-plugins.git plugins
make deb DEBIAN_PKG="out/zaf-$ZAF_VERSION-git.deb" \
ZAF_OPTIONS="ZAF_GIT=1 ZAF_REPO_GITURL='https://github.com/limosek/zaf-plugins.git'"
make deb DEBIAN_PKG="out/zaf-$ZAF_VERSION.deb" \
ZAF_OPTIONS="ZAF_GIT=0"
make deb DEBIAN_PKG="out/zaf-$ZAF_VERSION-all.deb" ZAF_OPTIONS="ZAF_GIT=0" \
PLUGINS="./plugins/fsx ./plugins/openssh ./plugins/psx ./plugins/tcqos ./plugins/zaf ./plugins/fail2ban"

57
deb.mk
View File

@@ -1,57 +0,0 @@
# Debian specific config
DEBIAN_DIR=tmp/deb
DEBIAN_CTRL=$(DEBIAN_DIR)/DEBIAN
ifeq ($(DEBIAN_PKG),)
DEBIAN_PKG=$(shell . lib/zaf.lib.sh; echo out/zaf-$$ZAF_VERSION.deb)
endif
$(DEBIAN_PKG): clean deb-init deb-deps deb-control deb-scripts deb-cp deb-package
deb-init:
@mkdir -p tmp out $(DEBIAN_DIR)
deb-deps: $(CONTROLFILES)
@which dpkg >/dev/null && which dpkg-buildpackage >/dev/null && which dch >/dev/null || { echo "You need essential debian developer tools. Please install them:\nsudo apt-get install build-essential devscripts debhelper"; exit 2; }
deb-control:
@mkdir -p $(DEBIAN_CTRL)
@. lib/zaf.lib.sh; \
. lib/ctrl.lib.sh; \
for p in $(PLUGINS); do \
DEPENDS="$$DEPENDS,$$(zaf_ctrl_get_global_option $$p/control.zaf Depends-dpkg | tr ' ' ',')"; \
done; \
[ "$$ZAF_GITBRANCH" = "master" ] && master=master; \
zaf_far '{ZAF_VERSION}' "$${ZAF_VERSION}$$master" <files/debian/control.template | zaf_far '{ZAF_DEPENDS}' "$$DEPENDS" >$(DEBIAN_CTRL)/control
deb-scripts:
@. lib/zaf.lib.sh; \
. lib/ctrl.lib.sh; \
cat files/debian/postinst.template >$(DEBIAN_CTRL)/postinst
@chmod +x $(DEBIAN_CTRL)/postinst
@cp files/debian/preinst.template $(DEBIAN_CTRL)/preinst
@chmod +x $(DEBIAN_CTRL)/preinst
@cp files/debian/prerm.template $(DEBIAN_CTRL)/prerm
@chmod +x $(DEBIAN_CTRL)/prerm
deb-cp:
@mkdir -p $(DEBIAN_DIR)
@set -e; INSTALL_PREFIX=$(DEBIAN_DIR) ZAF_DEBUG=$(ZAF_DEBUG) ./install.sh auto $(ZAF_OPTIONS) ZAF_PLUGINS="$(ZAF_PLUGINS)" $(AGENT_OPTIONS)
@cat lib/*lib.sh install.sh >$(DEBIAN_DIR)/usr/lib/zaf/install.sh
@chmod +x $(DEBIAN_DIR)/usr/lib/zaf/install.sh
@rm -rf $(DEBIAN_DIR)/tmp
@cp $(DEBIAN_DIR)/etc/zaf.conf tmp/zaf.conf
@grep -E "$$(echo $(ZAF_EXPORT_OPTS) | tr ' ' '|')=" tmp/zaf.conf >$(DEBIAN_DIR)/etc/zaf.conf
ifneq ($(AGENT_OPTIONS),)
@echo "ZAF_AGENT_OPTIONS=\"$(AGENT_OPTIONS)\"" >>$(DEBIAN_DIR)/etc/zaf.conf
endif
deb-package:
@dpkg-deb -b $(DEBIAN_DIR) $(DEBIAN_PKG)
@echo "\nCheck configuration:"
@cat $(DEBIAN_DIR)/etc/zaf.conf
@echo PLUGINS embedded: $(ZAF_PLUGINS)
@echo

View File

@@ -1,39 +0,0 @@
pkgname="zaf-git"
pkgver=${PKGVER:-autogenerated}
pkgrel=${PKGREL:-1}
pkgdesc="zabbix agent framework - a cli tool for zabbix"
arch=("i686" "x86_64")
license=("GPL")
depends=(
"lsb-release"
"git"
)
backup=(
"etc/zaf.conf"
)
source=(
"zaf-git::git://github.com/limosek/zaf#branch=master"
)
md5sums=("SKIP")
pkgver() {
cd "$srcdir/$pkgname"
local date=$(git log -1 --format="%cd" --date=short | sed s/-//g)
local count=$(git rev-list --count HEAD)
local commit=$(git rev-parse --short HEAD)
echo "$date.${count}_$commit"
}
build() {
cd "$srcdir/$pkgname"
:
}
package() {
cd "$srcdir/$pkgname"
INSTALL_PREFIX="$pkgdir" \
./install.sh auto {ZAF_OPTIONS} {AGENT_OPTIONS}
}

View File

@@ -1,10 +0,0 @@
#!/bin/sh
case $1 in
configure)
zaf agent-config
;;
esac
true

17
files/postinst.template Normal file
View File

@@ -0,0 +1,17 @@
#!/bin/sh
case $1 in
configure)
if [ -f /usr/lib/zaf/install.sh ] && [ -f /etc/zaf.conf ] && [ -f /usr/lib/zaf/zaf.lib.sh ] && which zaf >/dev/null; then
. /etc/zaf.conf
. /usr/lib/zaf/zaf.lib.sh
. /usr/lib/zaf/os.lib.sh
. /usr/lib/zaf/ctrl.lib.sh
cd /usr/lib/zaf && /usr/lib/zaf/install.sh reconf
[ -n "{PLUGINS}" ] && zaf reinstall {PLUGINS} {IPLUGINS}
fi
;;
esac
true

View File

@@ -5,50 +5,58 @@ if [ -z "$ZAF_URL" ]; then
# Runing as standalone install.sh. We have to download rest of files first. # Runing as standalone install.sh. We have to download rest of files first.
ZAF_URL="https://github.com/limosek/zaf/" ZAF_URL="https://github.com/limosek/zaf/"
fi fi
if [ -z "$ZAF_RAW_URL" ]; then
ZAF_RAW_URL="https://raw.githubusercontent.com/limosek/zaf"
fi
[ -z "$ZAF_GITBRANCH" ] && ZAF_GITBRANCH=1.3 [ -z "$ZAF_GITBRANCH" ] && ZAF_GITBRANCH=1.1
############### Functions
# Lite version of zaf_fetch_url, full version will be loaded later # Lite version of zaf_fetch_url, full version will be loaded later
zaf_fetch_url(){ zaf_fetch_url(){
if zaf_which curl >/dev/null 2>/dev/null; then if [ -z "$ZAF_OFFLINE" ]; then
echo curl -f -k -s -L -o - "$1" >&2; echo curl -f -k -s -L -o - "$1" >&2; curl -f -k -s -L -o - "$1"
curl -f -k -s -L -o - "$1"
else else
wget --no-check-certificate -O - "$1" echo "Offline mode wants to download $1. Exiting." >&2
exit 2
fi fi
} }
# Lite version of zaf_which, full version will be loaded later # Download tgz and extract to /tmp/zaf-installer
zaf_which() {
if which >/dev/null 2>/dev/null; then
which "$1"
else
for i in /bin /sbin /usr/bin /usr/sbin /usr/local/bin /usr/local/sbin; do
[ -x $i/$1 ] && { echo $i/$1; return; }
done
return 1
fi
}
# Lite version of zaf_err, full version will be loaded later
zaf_err() {
logger ${ZAF_LOG_STDERR} -p user.err -t zaf-error -- $@
logger ${ZAF_LOG_STDERR} -p user.err -t zaf-error "Exiting with error!"
exit 1
}
# Download tgz and extract to tmpdir
zaf_download_files() { zaf_download_files() {
[ -z $ZAF_DIR ] && zaf_err "ZAF_DIR not set!" rm -rf /tmp/zaf-installer
rm -rf $ZAF_DIR zaf_fetch_url $ZAF_URL/archive/$ZAF_GITBRANCH.tar.gz | tar -f - -C /tmp -zx && mv /tmp/zaf-$ZAF_GITBRANCH /tmp/zaf-installer
zaf_fetch_url $ZAF_URL/archive/$ZAF_GITBRANCH.tar.gz | tar -f - -C $ZAF_TMP_DIR -zx && mv $ZAF_TMP_DIR/zaf-$ZAF_GITBRANCH $ZAF_DIR || zaf_err "Cannot download and unpack zaf!"
} }
if ! [ -f README.md ]; then
# We are runing from stdin
if ! which curl >/dev/null;
then
zaf_err "Curl not found. Cannot continue. Please install it."
fi
echo "Installing from url $url..."
[ -z "$*" ] && auto=auto
zaf_download_files && cd /tmp/zaf-installer && exec ./install.sh $auto "$@"
echo "Error downloading and runing installer!" >&2
exit 2
fi
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
for pair in "$@"; do
echo $pair | grep -q '^ZAF\_' || continue
option=$(echo $pair|cut -d '=' -f 1)
value=$(echo $pair|cut -d '=' -f 2-)
eval "C_${option}='$value'"
zaf_wrn "Overriding $option from cmdline."
done
[ -z "$ZAF_CFG_FILE" ] && ZAF_CFG_FILE=$INSTALL_PREFIX/etc/zaf.conf
[ -n "$C_ZAF_DEBUG" ] && ZAF_DEBUG=$C_ZAF_DEBUG
# Read option. If it is already set in zaf.conf, it is skipped. If env variable is set, it is used instead of default # 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. # It sets global variable name on result.
# $1 - option name # $1 - option name
@@ -58,7 +66,6 @@ zaf_download_files() {
zaf_get_option(){ zaf_get_option(){
local opt local opt
ZAF_HELP_OPTS="$ZAF_HELP_OPTS\n$1 $2 [$3]"
eval opt=\$C_$1 eval opt=\$C_$1
if [ -n "$opt" ]; then if [ -n "$opt" ]; then
eval "$1='$opt'" eval "$1='$opt'"
@@ -102,73 +109,54 @@ zaf_set_option(){
fi fi
} }
# Set config option in zabbix config file # Set config option in zabbix agent config file
# $1 config file # $1 option
# $2 option # $2 value
# $3 value zaf_set_agent_option() {
zaf_set_zabbix_option() { local option="$1"
local cfgfile="$1" local value="$2"
local option="$2" if grep -q ^$option\= $ZAF_AGENT_CONFIG; then
local value="$3" zaf_dbg "Setting option $option in $ZAF_AGENT_CONFIG."
if grep -q ^$option\= $cfgfile; then sed -i "s/$option=\(.*\)/$option=$2/" $ZAF_AGENT_CONFIG
zaf_dbg "Setting option $option in $cfgfile to $3."
sed -i "s/$option=\(.*\)/$option=$value/" $cfgfile
else else
zaf_move_zabbix_option "$1" "$2" "$3" zaf_move_agent_option "$1" "$2"
fi fi
} }
# Get config option from zabbix config file # Unset config option in zabbix agent config file
# $1 config file # $1 option
# $2 option zaf_unset_agent_option() {
zaf_get_zabbix_option() { local option="$1"
local cfgfile="$1" local value="$2"
local option="$2" if grep -q ^$option\= $ZAF_AGENT_CONFIG; then
grep ^$option\= $cfgfile | cut -d '=' -f 2-; zaf_dbg "Unsetting option $option in $ZAF_AGENT_CONFIG."
} sed -i "s/$option=\(.*\)/#$option=$2/" $ZAF_AGENT_CONFIG
# Unset config option in zabbix config file
# $1 config file
# $2 option
zaf_unset_zabbix_option() {
local cfgfile="$1"
local option="$2"
local value="$3"
if grep -q ^$option\= $cfgfile; then
zaf_dbg "Unsetting option $option in $cfgfile."
sed -i "s/$option=\(.*\)/#$option=$2/" $cfgfile
fi fi
} }
# Add config option in zabbix config file # Add config option in zabbix agent config file
# $1 config file # $1 option
# $2 option # $2 value
# $3 value zaf_add_agent_option() {
zaf_add_zabbix_option() { local option="$1"
local cfgfile="$1" local value="$2"
local option="$2" if ! grep -q "^$1=$2" $ZAF_AGENT_CONFIG; then
local value="$3" zaf_dbg "Adding option $option to $ZAF_AGENT_CONFIG."
if ! grep -q "^$option=$value" $cfgfile; then echo "$option=$value" >>$ZAF_AGENT_CONFIG
zaf_dbg "Adding option $option to $cfgfile."
echo "$option=$value" >>$cfgfile
fi fi
} }
# Move config option fron zabbix config file to zaf options file and set value # Move config option fron zabbix agent config file to zaf options file and set value
# $1 config file # $1 option
# $2 options file # $2 value
# $3 option zaf_move_agent_option() {
# $4 value local option="$1"
zaf_move_zabbix_option() { local value="$2"
local cfgfile="$1" if grep -q ^$option\= $ZAF_AGENT_CONFIG; then
local optsfile="$2" zaf_dbg "Moving option $option from $ZAF_AGENT_CONFIG to ."
local option="$3" sed -i "s/$option=(.*)/$option=$2/" $ZAF_AGENT_CONFIG
local value="$4"
if grep -q ^$option\= $cfgfile; then
zaf_dbg "Moving option $option from $cfgfile to $optsfile."
sed -i "s/$option=(.*)/$option=$value/" $cfgfile
fi fi
[ -n "$value" ] && echo "$option=$value" >> "$optsfile" [ -n "$value" ] && echo "$option=$value" >> "$ZAF_AGENT_CONFIGD/zaf_options.conf"
} }
# Automaticaly configure agent if supported # Automaticaly configure agent if supported
@@ -178,7 +166,6 @@ zaf_configure_agent() {
local option local option
local value local value
local options local options
local changes
zaf_install_dir "$ZAF_AGENT_CONFIGD" zaf_install_dir "$ZAF_AGENT_CONFIGD"
echo -n >"$ZAF_AGENT_CONFIGD/zaf_options.conf" || zaf_err "Cannot access $ZAF_AGENT_CONFIGD/zaf_options.conf" echo -n >"$ZAF_AGENT_CONFIGD/zaf_options.conf" || zaf_err "Cannot access $ZAF_AGENT_CONFIGD/zaf_options.conf"
@@ -188,48 +175,18 @@ zaf_configure_agent() {
option=$(echo $pair|cut -d '=' -f 1|cut -d '_' -f 2) option=$(echo $pair|cut -d '=' -f 1|cut -d '_' -f 2)
value=$(echo $pair|cut -d '=' -f 2-) value=$(echo $pair|cut -d '=' -f 2-)
if [ -n "$value" ]; then if [ -n "$value" ]; then
zaf_set_zabbix_option "$ZAF_AGENT_CONFIG" "$option" "$value" zaf_set_agent_option "$option" "$value"
else else
zaf_unset_zabbix_option "$ZAF_AGENT_CONFIG" "$option" zaf_unset_agent_option "$option"
fi fi
options="$options Z_$option=$value" options="$options Z_$option=$value"
changes=1
done done
zaf_set_option ZAF_AGENT_OPTIONS "${options}" zaf_set_option ZAF_AGENT_OPTIONS "${options}"
[ -n "$changes" ] # Return false if no changes
} }
# Automaticaly configure server if supported
# Parameters are in format S_zabbixconfvar=value
zaf_configure_server() {
local pair
local option
local value
local options
local changes
zaf_install_dir "$ZAF_SERVER_CONFIGD"
echo -n >"$ZAF_SERVER_CONFIGD/zaf_options.conf" || zaf_err "Cannot access $ZAF_SERVER_CONFIGD/zaf_options.conf"
for pair in "$@"; do
echo $pair | grep -q '^S\_' || continue # Skip non S_ 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_zabbix_option "$ZAF_SERVER_CONFIG" "$option" "$value"
else
zaf_unset_zabbix_option "$ZAF_SERVER_CONFIG" "$option"
fi
options="$options S_$option=$value"
changes=1
done
zaf_set_option ZAF_SERVER_OPTIONS "${options}"
[ -n "$changes" ] # Return false if no changes
}
zaf_preconfigure(){ zaf_preconfigure(){
[ -z "$ZAF_OS" ] && zaf_detect_system zaf_detect_system
zaf_os_specific zaf_preconfigure_os zaf_os_specific zaf_configure_os
if ! zaf_is_root; then if ! zaf_is_root; then
[ -z "$INSTALL_PREFIX" ] && zaf_err "We are not root. Use INSTALL_PREFIX or become root." [ -z "$INSTALL_PREFIX" ] && zaf_err "We are not root. Use INSTALL_PREFIX or become root."
else else
@@ -251,43 +208,31 @@ zaf_configure(){
fi fi
fi fi
fi fi
if [ -z "$ZAF_GIT" ]; then if which git >/dev/null; then
if zaf_which git >/dev/null; then ZAF_GIT=1
ZAF_GIT=1 else
else ZAF_GIT=0
ZAF_GIT=0
fi
fi fi
zaf_get_option ZAF_GIT "Git is installed" "$ZAF_GIT" "$INSTALL_MODE" 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_CURL_INSECURE "Insecure curl (accept all certificates)" "1" "$INSTALL_MODE"
zaf_get_option ZAF_TMP_DIR "Tmp directory" "/tmp/" "$INSTALL_MODE" zaf_get_option ZAF_TMP_BASE "Tmp directory prefix (\$USER will be added)" "/tmp/zaf" "$INSTALL_MODE"
zaf_get_option ZAF_CACHE_DIR "Cache directory" "/tmp/zafc" "$INSTALL_MODE"
zaf_get_option ZAF_LIB_DIR "Libraries directory" "/usr/lib/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_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_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_GIT}" = 1 ] && zaf_get_option ZAF_REPO_GITURL "Git plugins repository" "https://github.com/limosek/zaf-plugins.git" "$INSTALL_MODE"
zaf_get_option ZAF_PROXY "http proxy used by zaf" "" "$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_URL "Plugins http[s] repository" "https://raw.githubusercontent.com/limosek/zaf-plugins/${ZAF_VERSION}/" "$INSTALL_MODE"
zaf_get_option ZAF_REPO_DIR "Plugins directory" "${ZAF_LIB_DIR}/repo" "$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" zaf_get_option ZAF_AGENT_CONFIG "Zabbix agent config" "/etc/zabbix/zabbix_agentd.conf" "$INSTALL_MODE"
[ -z "${ZAF_AGENT_CONFIGD}" ] && ! [ -d "${ZAF_AGENT_CONFIGD}" ] && [ -d "/etc/zabbix/zabbix_agentd.d" ] && ZAF_AGENT_CONFIGD="/etc/zabbix/zabbix_agentd.d" ! [ -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/" "$INSTALL_MODE" 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_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_AGENT_RESTART "Zabbix agent restart cmd" "service zabbix-agent restart" "$INSTALL_MODE"
zaf_get_option ZAF_SERVER_CONFIG "Zabbix server config" "/etc/zabbix/zabbix_server.conf" "$INSTALL_MODE"
[ -z "${ZAF_SERVER_CONFIGD}" ] && ! [ -d "${ZAF_SERVER_CONFIGD}" ] && [ -d "/etc/zabbix/zabbix_server.d" ] && ZAF_SERVER_CONFIGD="/etc/zabbix/zabbix_server.d"
zaf_get_option ZAF_SERVER_CONFIGD "Zabbix server config.d" "/etc/zabbix/zabbix_server.conf.d/" "$INSTALL_MODE"
zaf_get_option ZAF_SERVER_BIN "Zabbix server binary" "/usr/sbin/zabbix_server" "$INSTALL_MODE"
zaf_get_option ZAF_SUDOERSD "Sudo sudoers.d directory" "/etc/sudoers.d" "$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_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_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_USER "Zabbix API user" "zaf" "$INSTALL_MODE"
zaf_get_option ZAF_ZBXAPI_PASS "Zabbix API password" "" "$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" zaf_get_option ZAF_ZBXAPI_AUTHTYPE "Zabbix API authentication type" "internal" "$INSTALL_MODE"
zaf_get_option ZAF_PLUGINS "Plugins to autoinstall" "" "$INSTALL_MODE"
if zaf_is_root && ! [ -x $ZAF_AGENT_BIN ]; then 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." zaf_err "Zabbix agent ($ZAF_AGENT_BIN) not installed? Use ZAF_AGENT_BIN env variable to specify location. Exiting."
@@ -304,78 +249,44 @@ zaf_configure(){
zaf_set_option ZAF_AGENT_PKG "${ZAF_AGENT_PKG}" zaf_set_option ZAF_AGENT_PKG "${ZAF_AGENT_PKG}"
zaf_set_option ZAF_GIT "${ZAF_GIT}" zaf_set_option ZAF_GIT "${ZAF_GIT}"
zaf_set_option ZAF_CURL_INSECURE "${ZAF_CURL_INSECURE}" zaf_set_option ZAF_CURL_INSECURE "${ZAF_CURL_INSECURE}"
zaf_set_option ZAF_TMP_DIR "$ZAF_TMP_DIR" zaf_set_option ZAF_TMP_BASE "$ZAF_TMP_BASE"
zaf_set_option ZAF_CACHE_DIR "$ZAF_CACHE_DIR"
zaf_set_option ZAF_LIB_DIR "$ZAF_LIB_DIR" zaf_set_option ZAF_LIB_DIR "$ZAF_LIB_DIR"
zaf_set_option ZAF_BIN_DIR "$ZAF_BIN_DIR" zaf_set_option ZAF_BIN_DIR "$ZAF_BIN_DIR"
zaf_set_option ZAF_PLUGINS_DIR "$ZAF_PLUGINS_DIR" zaf_set_option ZAF_PLUGINS_DIR "$ZAF_PLUGINS_DIR"
zaf_set_option ZAF_REPO_URL "$ZAF_REPO_URL" zaf_set_option ZAF_REPO_URL "$ZAF_REPO_URL"
zaf_set_option ZAF_PROXY "$ZAF_PROXY"
[ "${ZAF_GIT}" = 1 ] && zaf_set_option ZAF_REPO_GITURL "$ZAF_REPO_GITURL" [ "${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_REPO_DIR "$ZAF_REPO_DIR"
zaf_set_option ZAF_AGENT_CONFIG "$ZAF_AGENT_CONFIG" zaf_set_option ZAF_AGENT_CONFIG "$ZAF_AGENT_CONFIG"
zaf_set_option ZAF_AGENT_CONFIGD "$ZAF_AGENT_CONFIGD" zaf_set_option ZAF_AGENT_CONFIGD "$ZAF_AGENT_CONFIGD"
zaf_set_option ZAF_AGENT_BIN "$ZAF_AGENT_BIN" zaf_set_option ZAF_AGENT_BIN "$ZAF_AGENT_BIN"
zaf_set_option ZAF_FILES_UID "$ZAF_FILES_UID"
zaf_set_option ZAF_FILES_GID "$ZAF_FILES_GID"
zaf_set_option ZAF_FILES_UMASK "$ZAF_FILES_UMASK"
zaf_set_option ZAF_AGENT_RESTART "$ZAF_AGENT_RESTART" zaf_set_option ZAF_AGENT_RESTART "$ZAF_AGENT_RESTART"
if [ -x "$ZAF_SERVER_BIN" ]; then
zaf_set_option ZAF_SERVER_CONFIG "$ZAF_SERVER_CONFIG"
zaf_set_option ZAF_SERVER_CONFIGD "$ZAF_SERVER_CONFIGD"
zaf_set_option ZAF_SERVER_EXTSCRIPTS "$(zaf_get_zabbix_option $ZAF_SERVER_CONFIG ExternalScripts)"
zaf_set_option ZAF_SERVER_BIN "$ZAF_SERVER_BIN"
else
zaf_set_option ZAF_SERVER_CONFIG ""
zaf_set_option ZAF_SERVER_BIN ""
zaf_set_option ZAF_SERVER_CONFIGD ""
zaf_set_option ZAF_SERVER_EXTSCRIPTS ""
fi
zaf_set_option ZAF_SUDOERSD "$ZAF_SUDOERSD" zaf_set_option ZAF_SUDOERSD "$ZAF_SUDOERSD"
zaf_set_option ZAF_CROND "$ZAF_CROND" zaf_set_option ZAF_CROND "$ZAF_CROND"
zaf_set_option ZAF_ZBXAPI_URL "$ZAF_ZBXAPI_URL" zaf_set_option ZAF_ZBXAPI_URL "$ZAF_ZBXAPI_URL"
zaf_set_option ZAF_ZBXAPI_USER "$ZAF_ZBXAPI_USER" zaf_set_option ZAF_ZBXAPI_USER "$ZAF_ZBXAPI_USER"
zaf_set_option ZAF_ZBXAPI_PASS "$ZAF_ZBXAPI_PASS" zaf_set_option ZAF_ZBXAPI_PASS "$ZAF_ZBXAPI_PASS"
zaf_set_option ZAF_ZBXAPI_AUTHTYPE "$ZAF_ZBXAPI_AUTHTYPE" zaf_set_option ZAF_ZBXAPI_AUTHTYPE "$ZAF_ZBXAPI_AUTHTYPE"
if [ -n "$ZAF_PLUGINS" ]; then [ -n "$ZAF_PREPACKAGED_DIR" ] && zaf_set_option ZAF_PREPACKAGED_DIR "$ZAF_PREPACKAGED_DIR"
for p in $(echo $ZAF_PLUGINS | tr ',' ' '); do
zaf_install_plugin $p
done
fi
if zaf_is_root; then if zaf_is_root; then
zaf_configure_agent $ZAF_AGENT_OPTIONS "$@" zaf_configure_agent $ZAF_AGENT_OPTIONS "$@"
zaf_add_zabbix_option "$ZAF_AGENT_CONFIG" "Include" "$ZAF_AGENT_CONFIGD" zaf_add_agent_option "Include" "$ZAF_AGENT_CONFIGD"
if [ -f "$ZAF_SERVER_BIN" ]; then
zaf_configure_server $ZAF_SERVER_OPTIONS "$@" && zaf_add_zabbix_option "$ZAF_SERVER_CONFIG" "Include" "$ZAF_SERVER_CONFIGD"
else
zaf_wrn "Skipping server config. Zabbix server binary '$ZAF_SERVER_BIN' not found."
fi
fi fi
if ! [ -d $ZAF_CACHE_DIR ]; then
mkdir -p "$ZAF_CACHE_DIR"
if zaf_is_root && [ -n "$ZAF_FILES_UID" ] && [ -n "$ZAF_FILES_GID" ]; then
zaf_wrn "Cache: Changing perms to $ZAF_CACHE_DIR (zabbix/$ZAF_ZABBIX_GID/0770)"
chown $ZAF_FILES_UID "$ZAF_CACHE_DIR"
chgrp $ZAF_FILES_GID "$ZAF_CACHE_DIR"
chmod $ZAF_FILES_UMASK "$ZAF_CACHE_DIR"
fi
fi
zaf_cache_init
} }
zaf_install_all() { zaf_install_all() {
rm -rif ${ZAF_TMP_DIR}
mkdir -p ${ZAF_TMP_DIR}
zaf_install_dir ${ZAF_LIB_DIR} zaf_install_dir ${ZAF_LIB_DIR}
for i in lib/zaf.lib.sh lib/plugin.lib.sh lib/os.lib.sh lib/ctrl.lib.sh lib/cache.lib.sh lib/zbxapi.lib.sh lib/JSON.sh; do 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_install $i ${ZAF_LIB_DIR}/ || zaf_err "Error installing $i"
done done
for i in lib/zaflock lib/zafcache lib/preload.sh; do 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 done
zaf_install_dir ${ZAF_BIN_DIR} zaf_install_dir ${ZAF_BIN_DIR}
for i in zaf; do 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 done
zaf_install_dir ${ZAF_PLUGINS_DIR} zaf_install_dir ${ZAF_PLUGINS_DIR}
zaf_install_dir ${ZAF_PLUGINS_DIR} zaf_install_dir ${ZAF_PLUGINS_DIR}
@@ -384,11 +295,8 @@ zaf_install_all() {
zaf_postconfigure() { zaf_postconfigure() {
if zaf_is_root; then if zaf_is_root; then
${INSTALL_PREFIX}/${ZAF_BIN_DIR}/zaf cache-clean
[ "${ZAF_GIT}" = 1 ] && ${INSTALL_PREFIX}/${ZAF_BIN_DIR}/zaf update [ "${ZAF_GIT}" = 1 ] && ${INSTALL_PREFIX}/${ZAF_BIN_DIR}/zaf update
${INSTALL_PREFIX}/${ZAF_BIN_DIR}/zaf reinstall zaf || zaf_err "Error installing zaf plugin." ${INSTALL_PREFIX}/${ZAF_BIN_DIR}/zaf reinstall zaf || zaf_err "Error installing zaf plugin."
${INSTALL_PREFIX}/${ZAF_BIN_DIR}/zaf agent-config || zaf_err "Error configuring agent."
zaf_os_specific zaf_postconfigure_os
if zaf_is_root && ! zaf_test_item zaf.framework_version; then if zaf_is_root && ! zaf_test_item zaf.framework_version; then
echo "Something is wrong with zabbix agent config." echo "Something is wrong with zabbix agent config."
echo "Ensure that zabbix_agentd reads ${ZAF_AGENT_CONFIG}" echo "Ensure that zabbix_agentd reads ${ZAF_AGENT_CONFIG}"
@@ -403,70 +311,17 @@ zaf_postconfigure() {
true true
} }
############ First stage Init
if ! [ -f README.md ]; then
# Hardcoded variables
ZAF_VERSION="1.3"
export ZAF_TMP_DIR="/tmp/zaf-installer"
export ZAF_DIR="$ZAF_TMP_DIR/zaf"
if [ -n "$ZAF_PROXY" ]; then
export ALL_PROXY="$ZAF_PROXY"
export http_proxy="$ZAF_PROXY"
export https_proxy="$ZAF_PROXY"
fi
mkdir -p $ZAF_TMP_DIR
if ! zaf_which curl >/dev/null && ! zaf_which wget >/dev/null;
then
zaf_err "Curl or wget not found. Cannot continue. Please install it."
fi
echo "Installing from url $url..."
[ -z "$*" ] && auto=auto
zaf_download_files && cd $ZAF_DIR && exec ./install.sh $auto "$@"
echo "Error downloading and runing installer!" >&2
exit 2
fi
# Try to load local downloaded libs
if ! type zaf_version >/dev/null 2>/dev/null; then
. lib/zaf.lib.sh
. lib/plugin.lib.sh
. lib/os.lib.sh
. lib/ctrl.lib.sh
. lib/cache.lib.sh
. lib/zbxapi.lib.sh
fi
# If something was wrong reading libs, then exit
if ! type zaf_version >/dev/null; then
echo "Problem loading libraries?"
exit 2
fi
########### Second stage init (all functions loaded)
[ -z "$ZAF_CFG_FILE" ] && ZAF_CFG_FILE=$INSTALL_PREFIX/etc/zaf.conf
if [ -f "${ZAF_CFG_FILE}" ]; then if [ -f "${ZAF_CFG_FILE}" ]; then
. "${ZAF_CFG_FILE}" . "${ZAF_CFG_FILE}"
fi fi
export ZAF_TMP_DIR="/tmp/zaf-installer" ZAF_TMP_DIR="/tmp/zaf-installer-tmp/"
export ZAF_DIR="$ZAF_TMP_DIR/zaf"
! [ -d $ZAF_TMP_DIR ] && mkdir -p $ZAF_TMP_DIR # If debug is on, do not remove tmp dir
zaf_debug_init stderr if [ "$ZAF_DEBUG" -le 3 ]; then
zaf_tmp_init trap "rm -rif ${ZAF_TMP_DIR}" EXIT
trap "rm -rif /tmp/zaf-installer" EXIT
# Read options as config for ZAF
for pair in "$@"; do
echo $pair | grep -q '^ZAF\_' || continue
option=$(echo $pair|cut -d '=' -f 1)
value=$(echo $pair|cut -d '=' -f 2-)
eval "C_${option}='$value'"
zaf_wrn "Overriding $option from cmdline."
done
[ -z "$C_ZAF_TMP_DIR" ] && C_ZAF_TMP_DIR="/tmp/"
if [ -n "$ZAF_PROXY" ]; then
export ALL_PROXY="$ZAF_PROXY"
fi fi
! [ -d "${ZAF_TMP_DIR}" ] && mkdir "${ZAF_TMP_DIR}"
case $1 in case $1 in
interactive) interactive)
@@ -533,16 +388,14 @@ install)
echo "install.sh {auto|interactive|debug-auto|debug-interactive|reconf} [Agent-Options] [Zaf-Options]" 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 "scratch means that config file will be created from scratch"
echo " Agent-Options: Z_Option=value [...]" echo " Agent-Options: Z_Option=value [...]"
echo " Server-Options: S_Option=value [...]"
echo " Zaf-Options: ZAF_OPT=value [...]" echo " Zaf-Options: ZAF_OPT=value [...]"
echo " To unset Agent-Option use Z_Option=''" echo " To unset Agent-Option use Z_Option=''"
echo echo
echo "Example 1 (default install): install.sh auto" echo "Example 1 (default install): install.sh auto"
echo 'Example 2 (preconfigure agent options): install.sh auto Z_Server=zabbix.server Z_ServerActive=zabbix.server Z_Hostname=$(hostname)' echo 'Example 2 (preconfigure agent options): install.sh auto A_Server=zabbix.server A_ServerActive=zabbix.server A_Hostname=$(hostname)'
echo 'Example 3 (preconfigure server options): install.sh auto S_StartPollers=10 S_ListenPort=10051' echo "Example 3 (preconfigure zaf packaging system to use): install.sh auto ZAF_PKG=opkg"
echo "Example 4 (preconfigure zaf packaging system to use): install.sh auto ZAF_PKG=opkg" echo "Example 4 (interactive): install.sh interactive"
echo "Example 5 (interactive): install.sh interactive" echo
echo
exit 1 exit 1
esac esac

10
ipk.mk
View File

@@ -1,10 +0,0 @@
# Makefile for generating openwrt ipk packages
# Contrinutions welcome :)
ifeq ($(IPK_PKG),)
IPK_PKG=$(shell . lib/zaf.lib.sh; echo out/zaf-$$ZAF_VERSION.ipk)
endif
$(IPK_PKG):
@echo "Not supported yet. Contributions welcomed! :) "; exit 2

View File

@@ -1,208 +0,0 @@
#!/bin/sh
throw() {
echo "$*" >&2
exit 1
}
BRIEF=0
LEAFONLY=0
PRUNE=0
NO_HEAD=0
NORMALIZE_SOLIDUS=0
usage() {
echo
echo "Usage: JSON.sh [-b] [-l] [-p] [-s] [-h]"
echo
echo "-p - Prune empty. Exclude fields with empty values."
echo "-l - Leaf only. Only show leaf nodes, which stops data duplication."
echo "-b - Brief. Combines 'Leaf only' and 'Prune empty' options."
echo "-n - No-head. Do not show nodes that have no path (lines that start with [])."
echo "-s - Remove escaping of the solidus symbol (stright slash)."
echo "-h - This help text."
echo
}
parse_options() {
set -- "$@"
local ARGN=$#
while [ "$ARGN" -ne 0 ]
do
case $1 in
-h) usage
exit 0
;;
-b) BRIEF=1
LEAFONLY=1
PRUNE=1
;;
-l) LEAFONLY=1
;;
-p) PRUNE=1
;;
-n) NO_HEAD=1
;;
-s) NORMALIZE_SOLIDUS=1
;;
?*) echo "ERROR: Unknown option."
usage
exit 0
;;
esac
shift 1
ARGN=$((ARGN-1))
done
}
awk_egrep () {
local pattern_string=$1
gawk '{
while ($0) {
start=match($0, pattern);
token=substr($0, start, RLENGTH);
print token;
$0=substr($0, start+RLENGTH);
}
}' pattern="$pattern_string"
}
tokenize () {
local GREP
local ESCAPE
local CHAR
if echo "test string" | egrep -ao --color=never "test" >/dev/null 2>&1
then
GREP='egrep -ao --color=never'
else
GREP='egrep -ao'
fi
if echo "test string" | egrep -o "test" >/dev/null 2>&1
then
ESCAPE='(\\[^u[:cntrl:]]|\\u[0-9a-fA-F]{4})'
CHAR='[^[:cntrl:]"\\]'
else
GREP=awk_egrep
ESCAPE='(\\\\[^u[:cntrl:]]|\\u[0-9a-fA-F]{4})'
CHAR='[^[:cntrl:]"\\\\]'
fi
local STRING="\"$CHAR*($ESCAPE$CHAR*)*\""
local NUMBER='-?(0|[1-9][0-9]*)([.][0-9]*)?([eE][+-]?[0-9]*)?'
local KEYWORD='null|false|true'
local SPACE='[[:space:]]+'
# Force zsh to expand $A into multiple words
local is_wordsplit_disabled=$(unsetopt 2>/dev/null | grep -c '^shwordsplit$')
if [ $is_wordsplit_disabled != 0 ]; then setopt shwordsplit; fi
$GREP "$STRING|$NUMBER|$KEYWORD|$SPACE|." | egrep -v "^$SPACE$"
if [ $is_wordsplit_disabled != 0 ]; then unsetopt shwordsplit; fi
}
parse_array () {
local index=0
local ary=''
read -r token
case "$token" in
']') ;;
*)
while :
do
parse_value "$1" "$index"
index=$((index+1))
ary="$ary""$value"
read -r token
case "$token" in
']') break ;;
',') ary="$ary," ;;
*) throw "EXPECTED , or ] GOT ${token:-EOF}" ;;
esac
read -r token
done
;;
esac
[ "$BRIEF" -eq 0 ] && value=$(printf '[%s]' "$ary") || value=
:
}
parse_object () {
local key
local obj=''
read -r token
case "$token" in
'}') ;;
*)
while :
do
case "$token" in
'"'*'"') key=$token ;;
*) throw "EXPECTED string GOT ${token:-EOF}" ;;
esac
read -r token
case "$token" in
':') ;;
*) throw "EXPECTED : GOT ${token:-EOF}" ;;
esac
read -r token
parse_value "$1" "$key"
obj="$obj$key:$value"
read -r token
case "$token" in
'}') break ;;
',') obj="$obj," ;;
*) throw "EXPECTED , or } GOT ${token:-EOF}" ;;
esac
read -r token
done
;;
esac
[ "$BRIEF" -eq 0 ] && value=$(printf '{%s}' "$obj") || value=
:
}
parse_value () {
local jpath="${1:+$1,}$2" isleaf=0 isempty=0 print=0
case "$token" in
'{') parse_object "$jpath" ;;
'[') parse_array "$jpath" ;;
# At this point, the only valid single-character tokens are digits.
''|[!0-9]) throw "EXPECTED value GOT ${token:-EOF}" ;;
*) value=$token
# if asked, replace solidus ("\/") in json strings with normalized value: "/"
[ "$NORMALIZE_SOLIDUS" -eq 1 ] && value=$(echo "$value" | sed 's#\\/#/#g')
isleaf=1
[ "$value" = '""' ] && isempty=1
;;
esac
[ "$value" = '' ] && return
[ "$NO_HEAD" -eq 1 ] && [ -z "$jpath" ] && return
[ "$LEAFONLY" -eq 0 ] && [ "$PRUNE" -eq 0 ] && print=1
[ "$LEAFONLY" -eq 1 ] && [ "$isleaf" -eq 1 ] && [ $PRUNE -eq 0 ] && print=1
[ "$LEAFONLY" -eq 0 ] && [ "$PRUNE" -eq 1 ] && [ "$isempty" -eq 0 ] && print=1
[ "$LEAFONLY" -eq 1 ] && [ "$isleaf" -eq 1 ] && \
[ $PRUNE -eq 1 ] && [ $isempty -eq 0 ] && print=1
[ "$print" -eq 1 ] && printf "[%s]\t%s\n" "$jpath" "$value"
:
}
parse () {
read -r token
parse_value
read -r token
case "$token" in
'') ;;
*) throw "EXPECTED EOF GOT $token" ;;
esac
}
if ([ "$0" = "$BASH_SOURCE" ] || ! [ -n "$BASH_SOURCE" ]);
then
parse_options "$@"
tokenize | parse
fi
# vi: expandtab sw=2 ts=2

View File

@@ -1,54 +1,18 @@
# Zaf cache related functions # Zaf cache related functions
zaf_cache_init(){
local files
local file
if [ "$ZAF_CACHE_DIR" = "/tmp/zafc" ] && ! [ -d "$ZAF_CACHE_DIR" ]; then
mkdir -p $ZAF_CACHE_DIR
chown $ZAF_FILES_UID $ZAF_CACHE_DIR >/dev/null 2>/dev/null
fi
if ! [ -w $ZAF_CACHE_DIR ]; then
zaf_dbg "Cache dir $ZAF_CACHE_DIR is not accessible! Disabling cache."
fi
}
zaf_cache_clean(){ zaf_cache_clean(){
local files if [ -n "$ZAF_CACHE_DIR" ]; then
if [ -w "$ZAF_CACHE_DIR" ]; then
zaf_wrn "Removing cache entries" zaf_wrn "Removing cache entries"
files=$(find $ZAF_CACHE_DIR/ -type f) rm -rf "$ZAF_CACHE_DIR"
[ -n "$files" ] && rm -f $files
else else
zaf_dbg "Cache dir not set. Disabling cache." zaf_err "Cache dir not set."
fi fi
zaf_cache_init mkdir -p "$ZAF_CACHE_DIR"
} }
# Get cache key from requested param # Get cache key from requested param
zaf_cache_key(){ zaf_cache_key(){
echo "$1" | (md5sum - ||md5) 2>/dev/null | cut -d ' ' -f 1 echo "$1" | md5sum - | cut -d ' ' -f 1
}
# Wait for lock
# $1 - key
zaf_cache_lock(){
local lockfile
lockfile="${ZAF_CACHE_DIR}/${key}.lock"
[ -f "$lockfile" ] && sleep 1
[ -f "$lockfile" ] && return 1
return 0
}
# Unlock cache key
# $1 - key
zaf_cache_unlock(){
local lockfile
lockfile="${ZAF_CACHE_DIR}/${key}.lock"
rm -f $lockfile
} }
# Put object into cache # Put object into cache
@@ -56,53 +20,34 @@ zaf_cache_unlock(){
# $2 value # $2 value
# $3 lifetime in seconds # $3 lifetime in seconds
zaf_tocache(){ zaf_tocache(){
! [ -w $ZAF_CACHE_DIR ] && return ! [ -w $ZAF_CACHE_DIR ] && return 1
local key local key
local value local value
local expiry
local infofile
local datafile
key=$(zaf_cache_key "$1") key=$(zaf_cache_key "$1")
datafile=${ZAF_CACHE_DIR}/$key echo "$2" >$ZAF_CACHE_DIR/$key
infofile=${ZAF_CACHE_DIR}/${key}.info echo "$1" >$ZAF_CACHE_DIR/$key.info
touch -m -d "$3 seconds" $ZAF_CACHE_DIR/$key.info
zaf_cache_lock "$key" || { zaf_wrn "Cache: Entry $1[$key] locked!"; return 1; } zaf_trc "Cache: Saving entry $1($key)"
rm -f $datafile $infofile
echo "$2" >$datafile
echo "$1" >$infofile
expiry=$(zaf_date_add "$3")
zaf_trc "Cache: Saving entry $1[$key,expiry=$expiry]"
touch -m -d "$expiry" $infofile
zaf_cache_unlock $key
} }
# Put object into cache from stdin and copy to stdout # Put object into cache from stdin and copy to stdout
# $1 key # $1 key
# $2 lifetime in seconds # $2 lifetime in seconds
zaf_tocache_stdin(){ zaf_tocache_stdin(){
! [ -w $ZAF_CACHE_DIR ] && { cat; return; } ! [ -w $ZAF_CACHE_DIR ] && return 1
local key local key
local expiry
local infofile
local datafile
key=$(zaf_cache_key "$1") key=$(zaf_cache_key "$1")
datafile=${ZAF_CACHE_DIR}/$key cat >$ZAF_CACHE_DIR/$key
lockfile=${ZAF_CACHE_DIR}/${key}.lock if [ -s $ZAF_CACHE_DIR/$key ]; then
infofile=${ZAF_CACHE_DIR}/${key}.info zaf_trc "Cache: Saving entry $1($key)"
echo "$1" >$ZAF_CACHE_DIR/$key.info
zaf_cache_lock "$key" || { zaf_wrn "Cache: Entry $1[$key] locked!"; return 1; } touch -m -d "$2 seconds" $ZAF_CACHE_DIR/$key.info
cat $ZAF_CACHE_DIR/$key
rm -f $datafile $infofile else
cat >$datafile rm $ZAF_CACHE_DIR/$key
expiry="$(zaf_date_add $2)" fi
echo "$1 [key=$key,expiry=$expiry]" >$infofile
zaf_trc "Cache: Saving entry $1[key=$key,expiry=$expiry]"
touch -m -d "$expiry" $infofile
zaf_cache_unlock "$key"
cat $datafile
} }
# Remove entry from cache # Remove entry from cache
@@ -112,39 +57,23 @@ zaf_cache_delentry(){
local key local key
key=$(zaf_cache_key "$1") key=$(zaf_cache_key "$1")
zaf_trc "Cache: removing $1($key) from cache" zaf_trc "Cache: removing $1($key) from cache"
rm -f "${ZAF_CACHE_DIR}/$key*" rm -f "$ZAF_CACHE_DIR/$key*"
}
# List entries in cache
zaf_cache_list(){
! [ -w $ZAF_CACHE_DIR ] && return 1
local i
ls ${ZAF_CACHE_DIR}/*info >/dev/null 2>/dev/null || return 1
local key
for i in ${ZAF_CACHE_DIR}/*info; do
cat $i
done
} }
# Get object from cache # Get object from cache
# $1 key # $1 key
zaf_fromcache(){ zaf_fromcache(){
! [ -w $ZAF_CACHE_DIR ] && return 3 ! [ -r $ZAF_CACHE_DIR ] || [ -n "$ZAF_NOCACHE" ] && return 1
local key local key
local value local value
local infofile
local datafile
key=$(zaf_cache_key "$1") key=$(zaf_cache_key "$1")
datafile="${ZAF_CACHE_DIR}/${key}" if [ -f $ZAF_CACHE_DIR/$key ]; then
infofile="${ZAF_CACHE_DIR}/${key}.info" if [ "$ZAF_CACHE_DIR/$key.info" -nt "$ZAF_CACHE_DIR/$key" ]; then
if [ -f $datafile ]; then
if [ "$infofile" -nt "$datafile" ]; then
zaf_trc "Cache: serving $1($key) from cache" zaf_trc "Cache: serving $1($key) from cache"
cat "$datafile" cat $ZAF_CACHE_DIR/$key
else else
zaf_cache_delentry $key zaf_trc "Cache: removing old entry $1"
rm -f "$ZAF_CACHE_DIR/$key*"
return 2 return 2
fi fi
else else
@@ -153,4 +82,3 @@ zaf_fromcache(){
fi fi
} }

View File

@@ -5,29 +5,10 @@ zaf_ctrl_get_items() {
grep '^Item ' | cut -d ' ' -f 2 | cut -d ':' -f 1 | tr '\r\n' ' ' grep '^Item ' | cut -d ' ' -f 2 | cut -d ':' -f 1 | tr '\r\n' ' '
} }
# Get external item list from control on stdin
zaf_ctrl_get_extitems() {
grep '^ExtItem ' | cut -d ' ' -f 2 | cut -d ':' -f 1 | tr '\r\n' ' '
}
# Get external item body from stdin
# $1 itemname
zaf_ctrl_get_extitem_block() {
grep -v '^#' | awk '/^ExtItem '$1'/ { i=0;
while (i==0) {
getline;
if (/^\/ExtItem/) exit;
print $0;
}};
END {
exit i==0;
}'
}
# Get item body from stdin # Get item body from stdin
# $1 itemname # $1 itemname
zaf_ctrl_get_item_block() { zaf_ctrl_get_item_block() {
grep -vE '^#[a-zA-Z ]' | awk '/^Item '$1'/ { i=0; grep -v '^#' | awk '/^Item '$1'/ { i=0;
while (i==0) { while (i==0) {
getline; getline;
if (/^\/Item/) exit; if (/^\/Item/) exit;
@@ -41,10 +22,10 @@ zaf_ctrl_get_item_block() {
# Get global plugin block body from stdin # Get global plugin block body from stdin
# $1 itemname # $1 itemname
zaf_ctrl_get_global_block() { zaf_ctrl_get_global_block() {
grep -vE '^#[a-zA-Z ]' | awk '{ i=0; print $0; grep -v '^#' | awk '{ i=0; print $0;
while (i==0) { while (i==0) {
getline; getline;
if (/^(Item |ExtItem)/) exit; if (/^Item /) exit;
print $0; print $0;
}}' }}'
} }
@@ -52,7 +33,7 @@ zaf_ctrl_get_global_block() {
# Get item multiline option # Get item multiline option
# $1 optionname # $1 optionname
zaf_block_get_moption() { zaf_block_get_moption() {
awk '/^'$1'::$/ { i=0; if (!/::/) print $0; awk '/^'$1'::$/ { i=0; print $0;
while (i==0) { while (i==0) {
getline; getline;
if (/^::$/) {i=1; continue;}; if (/^::$/) {i=1; continue;};
@@ -76,7 +57,7 @@ zaf_block_get_option() {
zaf_ctrl_get_global_option() { zaf_ctrl_get_global_option() {
local ctrlvar local ctrlvar
local ctrlopt local ctrlopt
ctrlopt="ZAF_CTRL_$(zaf_stripctrl $2)" ctrlopt="ZAF_CTRL_$(zaf_stripctrl $2)"
eval ctrlvar=\$$ctrlopt eval ctrlvar=\$$ctrlopt
if [ -n "$ctrlvar" ]; then if [ -n "$ctrlvar" ]; then
@@ -87,7 +68,6 @@ zaf_ctrl_get_global_option() {
|| zaf_ctrl_get_global_block <$1 | zaf_block_get_option "$2" || zaf_ctrl_get_global_block <$1 | zaf_block_get_option "$2"
fi fi
} }
# Get item specific option (single or multiline) # Get item specific option (single or multiline)
# $1 - control file # $1 - control file
# $2 - item name # $2 - item name
@@ -104,41 +84,19 @@ zaf_ctrl_get_item_option() {
else else
zaf_ctrl_get_item_block <$1 "$2" | zaf_block_get_moption "$3" \ zaf_ctrl_get_item_block <$1 "$2" | zaf_block_get_moption "$3" \
|| zaf_ctrl_get_item_block <$1 "$2" | zaf_block_get_option "$3" || zaf_ctrl_get_item_block <$1 "$2" | zaf_block_get_option "$3"
fi
}
# Get external item specific option (single or multiline)
# $1 - control file
# $2 - item name
# $3 - option name
zaf_ctrl_get_extitem_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_extitem_block <$1 "$2" | zaf_block_get_moption "$3" \
|| zaf_ctrl_get_extitem_block <$1 "$2" | zaf_block_get_option "$3"
fi fi
} }
# Check dependencies based on control file # Check dependencies based on control file
zaf_ctrl_check_deps() { zaf_ctrl_check_deps() {
local deps local deps
if [ -n "$ZAF_PKG" ]; then deps=$(zaf_ctrl_get_global_block <$1 | zaf_block_get_option "Depends-${ZAF_PKG}" )
deps=$(zaf_ctrl_get_global_block <$1 | zaf_block_get_option "Depends-${ZAF_PKG}" ) if ! zaf_os_specific zaf_check_deps $deps; then
zaf_err "Missing one of dependend system packages: $deps"
if ! zaf_os_specific zaf_check_deps $deps; then
zaf_err "Missing one of dependend system packages: $deps"
fi
fi fi
deps=$(zaf_ctrl_get_global_block <$1 | zaf_block_get_option "Depends-bin" ) deps=$(zaf_ctrl_get_global_block <$1 | zaf_block_get_option "Depends-bin" )
for cmd in $deps; do for cmd in $deps; do
if ! zaf_which $cmd >/dev/null; then if ! which $cmd >/dev/null; then
zaf_err "Missing binary dependency $cmd. Please install it first." zaf_err "Missing binary dependency $cmd. Please install it first."
fi fi
done done
@@ -157,19 +115,16 @@ zaf_ctrl_sudo() {
pdir="$3" pdir="$3"
plugin=$1 plugin=$1
sudo=$(zaf_ctrl_get_global_option $2 "Sudo" | zaf_far '{PLUGINDIR}' "${plugindir}")
[ -z "$sudo" ] || [ -z "$ZAF_SUDOERSD" ] && return
! [ -d "$ZAF_SUDOERSD" ] && { zaf_wrn "$ZAF_SUDOERSD nonexistent! Skipping sudo install!"; return 1; }
zaf_dbg "Installing sudoers entry $ZAF_SUDOERSD/zaf_$plugin" 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 [ -z "$sudo" ] && return # Nothing to install
if ! zaf_which sudo >/dev/null; then if ! which sudo >/dev/null; then
zaf_wrn "Sudo needed bud not installed?" zaf_wrn "Sudo needed bud not installed?"
fi fi
cmd=$(echo $sudo | cut -d ' ' -f 1) cmd=$(echo $sudo | cut -d ' ' -f 1)
parms=$(echo $sudo | cut -d ' ' -f 2-) parms=$(echo $sudo | cut -d ' ' -f 2-)
if zaf_which $cmd >/dev/null ; then if which $cmd >/dev/null ; then
(echo "zabbix ALL=NOPASSWD:SETENV: $(zaf_which $cmd) $(echo $parms | tr '%' '*')";echo) >$ZAF_SUDOERSD/zaf_$plugin || zaf_err "Error during zaf_ctrl_sudo" (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 chmod 0440 $ZAF_SUDOERSD/zaf_$plugin
else else
zaf_err "Cannot find binary '$cmd' to put into sudoers." zaf_err "Cannot find binary '$cmd' to put into sudoers."
@@ -187,10 +142,8 @@ zaf_ctrl_cron() {
pdir="$3" pdir="$3"
plugin=$1 plugin=$1
cron=$(zaf_ctrl_get_global_option $2 "Cron")
[ -z "$cron" ] || [ -z "$ZAF_CROND" ] && return
! [ -d "$ZAF_CROND" ] && { zaf_wrn "$ZAF_CROND nonexistent! Skipping cron install!"; return 1; }
zaf_dbg "Installing cron entry $ZAF_CROND/zaf_$plugin" zaf_dbg "Installing cron entry $ZAF_CROND/zaf_$plugin"
cron=$(zaf_ctrl_get_global_option $2 "Cron")
[ -z "$cron" ] && return # Nothing to install [ -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" zaf_ctrl_get_global_option $2 "Cron" | zaf_far '{PLUGINDIR}' "${plugindir}" >$ZAF_CROND/zaf_$plugin || zaf_err "Error during zaf_ctrl_cron"
} }
@@ -216,144 +169,67 @@ zaf_ctrl_install() {
done done
files=$(zaf_ctrl_get_global_option $2 "Install-files") files=$(zaf_ctrl_get_global_option $2 "Install-files")
for f in $files; do for f in $files; do
zaf_fetch_url "$1/$f" >"${ZAF_TMP_DIR}/$f" zaf_fetch_url "$1/$b" >"${ZAF_TMP_DIR}/$b"
zaf_install "${ZAF_TMP_DIR}/$f" "$pdir" zaf_install "${ZAF_TMP_DIR}/$b" "$pdir"
done done
script=$(zaf_ctrl_get_global_option $2 "Install-script")
[ -n "$script" ] && eval "$script"
true true
) || zaf_err "Error during zaf_ctrl_install" ) || zaf_err "Error during zaf_ctrl_install"
} }
# Generates zabbix items cfg from control file # Generates zabbix cfg from control file
# $1 control # $1 control
# $2 pluginname # $2 pluginname
zaf_ctrl_generate_items_cfg() { zaf_ctrl_generate_cfg() {
local items local items
local cmd local cmd
local iscript local iscript
local ikey local ikey
local lock local lock
local cache local cache
local tmpfile
local pname
local pdefault
local pregex
local prest
local zafparms
items=$(zaf_ctrl_get_items <"$1") items=$(zaf_ctrl_get_items <"$1")
tmpfile=$ZAF_TMP_DIR/gencfg$$
(set -e (set -e
for i in $items; do for i in $items; do
iscript=$(zaf_stripctrl $i) iscript=$(zaf_stripctrl $i)
zafparams="" params=$(zaf_ctrl_get_item_option $1 $i "Parameters")
zaf_ctrl_get_item_option $1 $i "Parameters" >$tmpfile if [ -n "$params" ]; then
if [ -s "$tmpfile" ]; then
ikey="$2.$i[*]" ikey="$2.$i[*]"
args="" args=""
apos=1; apos=1;
while read pname pdefault pregex prest; do for p in $params; do
zafparams="$zafparams value=\"\$$apos\"; zaf_agentparm $pname $pdefault $pregex; export $pname; "
args="$args \$$apos" args="$args \$$apos"
apos=$(expr $apos + 1) apos=$(expr $apos + 1)
done <$tmpfile done
else else
ikey="$2.$i" ikey="$2.$i"
zafparams=""
args=""
fi fi
env="export ITEM_KEY='$ikey'; export PLUGIN='$2'; export PATH=${ZAF_PLUGINS_DIR}/$2:$ZAF_LIB_DIR:\$PATH; cd ${ZAF_PLUGINS_DIR}/$2; . $ZAF_LIB_DIR/preload.sh; "
lock=$(zaf_ctrl_get_item_option $1 $i "Lock") lock=$(zaf_ctrl_get_item_option $1 $i "Lock")
if [ -n "$lock" ]; then if [ -n "$lock" ]; then
lock="${ZAF_LIB_DIR}/zaflock $lock " lock="${ZAF_LIB_DIR}/zaflock $lock "
fi fi
cache=$(zaf_ctrl_get_item_option $1 $i "Cache") cache=$(zaf_ctrl_get_item_option $1 $i "Cache")
if [ -n "$cache" ]; then if [ -n "$cache" ]; then
cache="${ZAF_LIB_DIR}/zafcache '$cache' " cache="_cache '$cache' "
fi fi
cmd=$(zaf_ctrl_get_item_option $1 $i "Cmd") cmd=$(zaf_ctrl_get_item_option $1 $i "Cmd")
if [ -n "$cmd" ]; then if [ -n "$cmd" ]; then
printf "%s" "UserParameter=$ikey,${env}${zafparams}${preload}${cache}${lock}${cmd}"; echo $(which echo) "UserParameter=$ikey,${ZAF_LIB_DIR}/preload.sh $cache $lock$cmd";
continue continue
fi fi
cmd=$(zaf_ctrl_get_item_option $1 $i "Script") cmd=$(zaf_ctrl_get_item_option $1 $i "Script")
if [ -n "$cmd" ]; then if [ -n "$cmd" ]; then
( echo "#!/bin/sh" zaf_ctrl_get_item_option $1 $i "Script" | \
echo ". $ZAF_LIB_DIR/preload.sh; " 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_ctrl_get_item_option $1 $i "Script" >${ZAF_TMP_DIR}/${iscript}.sh;
) >${ZAF_TMP_DIR}/${iscript}.sh; zaf_install_bin ${ZAF_TMP_DIR}/${iscript}.sh ${ZAF_PLUGINS_DIR}/$2/
[ -z "$3" ] && zaf_install_bin ${ZAF_TMP_DIR}/${iscript}.sh ${ZAF_PLUGINS_DIR}/$2/ $(which echo) "UserParameter=$ikey,$cache $lock${ZAF_PLUGINS_DIR}/$2/${iscript}.sh $args";
printf "%s" "UserParameter=$ikey,${env}${preload}${zafparams}${cache}${lock}${ZAF_PLUGINS_DIR}/$2/${iscript}.sh ${args}"; echo
rm -f ${ZAF_TMP_DIR}/${iscript}.sh
continue; continue;
fi fi
zaf_err "Item $i declared in control file but has no Cmd, Function or Script!" zaf_err "Item $i declared in control file but has no Cmd, Function or Script!"
done done
) || zaf_err "Error during zaf_ctrl_generate_items_cfg" ) || zaf_err "Error during zaf_ctrl_generate_cfg"
rm -f $tmpfile
} }
# Generates zabbix items cfg from control file
# $1 control
# $2 pluginname
zaf_ctrl_generate_extitems_cfg() {
local items
local cmd
local iscript
local ikey
local lock
local cache
local tmpfile
local pname
local pdefault
local pregex
local prest
local zafparms
items=$(zaf_ctrl_get_extitems <"$1")
tmpfile=$ZAF_TMP_DIR/gencfg$$
(set -e
for i in $items; do
iscript=$(zaf_stripctrl $i)
zaf_ctrl_get_extitem_option $1 $i "Parameters" >$tmpfile
ikey="$2.$i"
if [ -s "$tmpfile" ]; then
args=""
apos=1;
while read pname pdefault pregex prest; do
zafparams="$zafparams value=\"\$$apos\"; zaf_agentparm $pname $pdefault $pregex; export $pname; "
args="$args \$$apos"
apos=$(expr $apos + 1)
done <$tmpfile
else
zafparams=""
args=""
fi
env="export ITEM_KEY='$ikey'; export PLUGIN='$2'; export PATH=${ZAF_PLUGINS_DIR}/$2:$ZAF_LIB_DIR:\$PATH; cd ${ZAF_PLUGINS_DIR}/$2; . $ZAF_LIB_DIR/preload.sh; "
lock=$(zaf_ctrl_get_extitem_option $1 $i "Lock")
if [ -n "$lock" ]; then
lock="${ZAF_LIB_DIR}/zaflock $lock "
fi
cache=$(zaf_ctrl_get_extitem_option $1 $i "Cache")
if [ -n "$cache" ]; then
cache="${ZAF_LIB_DIR}/zafcache '$cache' "
fi
cmd=$(zaf_ctrl_get_extitem_option $1 $i "Cmd")
if [ -n "$cmd" ]; then
echo "#!/bin/sh" >${ZAF_SERVER_EXTSCRIPTS}/$ikey
chmod +x ${ZAF_SERVER_EXTSCRIPTS}/$ikey
(printf "%s" "${env}${zafparams}${preload}${cache}${lock}${cmd}"; echo) >>${ZAF_SERVER_EXTSCRIPTS}/$ikey
continue
fi
cmd=$(zaf_ctrl_get_extitem_option $1 $i "Script")
if [ -n "$cmd" ]; then
echo "#!/bin/sh" >${ZAF_SERVER_EXTSCRIPTS}/$ikey
chmod +x ${ZAF_SERVER_EXTSCRIPTS}/$ikey
(printf "%s" "${env}${zafparams}${preload}${cache}${lock}${cmd}"; echo) >>${ZAF_SERVER_EXTSCRIPTS}/$ikey
continue;
fi
zaf_err "External item $i declared in control file but has no Cmd, Function or Script!"
done
) || zaf_err "Error during zaf_ctrl_generate_extitems_cfg"
rm -f $tmpfile
}

View File

@@ -1,71 +1,43 @@
# Os related functions # Os related functions
zaf_preconfigure_os_openwrt() { zaf_configure_os_openwrt() {
ZAF_AGENT_RESTART="/etc/init.d/zabbix_agentd restart" ZAF_AGENT_RESTART="/etc/init.d/zabbix_agentd restart"
ZAF_AGENT_CONFIGD="/var/run/zabbix_agentd.conf.d/" ZAF_AGENT_CONFIGD="/var/run/zabbix_agentd.conf.d/"
ZAF_AGENT_CONFIG="/etc/zabbix_agentd.conf" ZAF_AGENT_CONFIG="/etc/zabbix_agentd.conf"
ZAF_AGENT_PKG="zabbix-agentd" ZAF_AGENT_PKG="zabbix-agentd"
ZAF_CURL_INSECURE=1 ZAF_CURL_INSECURE=1
} }
zaf_preconfigure_os_beesip() {
zaf_preconfigure_os_openwrt
}
zaf_preconfigure_os_freebsd() { zaf_configure_os_freebsd() {
ZAF_AGENT_PKG="zabbix3-agent" ZAF_AGENT_PKG="zabbix3-agent"
ZAF_AGENT_CONFIG="/usr/local/etc/zabbix3/zabbix_agentd.conf" ZAF_AGENT_CONFIG="/usr/local/etc/zabbix3/zabbix_agentd.conf"
ZAF_AGENT_CONFIGD="/usr/local/etc/zabbix3/zabbix_agentd.conf.d/" ZAF_AGENT_CONFIGD="/usr/local/etc/zabbix3/zabbix_agentd.conf.d/"
ZAF_AGENT_BIN="/usr/local/sbin/zabbix_agentd" ZAF_AGENT_BIN="/usr/local/sbin/zabbix_agentd"
ZAF_AGENT_RESTART="service zabbix_agentd restart" ZAF_AGENT_RESTART="service zabbix_agentd restart"
ZAF_SUDOERSD="/usr/local/etc/sudoers.d"
}
zaf_postconfigure_os_openwrt() {
if ! grep -q "zaf agent-config" /etc/init.d/zabbix_agentd; then
sed -i 's/uci2config $NAME/uci2config $NAME; zaf agent-config/' /etc/init.d/zabbix_agentd
fi
}
zaf_postconfigure_os_beesip() {
zaf_postconfigure_os_openwrt
}
zaf_which() {
if which >/dev/null 2>/dev/null; then
which "$1"
else
for i in /bin /sbin /usr/bin /usr/sbin /usr/local/bin /usr/local/sbin; do
[ -x $i/$1 ] && { echo $i/$1; return; }
done
return 1
fi
} }
zaf_detect_system() { zaf_detect_system() {
ZAF_FILES_UID=zabbix if which dpkg >/dev/null; then
ZAF_FILES_GID=zabbix
ZAF_FILES_UMASK=0770
if zaf_which dpkg >/dev/null; then
ZAF_PKG=dpkg ZAF_PKG=dpkg
ZAF_OS=$(lsb_release -is|zaf_tolower) ZAF_OS=$(lsb_release -is|zaf_tolower)
ZAF_OS_CODENAME=$(lsb_release -cs|zaf_tolower) ZAF_OS_CODENAME=$(lsb_release -cs|zaf_tolower)
ZAF_CURL_INSECURE=0 ZAF_CURL_INSECURE=0
ZAF_AGENT_PKG="zabbix-agent" ZAF_AGENT_PKG="zabbix-agent"
return return
else if zaf_which rpm >/dev/null; then else if which rpm >/dev/null; then
ZAF_PKG="rpm" ZAF_PKG="rpm"
ZAF_OS=$(lsb_release -is|zaf_tolower) ZAF_OS=$(lsb_release -is|zaf_tolower)
ZAF_OS_CODENAME=$(lsb_release -cs|zaf_tolower) ZAF_OS_CODENAME=$(lsb_release -cs|zaf_tolower)
ZAF_CURL_INSECURE=0 ZAF_CURL_INSECURE=0
ZAF_AGENT_PKG="zabbix-agent" ZAF_AGENT_PKG="zabbix-agent"
return return
else if zaf_which opkg >/dev/null; then else if which opkg >/dev/null; then
ZAF_PKG="opkg" ZAF_PKG="opkg"
. /etc/openwrt_release . /etc/openwrt_release
ZAF_OS="$(echo $DISTRIB_ID|zaf_tolower)" ZAF_OS="$(echo $DISTRIB_ID|zaf_tolower)"
ZAF_OS_CODENAME="$(echo $DISTRIB_CODENAME|zaf_tolower)" ZAF_OS_CODENAME="$(echo $DISTRIB_CODENAME|zaf_tolower)"
return return
else if zaf_which pkg >/dev/null; then else if which pkg >/dev/null; then
ZAF_PKG="pkg" ZAF_PKG="pkg"
ZAF_OS="freebsd" ZAF_OS="freebsd"
ZAF_OS_CODENAME="$(freebsd-version|cut -d '-' -f 1)" ZAF_OS_CODENAME="$(freebsd-version|cut -d '-' -f 1)"
@@ -102,7 +74,7 @@ zaf_os_specific(){
} }
zaf_is_root(){ zaf_is_root(){
[ "$USER" = "root" ] || [ "$EUID" = "0" ] || [ -n "$ZAF_ISROOT" ] [ "$USER" = "root" ]
} }
# Install file, bin or directory and respect install prefix # Install file, bin or directory and respect install prefix
@@ -110,19 +82,19 @@ zaf_is_root(){
# $2 - directory # $2 - directory
zaf_install(){ zaf_install(){
zaf_dbg "Install file $1 to $INSTALL_PREFIX/$2/$(basename $1)" zaf_dbg "Install file $1 to $INSTALL_PREFIX/$2/$(basename $1)"
$ZAF_DO cp "$1" "$INSTALL_PREFIX/$2/$(basename $1)" || zaf_err "Cannot create file $INSTALL_PREFIX/$2/$(basename $1)!" $ZAF_DO cp "$1" "$INSTALL_PREFIX/$2/$(basename $1)"
} }
# $1 - src file # $1 - src file
# $2 - directory # $2 - directory
zaf_install_bin(){ zaf_install_bin(){
zaf_dbg "Install binary $1 to $INSTALL_PREFIX/$2/$(basename $1)" zaf_dbg "Install binary $1 to $INSTALL_PREFIX/$2/$(basename $1)"
$ZAF_DO cp "$1" "$INSTALL_PREFIX/$2/$(basename $1)" || zaf_err "Cannot create binary $INSTALL_PREFIX/$2/$(basename $1)!" $ZAF_DO cp "$1" "$INSTALL_PREFIX/$2/$(basename $1)"
$ZAF_DO chmod +x "$INSTALL_PREFIX/$2/$(basename $1)" $ZAF_DO chmod +x "$INSTALL_PREFIX/$2/$(basename $1)"
} }
# $1 - directory # $1 - directory
zaf_install_dir(){ zaf_install_dir(){
zaf_dbg "Install directory $1 to $INSTALL_PREFIX/$1" zaf_dbg "Install directory $1 to $INSTALL_PREFIX/$1"
$ZAF_DO mkdir -p "$INSTALL_PREFIX/$1" || zaf_err "Cannot create directory $INSTALL_PREFIX/$1!" $ZAF_DO mkdir -p "$INSTALL_PREFIX/$1"
} }
# $1 - file # $1 - file
zaf_touch(){ zaf_touch(){
@@ -140,6 +112,15 @@ zaf_uninstall(){
fi fi
} }
# Automaticaly install agent on debian
# For another os, create similar function (install_zabbix_centos)
zaf_install_agent_debian() {
zaf_fetch_url "http://repo.zabbix.com/zabbix/3.0/debian/pool/main/z/zabbix-release/zabbix-release_3.0-1+${ZAF_OS_CODENAME}_all.deb" >"/tmp/zaf-installer/zabbix-release_3.0-1+${ZAF_OS_CODENAME}_all.deb" \
&& dpkg -i "/tmp/zaf-installer/zabbix-release_3.0-1+${ZAF_OS_CODENAME}_all.deb" \
&& apt-get update \
&& apt-get install -y -q $ZAF_AGENT_PKG
}
zaf_install_agent_opkg() { zaf_install_agent_opkg() {
opkg update && \ opkg update && \
opkg install $ZAF_AGENT_PKG opkg install $ZAF_AGENT_PKG
@@ -149,7 +130,7 @@ zaf_install_agent_opkg() {
# $* - packages # $* - packages
zaf_check_deps_dpkg() { zaf_check_deps_dpkg() {
for i in $*; do for i in $*; do
dpkg-query -f '${db:Status-Status},${Package}\n' -W $* 2>/dev/null | grep -qE "^(installed|^,)" dpkg-query -f '${Status},${Package}\n' -W $* 2>/dev/null | grep -q "^install ok"
done done
} }

View File

@@ -1,320 +0,0 @@
# Plugin related functions
# Update repo
zaf_update_repo() {
[ "$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_REPO_GITURL}" "${ZAF_REPO_DIR}"
(cd ${ZAF_REPO_DIR} && git pull)
fi
}
# 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
if [ "$(zaf_url_info $1)" = "path" ]; then
url="$1" # plugin with path - from directory
else
if [ "$(zaf_url_info $1)" = "url" ]; then
url="$1" # plugin with http[s] url
else
if [ -n "${ZAF_REPO_URL}" ]; then
url="${ZAF_REPO_URL}/$1"
else
zaf_err "Cannot find plugin $1"
fi
fi
fi
echo $url
}
# $1 - control
zaf_plugin_info() {
local control="$1"
local items
! [ -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)
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}")
peitems=$(zaf_ctrl_get_extitems <"${control}")
echo
echo -n "Plugin '$plugin' "; [ -n "$pversion" ] && echo -n "version ${pversion}"; echo ":"
echo "$pdescription"; echo
[ -n "$pmaintainer" ] && echo "Maintainer: $pmaintainer"
[ -n "$purl" ] && echo "Url: $purl"
[ -n "$phome" ] && echo "Home: $phome"
echo
if zaf_is_plugin "$(basename $plugin)"; then
items=$(zaf_list_plugin_items $plugin)
[ -n "$items" ] && { echo -n "Defined items: "; echo $items; }
items=$(zaf_list_plugin_items $plugin test)
[ -n "$items" ] && { echo -n "Test items: "; echo $items; }
items=$(zaf_list_plugin_items $plugin precache)
[ -n "$items" ] && { echo -n "Precache items: "; echo $items; }
[ -n "$peitems" ] && { echo -n "External check items: "; echo $peitems; }
else
echo "Items: $pitems"
fi
echo
}
# Prepare plugin into dir
# $1 is url, directory or plugin name (will be searched in default plugin dir).
# $2 is directory to prepare.
zaf_prepare_plugin() {
local url
local plugindir
local control
local pluginname
url=$(zaf_get_plugin_url "$1")/control.zaf || exit $?
plugindir="$2"
control=${plugindir}/control.zaf
if [ "$(zaf_url_info $1)" = "path" ] && [ "$url" = "$control" ]; then
zaf_err "prepare_plugin: Cannot install from itself!"
fi
zaf_install_dir "$plugindir"
zaf_dbg "Fetching control file from $url ..."
if zaf_fetch_url "$url" >"${INSTALL_PREFIX}/${control}" && [ -s "${INSTALL_PREFIX}/${control}" ]; then
[ -z "${INSTALL_PREFIX}" ] && zaf_ctrl_check_deps "${control}"
pluginname=$(zaf_ctrl_get_global_block <"${INSTALL_PREFIX}/${control}" | zaf_block_get_option Plugin)
[ "$(basename $plugindir)" != "$pluginname" ] && zaf_err "prepare_plugin: Plugin name mismach ($plugindir vs ${INSTALL_PREFIX}/${control})!"
true
else
rm -rf "$plugindir"
zaf_err "prepare_plugin: Cannot fetch or write control file ${INSTALL_PREFIX}/$control from url $url!"
fi
}
zaf_install_plugin() {
local url
local plugin
local plugindir
local tmpplugindir
local control
local version
plugin=$(basename "$1")
plugindir="${ZAF_PLUGINS_DIR}/${plugin}"
if [ -n "$plugin" ] && zaf_prepare_plugin "$1" $plugindir; then
url=$(zaf_get_plugin_url "$1")
zaf_wrn "Installing plugin $plugin"
zaf_dbg "Source url: $url, Destination dir: $plugindir"
control=${plugindir}/control.zaf
[ "$ZAF_DEBUG" -gt 1 ] && zaf_plugin_info "${control}"
if [ -z "${INSTALL_PREFIX}" ]; then
zaf_ctrl_check_deps "${control}"
zaf_ctrl_sudo "$plugin" "${control}" "${plugindir}"
zaf_ctrl_cron "$plugin" "${control}" "${plugindir}"
zaf_ctrl_generate_items_cfg "${control}" "${plugin}" \
| zaf_far '{PLUGINDIR}' "${plugindir}" >${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf
zaf_dbg "Generated ${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf"
zaf_ctrl_generate_extitems_cfg "${control}" "${plugin}"
else
zaf_touch "${plugindir}/postinst.need"
fi
zaf_ctrl_install "$url" "${control}" "${plugindir}"
else
zaf_err "Cannot install plugin '$plugin' to $plugindir!"
fi
}
zaf_postinstall_plugin() {
local url
local plugin
local plugindir
local tmpplugindir
local control
local version
plugin=$(basename "$1")
plugindir="${ZAF_PLUGINS_DIR}/${plugin}"
control=${plugindir}/control.zaf
[ "$ZAF_DEBUG" -gt 1 ] && zaf_plugin_info "${control}"
zaf_ctrl_check_deps "${control}"
zaf_ctrl_sudo "$plugin" "${control}" "${plugindir}"
zaf_ctrl_cron "$plugin" "${control}" "${plugindir}"
zaf_ctrl_generate_items_cfg "${control}" "${plugin}" \
| zaf_far '{PLUGINDIR}' "${plugindir}" >${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf
zaf_dbg "Generated ${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf"
zaf_ctrl_generate_extitems_cfg "${control}" "${plugin}"
}
# List installed plugins
# $1 - plugin
zaf_list_plugins() {
local cfile
local plugin
ls -1 ${ZAF_PLUGINS_DIR} | while read p; do
zaf_is_plugin "$(basename $p)" && echo $p
done
}
zaf_is_plugin() {
[ -d "$ZAF_PLUGINS_DIR/$1" ] && [ -f "$ZAF_PLUGINS_DIR/$1/control.zaf" ] && return
false
}
zaf_discovery_plugins() {
zaf_list_plugins | zaf_discovery '{#PLUGIN}'
}
# $1 plugin
# $2 ctrl_option
zaf_plugin_option() {
local plugindir
local cfile
if [ -z "$1" ]; then
zaf_err "Missing plugin name.";
fi
if zaf_is_plugin "$1"; then
plugindir="${ZAF_PLUGINS_DIR}/$1"
cfile="$plugindir/control.zaf"
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. ";
fi
plugindir="${ZAF_PLUGINS_DIR}/$1"
cfile="$plugindir/control.zaf"
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")
case $2 in
test)
for tp in $testparms; do
if [ -n "$p" ]; then
echo -n "$1.$i[$tp] "
else
echo -n "$1.$i "
fi
done
;;
precache)
for tp in $precache; do
echo -n "$1.$i[$tp] "
done
;;
*)
if [ -n "$p" ]; then
echo -n "$1.$i[] "
else
echo -n "$1.$i "
fi
;;
esac
done
echo
}
zaf_item_info() {
local plugin
local item
local param
plugin=$(echo $1 | cut -d '.' -f 1)
item=$(echo $1 | cut -d '.' -f 2-)
if zaf_is_plugin $plugin; then
if zaf_ctrl_get_items <$ZAF_PLUGINS_DIR/$plugin/control.zaf | grep -wq "$item"; then
echo "Item $1:"
echo -n "Cache: "; zaf_ctrl_get_item_option $ZAF_PLUGINS_DIR/$plugin/control.zaf "$item" "Cache"; echo
echo "Parameters:"
zaf_ctrl_get_item_option $ZAF_PLUGINS_DIR/$plugin/control.zaf "$item" "Parameters" ; echo
echo "Testparameters:"
zaf_ctrl_get_item_option $ZAF_PLUGINS_DIR/$plugin/control.zaf "$item" "Testparameters"; echo
echo "Precache:"
zaf_ctrl_get_item_option $ZAF_PLUGINS_DIR/$plugin/control.zaf "$item" "Precache"; echo
grep "UserParameter=$1" $ZAF_AGENT_CONFIGD/zaf_${plugin}.conf
else
zaf_err "No such item $item."
fi
else
zaf_err "No such plugin $plugin."
fi
}
zaf_list_items() {
for p in $(zaf_list_plugins); do
echo $p: $(zaf_list_plugin_items $p)
done
}
zaf_get_item() {
if which zabbix_get >/dev/null; then
zaf_trc zabbix_get -s localhost -k "'$1'"
(zabbix_get -s localhost -k "$1" | tr '\n' ' '; echo) || 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_trc $ZAF_AGENT_BIN -t "'$1'"
if $ZAF_AGENT_BIN -t "$1" | grep ZBX_NOTSUPPORTED; then
return 1
else
$ZAF_AGENT_BIN -t "$1" | tr '\n' ' '
echo
fi
}
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() {
! 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_$1.conf ${ZAF_CROND}/zaf_$1 ${ZAF_SUDOERSD}/zaf_$1
}

View File

@@ -2,36 +2,36 @@
. /etc/zaf.conf . /etc/zaf.conf
[ -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}/zaf.lib.sh
. ${ZAF_LIB_DIR}/plugin.lib.sh
. ${ZAF_LIB_DIR}/ctrl.lib.sh . ${ZAF_LIB_DIR}/ctrl.lib.sh
. ${ZAF_LIB_DIR}/os.lib.sh . ${ZAF_LIB_DIR}/os.lib.sh
. ${ZAF_LIB_DIR}/zbxapi.lib.sh . ${ZAF_LIB_DIR}/zbxapi.lib.sh
. ${ZAF_LIB_DIR}/cache.lib.sh . ${ZAF_LIB_DIR}/cache.lib.sh
# Plugin specific functions if exists
[ -f ./functions.sh ] && . ./functions.sh
if ! type zaf_version >/dev/null; then
echo "Problem loading libraries?"
exit 2
fi
zaf_debug_init
zaf_tmp_init
zaf_cache_init
export ZAF_LIB_DIR export ZAF_LIB_DIR
export ZAF_TMP_DIR export ZAF_TMP_DIR
export ZAF_CACHE_DIR
export ZAF_PLUGINS_DIR export ZAF_PLUGINS_DIR
export ZAF_DEBUG
unset ZAF_LOG_STDERR
export PATH
if [ "$(basename $0)" = "preload.sh" ] && [ -n "$*" ]; then if [ "$1" = "_cache" ] || [ "$1" = "_nocache" ] ; then
tmpf=$(zaf_tmpfile preload) [ "$1" = "_nocache" ] && export ZAF_NOCACHE=1
$@ 2>$tmpf shift
[ -s $tmpf ] && zaf_wrn <$tmpf 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 fi

View File

@@ -1,7 +1,7 @@
# Hardcoded variables # Hardcoded variables
ZAF_VERSION="1.3" ZAF_VERSION="1.1"
ZAF_GITBRANCH="1.3" ZAF_GITBRANCH="1.1"
ZAF_URL="https://github.com/limosek/zaf" ZAF_URL="https://github.com/limosek/zaf"
ZAF_RAW_URL="https://raw.githubusercontent.com/limosek/zaf" ZAF_RAW_URL="https://raw.githubusercontent.com/limosek/zaf"
@@ -11,77 +11,24 @@ zaf_msg() {
echo $@ echo $@
} }
zaf_trc() { zaf_trc() {
[ "$ZAF_DEBUG" -ge "3" ] && logger -p user.info ${ZAF_LOG_STDERR} -t zaf-trace -- $@ [ "$ZAF_DEBUG" -ge "3" ] && logger -s -t zaf -- $@
return 0
} }
zaf_dbg() { zaf_dbg() {
[ "$ZAF_DEBUG" -ge "2" ] && logger -p user.debug ${ZAF_LOG_STDERR} -t zaf-debug -- $@ [ "$ZAF_DEBUG" -ge "2" ] && logger -s -t zaf -- $@
return 0
} }
zaf_wrn() { zaf_wrn() {
[ "$ZAF_DEBUG" -ge "1" ] && logger -p user.warn ${ZAF_LOG_STDERR} -t zaf-warning -- $@ [ "$ZAF_DEBUG" -ge "1" ] && logger -s -t zaf -- $@
return 0
} }
zaf_err() { zaf_err() {
logger ${ZAF_LOG_STDERR} -p user.err -t zaf-error -- $@ logger -s -t zaf -- $@
logger ${ZAF_LOG_STDERR} -p user.err -t zaf-error "Exiting with error!" logger -s -t zaf "Exiting with error!"
exit 1 exit 1
} }
# Help option
# $1 - key
# $2 -
zaf_hlp() {
local kl
local dl
local cols
cols=$COLUMNS
[ -z "$cols" ] && cols=120
kl=$(expr $cols / 3)
dl=$(expr $cols - $kl)
printf %-${kl}s%-${dl}s%b "$1" "$2" "\n"
}
# $1 if nonempty, log to stderr too
zaf_debug_init() {
[ -z "$ZAF_DEBUG" ] && ZAF_DEBUG=1
export ZAF_DEBUG
[ -n "$1" ] && export ZAF_LOG_STDERR="-s"
}
zaf_tmp_init() {
[ -z "$ZAF_TMP_DIR" ] && ZAF_TMP_DIR=/tmp/
! [ -w "$ZAF_TMP_DIR" ] && zaf_err "Tmp dir $ZAF_TMP_DIR is not writable."
}
zaf_version(){ zaf_version(){
echo $ZAF_VERSION echo $ZAF_VERSION
} }
# Add parameter for agent check
# $1 parameter name (will be set to var)
# $2 if nonempty, it is default value. If empty, parameter is mandatory
# $3 if nonempty, regexp to test
zaf_agentparm(){
local name
local default
local regexp
name="$1"
default="$2"
regexp="$3"
[ -z "$value" ] && [ -z "$default" ] && zaf_err "$ITEM_KEY: Missing mandatory parameter $name."
if [ -z "$value" ]; then
value="$default"
else
if [ -n "$regexp" ]; then
echo "$value" | grep -qE "$regexp" || zaf_err "$ITEM_KEY: Bad parameter '$name' value '$value' (not in regexp '$regexp')."
fi
fi
eval $name=$value
zaf_trc "$ITEM_KEY: Param $name set to $value"
}
# Fetch url to stdout # Fetch url to stdout
# $1 url # $1 url
# It supports real file, file:// and other schemes known by curl # It supports real file, file:// and other schemes known by curl
@@ -102,33 +49,13 @@ zaf_fetch_url() {
case $scheme in case $scheme in
http|https|ftp|file) http|https|ftp|file)
[ "$scheme" != "file" ] && [ -n "$ZAF_OFFLINE" ] && zaf_err "Cannot download $1 in offline mode!" [ "$scheme" != "file" ] && [ -n "$ZAF_OFFLINE" ] && zaf_err "Cannot download $1 in offline mode!"
if zaf_which curl >/dev/null 2>/dev/null; then [ "${ZAF_CURL_INSECURE}" = "1" ] && insecure="-k"
[ "${ZAF_CURL_INSECURE}" = "1" ] && insecure="-k" zaf_dbg curl $insecure -f -s -L -o - $1
zaf_dbg curl $insecure -f -s -L -o - $1 curl $insecure -f -s -L -o - "$1" | zaf_tocache_stdin "$1" 120
curl $insecure -f -s -L -o - "$1" | zaf_tocache_stdin "$1" 120
else
[ "${ZAF_CURL_INSECURE}" = "1" ] && insecure="--no-check-certificate"
zaf_dbg wget $insecure -O - $1
wget $insecure -O - "$1" | zaf_tocache_stdin "$1" 120
fi
;; ;;
esac esac
} }
# Get info about url
# returns path or url
zaf_url_info() {
if echo "$1" | grep -q '^/'; then
echo "path"
else
if echo "$1" | grep -q ^http; then
echo "url"
else
echo "unknown"
fi
fi
}
# Find and replace string # Find and replace string
zaf_far(){ zaf_far(){
local f local f
@@ -151,7 +78,6 @@ zaf_bglimit(){
local maxbg local maxbg
local maxnumber local maxnumber
local cnumber local cnumber
[ -z "$BASH_VERSION" ] && { zaf_dbg "Job server not available. Use bash!"; return 1; }
if [ $# -eq 0 ] ; then if [ $# -eq 0 ] ; then
maxbg=5 maxbg=5
else else
@@ -245,6 +171,257 @@ zaf_check_agent_config() {
${ZAF_AGENT_BIN} -t zaf.version ${ZAF_AGENT_BIN} -t zaf.version
} }
# Update repo
zaf_update_repo() {
[ "$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_REPO_GITURL}" "${ZAF_REPO_DIR}"
(cd ${ZAF_REPO_DIR} && git pull)
fi
}
# 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
if echo "$1" | grep -q '/'; then
url="$1" # plugin with path - from directory
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
if [ -n "${ZAF_PREPACKAGED_DIR}" ] && [ -d "${ZAF_PREPACKAGED_DIR}/$1" ]; then
url="${ZAF_PREPACKAGED_DIR}/$1"
else
if [ -n "${ZAF_REPO_URL}" ]; then
url="${ZAF_REPO_URL}/$1"
else
zaf_err "Cannot find plugin $1"
fi
fi
fi
fi
fi
echo $url
}
# $1 - control
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)
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}")
echo
echo -n "Plugin '$plugin' "; [ -n "$pversion" ] && echo -n "version ${pversion}"; echo ":"
echo "$pdescription"; echo
[ -n "$pmaintainer" ] && echo "Maintainer: $pmaintainer"
[ -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
}
# Prepare plugin into dir
# $1 is url, directory or plugin name (will be searched in default plugin dir).
# $2 is directory to prepare.
zaf_prepare_plugin() {
local url
local plugindir
local control
url=$(zaf_get_plugin_url "$1")/control.zaf || exit $?
plugindir="$2"
control=${plugindir}/control.zaf
zaf_install_dir "$plugindir"
zaf_dbg "Fetching control file from $url ..."
if zaf_fetch_url "$url" >"${control}"; then
zaf_ctrl_check_deps "${control}"
else
zaf_err "prepare_plugin: Cannot fetch or write control file $control from url $url!"
fi
}
zaf_install_plugin() {
local url
local plugin
local plugindir
local control
local version
if zaf_prepare_plugin "$1" "${ZAF_TMP_DIR}/plugin"; then
url=$(zaf_get_plugin_url "$1")
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 [ -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 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!"
fi
else
zaf_err "Cannot prepare plugin $1"
fi
}
# List installed plugins
# $1 - plugin
zaf_list_plugins() {
local cfile
local plugin
ls -1 ${ZAF_PLUGINS_DIR} | while read p; do
zaf_is_plugin "$(basename $p)" && echo $p
done
}
zaf_is_plugin() {
[ -d "$ZAF_PLUGINS_DIR/$1" ] && [ -f "$ZAF_PLUGINS_DIR/$1/control.zaf" ] && return
false
}
zaf_discovery_plugins() {
zaf_list_plugins | zaf_discovery '{#PLUGIN}'
}
# $1 plugin
# $2 ctrl_option
zaf_plugin_option() {
local plugindir
local cfile
if [ -z "$1" ]; then
zaf_err "Missing plugin name.";
fi
if zaf_is_plugin "$1"; then
plugindir="${ZAF_PLUGINS_DIR}/$1"
cfile="$plugindir/control.zaf"
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. ";
fi
plugindir="${ZAF_PLUGINS_DIR}/$1"
cfile="$plugindir/control.zaf"
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
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
[ "$2" != "precache" ] && echo -n "$key "
done
echo
}
zaf_list_items() {
for p in $(zaf_list_plugins); do
echo $p: $(zaf_list_plugin_items $p)
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() {
! 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_$1.conf ${ZAF_CROND}/zaf_$1 ${ZAF_SUDOERSD}/zaf_$1
}
zaf_tolower() { zaf_tolower() {
tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz' tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'
} }
@@ -258,41 +435,4 @@ zaf_stripctrl() {
echo $1 | tr '[]*&;:.-' '________' echo $1 | tr '[]*&;:.-' '________'
} }
# Unescape string on stdin
# $1 - list of chars to unescape
zaf_strunescape() {
sed -e 's#\\\(['"$1"']\)#\1#g'
}
# Escape string on stdin
# $1 - list of chars to escape
zaf_strescape() {
sed -e 's#\(['"$1"']\)#\\\1#g'
}
# Add seconds to current date and return date in YYYY-MM-DD hh:mm:ss
# $1 seconds
zaf_date_add() {
date -d "$1 seconds" "+%Y-%m-%d %H:%M:%S" 2>/dev/null || date -d "$(expr $(date +%s) + $1)" -D %s "+%Y-%m-%d %H:%M:%S"
}
# Create temp file and return its name
# $1 prefix or empty
zaf_tmpfile() {
echo "$ZAF_TMP_DIR/tmp$1"
}
# return random number
zaf_random() {
hexdump -n 2 -e '/2 "%u"' /dev/urandom
}
# Emulate sudo
zaf_sudo() {
if zaf_is_root || ! zaf_which sudo >/dev/null 2>/dev/null; then
$@
else
sudo $@
fi
}

View File

@@ -1,26 +0,0 @@
#!/bin/sh
. $(dirname $0)/preload.sh
help() {
zaf_err "$0 seconds cmd..."
}
seconds=$1
shift
[ -z "$*" ] && help
zaf_trc "zafcache($seconds): $ITEM_KEY,'$2' '$3' '$4' '$5' '$6' '$7' '$8' '$9'"
if [ -z "$ITEM_KEY" ]; then
key=$(echo $*|tr ' ' '_')
else
key="$(echo ${ITEM_KEY}|cut -d '[' -f 1)[$(shift; echo $*|tr ' ' ',')]"
fi
if ! zaf_fromcache "$key"; then
tmpf=$(zaf_tmpfile cache)
$@ 2>"$tmpf" | zaf_tocache_stdin "$key" "$seconds"
[ -s $tmpf ] && zaf_wrn <$tmpf
fi

View File

@@ -1,29 +0,0 @@
#!/bin/sh
. /etc/zaf.conf
. ${ZAF_LIB_DIR}/zaf.lib.sh
. ${ZAF_LIB_DIR}/plugin.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
if ! type zaf_version >/dev/null; then
echo "Problem loading libraries?"
exit 2
fi
zaf_debug_init
zaf_tmp_init
zaf_cache_init
export ZAF_LIB_DIR
export ZAF_TMP_DIR
export ZAF_CACHE_DIR
export ZAF_PLUGINS_DIR
export ZAF_DEBUG
unset ZAF_LOG_STDERR
export PATH
export ZAF_ITEM=$(basename $0)

View File

@@ -1,9 +1,12 @@
#!/bin/sh #!/bin/sh
. $(dirname $0)/preload.sh . /etc/zaf.conf
. ${ZAF_LIB_DIR}/zaf.lib.sh
help() { help() {
zaf_err "$0 key cmd" echo "$0 key cmd"
exit 2
} }
lkey="$1" lkey="$1"
@@ -13,7 +16,7 @@ shift
[ -z "${ZAF_LOCK_SECONDS}" ] && seconds=5 [ -z "${ZAF_LOCK_SECONDS}" ] && seconds=5
[ -z "${ZAF_LOCK_FORCE}" ] && force=1 [ -z "${ZAF_LOCK_FORCE}" ] && force=1
lockfile="/tmp/zaflock_${lkey}" lockfile="${ZAF_TMP_DIR}/zaflock_${lkey}"
i=0 i=0
while [ -f "$lockfile" ] && [ $i -lt $seconds ]; do while [ -f "$lockfile" ] && [ $i -lt $seconds ]; do
sleep 1 sleep 1
@@ -28,6 +31,7 @@ if [ -f "$lockfile" ] && [ -z "$force" ]; then
exit 1 exit 1
fi fi
[ -n "$*" ] && $@ 2>"$lockfile" touch "$lockfile"
[ -s "$lockfile" ] && zaf_wrn <"$lockfile" [ -n "$*" ] && $@
rm -f "$lockfile"

View File

@@ -1,58 +1,38 @@
# Call api function
# Call api function and use cache
# $1 - query string # $1 - query string
zaf_zbxapi_do() { zaf_zbxapi_do() {
local result local result
local query
local tmpfile
tmpfile=$ZAF_TMP_DIR/zapi$$
query="$1"
zaf_trc "Zabbix API: $query"
curl -s -f -L -X POST -H 'Content-Type: application/json-rpc' -d "$query" "$ZAF_ZBXAPI_URL" >$tmpfile
if [ $? = 0 ] && $ZAF_LIB_DIR/JSON.sh -b <$tmpfile | grep -q '"result"'; then
zaf_trc "API OK"
cat $tmpfile
rm -f $tmpfile
else
zaf_err "Error processing API request. ($?,$tmpfile)"
fi
}
# Call api function and cache results
# $1 - query string
zaf_zbxapi_do_cache() {
local result
local tmpfile
local query
query="$(echo $1 | tr '\n' ' ')"
tmpfile=$ZAF_TMP_DIR/zcapi$$
if ! zaf_fromcache "$1"; then if ! zaf_fromcache "$1"; then
zaf_zbxapi_do "$1" >$tmpfile zaf_trc "Zabbix API: $1"
[ -s "$tmpfile" ] && cat $tmpfile | zaf_tocache_stdin "$query" 60 result=$(curl -s -f -L -X POST -H 'Content-Type: application/json-rpc' -d "$1" "$ZAF_ZBXAPI_URL")
rm -f $tmpfile 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 fi
} }
# Extract one result from JSON response # Extract result from JSON response
zaf_zbxapi_getresult() { zaf_zbxapi_getresult() {
$ZAF_LIB_DIR/JSON.sh -b | grep '\["result"\]' | tr '\t' ' ' | cut -d ' ' -f 2- sed -e 's/\({"jsonrpc":"2.0","result":\)\(.*\),\("id":.*\)/\2/g' | sed -e 's/^\[\]$//'
} }
# Extract XML result from JSON response # Extract XML result from JSON response
zaf_zbxapi_getxml() { zaf_zbxapi_getxml() {
zaf_zbxapi_getstring | zaf_strunescape '</">' | zaf_far '\\n' "\n" 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 # Extract string from JSON response result
zaf_zbxapi_getstring() { zaf_zbxapi_getstring() {
zaf_zbxapi_getresult | sed -e 's/^"//' -e 's/"$//' sed -e 's/^"//' -e 's/"$//' -e 's/\\n/'\\n'/g'
} }
# Extract value from JSON response result # Extract value from JSON response result
# $1 key # $1 key
zaf_zbxapi_getvalues() { zaf_zbxapi_getvalue() {
$ZAF_LIB_DIR/JSON.sh -b | grep '\["result",.*,"'$1'"]' | tr '\t' ' ' | cut -d ' ' -f 2- | sed -e 's/^"//' -e 's/"$//' tr ',' '\n' | grep "\"$1\":" | cut -d '"' -f 4
} }
# Zabbix API related functions # Zabbix API related functions
@@ -78,195 +58,123 @@ zaf_zbxapi_login(){
if [ "$ZAF_ZBXAPI_AUTHTYPE" = "http" ] ; then 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-) 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 fi
result=$(zaf_zbxapi_do_cache "$authstr") result=$(zaf_zbxapi_do "$authstr")
ZAF_ZBXAPI_AUTH=$(echo $result |zaf_zbxapi_getstring) ZAF_ZBXAPI_AUTH=$(echo $result |zaf_zbxapi_getresult| zaf_zbxapi_getstring)
[ -z "$ZAF_ZBXAPI_AUTH" ] && zaf_err "Cannot login into API"
zaf_dbg "Logged into zabbix API ($ZAF_ZBXAPI_AUTH)" zaf_dbg "Logged into zabbix API ($ZAF_ZBXAPI_AUTH)"
} }
# Get object from zabbix API
# $1 object_type
# $2 filter
# $3 params
# $4 output
# $5 id
zaf_zbxapi_get_object() {
local obj
local filter
local params
local str
local output
local id
local result
obj=$1
filter=$2
params=$3
output=$4
id=$5
[ -z "$id" ] && id=1
[ -n "$filter" ] && filter='"filter": {'$filter'},';
[ -z "$output" ] && output="shorten";
if [ -n "$params" ]; then
params='"params": {'$params', '$filter' "output":"'$output'"}';
else
params='"params": {'$filter' "output":"'$output'"}';
fi
str='{ "method": "'$obj'.get", "jsonrpc": "2.0", "auth": "'$ZAF_ZBXAPI_AUTH'",'$params', "id": "'$id'" }'
result=$(zaf_zbxapi_do_cache "$str")
[ -z "$result" ] && zaf_dbg "API call result empty or error! ($str)"
echo $result
}
# $1 hostgroup name # $1 hostgroup name
zaf_zbxapi_gethostgroupid() { zaf_zbxapi_gethostgroupid() {
local result local hstr
local filter
result=$(zaf_zbxapi_get_object "hostgroup" '"name": ["'$1'"]') local gfilter
[ -z "$result" ] && zaf_err "HostGroup $1 not found!"
echo $result |zaf_zbxapi_getvalues groupid hstr='{
} "method": "hostgroup.get",
"jsonrpc": "2.0",
# $1 hostid "auth": "'$ZAF_ZBXAPI_AUTH'",
# $2 property or null for all "params": {
zaf_zbxapi_gethost() { "filter": {
local result "name": ["'$1'"]
},
result=$(zaf_zbxapi_get_object "host" '' '"hostids": ["'$1'"]' 'extend') "output": "shorten"
[ -z "$result" ] && zaf_err "Hostid $1 not found!" },
if [ -z "$2" ]; then "id": 1
echo $result }'
else zaf_zbxapi_do "$hstr" | zaf_zbxapi_getresult | tr ',' '\n' | cut -d '"' -f 4
echo $result |zaf_zbxapi_getvalues $2
fi
} }
# $1 hostname # $1 hostname
zaf_zbxapi_gethostid() { zaf_zbxapi_gethostid() {
local result local hstr
local host
result=$(zaf_zbxapi_get_object "host" '"host": ["'$1'"]') local groupid
[ -z "$result" ] && zaf_err "Host $1 not found!" local filter
echo $result |zaf_zbxapi_getvalues hostid local gfilter
}
# $1 hostname
# $2 inv field or empty for json
zaf_zbxapi_gethostinventory() {
local result
result=$(zaf_zbxapi_get_object "host" '"host": ["'$1'"]' '"withInventory": "true", "selectInventory": "extend"')
[ -z "$result" ] && zaf_err "Host $1 not found!"
if [ -z "$2" ]; then
echo $result
else
echo $result |zaf_zbxapi_getvalues $2
fi
}
# $1 hostname
zaf_zbxapi_gettemplateid() {
local result
result=$(zaf_zbxapi_get_object "template" '"host": ["'$1'"]')
[ -z "$result" ] && zaf_err "Template $1 not found!"
echo $result |zaf_zbxapi_getvalues templateid
}
# $1 templateid
# $2 property or null for all
zaf_zbxapi_gettemplate() {
local result
result=$(zaf_zbxapi_get_object "template" '' '"templateids": ["'$1'"]' 'extend')
[ -z "$result" ] && zaf_err "Templateid $1 not found!"
if [ -z "$2" ]; then
echo $result
else
echo $result |zaf_zbxapi_getvalues $2
fi
}
# $1 hostgroupid
zaf_zbxapi_gethostsingroup() {
local result
result=$(zaf_zbxapi_get_object "host" '' '"groupids": ["'$1'"]')
[ -z "$result" ] && zaf_wrn "No hosts in groupid '$1'"
echo $result | zaf_zbxapi_getvalues "hostid"
}
# Get all hostids in system
zaf_zbxapi_gethostids() {
local result
result=$(zaf_zbxapi_get_object "host")
echo $result | zaf_zbxapi_getvalues "hostid"
}
# Get all templateids in system
zaf_zbxapi_gettemplateids() {
local result
result=$(zaf_zbxapi_get_object "template")
echo $result | zaf_zbxapi_getvalues "templateid"
}
# $1 hostgroupid
zaf_zbxapi_gettemplatesingroup() {
local result
result=$(zaf_zbxapi_get_object "template" '' '"groupids": ["'$1'"]')
[ -z "$result" ] && zaf_wrn "No templates in groupid '$1'"
echo $result | zaf_zbxapi_getvalues "templateid"
}
# $1 map or null for all
zaf_zbxapi_getmapid() {
local result
if [ -n "$1" ]; then
result=$(zaf_zbxapi_get_object "map" '"name": ["'$1'"]')
else
result=$(zaf_zbxapi_get_object "map")
fi
[ -z "$result" ] && zaf_err "Map $1 not found"
echo $result | zaf_zbxapi_getvalues "sysmapid"
}
# $1 mapid
# $2 property or null for all
zaf_zbxapi_getmap() {
local result
result=$(zaf_zbxapi_get_object "map" '' '"sysmapids": ["'$1'"]' 'extend')
[ -z "$result" ] && zaf_err "Mapid $1 not found"
if [ -z "$2" ]; then
echo $result
else
echo $result |zaf_zbxapi_getvalues $2
fi
}
# Object backup
# $1 object
# $2 id
zaf_zbxapi_export_object(){
local bkpstr
local obj
local id
obj="$1" host="$1"
id="$2" 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=' bkpstr='
{ {
"method": "configuration.export", "method": "configuration.export",
"jsonrpc": "2.0", "jsonrpc": "2.0",
"params": { "params": {
"options": { "options": {
"'$obj'": [ "hosts": [
"'$id'" "'$host'"
] ]
}, },
"format": "xml" "format": "xml"
@@ -274,98 +182,7 @@ zaf_zbxapi_export_object(){
"auth": "'$ZAF_ZBXAPI_AUTH'", "auth": "'$ZAF_ZBXAPI_AUTH'",
"id": 1 "id": 1
}' }'
zaf_zbxapi_do_cache "$bkpstr" | zaf_zbxapi_getxml zaf_zbxapi_do "$bkpstr" | zaf_zbxapi_getxml
}
# Host backup
# $1 hostid
zaf_zbxapi_export_host(){
zaf_zbxapi_export_object hosts "$1"
}
# Template backup
# $1 templateid
zaf_zbxapi_export_template(){
zaf_zbxapi_export_object templates "$1"
}
# Map backup
# $1 mapid
zaf_zbxapi_export_map(){
zaf_zbxapi_export_object maps "$1"
}
# Import template into zabbix
# $1 template file or stdin
zaf_zbxapi_import_config(){
local xmlstr
local impstr
if [ -z "$1" ]; then
xmlstr=$(zaf_strescape '"')
else
! [ -f "$1" ] && return 1
xmlstr=$(zaf_strescape '"\n\r' <$1)
fi
impstr='
{
"method": "configuration.import",
"jsonrpc": "2.0",
"params": {
"format": "xml",
"rules": {
"applications": {
"createMissing": true,
"updateExisting": true
},
"discoveryRules": {
"createMissing": true,
"updateExisting": true
},
"graphs": {
"createMissing": true,
"updateExisting": true
},
"hosts": {
"createMissing": true,
"updateExisting": true
},
"items": {
"createMissing": true,
"updateExisting": true
},
"templates": {
"createMissing": true,
"updateExisting": true
},
"triggers": {
"createMissing": true,
"updateExisting": true
},
"maps": {
"createMissing": true,
"updateExisting": true
},
"screens": {
"createMissing": true,
"updateExisting": true
},
"items": {
"createMissing": true,
"updateExisting": true
},
"valueMaps": {
"createMissing": true,
"updateExisting": true
}
},
"source": "'$xmlstr'"
},
"auth": "'$ZAF_ZBXAPI_AUTH'",
"id": 3
}'
zaf_zbxapi_do "$impstr" | zaf_zbxapi_getresult | grep -q true
} }

10
rpm.mk
View File

@@ -1,10 +0,0 @@
# Makefile for generating rpm packages
# Contrinutions welcome :)
ifeq ($(RPM_PKG),)
RPM_PKG=$(shell . lib/zaf.lib.sh; echo out/zaf-$$ZAF_VERSION.rpm)
endif
$(RPM_PKG):
@echo "Not supported yet. Contributions welcomed! :) "; exit 2

319
zaf
View File

@@ -25,15 +25,25 @@ ZAF_CFG_FILE="/etc/zaf.conf"
[ -n "$secondstage" ] && exec $0 $params [ -n "$secondstage" ] && exec $0 $params
fi fi
if [ -n "$ZAF_PROXY" ]; then [ -z "$ZAF_TMP_BASE" ] && ZAF_TMP_BASE=/tmp/zaf
export ALL_PROXY="$ZAF_PROXY" ZAF_TMP_DIR="${ZAF_TMP_BASE}-${USER}"
export http_proxy="$ZAF_PROXY" [ -z "$ZAF_CACHE_DIR" ] && ZAF_CACHE_DIR=${ZAF_TMP_BASE}c
export https_proxy="$ZAF_PROXY"
[ -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 fi
if [ -f ./lib/zaf.lib.sh ]; then if [ -f ./lib/zaf.lib.sh ]; then
. ./lib/zaf.lib.sh . ./lib/zaf.lib.sh
. ./lib/plugin.lib.sh
. ./lib/os.lib.sh . ./lib/os.lib.sh
. ./lib/ctrl.lib.sh . ./lib/ctrl.lib.sh
. ./lib/cache.lib.sh . ./lib/cache.lib.sh
@@ -41,7 +51,6 @@ if [ -f ./lib/zaf.lib.sh ]; then
[ -f ./lib/zaf.${ZAF_OS}.sh ] && . ./lib/zaf.${ZAF_OS}.sh [ -f ./lib/zaf.${ZAF_OS}.sh ] && . ./lib/zaf.${ZAF_OS}.sh
else else
. ${ZAF_LIB_DIR}/zaf.lib.sh . ${ZAF_LIB_DIR}/zaf.lib.sh
. ${ZAF_LIB_DIR}/plugin.lib.sh
. ${ZAF_LIB_DIR}/os.lib.sh . ${ZAF_LIB_DIR}/os.lib.sh
. ${ZAF_LIB_DIR}/ctrl.lib.sh . ${ZAF_LIB_DIR}/ctrl.lib.sh
. ${ZAF_LIB_DIR}/cache.lib.sh . ${ZAF_LIB_DIR}/cache.lib.sh
@@ -49,71 +58,39 @@ else
[ -f ${ZAF_LIB_DIR}/zaf.${ZAF_OS}.sh ] && . ${ZAF_LIB_DIR}/zaf.${ZAF_OS}.sh [ -f ${ZAF_LIB_DIR}/zaf.${ZAF_OS}.sh ] && . ${ZAF_LIB_DIR}/zaf.${ZAF_OS}.sh
fi fi
zaf_debug_init stderr if zaf_is_root; then
zaf_tmp_init chgrp zabbix "${ZAF_CACHE_DIR}"
zaf_cache_init chmod g+w "${ZAF_CACHE_DIR}"
fi
case $1 in case $1 in
check-agent-config)
zaf_check_agent_config
;;
cache-clean) cache-clean)
zaf_cache_clean zaf_cache_clean
;; ;;
cache-list)
zaf_cache_list
;;
cache-test1)
for i in test1 test2 test3; do
echo "To cache:"
echo "$i" | zaf_tocache_stdin "test" 10
done
;;
cache-test2)
for i in test1 test2 test3; do
echo "From cache:"
zaf_fromcache "test"
done
;;
version)
echo "$ZAF_VERSION"
[ $ZAF_DEBUG -gt 1 ] && set |grep -E ^ZAF_
;;
###### Agent related commands
userparms) userparms)
for plugin in $(zaf_list_plugins); do for plugin in $(zaf_list_plugins); do
plugindir=$ZAF_PLUGINS_DIR/$plugin plugindir=$ZAF_PLUGINS_DIR/$plugin
control=$plugindir/control.zaf control=$plugindir/control.zaf
zaf_ctrl_generate_items_cfg "${control}" "${plugin}" "noscripts" \ zaf_ctrl_generate_cfg "${control}" "${plugin}" \
| zaf_far '{PLUGINDIR}' "${plugindir}" | zaf_far '{PLUGINDIR}' "${plugindir}"
done done
;; ;;
agent-config) agent-config)
[ -n "$2" ] && force=yes
for p in $(zaf_list_plugins); do
if [ -f "$ZAF_PLUGINS_DIR/$p/postinst.need" ]; then
zaf_wrn "Calling postinstalation of plugin $p"
zaf_postinstall_plugin $p && rm -f "$ZAF_PLUGINS_DIR/$p/postinst.need"
force=yes
fi
done
zaf_wrn "Generating config files in $ZAF_AGENT_CONFIGD ..."
for plugin in $(zaf_list_plugins); do for plugin in $(zaf_list_plugins); do
plugindir=$ZAF_PLUGINS_DIR/$plugin plugindir=$ZAF_PLUGINS_DIR/$plugin
control=$plugindir/control.zaf control=$plugindir/control.zaf
if ! [ -f "${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf" ] || [ "$control" -nt "${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf" ] || [ -n "$force" ]; then zaf_ctrl_generate_cfg "${control}" "${plugin}" \
zaf_dbg "Generating ${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf"
zaf_ctrl_generate_items_cfg "${control}" "${plugin}" \
| zaf_far '{PLUGINDIR}' "${plugindir}" >${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf | zaf_far '{PLUGINDIR}' "${plugindir}" >${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf
fi
done done
;; ;;
server-config)
zaf_wrn "Not implemented yet"
;;
###### Plugins related commands
update) update)
zaf_wrn "Updating repository ${ZAF_REPO_GITURL}..." zaf_wrn "Updating repository ${ZAF_REPO_GITURL}..."
zaf_update_repo zaf_update_repo
@@ -128,25 +105,23 @@ show)
zaf_plugin_info $ZAF_PLUGINS_DIR/$plugin/control.zaf zaf_plugin_info $ZAF_PLUGINS_DIR/$plugin/control.zaf
done done
else else
if echo $1 | grep -vq "\."; then if zaf_list_plugins | grep -q "^$1"; then
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."
zaf_is_plugin $1 && zaf_plugin_info $ZAF_PLUGINS_DIR/$1/control.zaf || zaf_err "Plugin $1 not installed."
else
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 $1 not installed."
fi
fi
else else
zaf_item_info "$1" 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 $1 not installed."
fi
fi fi
fi fi
;; ;;
plugins) plugins)
zaf_list_plugins zaf_list_plugins
;; ;;
items) items)
shift shift
if [ -z "$1" ]; then if [ -z "$1" ]; then
@@ -155,6 +130,7 @@ items)
zaf_list_plugin_items "$1" zaf_list_plugin_items "$1"
fi fi
;; ;;
test) test)
[ "$USER" != "zabbix" ] && zaf_wrn "You are not zabbix user. Test will be run with your privileges and sudo access!" [ "$USER" != "zabbix" ] && zaf_wrn "You are not zabbix user. Test will be run with your privileges and sudo access!"
shift shift
@@ -170,7 +146,7 @@ test)
for p in $plugins; do for p in $plugins; do
! zaf_is_plugin $p && zaf_err "Unknown plugin $p" ! zaf_is_plugin $p && zaf_err "Unknown plugin $p"
for i in $(zaf_list_plugin_items $p test); do for i in $(zaf_list_plugin_items $p test); do
zaf_test_item $i echo $i: $(zaf_test_item $i)
echo echo
done done
done done
@@ -193,6 +169,7 @@ get)
done done
done done
;; ;;
precache) precache)
shift shift
for i in $*; do for i in $*; do
@@ -209,6 +186,7 @@ precache)
fi fi
done done
;; ;;
install) install)
shift shift
[ -z "$1" ] && echo "$0 install plugin [plugin]..." [ -z "$1" ] && echo "$0 install plugin [plugin]..."
@@ -222,6 +200,7 @@ install)
done done
[ -n "$installed" ] && zaf_is_root && zaf_restart_agent [ -n "$installed" ] && zaf_is_root && zaf_restart_agent
;; ;;
reinstall) reinstall)
shift shift
[ -z "$1" ] && echo "$0 reinstall plugin [plugin]..." [ -z "$1" ] && echo "$0 reinstall plugin [plugin]..."
@@ -235,6 +214,7 @@ reinstall)
done done
[ -n "$reinstalled" ] && zaf_is_root && zaf_restart_agent [ -n "$reinstalled" ] && zaf_is_root && zaf_restart_agent
;; ;;
remove) remove)
shift shift
[ -z "$1" ] && echo "$0 remove plugin [plugin]..." [ -z "$1" ] && echo "$0 remove plugin [plugin]..."
@@ -247,18 +227,19 @@ remove)
[ -n "$removed" ] && zaf_is_root && zaf_restart_agent [ -n "$removed" ] && zaf_is_root && zaf_restart_agent
;; ;;
###### Zaf related commands
self-upgrade) self-upgrade)
shift shift
[ -z "$1" ] && auto=auto [ -z "$1" ] && auto=auto
zaf_os_specific zaf_check_deps zaf && zaf_err "Zaf is installed as system package. Cannot self-upgrade." zaf_os_specific zaf_check_deps zaf && zaf_err "Zaf is installed as system package. Cannot self-upgrade."
if ! zaf_which curl >/dev/null; rm -rf /tmp/zaf-installer && mkdir /tmp/zaf-installer
if ! which curl >/dev/null;
then then
zaf_err "Curl not found. Cannot continue. Please install it." zaf_err "Curl not found. Cannot continue. Please install it."
fi fi
zaf_fetch_url $ZAF_RAW_URL/$ZAF_GITBRANCH/install.sh | exec sh -s $auto "$@" zaf_fetch_url $ZAF_RAW_URL/$ZAF_GITBRANCH/install.sh | exec sh -s $auto "$@"
exit exit
;; ;;
self-remove) self-remove)
shift shift
zaf_os_specific zaf_check_deps zaf && zaf_err "Zaf is installed as system package. Cannot self-remove." zaf_os_specific zaf_check_deps zaf && zaf_err "Zaf is installed as system package. Cannot self-remove."
@@ -275,157 +256,56 @@ self-remove)
echo "To continue, please do $0 self-remove force" echo "To continue, please do $0 self-remove force"
fi fi
;; ;;
###### API related commands
api) api)
zaf_zbxapi_login zaf_zbxapi_login
case $2 in case $2 in
get-*-id) hostid)
obj=$(echo $2|cut -d '-' -f 2) zaf_zbxapi_gethostid "$3"
[ -z "$3" ] && zaf_err "$0 $1 $2 <$obj>"
eval zaf_zbxapi_get${obj}id "$3"
;; ;;
get-byid-*|get-id-*) host)
obj=$(echo $2|cut -d '-' -f 3) zaf_zbxapi_gethost "$3"
[ -z "$3" ] && zaf_err "$0 $1 $2 <id>" ;;
eval zaf_zbxapi_get${obj} "$3" "$4" hostgroupid)
zaf_zbxapi_gethostgroupid "$3"
;;
hosts)
gid=$(zaf_zbxapi_gethostgroupid "$3")
zaf_zbxapi_gethostsingroup $gid
;; ;;
get-host-ids) backup-group)
if [ -n "$3" ]; then
gid=$(zaf_zbxapi_gethostgroupid "$3") || exit 1
zaf_dbg "Selecting all hosts in group $3($gid)"
zaf_zbxapi_gethostsingroup $gid
else
zaf_dbg "Selecting all hosts in system"
zaf_zbxapi_gethostids
fi
;;
get-inventory)
[ -z "$3" ] && zaf_err "$0 $1 field [host]. If host unspecified, use hostname"
if [ -z "$4" ]; then
host=$(hostname)
else
host="$4"
fi
zaf_zbxapi_gethostinventory $host $3
;;
get-template-ids)
if [ -n "$3" ]; then
gid=$(zaf_zbxapi_gethostgroupid "$3") || exit 1
zaf_dbg "Selecting all templates in group $3($gid)"
zaf_zbxapi_gettemplatesingroup $gid
else
zaf_dbg "Selecting all templates in system"
zaf_zbxapi_gettemplateids
fi
;;
get-map-ids)
zaf_zbxapi_getmapid
;;
export-hosts)
shift; shift shift; shift
[ -z "$1" ] && zaf_err "$0 api export-hosts dir [hostgroup]" gid=$(zaf_zbxapi_gethostgroupid "$1")
dir="$1"
shift shift
if [ -n "$1" ]; then hosts=$(zaf_zbxapi_gethostsingroup $gid)
gid=$(zaf_zbxapi_gethostgroupid "$1") || exit 1 dir="."
shift [ -n "$1" ] && dir="$1"
hosts=$(zaf_zbxapi_gethostsingroup $gid)
else
hosts=$(zaf_zbxapi_gethostids)
fi
zaf_wrn "Will backup this hosts: $hosts" zaf_wrn "Will backup this hosts: $hosts"
zaf_wrn "Output dir: $dir" zaf_wrn "Output dir: $dir"
for h in $hosts; do for h in $hosts; do
if zaf_bglimit 5; then zaf_bglimit 5
( (hn=$(zaf_zbxapi_gethost $h)
hn=$(zaf_zbxapi_gethost $h name) zaf_wrn "Exporting host $hn($h)..."
zaf_wrn "Exporting host $hn($h)..." zaf_zbxapi_backuphost $h >$dir/$hn.xml )
zaf_zbxapi_export_host $h >"$dir/$hn.xml"
) &
else
hn=$(zaf_zbxapi_gethost $h name)
zaf_wrn "Exporting host $hn($h)..."
zaf_zbxapi_export_host $h >"$dir/$hn.xml"
fi
done done
wait wait
;; ;;
export-host) backup-host)
shift; shift shift; shift
hostid=$(zaf_zbxapi_gethostid "$1") || exit 1 hostid=$(zaf_zbxapi_gethostid "$1")
zaf_wrn "Exporting host $3($hostid)..." zaf_wrn "Exporting host $3($hostid)..."
zaf_zbxapi_export_host $hostid zaf_zbxapi_backuphost $hostid
;;
export-template)
shift; shift
templateid=$(zaf_zbxapi_gettemplateid "$1") || exit 1
zaf_wrn "Exporting template $3($hostid)..."
zaf_zbxapi_export_template $templateid
;;
export-templates)
shift; shift
[ -z "$1" ] && zaf_err "$0 api export-templates dir"
dir="$1"
templates=$(zaf_zbxapi_gettemplateids)
zaf_wrn "Will backup this templates: $templates"
zaf_wrn "Output dir: $dir"
for t in $templates; do
if zaf_bglimit 5; then
(
tn=$(zaf_zbxapi_gettemplate $t name)
zaf_wrn "Exporting template $tn($t)..."
zaf_zbxapi_export_template $t >"$dir/$tn.xml"
) &
else
tn=$(zaf_zbxapi_gettemplate $t name)
zaf_wrn "Exporting template $tn($t)..."
zaf_zbxapi_export_template $t >"$dir/$tn.xml"
fi
done
wait
;;
import-template)
shift; shift
if zaf_is_plugin $1; then
if [ -f "$ZAF_PLUGINS_DIR/$1/template.xml" ]; then
template="$ZAF_PLUGINS_DIR/$1/template.xml"
zaf_wrn "Importing template $template"
zaf_zbxapi_import_config $template || zaf_err "Error importing template"
else
url="$(zaf_plugin_option $1 Template)"
if [ -n "$url" ]; then
zaf_fetch_url $url | zaf_zbxapi_import_config || zaf_err "Error importing template"
else
url="$(zaf_plugin_option $1 Url)"
zaf_fetch_url $url/template.xml | zaf_zbxapi_import_config || zaf_err "Error importing template"
fi
fi
else
if [ -f $1 ]; then
zaf_wrn "Importing template $1"
zaf_zbxapi_import_config $1 || zaf_err "Error importing template"
else
zaf_err "Unknown plugin $1!"
fi
fi
;; ;;
*) *)
echo "$0 api command [parameters]" echo "$0 api command [parameters]"
for i in host template map; do echo "hostid 'host' Get hostid from hostname"
zaf_hlp "get-${i}-id $i" "Get $i id" echo "host 'hostid' Get hostname from hostid"
zaf_hlp "get-byid-${i} id [property]" "Get $i property from id. Leave empty property for JSON" echo "hostgroupid 'hostgroup' Get hostgroup id from hostgroup"
done echo "hosts 'hostgroup' Get hosts in group"
zaf_hlp "get-inventory host [fields]" "Get inventory fields [or all fields]" echo "backup-group 'hostgroup' [dir] Backup all hosts in group (get their config from zabbix and save to dir/hostname.xml)"
zaf_hlp "get-host-ids [hg]" "Get all hostids or hostids in group hg" echo "backup-host 'host' Backup host (get config from zabbix to stdout)"
zaf_hlp "get-template-ids [hg]" "Get all templateids or templateids in group hg"
zaf_hlp "get-map-ids" "Get all mapids"
zaf_hlp "export-hosts dir [hg]" "Backup all hosts [in group hg] (get their config from zabbix and save to dir/hostname.xml)"
zaf_hlp "export-host host" "Backup host (get config from zabbix to stdout)"
zaf_hlp "import-template {plugin|file}" "Import template for plugin or from file"
zaf_hlp "export-template name" "Export template to stdout"
zaf_hlp "export-templates dir" "Export all template to dir"
echo echo
[ -n "$2" ] && zaf_err "Bad API command '$2'!" exit
;; ;;
esac esac
;; ;;
@@ -433,36 +313,25 @@ api)
*) *)
echo "$0 Version ${ZAF_VERSION}. Please use some of this commands:" echo "$0 Version ${ZAF_VERSION}. Please use some of this commands:"
echo "$0 Cmd [ZAF_OPTION=value] [ZAF_CTRL_Option=value] [ZAF_CTRLI_Item_Option=value] ..." echo "$0 Cmd [ZAF_OPTION=value] [ZAF_CTRL_Option=value] [ZAF_CTRLI_Item_Option=value] ..."
echo "Plugin manipulation commands:" echo "Commands:"
zaf_hlp "$0 update" "To update repo (not plugins, similar to apt-get update)" echo "$0 update To update repo (not plugins, similar to apt-get update)"
zaf_hlp "$0 upgrade" "To upgrade installed plugins from repo" echo "$0 upgrade To upgrade installed plugins from repo"
zaf_hlp "$0 install plugin" "To install plugin" echo "$0 plugins To list installed plugins"
zaf_hlp "$0 remove plugin" "To remove plugin" 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 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 echo
echo "Plugin info commands:"
zaf_hlp "$0 plugins" "To list installed plugins"
zaf_hlp "$0 show [plugin]" "To show installed plugins or plugin info"
zaf_hlp "$0 items [plugin]" "To list all suported items [for plugin]"
echo
echo "Plugin diagnostic commands:"
zaf_hlp "$0 test [plugin[.item]]" "To test [all] suported items by zabbix_agentd [for plugin]"
zaf_hlp "$0 get [plugin[.item]]" "To test [all] suported items by zabbix_get [for plugin]"
zaf_hlp "$0 precache [plugin[.item]]" "To precache [all] suported items"
echo
echo "Zabbix API commands:"
zaf_hlp "$0 api" "To zabbix API functions. See $0 api for more info."
echo
echo "Agent config info commands:"
zaf_hlp "$0 userparms" "See userparms generated from zaf on stdout"
zaf_hlp "$0 agent-config [force]" "Reconfigure zabbix userparms in $ZAF_AGENT_CONFIGD"
echo
echo "Zaf related commands:"
zaf_hlp "$0 self-upgrade" "To self-upgrade zaf"
zaf_hlp "$0 self-remove" "To self-remove zaf and its config"
zaf_hlp "$0 cache-clean" "To remove all entries from cache"
zaf_hlp "$0 cache-list" "To show all entries in cache"
echo
[ -n "$1" ] && zaf_err "Bad command '$1'!"
;; ;;
esac esac