mirror of
				https://github.com/limosek/zaf.git
				synced 2025-10-31 09:37:37 +01:00 
			
		
		
		
	Repaired functions
This commit is contained in:
		| @@ -28,7 +28,7 @@ done | |||||||
|  |  | ||||||
| [ -z "$ZAF_CFG_FILE" ] && ZAF_CFG_FILE=$INSTALL_PREFIX/etc/zaf.conf | [ -z "$ZAF_CFG_FILE" ] && ZAF_CFG_FILE=$INSTALL_PREFIX/etc/zaf.conf | ||||||
| [ -n "$C_ZAF_DEBUG" ] && ZAF_DEBUG=$C_ZAF_DEBUG | [ -n "$C_ZAF_DEBUG" ] && ZAF_DEBUG=$C_ZAF_DEBUG | ||||||
| [ -z "$ZAF_DEBUG" ] && ZAF_DEBUG=0 | [ -z "$ZAF_DEBUG" ] && ZAF_DEBUG=1 | ||||||
|  |  | ||||||
| if [ -f $(dirname $0)/lib/zaf.lib.sh ]; then | if [ -f $(dirname $0)/lib/zaf.lib.sh ]; then | ||||||
|     . $(dirname $0)/lib/zaf.lib.sh |     . $(dirname $0)/lib/zaf.lib.sh | ||||||
| @@ -149,7 +149,7 @@ zaf_configure(){ | |||||||
| 	zaf_get_option ZAF_LIB_DIR "Libraries directory" "/usr/lib/zaf" "$1" | 	zaf_get_option ZAF_LIB_DIR "Libraries directory" "/usr/lib/zaf" "$1" | ||||||
|         zaf_get_option ZAF_BIN_DIR "Directory to put binaries" "/usr/bin" "$1" |         zaf_get_option ZAF_BIN_DIR "Directory to put binaries" "/usr/bin" "$1" | ||||||
| 	zaf_get_option ZAF_PLUGINS_DIR "Plugins directory" "${ZAF_LIB_DIR}/plugins" "$1" | 	zaf_get_option ZAF_PLUGINS_DIR "Plugins directory" "${ZAF_LIB_DIR}/plugins" "$1" | ||||||
| 	zaf_get_option ZAF_PLUGINS_REPO "Plugins reposiory" "https://raw.githubusercontent.com/limosek/zaf-plugins/master/" "$1" | 	zaf_get_option ZAF_PLUGINS_REPO "Plugins reposiory" "https://github.com/limosek/zaf-plugins.git" "$1" | ||||||
| 	zaf_get_option ZAF_REPO_DIR "Plugins directory" "${ZAF_LIB_DIR}/repo" "$1" | 	zaf_get_option ZAF_REPO_DIR "Plugins directory" "${ZAF_LIB_DIR}/repo" "$1" | ||||||
| 	zaf_get_option ZAF_AGENT_CONFIG "Zabbix agent config" "/etc/zabbix/zabbix_agentd.conf" "$1" | 	zaf_get_option ZAF_AGENT_CONFIG "Zabbix agent config" "/etc/zabbix/zabbix_agentd.conf" "$1" | ||||||
| 	! [ -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" | ||||||
| @@ -233,8 +233,8 @@ install) | |||||||
| 	zaf_install $(zaf_getrest lib/zaf.lib.sh) ${ZAF_LIB_DIR} | 	zaf_install $(zaf_getrest lib/zaf.lib.sh) ${ZAF_LIB_DIR} | ||||||
|         zaf_install $(zaf_getrest lib/os.lib.sh) ${ZAF_LIB_DIR} |         zaf_install $(zaf_getrest lib/os.lib.sh) ${ZAF_LIB_DIR} | ||||||
|         zaf_install $(zaf_getrest lib/ctrl.lib.sh) ${ZAF_LIB_DIR} |         zaf_install $(zaf_getrest lib/ctrl.lib.sh) ${ZAF_LIB_DIR} | ||||||
| 	zaf_install $(zaf_getrest lib/jshn.sh) ${ZAF_LIB_DIR} |  | ||||||
| 	zaf_install_bin $(zaf_getrest lib/zaflock) ${ZAF_LIB_DIR} | 	zaf_install_bin $(zaf_getrest lib/zaflock) ${ZAF_LIB_DIR} | ||||||
|  | 	zaf_install_bin $(zaf_getrest lib/preload.sh) ${ZAF_LIB_DIR} | ||||||
| 	zaf_install_dir ${ZAF_TMP_DIR}/p/zaf | 	zaf_install_dir ${ZAF_TMP_DIR}/p/zaf | ||||||
| 	zaf_install_dir ${ZAF_PLUGINS_DIR} | 	zaf_install_dir ${ZAF_PLUGINS_DIR} | ||||||
|         zaf_install_dir ${ZAF_BIN_DIR} |         zaf_install_dir ${ZAF_BIN_DIR} | ||||||
|   | |||||||
| @@ -67,6 +67,7 @@ zaf_ctrl_get_item_option() { | |||||||
| 	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_moption "$3" || zaf_ctrl_get_item_block <$1 "$2" | zaf_block_get_option "$3" | ||||||
| } | } | ||||||
|  |  | ||||||
|  | # Check dependencies based on control file | ||||||
| zaf_ctrl_check_deps() { | zaf_ctrl_check_deps() { | ||||||
| 	local deps | 	local deps | ||||||
| 	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}" ) | ||||||
| @@ -90,14 +91,15 @@ zaf_ctrl_install() { | |||||||
| 	local cmd | 	local cmd | ||||||
|  |  | ||||||
| 	pdir="$2" | 	pdir="$2" | ||||||
| 	binaries=$(zaf_ctrl_get_global_block <$1 | zaf_block_get_option "Install-bin" | zaf_far '{PLUGINDIR}' "$plugindir" ) | 	export ZAF_PLUGIN_DIR="$pdir" | ||||||
|  | 	binaries=$(zaf_ctrl_get_global_block <$1 | zaf_block_get_option "Install-bin") | ||||||
| 	for b in $binaries; do | 	for b in $binaries; do | ||||||
| 		zaf_fetch_url "$url/$b" >"${ZAF_TMP_DIR}/$b" | 		zaf_fetch_url "$url/$b" >"${ZAF_TMP_DIR}/$b" | ||||||
|                 zaf_install_bin "${ZAF_TMP_DIR}/$b" "$pdir" |                 zaf_install_bin "${ZAF_TMP_DIR}/$b" "$pdir" | ||||||
| 	done | 	done | ||||||
| 	script=$(zaf_ctrl_get_global_block <$1 | zaf_block_get_moption "Install-script" | zaf_far '{PLUGINDIR}' "$plugindir" ) | 	script=$(zaf_ctrl_get_global_block <$1 | zaf_block_get_moption "Install-script") | ||||||
| 	[ -n "$script" ] && eval "$script" | 	[ -n "$script" ] && eval "$script" | ||||||
| 	cmd=$(zaf_ctrl_get_global_block <$1 | zaf_block_get_option "Install-cmd" | zaf_far '{PLUGINDIR}' "$plugindir" ) | 	cmd=$(zaf_ctrl_get_global_block <$1 | zaf_block_get_option "Install-cmd") | ||||||
| 	[ -n "$cmd" ] && $cmd | 	[ -n "$cmd" ] && $cmd | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -107,28 +109,34 @@ zaf_ctrl_install() { | |||||||
| zaf_ctrl_generate_cfg() { | zaf_ctrl_generate_cfg() { | ||||||
| 	local items | 	local items | ||||||
| 	local cmd | 	local cmd | ||||||
| 	local ilock | 	local ikey | ||||||
|  | 	local lock | ||||||
|  |  | ||||||
| 	items=$(zaf_ctrl_get_items <"$1") | 	items=$(zaf_ctrl_get_items <"$1") | ||||||
| 	for i in $items; do | 	for i in $items; do | ||||||
|             ilock=$(echo $i | tr -d '[]*&;:') |             ikey=$(echo $i | tr -d '[]*&;:') | ||||||
|  | 	    lock=$(zaf_ctrl_get_item_option $1 $i "Lock") | ||||||
|  | 	    if [ -n "$lock" ]; then | ||||||
|  | 		lock="${ZAF_LIB_DIR}/zaflock $lock " | ||||||
|  | 	    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 | ||||||
|                 echo "UserParameter=$2.${i},$cmd"; |                 echo "UserParameter=$2.${i},$lock$cmd"; | ||||||
|                 continue |                 continue | ||||||
|             fi |             fi | ||||||
|             cmd=$(zaf_ctrl_get_item_option $1 $i "Function") |             cmd=$(zaf_ctrl_get_item_option $1 $i "Function") | ||||||
|             if [ -n "$cmd" ]; then |             if [ -n "$cmd" ]; then | ||||||
|                 echo "UserParameter=$2.${i},${ZAF_LIB_DIR}/preload.sh $cmd"; |                 echo "UserParameter=$2.${i},${ZAF_LIB_DIR}/preload.sh $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 | ||||||
|                 zaf_ctrl_get_item_option $1 $i "Script" >${ZAF_TMP_DIR}/${ilock}.sh; |                 zaf_ctrl_get_item_option $1 $i "Script" >${ZAF_TMP_DIR}/${ikey}.sh; | ||||||
|                 zaf_install_bin ${ZAF_TMP_DIR}/${ilock}.sh ${ZAF_PLUGINS_DIR}/$2/ |                 zaf_install_bin ${ZAF_TMP_DIR}/${ikey}.sh ${ZAF_PLUGINS_DIR}/$2/ | ||||||
|                 echo "UserParameter=$2.${i},${ZAF_PLUGINS_DIR}/$2/${ilock}.sh"; |                 echo "UserParameter=$2.${i},${ZAF_LIB_DIR}/preload.sh $lock${ZAF_PLUGINS_DIR}/$2/${ikey}.sh"; | ||||||
|                 continue; |                 continue; | ||||||
|             fi |             fi | ||||||
| 	done | 	done | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										280
									
								
								lib/jshn.sh
									
									
									
									
									
								
							
							
						
						
									
										280
									
								
								lib/jshn.sh
									
									
									
									
									
								
							| @@ -1,280 +0,0 @@ | |||||||
| # functions for parsing and generating json |  | ||||||
|  |  | ||||||
| _json_get_var() { |  | ||||||
| 	# dest=$1 |  | ||||||
| 	# var=$2 |  | ||||||
| 	eval "$1=\"\$${JSON_PREFIX}$2\"" |  | ||||||
| } |  | ||||||
|  |  | ||||||
| _json_set_var() { |  | ||||||
| 	# var=$1 |  | ||||||
| 	local ___val="$2" |  | ||||||
| 	eval "${JSON_PREFIX}$1=\"\$___val\"" |  | ||||||
| } |  | ||||||
|  |  | ||||||
| __jshn_raw_append() { |  | ||||||
| 	# var=$1 |  | ||||||
| 	local value="$2" |  | ||||||
| 	local sep="${3:- }" |  | ||||||
|  |  | ||||||
| 	eval "export -- \"$1=\${$1:+\${$1}\${value:+\$sep}}\$value\"" |  | ||||||
| } |  | ||||||
|  |  | ||||||
| _jshn_append() { |  | ||||||
| 	# var=$1 |  | ||||||
| 	local _a_value="$2" |  | ||||||
| 	eval "${JSON_PREFIX}$1=\"\${${JSON_PREFIX}$1} \$_a_value\"" |  | ||||||
| } |  | ||||||
|  |  | ||||||
| _get_var() { |  | ||||||
| 	# var=$1 |  | ||||||
| 	# value=$2 |  | ||||||
| 	eval "$1=\"\$$2\"" |  | ||||||
| } |  | ||||||
|  |  | ||||||
| _set_var() { |  | ||||||
| 	# var=$1 |  | ||||||
| 	local __val="$2" |  | ||||||
| 	eval "$1=\"\$__val\"" |  | ||||||
| } |  | ||||||
|  |  | ||||||
| _json_inc() { |  | ||||||
| 	# var=$1 |  | ||||||
| 	# dest=$2 |  | ||||||
|  |  | ||||||
| 	let "${JSON_PREFIX}$1 += 1" "$2 = ${JSON_PREFIX}$1" |  | ||||||
| } |  | ||||||
|  |  | ||||||
| _json_add_generic() { |  | ||||||
| 	# type=$1 |  | ||||||
| 	# name=$2 |  | ||||||
| 	# value=$3 |  | ||||||
| 	# cur=$4 |  | ||||||
|  |  | ||||||
| 	local var |  | ||||||
| 	if [ "${4%%[0-9]*}" = "J_A" ]; then |  | ||||||
| 		_json_inc "S_$4" var |  | ||||||
| 	else |  | ||||||
| 		var="${2//[^a-zA-Z0-9_]/_}" |  | ||||||
| 		[[ "$var" == "$2" ]] || export -- "${JSON_PREFIX}N_${4}_${var}=$2" |  | ||||||
| 	fi |  | ||||||
|  |  | ||||||
| 	export -- \ |  | ||||||
| 		"${JSON_PREFIX}${4}_$var=$3" \ |  | ||||||
| 		"${JSON_PREFIX}T_${4}_$var=$1" |  | ||||||
| 	_jshn_append "JSON_UNSET" "${4}_$var" |  | ||||||
| 	_jshn_append "K_$4" "$var" |  | ||||||
| } |  | ||||||
|  |  | ||||||
| _json_add_table() { |  | ||||||
| 	# name=$1 |  | ||||||
| 	# type=$2 |  | ||||||
| 	# itype=$3 |  | ||||||
| 	local cur seq |  | ||||||
|  |  | ||||||
| 	_json_get_var cur JSON_CUR |  | ||||||
| 	_json_inc JSON_SEQ seq |  | ||||||
|  |  | ||||||
| 	local table="J_$3$seq" |  | ||||||
| 	_json_set_var "U_$table" "$cur" |  | ||||||
| 	export -- "${JSON_PREFIX}K_$table=" |  | ||||||
| 	unset "${JSON_PREFIX}S_$table" |  | ||||||
| 	_json_set_var JSON_CUR "$table" |  | ||||||
| 	_jshn_append "JSON_UNSET" "$table" |  | ||||||
|  |  | ||||||
| 	_json_add_generic "$2" "$1" "$table" "$cur" |  | ||||||
| } |  | ||||||
|  |  | ||||||
| _json_close_table() { |  | ||||||
| 	local _s_cur |  | ||||||
|  |  | ||||||
| 	_json_get_var _s_cur JSON_CUR |  | ||||||
| 	_json_get_var "${JSON_PREFIX}JSON_CUR" "U_$_s_cur" |  | ||||||
| } |  | ||||||
|  |  | ||||||
| json_set_namespace() { |  | ||||||
| 	local _new="$1" |  | ||||||
| 	local _old="$2" |  | ||||||
|  |  | ||||||
| 	[ -n "$_old" ] && _set_var "$_old" "$JSON_PREFIX" |  | ||||||
| 	JSON_PREFIX="$_new" |  | ||||||
| } |  | ||||||
|  |  | ||||||
| json_cleanup() { |  | ||||||
| 	local unset tmp |  | ||||||
|  |  | ||||||
| 	_json_get_var unset JSON_UNSET |  | ||||||
| 	for tmp in $unset J_V; do |  | ||||||
| 		unset \ |  | ||||||
| 			${JSON_PREFIX}U_$tmp \ |  | ||||||
| 			${JSON_PREFIX}K_$tmp \ |  | ||||||
| 			${JSON_PREFIX}S_$tmp \ |  | ||||||
| 			${JSON_PREFIX}T_$tmp \ |  | ||||||
| 			${JSON_PREFIX}N_$tmp \ |  | ||||||
| 			${JSON_PREFIX}$tmp |  | ||||||
| 	done |  | ||||||
|  |  | ||||||
| 	unset \ |  | ||||||
| 		${JSON_PREFIX}JSON_SEQ \ |  | ||||||
| 		${JSON_PREFIX}JSON_CUR \ |  | ||||||
| 		${JSON_PREFIX}JSON_UNSET |  | ||||||
| } |  | ||||||
|  |  | ||||||
| json_init() { |  | ||||||
| 	json_cleanup |  | ||||||
| 	export -n ${JSON_PREFIX}JSON_SEQ=0 |  | ||||||
| 	export -- \ |  | ||||||
| 		${JSON_PREFIX}JSON_CUR="J_V" \ |  | ||||||
| 		${JSON_PREFIX}K_J_V= |  | ||||||
| } |  | ||||||
|  |  | ||||||
| json_add_object() { |  | ||||||
| 	_json_add_table "$1" object T |  | ||||||
| } |  | ||||||
|  |  | ||||||
| json_close_object() { |  | ||||||
| 	_json_close_table |  | ||||||
| } |  | ||||||
|  |  | ||||||
| json_add_array() { |  | ||||||
| 	_json_add_table "$1" array A |  | ||||||
| } |  | ||||||
|  |  | ||||||
| json_close_array() { |  | ||||||
| 	_json_close_table |  | ||||||
| } |  | ||||||
|  |  | ||||||
| json_add_string() { |  | ||||||
| 	local cur |  | ||||||
| 	_json_get_var cur JSON_CUR |  | ||||||
| 	_json_add_generic string "$1" "$2" "$cur" |  | ||||||
| } |  | ||||||
|  |  | ||||||
| json_add_int() { |  | ||||||
| 	local cur |  | ||||||
| 	_json_get_var cur JSON_CUR |  | ||||||
| 	_json_add_generic int "$1" "$2" "$cur" |  | ||||||
| } |  | ||||||
|  |  | ||||||
| json_add_boolean() { |  | ||||||
| 	local cur |  | ||||||
| 	_json_get_var cur JSON_CUR |  | ||||||
| 	_json_add_generic boolean "$1" "$2" "$cur" |  | ||||||
| } |  | ||||||
|  |  | ||||||
| json_add_double() { |  | ||||||
| 	local cur |  | ||||||
| 	_json_get_var cur JSON_CUR |  | ||||||
| 	_json_add_generic double "$1" "$2" "$cur" |  | ||||||
| } |  | ||||||
|  |  | ||||||
| # functions read access to json variables |  | ||||||
|  |  | ||||||
| json_load() { |  | ||||||
| 	eval "`jshn -r "$1"`" |  | ||||||
| } |  | ||||||
|  |  | ||||||
| json_dump() { |  | ||||||
| 	jshn "$@" ${JSON_PREFIX:+-p "$JSON_PREFIX"} -w  |  | ||||||
| } |  | ||||||
|  |  | ||||||
| json_get_type() { |  | ||||||
| 	local __dest="$1" |  | ||||||
| 	local __cur |  | ||||||
|  |  | ||||||
| 	_json_get_var __cur JSON_CUR |  | ||||||
| 	local __var="${JSON_PREFIX}T_${__cur}_${2//[^a-zA-Z0-9_]/_}" |  | ||||||
| 	eval "export -- \"$__dest=\${$__var}\"; [ -n \"\${$__var+x}\" ]" |  | ||||||
| } |  | ||||||
|  |  | ||||||
| json_get_keys() { |  | ||||||
| 	local __dest="$1" |  | ||||||
| 	local _tbl_cur |  | ||||||
|  |  | ||||||
| 	if [ -n "$2" ]; then |  | ||||||
| 		json_get_var _tbl_cur "$2" |  | ||||||
| 	else |  | ||||||
| 		_json_get_var _tbl_cur JSON_CUR |  | ||||||
| 	fi |  | ||||||
| 	local __var="${JSON_PREFIX}K_${_tbl_cur}" |  | ||||||
| 	eval "export -- \"$__dest=\${$__var}\"; [ -n \"\${$__var+x}\" ]" |  | ||||||
| } |  | ||||||
|  |  | ||||||
| json_get_values() { |  | ||||||
| 	local _v_dest="$1" |  | ||||||
| 	local _v_keys _v_val _select= |  | ||||||
| 	local _json_no_warning=1 |  | ||||||
|  |  | ||||||
| 	unset "$_v_dest" |  | ||||||
| 	[ -n "$2" ] && { |  | ||||||
| 		json_select "$2" || return 1 |  | ||||||
| 		_select=1 |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	json_get_keys _v_keys |  | ||||||
| 	set -- $_v_keys |  | ||||||
| 	while [ "$#" -gt 0 ]; do |  | ||||||
| 		json_get_var _v_val "$1" |  | ||||||
| 		__jshn_raw_append "$_v_dest" "$_v_val" |  | ||||||
| 		shift |  | ||||||
| 	done |  | ||||||
| 	[ -n "$_select" ] && json_select .. |  | ||||||
|  |  | ||||||
| 	return 0 |  | ||||||
| } |  | ||||||
|  |  | ||||||
| json_get_var() { |  | ||||||
| 	local __dest="$1" |  | ||||||
| 	local __cur |  | ||||||
|  |  | ||||||
| 	_json_get_var __cur JSON_CUR |  | ||||||
| 	local __var="${JSON_PREFIX}${__cur}_${2//[^a-zA-Z0-9_]/_}" |  | ||||||
| 	eval "export -- \"$__dest=\${$__var:-$3}\"; [ -n \"\${$__var+x}\${3+x}\" ]" |  | ||||||
| } |  | ||||||
|  |  | ||||||
| json_get_vars() { |  | ||||||
| 	while [ "$#" -gt 0 ]; do |  | ||||||
| 		local _var="$1"; shift |  | ||||||
| 		if [ "$_var" != "${_var#*:}" ]; then |  | ||||||
| 			json_get_var "${_var%%:*}" "${_var%%:*}" "${_var#*:}" |  | ||||||
| 		else |  | ||||||
| 			json_get_var "$_var" "$_var" |  | ||||||
| 		fi |  | ||||||
| 	done |  | ||||||
| } |  | ||||||
|  |  | ||||||
| json_select() { |  | ||||||
| 	local target="$1" |  | ||||||
| 	local type |  | ||||||
| 	local cur |  | ||||||
|  |  | ||||||
| 	[ -z "$1" ] && { |  | ||||||
| 		_json_set_var JSON_CUR "J_V" |  | ||||||
| 		return 0 |  | ||||||
| 	} |  | ||||||
| 	[[ "$1" == ".." ]] && { |  | ||||||
| 		_json_get_var cur JSON_CUR |  | ||||||
| 		_json_get_var cur "U_$cur" |  | ||||||
| 		_json_set_var JSON_CUR "$cur" |  | ||||||
| 		return 0 |  | ||||||
| 	} |  | ||||||
| 	json_get_type type "$target" |  | ||||||
| 	case "$type" in |  | ||||||
| 		object|array) |  | ||||||
| 			json_get_var cur "$target" |  | ||||||
| 			_json_set_var JSON_CUR "$cur" |  | ||||||
| 		;; |  | ||||||
| 		*) |  | ||||||
| 			[ -n "$_json_no_warning" ] || \ |  | ||||||
| 				echo "WARNING: Variable '$target' does not exist or is not an array/object" |  | ||||||
| 			return 1 |  | ||||||
| 		;; |  | ||||||
| 	esac |  | ||||||
| } |  | ||||||
|  |  | ||||||
| json_is_a() { |  | ||||||
| 	local type |  | ||||||
|  |  | ||||||
| 	json_get_type type "$1" |  | ||||||
| 	[ "$type" = "$2" ] |  | ||||||
| } |  | ||||||
							
								
								
									
										8
									
								
								lib/preload.sh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								lib/preload.sh
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,8 @@ | |||||||
|  | #!/bin/sh | ||||||
|  |  | ||||||
|  | . /etc/zaf.conf | ||||||
|  | . ${ZAF_LIB_DIR}/zaf.lib.sh | ||||||
|  |  | ||||||
|  | [ -n "$*" ] && $@ | ||||||
|  |  | ||||||
|  |  | ||||||
							
								
								
									
										107
									
								
								lib/zaf.lib.sh
									
									
									
									
									
								
							
							
						
						
									
										107
									
								
								lib/zaf.lib.sh
									
									
									
									
									
								
							| @@ -5,10 +5,10 @@ zaf_msg() { | |||||||
| 	echo $@ | 	echo $@ | ||||||
| } | } | ||||||
| zaf_dbg() { | zaf_dbg() { | ||||||
| 	[ "$ZAF_DEBUG" -ge "3" ] && logger -s -t zaf $@ | 	[ "$ZAF_DEBUG" -ge "2" ] && logger -s -t zaf $@ | ||||||
| } | } | ||||||
| zaf_wrn() { | zaf_wrn() { | ||||||
| 	[ "$ZAF_DEBUG" -ge "2" ] && logger -s -t zaf $@ | 	[ "$ZAF_DEBUG" -ge "1" ] && logger -s -t zaf $@ | ||||||
| } | } | ||||||
| zaf_err() { | zaf_err() { | ||||||
| 	logger -s -t zaf $@ | 	logger -s -t zaf $@ | ||||||
| @@ -56,48 +56,73 @@ zaf_far(){ | |||||||
| } | } | ||||||
|  |  | ||||||
| # Initialises discovery function | # Initialises discovery function | ||||||
| zaf_discovery_init(){ | zaf_discovery_begin(){ | ||||||
|   json_init | cat <<EOF | ||||||
|   json_add_array data | { | ||||||
|  |  "data":[ | ||||||
|  | EOF | ||||||
| } | } | ||||||
|  |  | ||||||
| # Add row(s) to discovery data | # Add row(s) to discovery data | ||||||
| zaf_discovery_add_row(){ | zaf_discovery_add_row(){ | ||||||
|   json_add_object "obj" |   local rows | ||||||
|  |   local row | ||||||
|  |  | ||||||
|  |   rows=$1 | ||||||
|  |   row=$2 | ||||||
|  |   shift;shift | ||||||
|  |   echo " {" | ||||||
|   while [ -n "$1" ]; do |   while [ -n "$1" ]; do | ||||||
|     json_add_string "$1" "$2" |     echo -n '  "{#'$1'}":"'$2'" ' | ||||||
|     shift |     shift;shift | ||||||
|     shift |     if [ -n "$1" ]; then | ||||||
|  | 	echo "," | ||||||
|  |     else | ||||||
|  | 	echo "" | ||||||
|  |     fi | ||||||
|   done |   done | ||||||
|   json_close_object |   if [ "$row" -lt "$rows" ]; then | ||||||
|  |   	echo " }," | ||||||
|  |   else | ||||||
|  | 	echo " }" | ||||||
|  |   fi | ||||||
| } | } | ||||||
|  |  | ||||||
| # Dumps json object | # Dumps json object | ||||||
| zaf_discovery_dump(){ | zaf_discovery_end(){ | ||||||
|  json_close_array | cat <<EOF | ||||||
|  json_dump |  ] | ||||||
|  | } | ||||||
|  | EOF | ||||||
| } | } | ||||||
|  |  | ||||||
| # Read standard input as discovery data. Columns are divided by space. | # Read standard input as discovery data. Columns are divided by space. | ||||||
| # Arguments are name of variables to discovery. | # Arguments are name of variables to discovery. | ||||||
| # Dumps json to stdout | # Dumps json to stdout | ||||||
| zaf_discovery(){ | zaf_discovery(){ | ||||||
|  |   local tmpfile | ||||||
|  |   local rows | ||||||
|  |   local a b c d e f g h i j row | ||||||
|  |  | ||||||
|  |   tmpfile="${ZAF_TMP_DIR}/disc$$" | ||||||
|  |   cat >$tmpfile | ||||||
|  |   rows=$(wc -l <$tmpfile) | ||||||
|   local a b c d e f g h i j; |   local a b c d e f g h i j; | ||||||
|   zaf_discovery_init |   zaf_discovery_begin | ||||||
|  |   row=1 | ||||||
|   while read a b c d e f g h i j; do |   while read a b c d e f g h i j; do | ||||||
|     zaf_discovery_add_row "$1" "${1:+${a}}" "$2" "${2:+${b}}" "$3" "${3:+${c}}" "$4" "${4:+${d}}" "$5" "${5:+${e}}" "$6" "${6:+${f}}" "$7" "${7:+${g}}" "$8" "${8:+${h}}" "$9" "${9:+${i}}" |     zaf_discovery_add_row "$rows" "$row" "$1" "${1:+${a}}" "$2" "${2:+${b}}" "$3" "${3:+${c}}" "$4" "${4:+${d}}" "$5" "${5:+${e}}" "$6" "${6:+${f}}" "$7" "${7:+${g}}" "$8" "${8:+${h}}" "$9" "${9:+${i}}" | ||||||
|   done |     row=$(expr $row + 1) | ||||||
|   zaf_discovery_dump |   done <$tmpfile | ||||||
|  |   zaf_discovery_end | ||||||
|  |   rm -f $tmpfile | ||||||
| } | } | ||||||
|  |  | ||||||
| ############################################ Zaf internal routines | ############################################ Zaf internal routines | ||||||
|  |  | ||||||
| zaf_version() { |  | ||||||
| 	echo master |  | ||||||
| } |  | ||||||
|  |  | ||||||
| # Restart zabbix agent | # Restart zabbix agent | ||||||
| zaf_restart_agent() { | zaf_restart_agent() { | ||||||
|  | 	zaf_wrn "Restarting agent (${ZAF_AGENT_RESTART})" | ||||||
| 	${ZAF_AGENT_RESTART} || zaf_err "Cannot restart Zabbix agent (${ZAF_AGENT_RESTART})!" | 	${ZAF_AGENT_RESTART} || zaf_err "Cannot restart Zabbix agent (${ZAF_AGENT_RESTART})!" | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -109,7 +134,7 @@ zaf_check_agent_config() { | |||||||
|  |  | ||||||
| # Update repo | # Update repo | ||||||
| zaf_update_repo() { | zaf_update_repo() { | ||||||
| 	[ "$ZAF_GIT" != 1 ] && { echo "Git is not installed."; return 1; } | 	[ "$ZAF_GIT" != 1 ] && { zaf_err "Git is not installed. Exiting."; } | ||||||
| 	! [ -d ${ZAF_REPO_DIR} ] && git clone "${ZAF_PLUGINS_REPO}" "${ZAF_REPO_DIR}" | 	! [ -d ${ZAF_REPO_DIR} ] && git clone "${ZAF_PLUGINS_REPO}" "${ZAF_REPO_DIR}" | ||||||
| 	[ -n "${ZAF_PLUGINS_REPO}" ] && cd ${ZAF_REPO_DIR} && git pull | 	[ -n "${ZAF_PLUGINS_REPO}" ] && cd ${ZAF_REPO_DIR} && git pull | ||||||
| } | } | ||||||
| @@ -121,7 +146,7 @@ zaf_update_repo() { | |||||||
| zaf_get_plugin_url() { | zaf_get_plugin_url() { | ||||||
| 	local url | 	local url | ||||||
| 	if echo "$1" | grep -q '/'; then | 	if echo "$1" | grep -q '/'; then | ||||||
| 		url="$1" 		# plugin with path - installing from directory | 		url="$1" 		# plugin with path - from directory | ||||||
| 	else | 	else | ||||||
| 		if echo "$1" | grep -q ^http; then | 		if echo "$1" | grep -q ^http; then | ||||||
| 			url="$1"	# plugin with http[s] url  | 			url="$1"	# plugin with http[s] url  | ||||||
| @@ -137,7 +162,6 @@ zaf_get_plugin_url() { | |||||||
| } | } | ||||||
|  |  | ||||||
| # $1 - control | # $1 - control | ||||||
| # $2 - if nonempty, show informarions instead of setting env |  | ||||||
| zaf_plugin_info() { | zaf_plugin_info() { | ||||||
| 	local control="$1" | 	local control="$1" | ||||||
|  |  | ||||||
| @@ -148,9 +172,8 @@ zaf_plugin_info() { | |||||||
| 	purl=$(zaf_ctrl_get_global_block <"${control}" | zaf_block_get_option Url) | 	purl=$(zaf_ctrl_get_global_block <"${control}" | zaf_block_get_option Url) | ||||||
| 	phome=$(zaf_ctrl_get_global_block <"${control}" | zaf_block_get_option Home) | 	phome=$(zaf_ctrl_get_global_block <"${control}" | zaf_block_get_option Home) | ||||||
| 	pitems=$(zaf_ctrl_get_items <"${control}") | 	pitems=$(zaf_ctrl_get_items <"${control}") | ||||||
| 	[ -z "$2" ] && return |  | ||||||
| 	echo | 	echo | ||||||
| 	echo -n "Plugin $plugin "; [ -n "$version" ] && echo -n "version ${pversion}"; echo ":" | 	echo -n "Plugin '$plugin' "; [ -n "$pversion" ] && echo -n "version ${pversion}"; echo ":" | ||||||
| 	echo "$pdescription"; echo | 	echo "$pdescription"; echo | ||||||
| 	[ -n "$pmaintainer" ] && echo "Maintainer: $pmaintainer" | 	[ -n "$pmaintainer" ] && echo "Maintainer: $pmaintainer" | ||||||
| 	[ -n "$purl" ] && echo "Url: $purl" | 	[ -n "$purl" ] && echo "Url: $purl" | ||||||
| @@ -160,17 +183,16 @@ zaf_plugin_info() { | |||||||
| 	echo | 	echo | ||||||
| } | } | ||||||
|  |  | ||||||
| # Prepare plugin into tmp dir  | # Prepare plugin into dir  | ||||||
| # $1 is url, directory or plugin name (will be searched in default plugin dir).  | # $1 is url, directory or plugin name (will be searched in default plugin dir).  | ||||||
| # $2 is directory to prepare.  | # $2 is directory to prepare.  | ||||||
| # $3 plugin name  |  | ||||||
| zaf_prepare_plugin() { | zaf_prepare_plugin() { | ||||||
| 	url=$(zaf_get_plugin_url "$1")/control.zaf | 	url=$(zaf_get_plugin_url "$1")/control.zaf | ||||||
| 	plugindir="$2" | 	plugindir="$2" | ||||||
| 	control=${plugindir}/control.zaf | 	control=${plugindir}/control.zaf | ||||||
|  | 	zaf_install_dir "$plugindir" | ||||||
| 	zaf_dbg "Fetching control file from $url ..." | 	zaf_dbg "Fetching control file from $url ..." | ||||||
| 	if zaf_fetch_url "$url" >"${control}"; then | 	if zaf_fetch_url "$url" >"${control}"; then | ||||||
| 		zaf_plugin_info "${control}" |  | ||||||
| 		zaf_ctrl_check_deps "${control}" | 		zaf_ctrl_check_deps "${control}" | ||||||
| 	else | 	else | ||||||
| 		zaf_err "Cannot fetch or write control file!" | 		zaf_err "Cannot fetch or write control file!" | ||||||
| @@ -178,27 +200,21 @@ zaf_prepare_plugin() { | |||||||
| } | } | ||||||
|  |  | ||||||
| zaf_install_plugin() { | zaf_install_plugin() { | ||||||
| 	mkdir "${ZAF_TMP_DIR}/plugin" |  | ||||||
| 	if zaf_prepare_plugin "$1" "${ZAF_TMP_DIR}/plugin"; then | 	if zaf_prepare_plugin "$1" "${ZAF_TMP_DIR}/plugin"; then | ||||||
|                 plugin=$(zaf_ctrl_get_global_block <"${ZAF_TMP_DIR}/plugin/control.zaf" | zaf_block_get_option Plugin) |                 plugin=$(zaf_ctrl_get_global_block <"${ZAF_TMP_DIR}/plugin/control.zaf" | zaf_block_get_option Plugin) | ||||||
| 		plugindir="${ZAF_PLUGINS_DIR}"/$plugin | 		plugindir="${ZAF_PLUGINS_DIR}"/$plugin | ||||||
| 		if zaf_prepare_plugin "$1" $plugindir; then | 		if zaf_prepare_plugin "$1" $plugindir; then | ||||||
|  | 			[ "$ZAF_DEBUG" -gt 0 ] && zaf_plugin_info "${control}" | ||||||
| 			zaf_ctrl_check_deps "${control}" | 			zaf_ctrl_check_deps "${control}" | ||||||
| 			zaf_ctrl_install "${control}" "${plugin}"  | 			zaf_ctrl_install "${control}" "${plugin}"  | ||||||
| 			zaf_ctrl_generate_cfg "${control}" "${plugin}"  | 			zaf_ctrl_generate_cfg "${control}" "${plugin}" >${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf | ||||||
| 			exit; | 			zaf_dbg "Generated ${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf" | ||||||
| 	#| \ |  | ||||||
| 				zaf_far '{PLUGINDIR}' "$plugindir" | \ |  | ||||||
| 				zaf_far '{ZAFLIBDIR}' "${ZAF_LIB_DIR}" | \ |  | ||||||
| 				zaf_far '{ZAFLOCK}' "${ZAF_LIB_DIR}/zaflock '$plugin' " \ |  | ||||||
| 				>$plugindir/zabbix.conf |  | ||||||
| 		else | 		else | ||||||
| 			zaf_err "Cannot install plugin $plugin to $plugindir!" | 			zaf_err "Cannot install plugin $plugin to $plugindir!" | ||||||
| 		fi | 		fi | ||||||
|         else |         else | ||||||
|             return 1 |             	zaf_err "Cannot prepare plugin $1" | ||||||
| 	fi | 	fi | ||||||
|  |  | ||||||
| } | } | ||||||
|  |  | ||||||
| # List installed plugins | # List installed plugins | ||||||
| @@ -206,7 +222,15 @@ zaf_install_plugin() { | |||||||
| zaf_list_plugins() { | zaf_list_plugins() { | ||||||
| 	local cfile | 	local cfile | ||||||
| 	local plugin | 	local plugin | ||||||
| 	cd ${ZAF_PLUGINS_DIR}; ls -1  | 	ls -1 ${ZAF_PLUGINS_DIR} | while read p; do | ||||||
|  | 		zaf_is_plugin "$(basename $p)" && echo $p | ||||||
|  | 	done | ||||||
|  | } | ||||||
|  |  | ||||||
|  | zaf_is_plugin() { | ||||||
|  | 	[ -d "$1" ] && [ -f "$1/control.zaf" ] && return | ||||||
|  | 	[ -d "$ZAF_PLUGINS_DIR/$1" ] && [ -f "$ZAF_PLUGINS_DIR/$1/control.zaf" ] && return | ||||||
|  | 	false | ||||||
| } | } | ||||||
|  |  | ||||||
| zaf_discovery_plugins() { | zaf_discovery_plugins() { | ||||||
| @@ -248,7 +272,8 @@ zaf_list_items() { | |||||||
| } | } | ||||||
|  |  | ||||||
| zaf_remove_plugin() { | zaf_remove_plugin() { | ||||||
| 	! [ -d ${ZAF_PLUGINS_DIR}/$1 ] && { echo "Plugin $1 not installed!"; exit 2; } | 	! [ -d ${ZAF_PLUGINS_DIR}/$1 ] && { zaf_err "Plugin $1 not installed!"; } | ||||||
|  | 	zaf_wrn "Removing plugin $1" | ||||||
| 	rm -rf ${ZAF_PLUGINS_DIR}/$1 | 	rm -rf ${ZAF_PLUGINS_DIR}/$1 | ||||||
| 	rm -f ${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf | 	rm -f ${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										53
									
								
								zaf
									
									
									
									
									
								
							
							
						
						
									
										53
									
								
								zaf
									
									
									
									
									
								
							| @@ -1,5 +1,9 @@ | |||||||
| #!/bin/sh | #!/bin/sh | ||||||
|  |  | ||||||
|  | # Hardcoded variables | ||||||
|  | ZAF_VERSION="trunk" | ||||||
|  | ZAF_URL="https://raw.githubusercontent.com/limosek/zaf/master/" | ||||||
|  |  | ||||||
| ZAF_CFG_FILE="/etc/zaf.conf" | ZAF_CFG_FILE="/etc/zaf.conf" | ||||||
| if [ -f $ZAF_CFG_FILE ]; then | if [ -f $ZAF_CFG_FILE ]; then | ||||||
| 	. $ZAF_CFG_FILE | 	. $ZAF_CFG_FILE | ||||||
| @@ -10,10 +14,10 @@ fi | |||||||
| [ -d $(dirname $0)/.git ] && ZAF_LIB_DIR=$(dirname $0)/lib | [ -d $(dirname $0)/.git ] && ZAF_LIB_DIR=$(dirname $0)/lib | ||||||
|  |  | ||||||
| [ -z "$ZAF_TMP_BASE" ] && ZAF_TMP_BASE=/tmp/zaf | [ -z "$ZAF_TMP_BASE" ] && ZAF_TMP_BASE=/tmp/zaf | ||||||
| ZAF_TMP_DIR="${ZAF_TMP_BASE}-${USER}-$$" | ZAF_TMP_DIR="${ZAF_TMP_BASE}-${USER}" | ||||||
| trap "rm -rif ${ZAF_TMP_DIR}" EXIT | trap "rm -rif ${ZAF_TMP_DIR}" EXIT | ||||||
| ! [ -d "${ZAF_TMP_DIR}" ] && mkdir "${ZAF_TMP_DIR}" | ! [ -d "${ZAF_TMP_DIR}" ] && mkdir "${ZAF_TMP_DIR}" | ||||||
| [ -z "$ZAF_DEBUG" ] && ZAF_DEBUG=0 | [ -z "$ZAF_DEBUG" ] && ZAF_DEBUG=1 | ||||||
|  |  | ||||||
| . ${ZAF_LIB_DIR}/zaf.lib.sh | . ${ZAF_LIB_DIR}/zaf.lib.sh | ||||||
| . ${ZAF_LIB_DIR}/os.lib.sh | . ${ZAF_LIB_DIR}/os.lib.sh | ||||||
| @@ -32,9 +36,16 @@ update) | |||||||
|  |  | ||||||
| show) | show) | ||||||
| 	if [ -z "$2" ]; then | 	if [ -z "$2" ]; then | ||||||
| 		zaf_show_installed_plugins | 		zaf_list_plugins | while read plugin; do | ||||||
|  | 			zaf_plugin_info $ZAF_PLUGINS_DIR/$plugin/control.zaf | ||||||
|  | 		done | ||||||
| 	else		 | 	else		 | ||||||
| 		zaf_show_plugin "$2" | 		if zaf_list_plugins | grep -q "^$2"; then | ||||||
|  | 			zaf_plugin_info $ZAF_PLUGINS_DIR/$2/control.zaf | ||||||
|  | 		else | ||||||
|  | 			zaf_prepare_plugin "$2" "$ZAF_TMP_DIR/plugin" | ||||||
|  | 			zaf_plugin_info "$ZAF_TMP_DIR/plugin/control.zaf" | ||||||
|  | 		fi | ||||||
| 	fi | 	fi | ||||||
| 	;; | 	;; | ||||||
|  |  | ||||||
| @@ -58,20 +69,48 @@ install) | |||||||
| 	shift; | 	shift; | ||||||
| 	[ -z "$@" ] && echo "$0 install plugin [plugin]..." | 	[ -z "$@" ] && echo "$0 install plugin [plugin]..." | ||||||
| 	for p in $@; do | 	for p in $@; do | ||||||
|  | 		if zaf_is_plugin "$p"; then | ||||||
|  | 			zaf_wrn "Plugin $p already installed. Skipping installation." | ||||||
|  | 			continue | ||||||
|  | 		fi | ||||||
| 		zaf_install_plugin "$p" | 		zaf_install_plugin "$p" | ||||||
|  | 		installed=1 | ||||||
| 	done | 	done | ||||||
|  | 	[ -n "$installed" ] && zaf_is_root && zaf_restart_agent | ||||||
|  | 	;; | ||||||
|  |  | ||||||
|  | reinstall) | ||||||
|  | 	shift; | ||||||
|  | 	[ -z "$@" ] && echo "$0 reinstall plugin [plugin]..." | ||||||
|  | 	for p in $@; do | ||||||
|  | 		if zaf_is_plugin "$p"; then | ||||||
|  | 			zaf_remove_plugin "$p" | ||||||
|  | 			reinstalled=1 | ||||||
|  | 		fi | ||||||
|  | 		zaf_install_plugin "$p" | ||||||
|  | 		reinstalled=1 | ||||||
|  | 	done | ||||||
|  | 	[ -n "$reinstalled" ] && zaf_is_root && zaf_restart_agent | ||||||
| 	;; | 	;; | ||||||
|  |  | ||||||
| remove) | remove) | ||||||
| 	zaf_remove_plugin "$2" | 	shift; | ||||||
|  | 	[ -z "$@" ] && echo "$0 remove plugin [plugin]..." | ||||||
|  | 	for p in $@; do | ||||||
|  | 		if zaf_is_plugin "$p"; then | ||||||
|  | 			zaf_remove_plugin "$p" | ||||||
|  | 			removed=1 | ||||||
|  | 		fi | ||||||
|  | 	done | ||||||
|  | 	[ -n "$removed" ] && zaf_is_root && zaf_restart_agent | ||||||
| 	;; | 	;; | ||||||
|  |  | ||||||
| self-upgrade) | self-upgrade) | ||||||
| 	rm -rf /tmp/zaf-installer && mkdir /tmp/zaf-installer | 	rm -rf /tmp/zaf-installer && mkdir /tmp/zaf-installer | ||||||
| 	if zaf_fetch_url https://raw.githubusercontent.com/limosek/zaf/master/install.sh >/tmp/zaf-installer/install.sh; then | 	if zaf_fetch_url $ZAF_URL/install.sh >/tmp/zaf-installer/install.sh; then | ||||||
| 		cd /tmp/zaf-installer && ./install.sh | 		cd /tmp/zaf-installer && ./install.sh | ||||||
| 	else | 	else | ||||||
| 		echo "Cannot fetch uri https://raw.githubusercontent.com/limosek/zaf/master/install.sh!";  | 		echo "Cannot fetch uri $ZAF_URL/install.sh!";  | ||||||
| 	fi | 	fi | ||||||
| 	;; | 	;; | ||||||
| 	 | 	 | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Lukas Macura
					Lukas Macura