mirror of
https://github.com/limosek/zaf.git
synced 2024-11-21 18:09:06 +01:00
Initial release
This commit is contained in:
parent
5e566a2046
commit
1e89b27028
15
install.sh
15
install.sh
@ -34,7 +34,7 @@ preconf(){
|
||||
readopt "Git plugins directory" "${ZAF_LIB_DIR}/repo"
|
||||
ZAF_REPO_DIR="$opt"
|
||||
|
||||
readopt "Plugins repository" "https://github.com/limosek/zaf.git/plugins"
|
||||
readopt "Plugins repository" ""
|
||||
ZAF_PLUGINS_REPO="$opt"
|
||||
|
||||
readopt "Default plugins to install" "process-info"
|
||||
@ -43,6 +43,9 @@ preconf(){
|
||||
readopt "Zabbix agent config" "/etc/zabbix/zabbix_agentd.conf"
|
||||
ZAF_AGENT_CONFIG="$opt"
|
||||
|
||||
readopt "Zabbix agent config.d" "/etc/zabbix/zabbix_agentd.conf.d/"
|
||||
ZAF_AGENT_CONFIGD="$opt"
|
||||
|
||||
readopt "Zabbix agent restart cmd" "service zabbix-agent restart"
|
||||
ZAF_AGENT_RESTART="$opt"
|
||||
|
||||
@ -73,6 +76,7 @@ preconf(){
|
||||
echo "ZAF_PLUGINS_REPO='$ZAF_PLUGINS_REPO'" >>/etc/zaf.conf
|
||||
echo "ZAF_AGENT_RESTART='$ZAF_AGENT_RESTART'" >>/etc/zaf.conf
|
||||
echo "ZAF_AGENT_CONFIG='$ZAF_AGENT_CONFIG'" >>/etc/zaf.conf
|
||||
echo "ZAF_AGENT_CONFIGD='$ZAF_AGENT_CONFIGD'" >>/etc/zaf.conf
|
||||
echo "ZAF_SUDO='$ZAF_SUDO'" >>/etc/zaf.conf
|
||||
}
|
||||
|
||||
@ -97,11 +101,20 @@ reconf)
|
||||
fi
|
||||
install $(getrest lib/zaf.lib.sh) ${ZAF_LIB_DIR}/
|
||||
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})."
|
||||
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}"
|
||||
echo "and there is Include=${ZAF_AGENT_CONFIGD} directive inside."
|
||||
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"
|
||||
;;
|
||||
esac
|
||||
|
150
lib/zaf.lib.sh
150
lib/zaf.lib.sh
@ -1,15 +1,159 @@
|
||||
|
||||
zaf_fetch_url() {
|
||||
local scheme
|
||||
local uri
|
||||
|
||||
scheme=$(echo $1|cut -d ':' -f 1)
|
||||
uri=$(echo $1|cut -d '/' -f 3-)
|
||||
if [ "$1" = "$scheme" ]; then
|
||||
cat "$1"
|
||||
fi
|
||||
case $scheme in
|
||||
http|https|ftp|file)
|
||||
curl -f -s -L -o - "$1";
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
zaf_far(){
|
||||
local f
|
||||
local t
|
||||
local sedcmd="sed"
|
||||
i=1
|
||||
while [ "$i" -lt "$#" ];
|
||||
do
|
||||
eval f=\$${i}
|
||||
i=$(expr $i + 1)
|
||||
eval t=\$${i}
|
||||
i=$(expr $i + 1)
|
||||
sedcmd="$sedcmd -e 's~$f~$t~g'"
|
||||
done
|
||||
eval $sedcmd
|
||||
}
|
||||
|
||||
|
||||
zaf_restart_agent() {
|
||||
${ZAF_AGENT_RESTART}
|
||||
}
|
||||
|
||||
zaf_check_agent_config() {
|
||||
zaf_restart_agent
|
||||
zabbix_agentd -t zaf.version
|
||||
}
|
||||
|
||||
# Update repo
|
||||
zaf_update_repo() {
|
||||
cd ${ZAF_REPO_DIR} && git pull
|
||||
[ -n "${ZAF_PLUGINS_REPO}" ] && cd ${ZAF_REPO_DIR} && git pull
|
||||
}
|
||||
|
||||
# List installed plugins
|
||||
zaf_list_installed_plugins() {
|
||||
cd ${ZAF_PLUGINS_DIR}; ls -d
|
||||
cd ${ZAF_PLUGINS_DIR}; ls --hide '.'
|
||||
}
|
||||
|
||||
# Check plugin url
|
||||
# $1 plugin uri
|
||||
# $2 local file to fetch
|
||||
zaf_plugin_fetch_control() {
|
||||
[ -z "$1" ] && return -1
|
||||
local name=$(basename "$1")
|
||||
zaf_fetch_url "$1/control" >"$2"
|
||||
}
|
||||
|
||||
# Get option from control file
|
||||
# $1 control file
|
||||
# $2 option
|
||||
zaf_ctrl_get_option() {
|
||||
grep -E '^(.*): ' "$1" | grep -F "$2:" | cut -d ' ' -f 2-
|
||||
}
|
||||
|
||||
zaf_ctrl_binary_deps() {
|
||||
local deps
|
||||
deps=$(zaf_ctrl_get_option "$1" Binary-Depends)
|
||||
for cmd in $deps; do
|
||||
if ! which $cmd >/dev/null; then
|
||||
echo "Missing binary dependency $cmd. Please install it first."
|
||||
exit 5
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
zaf_ctrl_install_bin() {
|
||||
local binaries
|
||||
local pdir
|
||||
binaries=$(zaf_ctrl_get_option "$1" Install-bin)
|
||||
pdir="${ZAF_PLUGINS_DIR}/${2}/"
|
||||
for b in $binaries; do
|
||||
zaf_fetch_url "$url/$b" >"$pdir/$b"
|
||||
chmod +x "$pdir/$b"
|
||||
done
|
||||
}
|
||||
|
||||
# Generates zabbix cfg from control file
|
||||
# $1 control
|
||||
# $2 pluginname
|
||||
zaf_ctrl_generate_cfg() {
|
||||
local items
|
||||
local cmd
|
||||
items=$(zaf_ctrl_get_option "$1" Item)
|
||||
for i in $items; do
|
||||
cmd=$(zaf_ctrl_get_option "$1" "Item-cmd-$i")
|
||||
echo "UserParameter=$2.${i},$cmd"
|
||||
done
|
||||
}
|
||||
|
||||
# Install plugin.
|
||||
# Parameter is url, directory or plugin name (will be searched in default plugin dir)
|
||||
zaf_install_plugin() {
|
||||
a
|
||||
local url
|
||||
local control
|
||||
local plugin
|
||||
local plugindir
|
||||
|
||||
if echo "$1" | grep -qv '/'; then
|
||||
url="${ZAF_REPO_DIR}/$1"
|
||||
else
|
||||
url="$1"
|
||||
fi
|
||||
plugin=$(basename "$url")
|
||||
echo Installing plugin $plugin from $url...
|
||||
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
|
||||
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_restart_agent
|
||||
cp $control "$plugindir"/
|
||||
zaf_fetch_url $url/template.xml >"$plugindir"/template.xml
|
||||
else
|
||||
echo "Cannot fetch control file!"
|
||||
exit 4
|
||||
fi
|
||||
}
|
||||
|
||||
zaf_plugin_info() {
|
||||
local items
|
||||
local plugindir
|
||||
|
||||
plugindir="${ZAF_PLUGINS_DIR}/$1"
|
||||
if [ -d "$plugindir" ]; then
|
||||
items=$(zaf_ctrl_get_option "$plugindir/control" Item)
|
||||
echo "Items supported:"
|
||||
echo "$items"
|
||||
else
|
||||
echo "Plugin $1 not installed"
|
||||
fi
|
||||
}
|
||||
|
||||
zaf_remove_plugin() {
|
||||
rm -rf ${ZAF_PLUGINS_DIR}/$1
|
||||
rm -f ${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -4,12 +4,14 @@ Maintainer: Lukas Macura <lukas@macura.cz>
|
||||
|
||||
Item: cpu[*]
|
||||
This item will return cpu usage of given process. In zabbix, it will be prefixed automaticaly by plugin name (processlist.cpu)
|
||||
Item-cmd-cpu[*]: TOTAL=0; for PROC in $(/bin/ps u -C $1 | /bin/grep -e '^$2' | /usr/bin/awk '{ print $$3 }'); do TOTAL=$(echo "$TOTAL $PROC" | /usr/bin/awk '{print $$1 + $$2}') ; done; echo $TOTAL
|
||||
Item-cmd-cpu[*]: {PLUGINDIR}/cpu-get.sh
|
||||
|
||||
Item: discovery
|
||||
Discovery of runing processes
|
||||
Item-cmd-discovery: echo "{\n \"data\":[" ; /bin/ps --no-headers caux | /usr/bin/awk '{ print " { \"{#PSUSER}\":\"" $1 "\", \"{#PSNAME}\":\"" $11 "\" },"}' | /usr/bin/sort | /usr/bin/uniq | /bin/sed -e 's/\//\\\//g' -e '$s/.$//' ; echo " ]\n}"
|
||||
Item-cmd-discovery: {PLUGINDIR}/cpu-get.sh
|
||||
|
||||
Binary-Depends: echo ps awk sort uniq sed
|
||||
|
||||
Install-bin: cpu-get.sh proc-discovery.sh
|
||||
|
||||
|
||||
|
6
plugins/process-info/cpu-get.sh
Executable file
6
plugins/process-info/cpu-get.sh
Executable file
@ -0,0 +1,6 @@
|
||||
#!/bin/sh
|
||||
|
||||
TOTAL=0
|
||||
for PROC in $(/bin/ps u -C $1 | /bin/grep -e '^$2' | /usr/bin/awk '{ print $$3 }'); do TOTAL=$(echo "$TOTAL $PROC" | /usr/bin/awk '{print $$1 + $$2}') ; done;
|
||||
echo $TOTAL
|
||||
|
4
plugins/process-info/proc-discovery.sh
Executable file
4
plugins/process-info/proc-discovery.sh
Executable file
@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
|
||||
echo "{\n \"data\":[" ; /bin/ps --no-headers caux | /usr/bin/awk '{ print " { \"{#PSUSER}\":\"" $1 "\", \"{#PSNAME}\":\"" $11 "\" },"}' | /usr/bin/sort | /usr/bin/uniq | /bin/sed -e 's/\//\\\//g' -e '$s/.$//' ; echo " ]\n}"
|
||||
|
20
zaf
20
zaf
@ -14,21 +14,37 @@ fi
|
||||
. ${ZAF_LIB_DIR}/zaf.lib.sh
|
||||
|
||||
case $1 in
|
||||
|
||||
check-agent-config)
|
||||
zaf_check_agent_config
|
||||
;;
|
||||
|
||||
update)
|
||||
zaf_update_repo
|
||||
;;
|
||||
|
||||
list-installed)
|
||||
list)
|
||||
zaf_list_installed_plugins
|
||||
;;
|
||||
|
||||
info)
|
||||
zaf_plugin_info "$2"
|
||||
;;
|
||||
|
||||
install)
|
||||
zaf_install_plugin "$2"
|
||||
;;
|
||||
|
||||
remove)
|
||||
zaf_remove_plugin "$2"
|
||||
;;
|
||||
|
||||
*)
|
||||
echo "$0 update"
|
||||
echo "$0 install plugin"
|
||||
echo "$0 list"
|
||||
echo "$0 install plugin"
|
||||
echo "$0 remove plugin"
|
||||
;;
|
||||
|
||||
esac
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user