mirror of
				https://github.com/limosek/zaf.git
				synced 2025-10-31 17:47:37 +01:00 
			
		
		
		
	Repaired merge of trunk
External items are working now
This commit is contained in:
		| @@ -53,7 +53,7 @@ zaf_ctrl_get_global_block() { | ||||
| # Get item multiline option | ||||
| # $1 optionname | ||||
| zaf_block_get_moption() { | ||||
| 	awk '/^'$1'::$/ { i=0;  | ||||
| 	awk '/^'$1'::$/ { i=0; if (!/::/) print $0; | ||||
| 	while (i==0) { | ||||
| 		getline; | ||||
| 		if (/^::$/) {i=1; continue;}; | ||||
| @@ -289,7 +289,7 @@ zaf_ctrl_generate_items_cfg() { | ||||
| 	rm -f $tmpfile | ||||
| } | ||||
|  | ||||
| # Generates zabbix cfg for external items from control file | ||||
| # Generates zabbix items cfg from control file | ||||
| # $1 control | ||||
| # $2 pluginname | ||||
| zaf_ctrl_generate_extitems_cfg() { | ||||
| @@ -299,47 +299,58 @@ zaf_ctrl_generate_extitems_cfg() { | ||||
| 	local ikey | ||||
| 	local lock | ||||
| 	local cache | ||||
| 	local tmpfile | ||||
| 	local pname | ||||
| 	local pdefault | ||||
| 	local pregex | ||||
| 	local prest | ||||
| 	local zafparms | ||||
|  | ||||
| 	items=$(zaf_ctrl_get_extitems <"$1") | ||||
| 	if [ -n "$items" ] && [ -z "${ZAF_SERVER_EXTSCRIPTS}" ] || ! [ -d "${ZAF_SERVER_EXTSCRIPTS}" ]; then | ||||
| 		zaf_err "Zabbix server external scripts directory '${ZAF_SERVER_EXTSCRIPTS}' unknown. Cannot add external item."  | ||||
| 	fi | ||||
| 	tmpfile=$ZAF_TMP_DIR/gencfg$$ | ||||
| 	(set -e | ||||
| 	for i in $items; do | ||||
|             iscript=$(zaf_stripctrl $i) | ||||
| 	    params=$(zaf_ctrl_get_extitem_option $1 $i "Parameters") | ||||
| 	    zaf_ctrl_get_extitem_option $1 $i "Parameters" >$tmpfile | ||||
| 	    ikey="$2.$i" | ||||
| 	    if [ -n "$params" ]; then | ||||
| 	    if [ -s "$tmpfile" ]; then | ||||
| 		args="" | ||||
| 		apos=1; | ||||
| 		for p in $params; do | ||||
| 		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 | ||||
| 		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="_cache '$cache' " | ||||
| 		cache="${ZAF_LIB_DIR}/zafcache '$cache' " | ||||
| 	    fi | ||||
|             cmd=$(zaf_ctrl_get_extitem_option $1 $i "Cmd") | ||||
|             if [ -n "$cmd" ]; then | ||||
|                 $(which echo) "UserParameter=$ikey,${ZAF_LIB_DIR}/preload.sh $cache $lock$cmd"; | ||||
| 		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 | ||||
|                 zaf_ctrl_get_extitem_option $1 $i "Script" >${ZAF_TMP_DIR}/${iscript}.sh; | ||||
|                 zaf_install_bin ${ZAF_TMP_DIR}/${iscript}.sh ${ZAF_PLUGINS_DIR}/$2/ | ||||
|                 $(which echo) -e "#!/bin/sh\n${ZAF_LIB_DIR}/preload.sh $cache $lock${ZAF_PLUGINS_DIR}/$2/${iscript}.sh $args" >${ZAF_SERVER_EXTSCRIPTS}/$ikey; | ||||
|                 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_items_cfg" | ||||
| 	) || zaf_err "Error during zaf_ctrl_generate_extitems_cfg" | ||||
| 	rm -f $tmpfile | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -55,6 +55,7 @@ zaf_plugin_info() { | ||||
| 	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 | ||||
| @@ -64,11 +65,12 @@ zaf_plugin_info() { | ||||
| 	echo  | ||||
| 	if zaf_is_plugin "$(basename $plugin)"; then | ||||
| 		items=$(zaf_list_plugin_items $plugin) | ||||
| 		[ -n "$items" ] && echo -n "Defined items: "; echo $items | ||||
| 		[ -n "$items" ] && { echo -n "Defined items: "; echo $items; } | ||||
| 		items=$(zaf_list_plugin_items $plugin test) | ||||
| 		[ -n "$items" ] && echo -n "Test items: "; echo $items | ||||
| 		[ -n "$items" ] && { echo -n "Test items: "; echo $items; } | ||||
| 		items=$(zaf_list_plugin_items $plugin precache) | ||||
| 		[ -n "$items" ] && echo -n "Precache items: "; echo $items | ||||
| 		[ -n "$items" ] && { echo -n "Precache items: "; echo $items; } | ||||
| 		[ -n "$peitems" ] && { echo -n "External check items: "; echo $peitems; } | ||||
| 	else | ||||
| 		echo "Items: $pitems" | ||||
| 	fi | ||||
| @@ -116,9 +118,10 @@ zaf_install_plugin() { | ||||
| 			zaf_ctrl_check_deps "${control}" | ||||
| 			zaf_ctrl_sudo "$plugin" "${control}" "${plugindir}" | ||||
| 			zaf_ctrl_cron "$plugin" "${control}" "${plugindir}" | ||||
| 			zaf_ctrl_generate_cfg "${control}" "${plugin}" \ | ||||
| 			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}"  | ||||
| 			zaf_ctrl_install "$url" "${control}" "${plugindir}" | ||||
| 		else | ||||
| 			zaf_err "Cannot install plugin '$plugin' to $plugindir!" | ||||
|   | ||||
							
								
								
									
										252
									
								
								lib/zaf.lib.sh
									
									
									
									
									
								
							
							
						
						
									
										252
									
								
								lib/zaf.lib.sh
									
									
									
									
									
								
							| @@ -222,258 +222,6 @@ zaf_check_agent_config() { | ||||
| 	${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_items_cfg "${control}" "${plugin}" \ | ||||
| 			  | zaf_far '{PLUGINDIR}' "${plugindir}" >${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf | ||||
| 			zaf_ctrl_generate_extitems_cfg "${control}" "${plugin}" | ||||
| 			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() { | ||||
| 	tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz' | ||||
| } | ||||
|   | ||||
							
								
								
									
										4
									
								
								zaf
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								zaf
									
									
									
									
									
								
							| @@ -67,7 +67,7 @@ userparms) | ||||
| 	for plugin in $(zaf_list_plugins); do | ||||
| 		plugindir=$ZAF_PLUGINS_DIR/$plugin | ||||
| 		control=$plugindir/control.zaf | ||||
| 		zaf_ctrl_generate_cfg "${control}" "${plugin}" "noscripts" \ | ||||
| 		zaf_ctrl_generate_items_cfg "${control}" "${plugin}" "noscripts" \ | ||||
| 			  | zaf_far '{PLUGINDIR}' "${plugindir}" | ||||
| 	done | ||||
| 	;; | ||||
| @@ -77,7 +77,7 @@ agent-config) | ||||
| 		plugindir=$ZAF_PLUGINS_DIR/$plugin | ||||
| 		control=$plugindir/control.zaf | ||||
| 		zaf_dbg "Generating ${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf" | ||||
| 		zaf_ctrl_generate_cfg "${control}" "${plugin}" \ | ||||
| 		zaf_ctrl_generate_items_cfg "${control}" "${plugin}" \ | ||||
| 			  | zaf_far '{PLUGINDIR}' "${plugindir}" >${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf | ||||
| 	done | ||||
| 	;; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Lukas Macura
					Lukas Macura