mirror of
				https://github.com/limosek/zaf.git
				synced 2025-10-31 17:47:37 +01:00 
			
		
		
		
	First useable version
This commit is contained in:
		
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -1 +1,2 @@ | ||||
| *~ | ||||
| plugins/ | ||||
|   | ||||
							
								
								
									
										23
									
								
								control
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								control
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,23 @@ | ||||
| Plugin: zaf | ||||
|  Basic items for Zabbix Agent Framework | ||||
|  | ||||
| Version: 0.1 | ||||
|  | ||||
| Url: https://raw.githubusercontent.com/limosek/zaf/master/ | ||||
|  | ||||
| Web: https://github.com/limosek/zaf/ | ||||
|  | ||||
| Maintainer: Lukas Macura <lukas@macura.cz> | ||||
|  | ||||
| Item: version | ||||
|  Returns version of zaf installed. | ||||
|  | ||||
| Item-cmd-version: {ZAFLIB} zaf_list_plugins | ||||
|  | ||||
| Item: plugins | ||||
|  Returns installed plugins | ||||
|  | ||||
| Item-cmd-plugins: {ZAFLOCK} zaf list | ||||
|  | ||||
| Binary-Depends: echo zaf | ||||
|  | ||||
							
								
								
									
										71
									
								
								install.sh
									
									
									
									
									
								
							
							
						
						
									
										71
									
								
								install.sh
									
									
									
									
									
								
							| @@ -2,6 +2,10 @@ | ||||
