mirror of
				https://github.com/limosek/zaf.git
				synced 2025-10-31 17:47:37 +01:00 
			
		
		
		
	Compare commits
	
		
			14 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|   | a3a854d615 | ||
|   | 3db7c1d5fc | ||
|   | 26b58a61d5 | ||
|   | 8abf0306f7 | ||
|   | f5fd888441 | ||
|   | b3a773bb3c | ||
|   | 8dcc0e595a | ||
|   | 8edce1bc43 | ||
|   | 1f42c13482 | ||
|   | d85b8f20d9 | ||
|   | a36ce5ff56 | ||
|   | 57f2edb9f5 | ||
|   | 224dc441e3 | ||
|   | d681ca95c4 | 
							
								
								
									
										8
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										8
									
								
								Makefile
									
									
									
									
									
								
							| @@ -5,9 +5,6 @@ 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),) | ||||
|  ZAF_DEBUG=0 | ||||
| endif | ||||
|  | ||||
| ifeq ($(ZAF_OPTIONS),) | ||||
|  ZAF_OPTIONS = ZAF_GIT=0 | ||||
| @@ -47,8 +44,7 @@ deb-control: | ||||
| 	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 | ||||
| 	zaf_far '{ZAF_VERSION}' "$$ZAF_VERSION" <files/control.template | zaf_far '{ZAF_DEPENDS}' "$$DEPENDS" >$(DEBIAN_CTRL)/control | ||||
|  | ||||
| deb-scripts: | ||||
| 	@. lib/zaf.lib.sh; \ | ||||
| @@ -62,7 +58,7 @@ deb-scripts: | ||||
|  | ||||
| deb-cp: | ||||
| 	@mkdir -p $(DEBIAN_DIR) | ||||
| 	@set -e; INSTALL_PREFIX=$(DEBIAN_DIR) ZAF_DEBUG=$(ZAF_DEBUG) ./install.sh auto $(ZAF_OPTIONS) $(AGENT_OPTIONS) | ||||
| 	@set -e; INSTALL_PREFIX=$(DEBIAN_DIR) ZAF_DEBUG=0 ./install.sh auto $(ZAF_OPTIONS) $(AGENT_OPTIONS) | ||||
| 	@. lib/zaf.lib.sh; \ | ||||
| 	. lib/ctrl.lib.sh; \ | ||||
| 	for p in $(PLUGINS); do \ | ||||
|   | ||||
							
								
								
									
										15
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										15
									
								
								README.md
									
									
									
									
									
								
							| @@ -19,24 +19,25 @@ So zaf is here for you :) | ||||
| * Sharing code. Many zabbix external checks share common shell functions.  | ||||
| * Simplification of userparameter functions (does not have to be one-line long code) | ||||
| * Zabbix discovery simplification. Creating zabbix item for discovery is not so easy in shell based system and result is not nice. But you can use framework function to do so. | ||||
| * OS packaging support (in future). | ||||
| * Zabbix agent autoinstallation and autoconfiguration suitable to use in puppet or another tool  | ||||
| * OS packaging support | ||||
|  | ||||
| ## Installing Zaf | ||||
| You need to be root and you must have curl installed on your system. Depending on your system, github certificates may not be available so you have to use *-k* option for curl (insecure). Default installation type is silent. So there will be no questions and everything will be autodetected. This simple command should be used on most systems: | ||||
| ``` | ||||
| curl -k https://raw.githubusercontent.com/limosek/zaf/1.1/install.sh | sh | ||||
| curl -k https://raw.githubusercontent.com/limosek/zaf/1.0/install.sh | sh | ||||
| ``` | ||||
|  | ||||
| ### Install options and autoconfiguration | ||||
| General parameters for install.sh on any system (simplest way how to install) | ||||
| ``` | ||||
| curl -k https://raw.githubusercontent.com/limosek/zaf/1.1/install.sh | \ | ||||
| curl -k https://raw.githubusercontent.com/limosek/zaf/1.0/install.sh | \ | ||||
|    sh -s {auto|interactive|debug-auto|debug-interactive} [Agent-Options] [Zaf-Options] | ||||
| ``` | ||||
| or use git version: | ||||
| ``` | ||||
| git clone https://github.com/limosek/zaf.git; cd zaf; git checkout 1.1 | ||||
| git clone https://github.com/limosek/zaf.git; cd zaf; git checkout 1.0 | ||||
| ./install.sh {auto|interactive|debug-auto|debug-interactive} [Agent-Options] [Zaf-Options] | ||||
|  Agent-Options: Z_Option=value [...] | ||||
|  Zaf-Options: ZAF_OPT=value [...] | ||||
| @@ -67,7 +68,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. | ||||
| 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.1/install.sh | sh -s auto \ | ||||
| curl -k https://raw.githubusercontent.com/limosek/zaf/1.0/install.sh | sh -s auto \ | ||||
|   Z_Server=zabbix.server.local \ | ||||
|   Z_ServerActive=zabbix.server.local \ | ||||
|   Z_HostnameItem=system.hostname Z_RefreshActiveChecks=60 \ | ||||
| @@ -79,10 +80,10 @@ You can make your own deb package with preconfigured option. It is up to you to | ||||
| ``` | ||||
| git clone https://github.com/limosek/zaf.git \ | ||||
|  && cd zaf \ | ||||
|  && git checkout 1.1 \ | ||||
|  && git checkout 1.0 \ | ||||
|  && git clone https://github.com/limosek/zaf-plugins.git \ | ||||
|  && make deb PLUGINS="./zaf-plugins/fsx" IPLUGINS="zaf" ZAF_OPTIONS="ZAF_GIT=0" AGENT_OPTIONS="Z_Server=zabbix.server Z_ServerActive=zabbix.server Z_StartAgents=8" | ||||
| sudo dpkg -i out/zaf-1.1.deb | ||||
| sudo dpkg -i out/zaf-1.0.deb | ||||
| ``` | ||||
| General usage: | ||||
| ``` | ||||
| @@ -124,7 +125,7 @@ During plugin installation, zaf will check all dependencies, do install binaries | ||||
| 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 Version 1.1. Please use some of this commands: | ||||
| zaf Version 1.0. Please use some of this commands: | ||||
| zaf update			To update repo | ||||
| zaf plugins			To list installed plugins | ||||
| zaf show [plugin]		To show installed plugins or plugin info | ||||
|   | ||||
| @@ -8,10 +8,9 @@ configure) | ||||
| 	. /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} | ||||
| 	zaf reinstall {PLUGINS} {IPLUGINS} | ||||
|   fi | ||||
| ;; | ||||
| esac | ||||
|  | ||||
| true | ||||
|  | ||||
|   | ||||
| @@ -1,12 +0,0 @@ | ||||
| ZAF_PKG='dpkg' | ||||
| ZAF_AGENT_PKG='zabbix-agent' | ||||
| ZAF_TMP_BASE='/tmp/zaf' | ||||
| ZAF_LIB_DIR='/usr/lib/zaf' | ||||
| ZAF_BIN_DIR='/usr/bin' | ||||
| ZAF_PLUGINS_DIR='/usr/lib/zaf/plugins' | ||||
| ZAF_PLUGINS_URL='https://raw.githubusercontent.com/limosek/zaf-plugins/master/' | ||||
| ZAF_PLUGINS_GITURL='https://github.com/limosek/zaf-plugins.git' | ||||
| ZAF_REPO_DIR='/usr/lib/zaf/repo' | ||||
| ZAF_AGENT_CONFIGD='/etc/zabbix/zabbix_agentd.d' | ||||
| ZAF_AGENT_BIN='/usr/sbin/zabbix_agentd' | ||||
| ZAF_AGENT_RESTART='service zabbix-agent restart' | ||||
							
								
								
									
										35
									
								
								install.sh
									
									
									
									
									
								
							
							
						
						
									
										35
									
								
								install.sh
									
									
									
									
									
								
							| @@ -3,11 +3,10 @@ | ||||