|  | ||||
| ZAF_CFG_FILE=/etc/zaf.conf | ||||
|  | ||||
| zaf_msg() { | ||||
| 	[ "$ZAF_DEBUG" = "1" ] && echo $@ | ||||
| } | ||||
|  | ||||
| # Read option. If it is already set in zaf.conf, it is skipped. If env variable is set, it is used instead of default | ||||
| # It sets global variable name on result. | ||||
| # $1 - option name | ||||
| @@ -13,7 +17,7 @@ zaf_get_option(){ | ||||
| 	eval opt=\$$1 | ||||
| 	if [ -n "$opt" ] && ! [ "$4" = "user" ]; then | ||||
| 		eval "$1='$opt'" | ||||
| 		echo "Got $2 <$1> from ENV: $opt" >&2 | ||||
| 		zaf_msg "Got $2 <$1> from ENV: $opt" >&2 | ||||
| 		return | ||||
| 	else | ||||
| 		opt="$3" | ||||
| @@ -26,9 +30,9 @@ zaf_get_option(){ | ||||
| 	fi | ||||
| 	if [ -z "$opt" ]; then | ||||
| 		opt="$3" | ||||
| 		echo "Got $2 <$1> from Defaults: $opt" >&2 | ||||
| 		zaf_msg "Got $2 <$1> from Defaults: $opt" >&2 | ||||
| 	else | ||||
| 		echo "Got $2 <$1> from USER: $opt" | ||||
| 		zaf_msg "Got $2 <$1> from USER: $opt" | ||||
| 	fi | ||||
| 	eval "$1='$opt'"	 | ||||
| } | ||||
| @@ -39,7 +43,9 @@ zaf_get_option(){ | ||||
| zaf_set_option(){ | ||||
| 	if ! grep -q "^$1=" ${ZAF_CFG_FILE}; then | ||||
| 		echo "$1='$2'" >>${ZAF_CFG_FILE} | ||||
| 		echo "Saving $1 to $2 in ${ZAF_CFG_FILE}" >&2 | ||||
| 		zaf_msg "Saving $1 to $2 in ${ZAF_CFG_FILE}" >&2 | ||||
| 	else | ||||
| 		zaf_msg "Preserving $1 to $2 in ${ZAF_CFG_FILE}" >&2 | ||||
| 	fi | ||||
| } | ||||
|  | ||||
| @@ -74,40 +80,60 @@ zaf_no_perms(){ | ||||
|  | ||||
| zaf_configure(){ | ||||
|  | ||||
| 	zaf_detect_pkg ZAF_PKG "Packaging system to use" "$(zaf_detect_pkg)"	 | ||||
| 	zaf_get_option ZAF_TMP_DIR "Tmp directory" "/tmp/zaf" | ||||
| 	zaf_get_option ZAF_LIB_DIR "Libraries directory" "/usr/lib/zaf" | ||||
| 	zaf_get_option ZAF_PLUGINS_DIR "Plugins directory" "${ZAF_LIB_DIR}/plugins" | ||||
| 	zaf_get_option ZAF_PLUGINS_REPO "Plugins reposiory" "git://github.com/limosek/zaf-plugins.git" | ||||
| 	zaf_get_option ZAF_AGENT_CONFIG "Zabbix agent config" "/etc/zabbix/zabbix_agentd.conf" | ||||
| 	zaf_get_option ZAF_AGENT_CONFIGD "Zabbix agent config.d" "/etc/zabbix/zabbix_agentd.conf.d/" | ||||
| 	zaf_get_option ZAF_AGENT_BIN "Zabbix agent binary" "/usr/sbin/zabbix_agentd" | ||||
| 	zaf_get_option ZAF_AGENT_RESTART "Zabbix agent restart cmd" "service zabbix-agent restart" | ||||
| 	[ "$1" = "user" ] && ZAF_DEBUG=1 | ||||
| 	zaf_detect_pkg ZAF_PKG "Packaging system to use" "$(zaf_detect_pkg)" "$1" | ||||
| 	zaf_get_option ZAF_TMP_BASE "Tmp directory prefix (\$USER will be added)" "/tmp/zaf" "$1" | ||||
| 	zaf_get_option ZAF_LIB_DIR "Libraries directory" "/usr/lib/zaf" "$1" | ||||
| 	zaf_get_option ZAF_PLUGINS_DIR "Plugins directory" "${ZAF_LIB_DIR}/plugins" "$1" | ||||
| 	zaf_get_option ZAF_PLUGINS_REPO "Plugins reposiory" "git://github.com/limosek/zaf-plugins.git" "$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_CONFIGD "Zabbix agent config.d" "/etc/zabbix/zabbix_agentd.conf.d/" "$1" | ||||
| 	zaf_get_option ZAF_AGENT_BIN "Zabbix agent binary" "/usr/sbin/zabbix_agentd" "$1" | ||||
| 	zaf_get_option ZAF_AGENT_RESTART "Zabbix agent restart cmd" "service zabbix-agent restart" "$1" | ||||
| 	 | ||||
| 	if ! which $ZAF_AGENT_BIN >/dev/null; then | ||||
| 		echo "Zabbix agent not installed? Use ZAF_ZABBIX_AGENT_BIN env variable to specify location. Exiting." | ||||
| 		exit 3 | ||||
| 	fi | ||||
| 	if which git >/dev/null; then | ||||
| 		ZAF_GIT=1 | ||||
| 	else | ||||
| 		ZAF_GIT="" | ||||
| 	fi | ||||
|  | ||||
| 	if ! [ -f "${ZAF_CFG_FILE}" ]; then | ||||
| 		touch "${ZAF_CFG_FILE}" || zaf_no_perms "${ZAF_CFG_FILE}" | ||||
| 	fi | ||||
| 	 | ||||
| 	zaf_set_option ZAF_PKG "${ZAF_PKG}" | ||||
| 	zaf_set_option ZAF_TMP_DIR "$ZAF_TMP_DIR" | ||||
| 	zaf_set_option ZAF_GIT "${ZAF_GIT}" | ||||
| 	zaf_set_option ZAF_TMP_BASE "$ZAF_TMP_BASE" | ||||
| 	zaf_set_option ZAF_LIB_DIR "$ZAF_LIB_DIR" | ||||
| 	zaf_set_option ZAF_PLUGINS_DIR "$ZAF_PLUGINS_DIR" | ||||
| 	zaf_set_option ZAF_PLUGINS_REPO "$ZAF_PLUGINS_REPO" | ||||
| 	zaf_set_option ZAF_REPO_DIR "$ZAF_REPO_DIR" | ||||
| 	zaf_set_option ZAF_AGENT_CONFIG "$ZAF_AGENT_CONFIG" | ||||
| 	zaf_set_option ZAF_AGENT_CONFIGD "$ZAF_AGENT_CONFIGD" | ||||
| 	zaf_set_option ZAF_AGENT_BIN "$ZAF_AGENT_BIN" | ||||
| 	zaf_set_option ZAF_AGENTRESTART "$ZAF_AGENT_RESTART" | ||||
| 	zaf_set_option ZAF_AGENT_RESTART "$ZAF_AGENT_RESTART" | ||||
| 	ZAF_TMP_DIR="${ZAF_TMP_BASE}-${USER}-$$" | ||||
| } | ||||
|  | ||||
| if [ -f "${ZAF_CFG_FILE}" ]; then | ||||
| 	. "${ZAF_CFG_FILE}" | ||||
| fi | ||||
| ZAF_TMP_DIR="${ZAF_TMP_BASE}-${USER}-$$" | ||||
|  | ||||
| case $1 in | ||||
| reconf) | ||||
| 	zaf_configure user | ||||
| 	$0 install | ||||
| 	;; | ||||
| defconf) | ||||
| 	zaf_configure silent | ||||
| 	$0 install | ||||
| 	;; | ||||
| *) | ||||
| 	zaf_configure | ||||
| 	rm -rif ${ZAF_TMP_DIR} | ||||
| @@ -115,10 +141,14 @@ case $1 in | ||||
| 	install -d ${ZAF_LIB_DIR} | ||||
| 	install -d ${ZAF_PLUGINS_DIR} | ||||
| 	install $(getrest lib/zaf.lib.sh) ${ZAF_LIB_DIR}/ | ||||
| 	install $(getrest lib/jshn.sh) ${ZAF_LIB_DIR}/ | ||||
| 	install $(getrest lib/zaflock) ${ZAF_LIB_DIR}/ | ||||
| 	mkdir -p ${ZAF_TMP_DIR}/p/zaf | ||||
| 	install $(getrest control) ${ZAF_TMP_DIR}/p/zaf/ | ||||
| 	install $(getrest template.xml) ${ZAF_TMP_DIR}/p/zaf/ | ||||
| 	mkdir -p ${ZAF_PLUGINS_DIR} | ||||
| 	echo "UserParameter=zaf.version,echo master" >${ZAF_AGENT_CONFIGD}/zaf_base.conf | ||||
| 	install $(getrest zaf) /usr/bin  | ||||
| 	echo "Install OK. Installing plugins (${ZAF_DEFAULT_PLUGINS})." | ||||
| 	install $(getrest zaf) /usr/bin/ | ||||
| 	/usr/bin/zaf install ${ZAF_TMP_DIR}/p/zaf/ | ||||
| 	if  ! /usr/bin/zaf check-agent-config; then | ||||
| 		echo "Something is wrong with zabbix agent config." | ||||
| 		echo "Ensure that zabbix_agentd reads ${ZAF_AGENT_CONFIG}" | ||||
| @@ -126,11 +156,8 @@ case $1 in | ||||
| 		echo "Does ${ZAF_AGENT_RESTART} work?" | ||||
| 		exit 1 | ||||
| 	fi | ||||
| 	for plugin in ${ZAF_DEFAULT_PLUGINS}; do | ||||
| 		/usr/bin/zaf install $plugin || exit $? | ||||
| 	done | ||||
| 	rm -rif ${ZAF_TMP_DIR} | ||||
| 	echo "Done" | ||||
| 	echo "Install OK. Use 'zaf' without parameters to continue." | ||||
| 	;; | ||||
| esac | ||||
|  | ||||
|   | ||||
							
								
								
									
										280
									
								
								lib/jshn.sh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										280
									
								
								lib/jshn.sh
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,280 @@ | ||||
| # 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" ] | ||||
| } | ||||
							
								
								
									
										123
									
								
								lib/zaf.lib.sh
									
									
									
									
									
								
							
							
						
						
									
										123
									
								
								lib/zaf.lib.sh
									
									
									
									
									
								
							| @@ -1,4 +1,11 @@ | ||||