| [ -z "$ZAF_DEBUG" ] && ZAF_DEBUG=1 | ||||
| if [ -z "$ZAF_URL" ]; then | ||||
| 	# Runing as standalone install.sh. We have to download rest of files first. | ||||
| 	[ -z "$ZAF_VERSION" ] && ZAF_VERSION=1.0 | ||||
| 	ZAF_URL="https://github.com/limosek/zaf/" | ||||
| fi | ||||
|  | ||||
| [ -z "$ZAF_GITBRANCH" ] && ZAF_GITBRANCH=1.1 | ||||
|  | ||||
| # Lite version of zaf_fetch_url, full version will be loaded later | ||||
| zaf_fetch_url(){ | ||||
| 	if [ -z "$ZAF_OFFLINE" ]; then | ||||
| @@ -21,11 +20,11 @@ zaf_fetch_url(){ | ||||
| # Download tgz and extract to /tmp/zaf-installer | ||||
| zaf_download_files() { | ||||
| 	rm -rf /tmp/zaf-installer | ||||
| 	zaf_fetch_url $ZAF_URL/archive/$ZAF_GITBRANCH.tar.gz | tar -f - -C /tmp -zx && mv /tmp/zaf-$ZAF_GITBRANCH /tmp/zaf-installer | ||||
| 	zaf_fetch_url $ZAF_URL/archive/$ZAF_VERSION.tar.gz | tar -f - -C /tmp -zx && mv /tmp/zaf-$ZAF_VERSION /tmp/zaf-installer | ||||
| } | ||||
|  | ||||
| if ! [ -f README.md ]; then | ||||
| 	# We are runing from stdin | ||||
| 	# We are runing from stdin	 | ||||
| 	if ! which curl >/dev/null; | ||||
| 	then | ||||
| 		zaf_err "Curl not found. Cannot continue. Please install it." | ||||
| @@ -41,8 +40,6 @@ 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 | ||||
| @@ -179,7 +176,7 @@ zaf_configure_agent() { | ||||
| 		else | ||||
| 			zaf_unset_agent_option "$option" | ||||
| 		fi | ||||
| 		options="$options Z_$option=$value" | ||||
| 		options="$options Z_$option='$value'" | ||||
| 	done | ||||
| 	zaf_set_option ZAF_AGENT_OPTIONS "${options}" | ||||
| } | ||||
| @@ -227,13 +224,7 @@ zaf_configure(){ | ||||
| 	zaf_get_option ZAF_AGENT_CONFIGD "Zabbix agent config.d" "/etc/zabbix/zabbix_agentd.conf.d/" "$INSTALL_MODE" | ||||
| 	zaf_get_option ZAF_AGENT_BIN "Zabbix agent binary" "/usr/sbin/zabbix_agentd" "$INSTALL_MODE" | ||||
| 	zaf_get_option ZAF_AGENT_RESTART "Zabbix agent restart cmd" "service zabbix-agent restart" "$INSTALL_MODE" | ||||
| 	zaf_get_option ZAF_SUDOERSD "Sudo sudoers.d directory" "/etc/sudoers.d" "$INSTALL_MODE" | ||||
| 	zaf_get_option ZAF_CROND "Cron.d directory" "/etc/cron.d" "$INSTALL_MODE" | ||||
| 	zaf_get_option ZAF_ZBXAPI_URL "Zabbix API url" "http://localhost/zabbix/api_jsonrpc.php" "$INSTALL_MODE" | ||||
| 	zaf_get_option ZAF_ZBXAPI_USER "Zabbix API user" "zaf" "$INSTALL_MODE" | ||||
| 	zaf_get_option ZAF_ZBXAPI_PASS "Zabbix API password" "" "$INSTALL_MODE" | ||||
| 	zaf_get_option ZAF_ZBXAPI_AUTHTYPE "Zabbix API authentication type" "internal" "$INSTALL_MODE" | ||||
|  | ||||
| 	 | ||||
| 	if zaf_is_root && ! [ -x $ZAF_AGENT_BIN ]; then | ||||
| 		zaf_err "Zabbix agent ($ZAF_AGENT_BIN) not installed? Use ZAF_AGENT_BIN env variable to specify location. Exiting." | ||||
| 	fi | ||||
| @@ -260,12 +251,6 @@ zaf_configure(){ | ||||
| 	zaf_set_option ZAF_AGENT_CONFIGD "$ZAF_AGENT_CONFIGD" | ||||
| 	zaf_set_option ZAF_AGENT_BIN "$ZAF_AGENT_BIN" | ||||
| 	zaf_set_option ZAF_AGENT_RESTART "$ZAF_AGENT_RESTART" | ||||
| 	zaf_set_option ZAF_SUDOERSD "$ZAF_SUDOERSD" | ||||
| 	zaf_set_option ZAF_CROND "$ZAF_CROND" | ||||
| 	zaf_set_option ZAF_ZBXAPI_URL "$ZAF_ZBXAPI_URL" | ||||
| 	zaf_set_option ZAF_ZBXAPI_USER "$ZAF_ZBXAPI_USER" | ||||
| 	zaf_set_option ZAF_ZBXAPI_PASS "$ZAF_ZBXAPI_PASS" | ||||
| 	zaf_set_option ZAF_ZBXAPI_AUTHTYPE "$ZAF_ZBXAPI_AUTHTYPE" | ||||
| 	[ -n "$ZAF_PREPACKAGED_DIR" ] && zaf_set_option ZAF_PREPACKAGED_DIR "$ZAF_PREPACKAGED_DIR" | ||||
|  | ||||
| 	if zaf_is_root; then | ||||
| @@ -278,15 +263,15 @@ zaf_install_all() { | ||||
| 	rm -rif ${ZAF_TMP_DIR} | ||||
| 	mkdir -p ${ZAF_TMP_DIR} | ||||
| 	zaf_install_dir ${ZAF_LIB_DIR} | ||||
| 	for i in lib/zaf.lib.sh lib/os.lib.sh lib/ctrl.lib.sh lib/cache.lib.sh lib/zbxapi.lib.sh README.md; do | ||||
| 		zaf_install $i ${ZAF_LIB_DIR}/ || zaf_err "Error installing $i" | ||||
| 	for i in lib/zaf.lib.sh lib/os.lib.sh lib/ctrl.lib.sh README.md; do | ||||
| 		zaf_install $i ${ZAF_LIB_DIR}/ | ||||
| 	done | ||||
| 	for i in lib/zaflock lib/preload.sh; do | ||||
| 		zaf_install_bin $i ${ZAF_LIB_DIR}/ || zaf_err "Error installing $i" | ||||
| 		zaf_install_bin $i ${ZAF_LIB_DIR}/ | ||||
| 	done | ||||
| 	zaf_install_dir ${ZAF_BIN_DIR} | ||||
| 	for i in zaf; do | ||||
| 		zaf_install_bin $i ${ZAF_BIN_DIR}/ || zaf_err "Error installing $i" | ||||
| 		zaf_install_bin $i ${ZAF_BIN_DIR}/ | ||||
| 	done | ||||
| 	zaf_install_dir ${ZAF_PLUGINS_DIR} | ||||
| 	zaf_install_dir ${ZAF_PLUGINS_DIR} | ||||
| @@ -307,7 +292,7 @@ zaf_postconfigure() { | ||||
| 	else | ||||
| 	    [ "${ZAF_GIT}" = 1 ] && [ -n  "${INSTALL_PREFIX}" ] && git clone "${ZAF_REPO_GITURL}" "${INSTALL_PREFIX}/${ZAF_REPO_DIR}" | ||||
|         fi | ||||
| 	zaf_wrn "Install done. Use 'zaf' to get started." | ||||
| 	zaf_wrn "Install done. Use 'zaf' to get started. Do not forget to do 'zaf upgrade' to upgrade plugins too!" | ||||
| 	true | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -1,84 +0,0 @@ | ||||
| # Zaf cache related functions | ||||
|  | ||||
| zaf_cache_clean(){ | ||||
| 	if [ -n "$ZAF_CACHE_DIR" ]; then | ||||
| 		zaf_wrn "Removing cache entries" | ||||
| 		rm -rf "$ZAF_CACHE_DIR" | ||||
| 	else | ||||
| 		zaf_err "Cache dir not set." | ||||
| 	fi | ||||
| 	mkdir -p "$ZAF_CACHE_DIR" | ||||
| } | ||||
|  | ||||
| # Get cache key from requested param | ||||
| zaf_cache_key(){ | ||||
| 	echo "$1" | md5sum - | cut -d ' ' -f 1 | ||||
| } | ||||
|  | ||||
| # Put object into cache | ||||
| # $1 key | ||||
| # $2 value | ||||
| # $3 lifetime in seconds | ||||
| zaf_tocache(){ | ||||
| 	! [ -w $ZAF_CACHE_DIR ] && return 1 | ||||
| 	local key | ||||
| 	local value | ||||
|  | ||||
| 	key=$(zaf_cache_key "$1") | ||||
| 	echo "$2" >$ZAF_CACHE_DIR/$key | ||||
| 	echo "$1" >$ZAF_CACHE_DIR/$key.info | ||||
| 	touch -m -d "$3 seconds" $ZAF_CACHE_DIR/$key.info | ||||
| 	zaf_trc "Cache: Saving entry $1($key)" | ||||
| } | ||||
|  | ||||
| # Put object into cache from stdin and copy to stdout | ||||
| # $1 key | ||||
| # $2 lifetime in seconds | ||||
| zaf_tocache_stdin(){ | ||||
| 	! [ -w $ZAF_CACHE_DIR ] && return 1 | ||||
| 	local key | ||||
|  | ||||
| 	key=$(zaf_cache_key "$1") | ||||
| 	cat >$ZAF_CACHE_DIR/$key | ||||
| 	if [ -s $ZAF_CACHE_DIR/$key ]; then | ||||
| 		zaf_trc "Cache: Saving entry $1($key)" | ||||
| 		echo "$1" >$ZAF_CACHE_DIR/$key.info | ||||
| 		touch -m -d "$2 seconds" $ZAF_CACHE_DIR/$key.info | ||||
| 		cat $ZAF_CACHE_DIR/$key | ||||
| 	else | ||||
| 		rm $ZAF_CACHE_DIR/$key | ||||
| 	fi | ||||
| } | ||||
|  | ||||
| # Remove entry from cache | ||||
| # $1 key | ||||
| zaf_cache_delentry(){ | ||||
| 	! [ -w $ZAF_CACHE_DIR ] && return 1 | ||||
| 	local key | ||||
| 	key=$(zaf_cache_key "$1") | ||||
| 	zaf_trc "Cache: removing $1($key) from cache" | ||||
| 	rm -f "$ZAF_CACHE_DIR/$key*" | ||||
| } | ||||
|  | ||||
| # Get object from cache | ||||
| # $1 key | ||||
| zaf_fromcache(){ | ||||
| 	! [ -r $ZAF_CACHE_DIR ] || [ -n "$ZAF_NOCACHE" ] && return 1 | ||||
| 	local key | ||||
| 	local value | ||||
| 	key=$(zaf_cache_key "$1") | ||||
| 	if [ -f $ZAF_CACHE_DIR/$key ]; then | ||||
| 		if [ "$ZAF_CACHE_DIR/$key.info" -nt "$ZAF_CACHE_DIR/$key" ]; then | ||||
| 			zaf_trc "Cache: serving $1($key) from cache" | ||||
| 			cat $ZAF_CACHE_DIR/$key | ||||
| 		else | ||||
| 			zaf_trc "Cache: removing old entry $1" | ||||
| 			rm -f "$ZAF_CACHE_DIR/$key*" | ||||
| 			return 2 | ||||
| 		fi | ||||
| 	else | ||||
| 		zaf_trc "Cache: missing entry $1($key)" | ||||
| 		return 1 | ||||
| 	fi | ||||
| } | ||||
|  | ||||
							
								
								
									
										116
									
								
								lib/ctrl.lib.sh
									
									
									
									
									
								
							
							
						
						
									
										116
									
								
								lib/ctrl.lib.sh
									
									
									
									
									
								
							| @@ -22,7 +22,7 @@ zaf_ctrl_get_item_block() { | ||||
| # Get global plugin block body from stdin | ||||
| # $1 itemname | ||||
| zaf_ctrl_get_global_block() { | ||||
| 	grep -v '^#' | awk '{ i=0; print $0; | ||||
| 	grep -v '^#' | awk '{ i=0; | ||||
| 	while (i==0) { | ||||
| 		getline; | ||||
| 		if (/^Item /) exit; | ||||
| @@ -33,7 +33,7 @@ zaf_ctrl_get_global_block() { | ||||
| # Get item multiline option | ||||
| # $1 optionname | ||||
| zaf_block_get_moption() { | ||||
| 	awk '/^'$1'::$/ { i=0; print $0; | ||||
| 	awk '/^'$1'::$/ { i=0; | ||||
| 	while (i==0) { | ||||
| 		getline; | ||||
| 		if (/^::$/) {i=1; continue;}; | ||||
| @@ -55,36 +55,16 @@ zaf_block_get_option() { | ||||
| # $1 - control file | ||||
| # $2 - option name | ||||
| zaf_ctrl_get_global_option() { | ||||
| 	local ctrlvar | ||||
| 	local ctrlopt | ||||
|  | ||||
| 	ctrlopt="ZAF_CTRL_$(zaf_stripctrl $2)" | ||||
| 	eval ctrlvar=\$$ctrlopt | ||||
| 	if [ -n "$ctrlvar" ]; then | ||||
| 		zaf_dbg "Overriding control field $2 from env $ctrlopt($ctrlvar)" | ||||
| 		echo $ctrlopt | ||||
| 	else | ||||
| 		zaf_ctrl_get_global_block <$1 | zaf_block_get_moption "$2" \ | ||||
| 		|| zaf_ctrl_get_global_block <$1 | zaf_block_get_option "$2" | ||||
| 	fi | ||||
| 	zaf_ctrl_get_global_block <$1 | zaf_block_get_moption "$2" \ | ||||
| 	|| zaf_ctrl_get_global_block <$1 | zaf_block_get_option "$2" | ||||
| } | ||||
| # Get item specific option (single or multiline) | ||||
| # $1 - control file | ||||
| # $2 - item name | ||||
| # $3 - option name | ||||
| zaf_ctrl_get_item_option() { | ||||
| 	local ctrlvar | ||||
| 	local ctrlopt | ||||
|  | ||||
| 	ctrlopt="ZAF_CTRLI_$(zaf_stripctrl $2)_$(zaf_stripctrl $3)" | ||||
| 	eval ctrlvar=\$$ctrlopt | ||||
| 	if [ -n "$ctrlvar" ]; then | ||||
| 		zaf_dbg "Overriding item control field $2/$3 from env $ctrlopt($ctrlvar)" | ||||
| 		echo $ctrlopt | ||||
| 	else | ||||
| 		zaf_ctrl_get_item_block <$1 "$2" | zaf_block_get_moption "$3" \ | ||||
| 		|| zaf_ctrl_get_item_block <$1 "$2" | zaf_block_get_option "$3" | ||||
| 	fi | ||||
| 	zaf_ctrl_get_item_block <$1 "$2" | zaf_block_get_moption "$3" \ | ||||
| 	|| zaf_ctrl_get_item_block <$1 "$2" | zaf_block_get_option "$3" | ||||
| } | ||||
|  | ||||
| # Check dependencies based on control file | ||||
| @@ -102,53 +82,7 @@ zaf_ctrl_check_deps() { | ||||
| 	done | ||||
| } | ||||
|  | ||||
| # Install sudo config from control | ||||
| # $1 plugin | ||||
| # $2 control | ||||
| # $3 plugindir | ||||
| zaf_ctrl_sudo() { | ||||
| 	local pdir | ||||
| 	local plugin | ||||
| 	local sudo | ||||
| 	local cmd | ||||
| 	local parms | ||||
|  | ||||
| 	pdir="$3" | ||||
| 	plugin=$1 | ||||
| 	zaf_dbg "Installing sudoers entry $ZAF_SUDOERSD/zaf_$plugin" | ||||
| 	sudo=$(zaf_ctrl_get_global_option $2 "Sudo" | zaf_far '{PLUGINDIR}' "${plugindir}") | ||||
| 	[ -z "$sudo" ] && return  # Nothing to install | ||||
| 	if ! which sudo >/dev/null; then | ||||
| 		zaf_wrn "Sudo needed bud not installed?" | ||||
| 	fi | ||||
| 	cmd=$(echo $sudo | cut -d ' ' -f 1) | ||||
| 	parms=$(echo $sudo | cut -d ' ' -f 2-) | ||||
| 	if which $cmd >/dev/null ; then | ||||
| 		(echo "zabbix ALL=NOPASSWD:SETENV: $(which $cmd) $(echo $parms | tr '%' '*')";echo) >$ZAF_SUDOERSD/zaf_$plugin || zaf_err "Error during zaf_ctrl_sudo" | ||||
| 		chmod 0440 $ZAF_SUDOERSD/zaf_$plugin | ||||
| 	else | ||||
| 		zaf_err "Cannot find binary '$cmd' to put into sudoers." | ||||
| 	fi | ||||
| } | ||||
|  | ||||
| # Install crontab config from control | ||||
| # $1 plugin | ||||
| # $2 control | ||||
| # $3 plugindir | ||||
| zaf_ctrl_cron() { | ||||
| 	local pdir | ||||
| 	local plugin | ||||
| 	local cron | ||||
|  | ||||
| 	pdir="$3" | ||||
| 	plugin=$1 | ||||
| 	zaf_dbg "Installing cron entry $ZAF_CROND/zaf_$plugin" | ||||
| 	cron=$(zaf_ctrl_get_global_option $2 "Cron") | ||||
| 	[ -z "$cron" ] && return # Nothing to install | ||||
| 	zaf_ctrl_get_global_option $2 "Cron" | zaf_far '{PLUGINDIR}' "${plugindir}" >$ZAF_CROND/zaf_$plugin || zaf_err "Error during zaf_ctrl_cron" | ||||
| } | ||||
|  | ||||
| # Install files defined to be installed in control to plugun directory | ||||
| # Install binaries from control | ||||
| # $1 pluginurl | ||||
| # $2 control | ||||
| # $3 plugindir | ||||
| @@ -156,26 +90,18 @@ zaf_ctrl_install() { | ||||
| 	local binaries | ||||
| 	local pdir | ||||
| 	local script | ||||
| 	local files | ||||
| 	local f | ||||
| 	local b | ||||
| 	local cmd | ||||
|  | ||||
| 	pdir="$3" | ||||
| 	(set -e | ||||
| 	binaries=$(zaf_ctrl_get_global_option $2 "Install-bin") | ||||
| 	for b in $binaries; do | ||||
| 		zaf_fetch_url "$1/$b" >"${ZAF_TMP_DIR}/$b" | ||||
|                 zaf_install_bin "${ZAF_TMP_DIR}/$b" "$pdir" | ||||
| 	done | ||||
| 	files=$(zaf_ctrl_get_global_option $2 "Install-files") | ||||
| 	for f in $files; do | ||||
| 		zaf_fetch_url "$1/$b" >"${ZAF_TMP_DIR}/$b" | ||||
|                 zaf_install "${ZAF_TMP_DIR}/$b" "$pdir" | ||||
| 	done | ||||
| 	script=$(zaf_ctrl_get_global_option $2 "Install-script") | ||||
| 	[ -n "$script" ] && eval "$script" | ||||
| 	true | ||||
| 	) || zaf_err "Error during zaf_ctrl_install" | ||||
| 	cmd=$(zaf_ctrl_get_global_option $2 "Install-cmd") | ||||
| 	[ -n "$cmd" ] && $cmd | ||||
| } | ||||
|  | ||||
| # Generates zabbix cfg from control file | ||||
| @@ -187,12 +113,10 @@ zaf_ctrl_generate_cfg() { | ||||
| 	local iscript | ||||
| 	local ikey | ||||
| 	local lock | ||||
| 	local cache | ||||
|  | ||||
| 	items=$(zaf_ctrl_get_items <"$1") | ||||
| 	(set -e | ||||
| 	for i in $items; do | ||||
|             iscript=$(zaf_stripctrl $i) | ||||
|             iscript=$(echo $i | tr -d '[]*&;:') | ||||
| 	    params=$(zaf_ctrl_get_item_option $1 $i "Parameters") | ||||
| 	    if [ -n "$params" ]; then | ||||
| 		ikey="$2.$i[*]" | ||||
| @@ -209,27 +133,25 @@ zaf_ctrl_generate_cfg() { | ||||
| 	    if [ -n "$lock" ]; then | ||||
| 		lock="${ZAF_LIB_DIR}/zaflock $lock " | ||||
| 	    fi | ||||
| 	    cache=$(zaf_ctrl_get_item_option $1 $i "Cache") | ||||
| 	    if [ -n "$cache" ]; then | ||||
| 		cache="_cache '$cache' " | ||||
| 	    fi | ||||
|             cmd=$(zaf_ctrl_get_item_option $1 $i "Cmd") | ||||
|             if [ -n "$cmd" ]; then | ||||
|                 $(which echo) "UserParameter=$ikey,${ZAF_LIB_DIR}/preload.sh $cache $lock$cmd"; | ||||
|                 $(which echo) "UserParameter=$ikey,${ZAF_LIB_DIR}/preload.sh $lock$cmd"; | ||||
|                 continue | ||||
|             fi | ||||
|             cmd=$(zaf_ctrl_get_item_option $1 $i "Function") | ||||
|             if [ -n "$cmd" ]; then | ||||
|                 $(which echo) "UserParameter=$ikey,${ZAF_LIB_DIR}/preload.sh $lock$cmd"; | ||||
|                 continue; | ||||
|             fi | ||||
|             cmd=$(zaf_ctrl_get_item_option $1 $i "Script") | ||||
|             if [ -n "$cmd" ]; then | ||||
|                 zaf_ctrl_get_item_option $1 $i "Script" | \ | ||||
| 		  zaf_far '{INCLUDES}' '. /etc/zaf.conf; . ${ZAF_LIB_DIR}/zaf.lib.sh; . ${ZAF_LIB_DIR}/ctrl.lib.sh; . ${ZAF_LIB_DIR}/zbxapi.lib.sh; . ${ZAF_LIB_DIR}/cache.lib.sh; ' \ | ||||
| 		  >${ZAF_TMP_DIR}/${iscript}.sh; | ||||
|                 zaf_ctrl_get_item_option $1 $i "Script" >${ZAF_TMP_DIR}/${iscript}.sh; | ||||
|                 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"; | ||||
|                 $(which echo) "UserParameter=$ikey,${ZAF_LIB_DIR}/preload.sh $lock${ZAF_PLUGINS_DIR}/$2/${iscript}.sh $args"; | ||||
|                 continue; | ||||
|             fi | ||||
| 	    zaf_err "Item $i declared in control file but has no Cmd, Function or Script!" | ||||
| 	done | ||||
| 	) || zaf_err "Error during zaf_ctrl_generate_cfg" | ||||
| } | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -4,34 +4,19 @@ | ||||
|  | ||||
| [ -z "$ZAF_TMP_BASE" ] && ZAF_TMP_BASE=/tmp/zaf | ||||
| ZAF_TMP_DIR="${ZAF_TMP_BASE}-${USER}" | ||||
| [ -z "$ZAF_CACHE_DIR" ] && ZAF_CACHE_DIR=${ZAF_TMP_BASE}c | ||||
|  | ||||
| trap "rm -rif ${ZAF_TMP_DIR}" EXIT | ||||
| ! [ -d "${ZAF_TMP_DIR}" ] && mkdir "${ZAF_TMP_DIR}" | ||||
| ! [ -d "${ZAF_CACHE_DIR}" ] && mkdir "${ZAF_CACHE_DIR}" | ||||
| [ -z "$ZAF_DEBUG" ] && ZAF_DEBUG=1 | ||||
|  | ||||
| . ${ZAF_LIB_DIR}/zaf.lib.sh | ||||
| . ${ZAF_LIB_DIR}/ctrl.lib.sh | ||||
| . ${ZAF_LIB_DIR}/os.lib.sh | ||||
| . ${ZAF_LIB_DIR}/zbxapi.lib.sh | ||||
| . ${ZAF_LIB_DIR}/cache.lib.sh | ||||
|  | ||||
| export ZAF_LIB_DIR | ||||
| export ZAF_TMP_DIR | ||||
| export ZAF_PLUGINS_DIR | ||||
|  | ||||
| if [ "$1" = "_cache" ] || [ "$1" = "_nocache" ] ; then | ||||
| 	[ "$1" = "_nocache" ] && export ZAF_NOCACHE=1 | ||||
| 	shift | ||||
| 	seconds=$1 | ||||
| 	shift | ||||
| 	parms=$(echo $*|tr -d ' ') | ||||
| 	if ! zaf_fromcache $parms; then | ||||
| 		([ "$(basename $0)" = "preload.sh" ] && [ -n "$*" ] && $@ ) | zaf_tocache_stdin $parms $seconds | ||||
| 	fi | ||||
| else | ||||
| 	[ "$(basename $0)" = "preload.sh" ] && [ -n "$*" ] && $@ | ||||
| fi | ||||
| [ "$(basename $0)" = "preload.sh" ] && [ -n "$*" ] && $@ | ||||
|  | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -1,7 +1,6 @@ | ||||
|  | ||||
| # Hardcoded variables | ||||
| ZAF_VERSION="1.1" | ||||
| ZAF_GITBRANCH="1.1" | ||||
| ZAF_VERSION="1.0" | ||||
| ZAF_URL="https://github.com/limosek/zaf" | ||||
| ZAF_RAW_URL="https://raw.githubusercontent.com/limosek/zaf" | ||||
|  | ||||
| @@ -10,9 +9,6 @@ ZAF_RAW_URL="https://raw.githubusercontent.com/limosek/zaf" | ||||
| zaf_msg() { | ||||
| 	echo $@ | ||||
| } | ||||
| zaf_trc() { | ||||
| 	[ "$ZAF_DEBUG" -ge "3" ] && logger -s -t zaf -- $@ | ||||
| } | ||||
| zaf_dbg() { | ||||
| 	[ "$ZAF_DEBUG" -ge "2" ] && logger -s -t zaf -- $@ | ||||
| } | ||||
| @@ -36,11 +32,7 @@ zaf_fetch_url() { | ||||
| 	local scheme | ||||
| 	local uri | ||||
| 	local insecure | ||||
| 	local out | ||||
| 	 | ||||
| 	if zaf_fromcache "$1"; then | ||||
| 		return | ||||
| 	fi | ||||
| 	scheme=$(echo $1|cut -d ':' -f 1) | ||||
| 	uri=$(echo $1|cut -d '/' -f 3-) | ||||
| 	if [ "$1" = "$scheme" ]; then | ||||
| @@ -51,7 +43,7 @@ zaf_fetch_url() { | ||||
| 		[ "$scheme" != "file" ] && [ -n "$ZAF_OFFLINE" ] && zaf_err "Cannot download $1 in offline mode!" | ||||
| 		[ "${ZAF_CURL_INSECURE}" = "1" ] && insecure="-k" | ||||
| 		zaf_dbg curl $insecure -f -s -L -o - $1 | ||||
| 		curl $insecure -f -s -L -o - "$1" | zaf_tocache_stdin "$1" 120 | ||||
| 		curl $insecure -f -s -L -o - "$1" | ||||
| 	;; | ||||
| 	esac  | ||||
| } | ||||
| @@ -73,27 +65,6 @@ zaf_far(){ | ||||
|    eval $sedcmd | ||||
| } | ||||
|  | ||||
| # Limit concurrent processes or continue | ||||
| zaf_bglimit(){ | ||||
|     local maxbg | ||||
|     local maxnumber | ||||
|     local cnumber | ||||
|     if [ $# -eq 0 ] ; then | ||||
|             maxbg=5 | ||||
|     else | ||||
| 	    maxbg=$1 | ||||
|     fi | ||||
|     maxnumber=$((0 + ${1:-0})) | ||||
|     while true; do | ||||
|             cnumber=$(jobs | wc -l) | ||||
|             if [ $cnumber -lt $maxnumber ]; then | ||||
|                     break | ||||
|             fi | ||||
| 	    zaf_dbg "Limiting next job due to $maxbg limit of bg jobs" | ||||
|             sleep 1 | ||||
|     done | ||||
| } | ||||
|  | ||||
| # Initialises discovery function | ||||
| zaf_discovery_begin(){ | ||||
| cat <<EOF | ||||
| @@ -231,14 +202,8 @@ zaf_plugin_info() { | ||||
| 	[ -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 | ||||
| 	echo "Items: $pitems" | ||||
| 	echo | ||||
| } | ||||
|  | ||||
| @@ -267,23 +232,16 @@ zaf_install_plugin() { | ||||
| 	local plugin | ||||
| 	local plugindir | ||||
| 	local control | ||||
| 	local version | ||||
|  | ||||
| 	if zaf_prepare_plugin "$1" "${ZAF_TMP_DIR}/plugin"; then | ||||
| 		url=$(zaf_get_plugin_url "$1") | ||||
| 		control="${ZAF_TMP_DIR}/plugin/control.zaf" | ||||
|                 plugin=$(zaf_ctrl_get_global_option $control Plugin) | ||||
| 		version=$(zaf_ctrl_get_global_option $control Version) | ||||
|                 plugin=$(zaf_ctrl_get_global_block <"${ZAF_TMP_DIR}/plugin/control.zaf" | zaf_block_get_option Plugin) | ||||
| 		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_DEBUG" -gt 0 ] && 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" | ||||
| @@ -348,15 +306,11 @@ 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. "; | ||||
| @@ -366,25 +320,12 @@ zaf_list_plugin_items() { | ||||
| 	items=$(zaf_ctrl_get_items <$cfile) | ||||
| 	for i in $items; do | ||||
| 		p=$(zaf_ctrl_get_item_option $cfile $i "Parameters") | ||||
| 		testparms=$(zaf_ctrl_get_item_option $cfile $i "Testparameters") | ||||
| 		precache=$(zaf_ctrl_get_item_option $cfile $i "Precache") | ||||
| 		if [ -n "$p" ]; then | ||||
| 			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 | ||||
| 			key="$1.$i[]" | ||||
| 		else | ||||
| 			key="$1.$i" | ||||
| 		fi | ||||
| 		[ "$2" != "precache" ] && echo -n "$key " | ||||
| 		echo -n "$key " | ||||
| 	done | ||||
| 	echo | ||||
| } | ||||
| @@ -406,20 +347,15 @@ zaf_get_item() { | ||||
| } | ||||
|  | ||||
| zaf_test_item() { | ||||
| 	[ "$USER" != "zabbix" ] && zaf_wrn "You are not zabbix user. Test will be run with your privileges and sudo access!" | ||||
| 	$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))" | ||||
| 	! [ -d ${ZAF_PLUGINS_DIR}/$1 ] && { zaf_err "Plugin $1 not installed!"; } | ||||
| 	zaf_wrn "Removing plugin $1" | ||||
| 	rm -rf ${ZAF_PLUGINS_DIR}/$1 | ||||
| 	rm -f ${ZAF_AGENT_CONFIGD}/zaf_$1.conf ${ZAF_CROND}/zaf_$1 ${ZAF_SUDOERSD}/zaf_$1 | ||||
| 	rm -f ${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf | ||||
| } | ||||
|  | ||||
| zaf_tolower() { | ||||
| @@ -430,9 +366,3 @@ zaf_toupper() { | ||||
| 	tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' | ||||
| } | ||||
|  | ||||
| # Return simplified key with discarded special chars. | ||||
| zaf_stripctrl() { | ||||
| 	echo $1 | tr '[]*&;:.-' '________' | ||||
| } | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -1,188 +0,0 @@ | ||||
| # Call api function  | ||||
| # $1 - query string | ||||
| zaf_zbxapi_do() { | ||||
| 	local result | ||||
| 	if ! zaf_fromcache "$1"; then | ||||
| 		zaf_trc "Zabbix API: $1" | ||||
| 		result=$(curl -s -f -L -X POST -H 'Content-Type: application/json-rpc' -d "$1" "$ZAF_ZBXAPI_URL") | ||||
| 		if [ $? = 0 ] && echo $result | grep -q '"result":'; then | ||||
| 			zaf_tocache "$1" "$result" 60		 | ||||
| 			echo $result | ||||
| 		else | ||||
| 			zaf_err "Error processing API request. ($?,$result)" | ||||
| 		fi | ||||
| 	fi | ||||
| } | ||||
|  | ||||
| # Extract result from JSON response | ||||
| zaf_zbxapi_getresult() { | ||||
| 	sed -e 's/\({"jsonrpc":"2.0","result":\)\(.*\),\("id":.*\)/\2/g' | sed -e 's/^\[\]$//' | ||||
| } | ||||
|  | ||||
| # Extract XML result from JSON response | ||||
| zaf_zbxapi_getxml() { | ||||
| 	zaf_zbxapi_getresult | sed -e 's/{"jsonrpc":"2.0","result":"//' | sed -e 's/","id"\:1}//' | sed -e 's#\\\([<">/]\)#\1#g' | ||||
| } | ||||
|  | ||||
| # Extract string from JSON response result | ||||
| zaf_zbxapi_getstring() { | ||||
| 	 sed -e 's/^"//'  -e 's/"$//' -e 's/\\n/'\\n'/g' | ||||
| } | ||||
|  | ||||
| # Extract value from JSON response result | ||||
| # $1 key | ||||
| zaf_zbxapi_getvalue() { | ||||
| 	 tr ',' '\n' | grep "\"$1\":" | cut -d '"' -f 4 | ||||
| } | ||||
|  | ||||
| # Zabbix API related functions | ||||
| # Parameters in global variables ZAF_ZBX_API_* | ||||
| # returns auth on stdout or false | ||||
| zaf_zbxapi_login(){ | ||||
|  local authstr | ||||
|  local user | ||||
|  local pass | ||||
|  local result | ||||
|  | ||||
|  [ -z "$ZAF_ZBXAPI_URL" ] || [ -z "$ZAF_ZBXAPI_USER" ] || [ -z "$ZAF_ZBXAPI_PASS" ] && zaf_err "Zabbix Api parameters not set! Set ZAF_ZBXAPI_URL, ZAF_ZBXAPI_USER and ZAF_ZBXAPI_PASS and try again." | ||||
|  authstr='{ | ||||
|    "method" : "user.login", | ||||
|    "params" : { | ||||
|       "password" : "'$ZAF_ZBXAPI_PASS'", | ||||
|       "user" : "'$ZAF_ZBXAPI_USER'" | ||||
|    }, | ||||
|    "id" : 0, | ||||
|    "jsonrpc" : "2.0" | ||||
|  }' | ||||
|   | ||||
|  if [ "$ZAF_ZBXAPI_AUTHTYPE" = "http" ] ; then | ||||
|     ZAF_ZBXAPI_URL=$(echo $ZAF_ZBXAPI_URL | cut -d '/' -f 1)//$ZAF_ZBXAPI_USER:$ZAF_ZBXAPI_PASS@$(echo $ZAF_ZBXAPI_URL | cut -d '/' -f 3-) | ||||
|  fi | ||||
|  result=$(zaf_zbxapi_do "$authstr") | ||||
|  ZAF_ZBXAPI_AUTH=$(echo $result |zaf_zbxapi_getresult| zaf_zbxapi_getstring) | ||||
|  zaf_dbg "Logged into zabbix API ($ZAF_ZBXAPI_AUTH)" | ||||
| } | ||||
|  | ||||
| # $1 hostgroup name | ||||
| zaf_zbxapi_gethostgroupid() { | ||||
|  local hstr | ||||
|  local filter | ||||
|  local gfilter | ||||
|   | ||||
|  hstr='{ | ||||
|     "method": "hostgroup.get", | ||||
|     "jsonrpc": "2.0", | ||||
|     "auth": "'$ZAF_ZBXAPI_AUTH'", | ||||
|     "params": { | ||||
| 	"filter": { | ||||
| 	 "name": ["'$1'"] | ||||
| 	}, | ||||
| 	"output": "shorten" | ||||
|     }, | ||||
|     "id": 1 | ||||
|  }' | ||||
|  zaf_zbxapi_do "$hstr" | zaf_zbxapi_getresult | tr ',' '\n' | cut -d '"' -f 4 | ||||
| } | ||||
|  | ||||
| # $1 hostname | ||||
| zaf_zbxapi_gethostid() { | ||||
|  local hstr | ||||
|  local host | ||||
|  local groupid | ||||
|  local filter | ||||
|  local gfilter | ||||
|   | ||||
|  host="$1" | ||||
|  if [ -n "$host" ] ; then | ||||
|    filter='"filter": { "host": [ "'$host'" ] },' | ||||
|  fi | ||||
|  hstr='{ | ||||
|     "method": "host.get", | ||||
|     "jsonrpc": "2.0", | ||||
|     "auth": "'$ZAF_ZBXAPI_AUTH'", | ||||
|     "params": { | ||||
| 	'$filter' | ||||
| 	"output": "shorten" | ||||
|     }, | ||||
|     "id": 1 | ||||
|  }' | ||||
|  zaf_zbxapi_do "$hstr" | zaf_zbxapi_getresult | tr ',' '\n' | cut -d '"' -f 4 | ||||
| } | ||||
|  | ||||
| # $1 hostid | ||||
| zaf_zbxapi_gethost() { | ||||
|  local hstr | ||||
|  local host | ||||
|  local groupid | ||||
|  local filter | ||||
|  local gfilter | ||||
|   | ||||
|  hostid="$1" | ||||
|  if [ -n "$hostid" ] ; then | ||||
|    filter='"hostids": [ "'$hostid'" ],' | ||||
|  fi | ||||
|  hstr='{ | ||||
|     "method": "host.get", | ||||
|     "jsonrpc": "2.0", | ||||
|     "auth": "'$ZAF_ZBXAPI_AUTH'", | ||||
|     "params": { | ||||
| 	'$filter' | ||||
| 	"output": "extend" | ||||
|     }, | ||||
|     "id": 1 | ||||
|  }' | ||||
|  zaf_zbxapi_do "$hstr" | zaf_zbxapi_getresult | zaf_zbxapi_getvalue host | ||||
| } | ||||
|  | ||||
| # $1 hostgroupid | ||||
| zaf_zbxapi_gethostsingroup() { | ||||
|  local hstr | ||||
|  local host | ||||
|  local groupid | ||||
|  local filter | ||||
|  local gfilter | ||||
|  | ||||
|  groupid="$1" | ||||
|  if [ -n "$groupid" ]; then | ||||
|    gfilter='"groupids": [ "'$groupid'" ],' | ||||
|  fi | ||||
|  | ||||
|  hstr='{ | ||||
|     "method": "host.get", | ||||
|     "jsonrpc": "2.0", | ||||
|     "auth": "'$ZAF_ZBXAPI_AUTH'", | ||||
|     "params": { | ||||
| 	'$gfilter' | ||||
| 	'$filter' | ||||
| 	"output": "shorten" | ||||
|     }, | ||||
|     "id": 1 | ||||
|  }' | ||||
|  zaf_zbxapi_do "$hstr" | zaf_zbxapi_getresult | tr ',' '\n' | cut -d '"' -f 4 | ||||
| } | ||||
|  | ||||
| # Host backup | ||||
| # $1 hostid | ||||
| zaf_zbxapi_backuphost(){ | ||||
|  local bkpstr | ||||
|   | ||||
|  host="$1" | ||||
|  bkpstr=' | ||||
|  { | ||||
|     "method": "configuration.export", | ||||
|     "jsonrpc": "2.0", | ||||
|     "params": { | ||||
|         "options": { | ||||
|             "hosts": [ | ||||
|                 "'$host'" | ||||
|             ] | ||||
|         }, | ||||
|         "format": "xml" | ||||
|     }, | ||||
|     "auth": "'$ZAF_ZBXAPI_AUTH'", | ||||
|     "id": 1 | ||||
| }' | ||||
|  zaf_zbxapi_do "$bkpstr" | zaf_zbxapi_getxml | ||||
| } | ||||
|  | ||||
|  | ||||
							
								
								
									
										195
									
								
								zaf
									
									
									
									
									
								
							
							
						
						
									
										195
									
								
								zaf
									
									
									
									
									
								
							| @@ -1,34 +1,23 @@ | ||||
| #!/bin/sh | ||||
|  | ||||
| if [ -z "$secondstage" ]; then | ||||
| ZAF_CFG_FILE="/etc/zaf.conf" | ||||
| 	if [ -f $ZAF_CFG_FILE ]; then | ||||
| 		. $ZAF_CFG_FILE | ||||
| 	else | ||||
| 		echo "Missing config file $ZAF_CFG_FILE! Exiting." | ||||
| 		exit 2 | ||||
| 	fi | ||||
|  | ||||
| 	# Read options as config for ZAF | ||||
| 	for pair in "$@"; do | ||||
|     		if echo $pair | grep -qE '^ZAF\_(.*)='; then | ||||
|     			option=$(echo $pair|cut -d '=' -f 1) | ||||
|     			value=$(echo $pair|cut -d '=' -f 2-) | ||||
|     			eval "${option}='$value'" | ||||
| 			export secondstage=1 | ||||
|     		else | ||||
| 			params="$params $pair" | ||||
|     		fi | ||||
| 	done | ||||
| 	export $(set |grep ^ZAF_ | cut -d '=' -f 1) | ||||
| 	# If some variables in cmd were stripped, rerun only with right arguments and exported variables | ||||
| 	[ -n "$secondstage" ] && exec $0 $params | ||||
| if [ -f $ZAF_CFG_FILE ]; then | ||||
| 	. $ZAF_CFG_FILE | ||||
| else | ||||
| 	echo "Missing config file $ZAF_CFG_FILE! Exiting." | ||||
| 	exit 2 | ||||
| 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 "${option}='$value'" | ||||
| done | ||||
|  | ||||
| [ -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 | ||||
|  | ||||
| [ -z "$ZAF_DEBUG" ] && ZAF_DEBUG=1 | ||||
| export ZAF_DEBUG | ||||
|  | ||||
| @@ -38,30 +27,37 @@ if [ "$ZAF_DEBUG" -le 3 ]; then | ||||
| fi | ||||
|  | ||||
| ! [ -d "${ZAF_TMP_DIR}" ] && mkdir "${ZAF_TMP_DIR}" | ||||
| if ! [ -d "${ZAF_CACHE_DIR}" ]; then | ||||
| 	mkdir "${ZAF_CACHE_DIR}" | ||||
| fi | ||||
|  | ||||
| if [ -f ./lib/zaf.lib.sh ]; then | ||||
| . ./lib/zaf.lib.sh | ||||
| . ./lib/os.lib.sh | ||||
| . ./lib/ctrl.lib.sh | ||||
| . ./lib/cache.lib.sh | ||||
| . ./lib/zbxapi.lib.sh | ||||
| [ -f ./lib/zaf.${ZAF_OS}.sh ] && . ./lib/zaf.${ZAF_OS}.sh | ||||
| else  | ||||
| . ${ZAF_LIB_DIR}/zaf.lib.sh | ||||
| . ${ZAF_LIB_DIR}/os.lib.sh | ||||
| . ${ZAF_LIB_DIR}/ctrl.lib.sh | ||||
| . ${ZAF_LIB_DIR}/cache.lib.sh | ||||
| . ${ZAF_LIB_DIR}/zbxapi.lib.sh | ||||
| [ -f ${ZAF_LIB_DIR}/zaf.${ZAF_OS}.sh ] && . ${ZAF_LIB_DIR}/zaf.${ZAF_OS}.sh | ||||
| fi | ||||
|  | ||||
| if zaf_is_root; then | ||||
| 	chgrp zabbix "${ZAF_CACHE_DIR}" | ||||
| 	chmod g+w "${ZAF_CACHE_DIR}" | ||||
| fi | ||||
| zaf_shift(){ | ||||
| 	local s | ||||
|  | ||||
| 	if echo $1 |grep -q "="; then | ||||
| 		if echo $2 |grep -q "="; then | ||||
| 			if echo $3 |grep -q "="; then | ||||
| 				s=3 | ||||
| 			else | ||||
| 				s=2 | ||||
| 			fi | ||||
| 		else | ||||
| 			s=1 | ||||
| 		fi | ||||
| 	else | ||||
| 		s=0 | ||||
| 	fi | ||||
| 	echo $s | ||||
| } | ||||
|  | ||||
| case $1 in | ||||
|  | ||||
| @@ -69,37 +65,18 @@ check-agent-config) | ||||
| 	zaf_check_agent_config | ||||
| 	;; | ||||
|  | ||||
| cache-clean) | ||||
| 	zaf_cache_clean | ||||
| 	;; | ||||
|  | ||||
| userparms) | ||||
| 	for plugin in $(zaf_list_plugins); do | ||||
| 		plugindir=$ZAF_PLUGINS_DIR/$plugin | ||||
| 		control=$plugindir/control.zaf | ||||
| 		zaf_ctrl_generate_cfg "${control}" "${plugin}" \ | ||||
| 			  | zaf_far '{PLUGINDIR}' "${plugindir}" | ||||
| 	done | ||||
| 	;; | ||||
|  | ||||
| agent-config) | ||||
| 	for plugin in $(zaf_list_plugins); do | ||||
| 		plugindir=$ZAF_PLUGINS_DIR/$plugin | ||||
| 		control=$plugindir/control.zaf | ||||
| 		zaf_ctrl_generate_cfg "${control}" "${plugin}" \ | ||||
| 			  | zaf_far '{PLUGINDIR}' "${plugindir}" >${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf | ||||
| 	done | ||||
| 	;; | ||||
|  | ||||
| update) | ||||
| 	zaf_wrn "Updating repository ${ZAF_REPO_GITURL}..." | ||||
| 	zaf_update_repo | ||||
| 	;; | ||||
| upgrade) | ||||
| 	ZAF_DEBUG=$ZAF_DEBUG $0 reinstall $(zaf_list_plugins) | ||||
| 	zaf_list_plugins | while read plugin; do | ||||
| 		ZAF_DEBUG=$ZAF_DEBUG $0 reinstall $plugin || zaf_err "Error upgrading $plugin" | ||||
| 	done | ||||
| 	;; | ||||
| show) | ||||
| 	shift; | ||||
| 	shift $(zaf_shift "$@") | ||||
| 	if [ -z "$1" ]; then | ||||
| 		zaf_list_plugins | while read plugin; do | ||||
| 			zaf_plugin_info $ZAF_PLUGINS_DIR/$plugin/control.zaf | ||||
| @@ -124,6 +101,7 @@ plugins) | ||||
|  | ||||
| items) | ||||
| 	shift | ||||
| 	shift $(zaf_shift "$@") | ||||
| 	if [ -z "$1" ]; then | ||||
| 		zaf_list_items | ||||
| 	else | ||||
| @@ -132,8 +110,8 @@ items) | ||||
| 	;; | ||||
|  | ||||
| test) | ||||
| 	[ "$USER" != "zabbix" ] && zaf_wrn "You are not zabbix user. Test will be run with your privileges and sudo access!" | ||||
| 	shift | ||||
| 	shift $(zaf_shift "$@") | ||||
| 	if echo $1|grep -q '\.'; then | ||||
| 		zaf_test_item "$1" | ||||
| 		exit | ||||
| @@ -145,14 +123,14 @@ test) | ||||
| 	fi | ||||
| 	for p in $plugins; do | ||||
| 		! 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); do  | ||||
| 			echo $i: $(zaf_test_item $i) | ||||
| 			echo | ||||
| 		done | ||||
| 	done | ||||
| 	;; | ||||
| get) | ||||
| 	shift | ||||
| 	shift $(zaf_shift "$@") | ||||
| 	if echo $1|grep -q '\.'; then | ||||
| 		zaf_get_item "$1" | ||||
| 		exit | ||||
| @@ -163,36 +141,18 @@ get) | ||||
| 		plugins="$(zaf_list_plugins)" | ||||
| 	fi | ||||
| 	for p in $plugins; do | ||||
| 		for i in $(zaf_list_plugin_items $p test); do  | ||||
| 		for i in $(zaf_list_plugin_items $p); do  | ||||
| 			echo $i: $(zaf_get_item $i) | ||||
| 			echo | ||||
| 		done | ||||
| 	done | ||||
| 	;; | ||||
|  | ||||
| precache) | ||||
| 	shift | ||||
| 	for i in $*; do | ||||
| 		if zaf_is_plugin $i; then | ||||
| 			for j in $(zaf_list_plugin_items $i precache); do | ||||
| 				item=$(echo $j | cut -d '[' -f 1) | ||||
| 				params=$(echo $j | cut -d '[' -f 2 | cut -d ']' -f 1 | tr ',' ' ') | ||||
| 				zaf_precache_item $params >/dev/null | ||||
| 			done | ||||
| 		else | ||||
| 			item=$(echo $i | cut -d '[' -f 1) | ||||
| 			params=$(echo $i | cut -d '[' -f 2 | cut -d ']' -f 1 | tr ',' ' ') | ||||
| 			zaf_precache_item $params >/dev/null | ||||
| 		fi | ||||
| 	done | ||||
| 	;; | ||||
|  | ||||
| install) | ||||
| 	shift | ||||
| 	shift $(zaf_shift "$@") | ||||
| 	[ -z "$1" ] && echo "$0 install plugin [plugin]..." | ||||
| 	for p in $@; do | ||||
| 		if zaf_is_plugin "$(basename $p)"; then | ||||
| 			zaf_wrn "Plugin $(basename $p) already installed. Skipping installation." | ||||
| 		if zaf_is_plugin "$p"; then | ||||
| 			zaf_wrn "Plugin $p already installed. Skipping installation." | ||||
| 			continue | ||||
| 		fi | ||||
| 		zaf_install_plugin "$p" | ||||
| @@ -203,6 +163,7 @@ install) | ||||
|  | ||||
| reinstall) | ||||
| 	shift | ||||
| 	shift $(zaf_shift "$@") | ||||
| 	[ -z "$1" ] && echo "$0 reinstall plugin [plugin]..." | ||||
| 	for p in $@; do | ||||
| 		if zaf_is_plugin "$p"; then | ||||
| @@ -217,6 +178,7 @@ reinstall) | ||||
|  | ||||
| remove) | ||||
| 	shift | ||||
| 	shift $(zaf_shift "$@") | ||||
| 	[ -z "$1" ] && echo "$0 remove plugin [plugin]..." | ||||
| 	for p in $@; do | ||||
| 		if zaf_is_plugin "$p"; then | ||||
| @@ -229,6 +191,7 @@ remove) | ||||
|  | ||||
| self-upgrade) | ||||
| 	shift | ||||
| 	shift $(zaf_shift "$@") | ||||
| 	[ -z "$1" ] && auto=auto | ||||
| 	zaf_os_specific zaf_check_deps zaf && zaf_err "Zaf is installed as system package. Cannot self-upgrade." | ||||
| 	rm -rf /tmp/zaf-installer && mkdir /tmp/zaf-installer | ||||
| @@ -236,12 +199,13 @@ self-upgrade) | ||||
| 	then | ||||
| 		zaf_err "Curl not found. Cannot continue. Please install it." | ||||
| 	fi | ||||
| 	zaf_fetch_url $ZAF_RAW_URL/$ZAF_GITBRANCH/install.sh | exec sh -s $auto "$@" | ||||
| 	zaf_fetch_url $ZAF_RAW_URL/$ZAF_VERSION/install.sh | exec sh -s $auto "$@" | ||||
| 	exit | ||||
| 	;; | ||||
| 	 | ||||
| self-remove) | ||||
| 	shift | ||||
| 	shift $(zaf_shift "$@") | ||||
| 	zaf_os_specific zaf_check_deps zaf && zaf_err "Zaf is installed as system package. Cannot self-remove." | ||||
| 	. /etc/zaf.conf | ||||
| 	if [ "$1" = "force" ]; then | ||||
| @@ -255,64 +219,11 @@ self-remove) | ||||
| 	  echo  | ||||
| 	  echo "To continue, please do $0 self-remove force" | ||||
| 	fi | ||||
|       	;; | ||||
|  | ||||
| api) | ||||
| 	zaf_zbxapi_login | ||||
| 	case $2 in | ||||
| 	hostid) | ||||
| 		zaf_zbxapi_gethostid "$3" | ||||
| 		;; | ||||
| 	host) | ||||
| 		zaf_zbxapi_gethost "$3" | ||||
| 		;;	 | ||||
| 	hostgroupid) | ||||
| 		zaf_zbxapi_gethostgroupid "$3" | ||||
| 		;;	 | ||||
| 	hosts) | ||||
| 		gid=$(zaf_zbxapi_gethostgroupid "$3") | ||||
| 		zaf_zbxapi_gethostsingroup $gid | ||||
| 		;; | ||||
| 	backup-group) | ||||
| 		shift; shift | ||||
| 		gid=$(zaf_zbxapi_gethostgroupid "$1") | ||||
| 		shift | ||||
| 		hosts=$(zaf_zbxapi_gethostsingroup $gid) | ||||
| 		dir="." | ||||
| 		[ -n "$1" ] && dir="$1" | ||||
| 		zaf_wrn "Will backup this hosts: $hosts" | ||||
| 		zaf_wrn "Output dir: $dir" | ||||
| 		for h in $hosts; do | ||||
| 			zaf_bglimit 5 | ||||
| 			(hn=$(zaf_zbxapi_gethost $h) | ||||
| 			zaf_wrn "Exporting host $hn($h)..." | ||||
| 			zaf_zbxapi_backuphost $h >$dir/$hn.xml ) | ||||
| 		done | ||||
| 		wait | ||||
| 		;; | ||||
| 	backup-host) | ||||
| 		shift; shift | ||||
| 		hostid=$(zaf_zbxapi_gethostid "$1") | ||||
| 		zaf_wrn "Exporting host $3($hostid)..." | ||||
| 		zaf_zbxapi_backuphost $hostid | ||||
| 		;; | ||||
| 	*) | ||||
| 		echo "$0 api command [parameters]" | ||||
| 		echo "hostid 'host'			Get hostid from hostname" | ||||
| 		echo "host 'hostid'			Get hostname from hostid" | ||||
| 		echo "hostgroupid 'hostgroup'		Get hostgroup id from hostgroup" | ||||
| 		echo "hosts 'hostgroup'			Get hosts in group" | ||||
| 		echo "backup-group 'hostgroup' [dir]	Backup all hosts in group (get their config from zabbix and save to dir/hostname.xml)"  | ||||
| 		echo "backup-host 'host'		Backup host (get config from zabbix to stdout)" | ||||
| 		echo | ||||
| 		exit | ||||
| 		;; | ||||
| 	esac | ||||
| 	;; | ||||
|       ;; | ||||
|  | ||||
| *) | ||||
| 	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] ..." | ||||
| 	echo "Commands:" | ||||
| 	echo "$0 update			To update repo (not plugins, similar to apt-get update)" | ||||
| 	echo "$0 upgrade		To upgrade installed plugins from repo"  | ||||
| @@ -321,16 +232,10 @@ api) | ||||
| 	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 | ||||
| 	;; | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user