|  | ||||
| . /etc/zaf.conf | ||||
| . ${ZAF_LIB_DIR}/jshn.sh | ||||
| ZAF_TMP_DIR="${ZAF_TMP_BASE}-${USER}-$$" | ||||
| trap "rm -rif ${ZAF_TMP_DIR}" EXIT | ||||
|  | ||||
| ! [ -d "${ZAF_TMP_DIR}" ] && mkdir "${ZAF_TMP_DIR}" | ||||
|  | ||||
| # Fetch url to stdout  | ||||
| # $1 url | ||||
| # It supports real file, file:// and other schemes known by curl | ||||
| @@ -48,14 +55,11 @@ zaf_check_agent_config() { | ||||
|  | ||||
| # Update repo | ||||
| zaf_update_repo() { | ||||
| 	[ "$ZAF_GIT" != 1 ] && { echo "Git is not installed."; return 1; } | ||||
| 	! [ -d ${ZAF_REPO_DIR} ] &&  git clone "${ZAF_PLUGINS_REPO}" "${ZAF_REPO_DIR}" | ||||
| 	[ -n "${ZAF_PLUGINS_REPO}" ] && cd ${ZAF_REPO_DIR} && git pull | ||||
| } | ||||
|  | ||||
| # List installed plugins | ||||
| zaf_list_installed_plugins() { | ||||
| 	cd ${ZAF_PLUGINS_DIR}; ls --hide '.' | ||||
| } | ||||
|  | ||||
| # Check plugin url | ||||
| # $1 plugin uri | ||||
| # $2 local file to fetch | ||||
| @@ -69,7 +73,20 @@ zaf_plugin_fetch_control() { | ||||
| # $1 control file | ||||
| # $2 option | ||||
| zaf_ctrl_get_option() { | ||||
| 	grep -E '^(.*): ' "$1" | grep -F "$2:" | cut -d ' ' -f 2- | ||||
| 	awk 'BEGIN { FS=": "; }; /^'$2': / { printf $2$3$4$5"\n"; }' <$1 | ||||
| } | ||||
|  | ||||
| # Get description from control file | ||||
| # $1 control file | ||||
| # $2 option | ||||
| zaf_ctrl_get_description() { | ||||
| 	awk \ | ||||
| 	"/^$2/"' { i=1; | ||||
|      		while (1) { | ||||
|         		getline; if (substr($0,0,1) != " ") exit; | ||||
|         		printf $0"\n"; | ||||
|       		} | ||||
|      	}' <$1 | ||||
| } | ||||
|  | ||||
| zaf_ctrl_binary_deps() { | ||||
| @@ -100,8 +117,11 @@ zaf_ctrl_install_bin() { | ||||
| zaf_ctrl_generate_cfg() { | ||||
| 	local items | ||||
| 	local cmd | ||||
| 	local ilock | ||||
|  | ||||
| 	items=$(zaf_ctrl_get_option "$1" Item) | ||||
| 	for i in $items; do | ||||
| 		ilock=$(echo $i | tr -d '[]*&;:') | ||||
| 		cmd=$(zaf_ctrl_get_option "$1" "Item-cmd-$i") | ||||
| 		echo "UserParameter=$2.${i},$cmd" | ||||
| 	done | ||||
| @@ -120,45 +140,118 @@ zaf_install_plugin() { | ||||
| 	else | ||||
| 		url="$1" | ||||
| 	fi | ||||
| 	plugin=$(basename "$url") | ||||
| 	echo Installing plugin $plugin from $url... | ||||
| 	plugin="plug$$" | ||||
| 	rm -rf ${ZAF_TMP_DIR}/${plugin} | ||||
| 	control=${ZAF_TMP_DIR}/${plugin}/control | ||||
| 	plugindir="${ZAF_PLUGINS_DIR}/${plugin}" | ||||
| 	mkdir -p "${ZAF_TMP_DIR}/${plugin}" | ||||
| 	if zaf_plugin_fetch_control "$url" "${control}"; then | ||||
| 		set -e | ||||
| 		plugin=$(zaf_ctrl_get_option "${control}" Plugin) | ||||
| 		if [ -n "$plugin" ]; then | ||||
| 			echo Installing plugin $plugin from $url... | ||||
| 			plugindir="${ZAF_PLUGINS_DIR}/${plugin}" | ||||
| 			zaf_ctrl_binary_deps "${control}" | ||||
| 			mkdir -p $plugindir | ||||
| 			zaf_ctrl_install_bin "${control}" "${plugin}"  | ||||
| 		zaf_ctrl_generate_cfg "${control}" "${plugin}" | zaf_far '{PLUGINDIR}' "$plugindir" >${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf | ||||
| 			zaf_ctrl_generate_cfg "${control}" "${plugin}" | \ | ||||
| 				zaf_far '{PLUGINDIR}' "$plugindir" | \ | ||||
| 				zaf_far '{ZAFLIB}' ". ${ZAF_LIB_DIR}/zaf.lib.sh; " | \ | ||||
| 				zaf_far '{ZAFLOCK}' "${ZAF_LIB_DIR}/zaflock '$plugin' " \ | ||||
| 				>${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf | ||||
| 			zaf_restart_agent | ||||
| 			cp $control "$plugindir"/ | ||||
| 			zaf_fetch_url $url/template.xml >"$plugindir"/template.xml | ||||
| 		else | ||||
| 			echo "Bad control file!" | ||||
| 			cat $control | ||||
| 			exit 4 | ||||
| 		fi | ||||
| 	else | ||||
| 		echo "Cannot fetch control file!" | ||||
| 		exit 4 | ||||
| 	fi | ||||
| } | ||||
|  | ||||
| zaf_plugin_info() { | ||||
| # Show installed plugins (human readable) | ||||
| # $1 - plugin | ||||
| zaf_show_installed_plugins() { | ||||
| 	local cfile | ||||
| 	local plugin | ||||
| 	cd ${ZAF_PLUGINS_DIR}; ls --hide '.' -1 | while read plugin; do | ||||
| 		cfile=${ZAF_PLUGINS_DIR}/$plugin/control | ||||
| 		echo Plugin $plugin: | ||||
| 		zaf_ctrl_get_description $cfile Plugin: | ||||
| 		echo "Homepage:" $(zaf_ctrl_get_option $cfile Web) | ||||
| 		echo "Maintainer:" $(zaf_ctrl_get_option $cfile Maintainer) | ||||
| 		echo | ||||
| 	done | ||||
| } | ||||
|  | ||||
| # List installed plugins | ||||
| # $1 - plugin | ||||
| zaf_list_plugins() { | ||||
| 	local cfile | ||||
| 	local plugin | ||||
| 	cd ${ZAF_PLUGINS_DIR}; ls --hide '.' -1  | ||||
| } | ||||
|  | ||||
| zaf_show_plugin() { | ||||
| 	local items | ||||
| 	local plugindir | ||||
| 	local cfile | ||||
| 	local tst | ||||
|  | ||||
| 	if [ -z "$1" ]; then | ||||
| 		echo "Missing plugin name"; | ||||
| 		exit 1 | ||||
| 	fi | ||||
| 	[ -n "$2" ] && tst=1 | ||||
| 	plugindir="${ZAF_PLUGINS_DIR}/$1" | ||||
| 	cfile="$plugindir/control" | ||||
| 	if [ -d "$plugindir" ] ; then | ||||
| 		items=$(zaf_ctrl_get_option "$plugindir/control" Item) | ||||
| 		echo "Items supported:" | ||||
| 		echo "$items" | ||||
| 		echo "Plugin $1:" | ||||
| 		zaf_ctrl_get_description "$cfile" "Plugin:" | ||||
| 		echo "Homepage:" $(zaf_ctrl_get_option $cfile Web) | ||||
| 		echo "Maintainer:" $(zaf_ctrl_get_option $cfile Maintainer)	 | ||||
| 		items=$(zaf_list_plugin_items $1) | ||||
| 		echo  | ||||
| 		echo "Supported items:" | ||||
| 		for i in $items; do | ||||
| 			echo -n "$1.$i: " | ||||
| 			[ -n "$tst" ] && ${ZAF_AGENT_BIN} -t "$1.$i" | ||||
| 			echo | ||||
| 			zaf_ctrl_get_description "$cfile" "Item: $i"; | ||||
| 			echo | ||||
| 		done | ||||
| 	else | ||||
| 		echo "Plugin $1 not installed"  | ||||
| 	fi | ||||
| } | ||||
|  | ||||
| zaf_list_plugin_items() { | ||||
| 	if [ -z "$1" ]; then | ||||
| 		echo "Missing plugin name"; | ||||
| 		exit 1 | ||||
| 	fi | ||||
| 	plugindir="${ZAF_PLUGINS_DIR}/$1" | ||||
| 	cfile="$plugindir/control" | ||||
| 	if [ -d "$plugindir" ] ; then | ||||
| 		zaf_ctrl_get_option "$cfile" Item | ||||
| 	else | ||||
| 		echo "Plugin $1 not installed"  | ||||
| 	fi | ||||
| } | ||||
|  | ||||
| zaf_list_items() { | ||||
| 	for p in $(zaf_list_plugins); do | ||||
| 		zaf_list_plugin_items $p | ||||
| 	done | ||||
| } | ||||
|  | ||||
| zaf_remove_plugin() { | ||||
| 	! [ -d ${ZAF_PLUGINS_DIR}/$1 ] && { echo "Plugin $1 not installed!"; exit 2; } | ||||
| 	rm -rf ${ZAF_PLUGINS_DIR}/$1 | ||||
| 	rm -f ${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf | ||||
| } | ||||
|  | ||||
|  | ||||
|  | ||||
|   | ||||
							
								
								
									
										37
									
								
								lib/zaflock
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								lib/zaflock
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,37 @@ | ||||
| #!/bin/sh | ||||
|  | ||||
| . /etc/zaf.conf | ||||
|  | ||||
| . ${ZAF_LIB_DIR}/zaf.lib.sh | ||||
|  | ||||
| help() { | ||||
| 	echo "$0 key cmd" | ||||
| 	exit 2 | ||||
| } | ||||
|  | ||||
| lkey="$1" | ||||
| [ -z "$lkey" ] && help | ||||
| shift | ||||
|  | ||||
| [ -z "${ZAF_LOCK_SECONDS}" ] && seconds=5 | ||||
| [ -z "${ZAF_LOCK_FORCE}" ] && force=1 | ||||
|  | ||||
| lockfile="${ZAF_TMP_DIR}/zaflock_${lkey}" | ||||
| i=0 | ||||
| while [ -f "$lockfile" ] && [ $i -lt $seconds ]; do | ||||
| 	sleep 1 | ||||
| 	i=$(expr $i + 1) | ||||
| done | ||||
| if [ -f "$lockfile" ] && [ -n "$force" ]; then | ||||
| 	logger -s -t "zlock" -p daemon.warn "Releasing $lockfile!" | ||||
| 	rm -f "$lockfile" | ||||
| fi | ||||
| if [ -f "$lockfile" ] && [ -z "$force" ]; then | ||||
| 	logger -s -t "zlock" -p daemon.err "Could not get lock for $lockfile!" | ||||
| 	exit 1 | ||||
| fi | ||||
|  | ||||
| touch "$lockfile" | ||||
| [ -n "$*" ] && $@ | ||||
| rm -f "$lockfile" | ||||
|  | ||||
							
								
								
									
										0
									
								
								template.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								template.xml
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										50
									
								
								zaf
									
									
									
									
									
								
							
							
						
						
									
										50
									
								
								zaf
									
									
									
									
									
								
							| @@ -1,14 +1,10 @@ | ||||
| #!/bin/sh | ||||
|  | ||||
| # Some defaults | ||||
|  | ||||
| ZAF_AGENT_RESTART="service zabbix-agentd restart" | ||||
| ZAF_AGENT_CONFIG="/etc/zabbix/zabbix_agentd.conf" | ||||
| ZAF_LIB_DIR="/usr/lib/zaf/" | ||||
| ZAF_PLUGINS_DIR="/usr/lib/zaf/plugins/" | ||||
|  | ||||
| if [ -f /etc/zaf.conf  ]; then | ||||
| 	. /etc/zaf.conf | ||||
| else | ||||
| 	echo "Missing config file /etc/zaf.conf! Exiting." | ||||
| 	exit 2 | ||||
| fi | ||||
|  | ||||
| . ${ZAF_LIB_DIR}/zaf.lib.sh | ||||
| @@ -23,12 +19,28 @@ update) | ||||
| 	zaf_update_repo | ||||
| 	;; | ||||
|  | ||||
| list) | ||||
| 	zaf_list_installed_plugins | ||||
| show) | ||||
| 	if [ -z "$2" ]; then | ||||
| 		zaf_show_installed_plugins | ||||
| 	else | ||||
| 		zaf_show_plugin "$2" | ||||
| 	fi | ||||
| 	;; | ||||
|  | ||||
| info) | ||||
| 	zaf_plugin_info "$2" | ||||
| list) | ||||
| 	zaf_list_plugins | ||||
| 	;; | ||||
|  | ||||
| list-items) | ||||
| 	if [ -z "$2" ]; then | ||||
| 		zaf_list_items | ||||
| 	else | ||||
| 		zaf_list_plugin_items "$2" | ||||
| 	fi | ||||
| 	;; | ||||
|  | ||||
| test-items) | ||||
| 	zaf_show_plugin "$2" tst | ||||
| 	;; | ||||
|  | ||||
| install) | ||||
| @@ -39,11 +51,19 @@ remove) | ||||
| 	zaf_remove_plugin "$2" | ||||
| 	;; | ||||
|  | ||||
| self-upgrade) | ||||
| 	curl -s https://raw.githubusercontent.com/limosek/zaf/master/install.sh | sh | ||||
| 	;; | ||||
|  | ||||
| *) | ||||
| 	echo "$0 update" | ||||
| 	echo "$0 list" | ||||
| 	echo "$0 install plugin" | ||||
| 	echo "$0 remove plugin"  | ||||
| 	echo "$0 update			To update repo"  | ||||
| 	echo "$0 list			To list installed plugins" | ||||
| 	echo "$0 show [plugin]		To show installed plugins or plugin info" | ||||
| 	echo "$0 list-items [plugin]	To list all suported items [for plugin]" | ||||
| 	echo "$0 test-items plugin	To test all suported items for plugin" | ||||
| 	echo "$0 install plugin		To install plugin" | ||||
| 	echo "$0 remove plugin		To remove plugin"  | ||||
| 	echo "$0 self-upgrade		To self-upgrade zaf" | ||||
| 	;; | ||||
|  | ||||
| esac | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Lukas Macura
					Lukas Macura