-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathzabbix-daemons-completion
130 lines (116 loc) · 4.61 KB
/
zabbix-daemons-completion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#
# Bash completion, common file for all daemons
#
_zabbix-daemons-runtime()
{
cur=$1
type=$2
process=$3
local runtime_loglevel_options="log_level_increase log_level_decrease"
local runtime_options="$runtime_simple_options $runtime_loglevel_options"
if [[ $cur =~ (${runtime_loglevel_options// /|})= ]]; then
# when main process starts supporting runtime loglevel changing, it should be added here, too
runtime_processes_common=( "configuration\ syncer" discoverer "history\ syncer" housekeeper
"http\ poller" "icmp\ pinger" "ipmi\ manager" "ipmi\ poller" "java\ poller" poller "preprocessing\ manager"
"preprocessing\ worker" self-monitoring "snmp\ trapper" "task\ manager" trapper "unreachable\ poller"
"vmware\ collector" )
runtime_processes_server=( "alert\ manager" "alert\ syncer" alerter "db\ watchdog" escalator "lld\ manager"
"lld\ worker" "proxy\ poller" timer )
runtime_processes_proxy=( "data\ sender" "heartbeat\ sender" )
runtime_processes_single="alert\ manager|alert\ syncer|configuration\ syncer|housekeeper|ipmi\ manager|preprocessing\ manager|self-monitoring|snmp\ trapper|db\ watchdog|data\ sender|heartbeat\ sender|collector"
case "$type" in
server)
runtime_processes=( "${runtime_processes_server[@]}" "${runtime_processes_common[@]}" )
;;
proxy)
runtime_processes=( "${runtime_processes_proxy[@]}" "${runtime_processes_common[@]}" )
;;
agentd)
runtime_processes=( collector listener "active\ checks" )
esac
if [[ ${cur} =~ = ]]; then
if [[ ${cur} =~ [a-z]$ ]]; then
# no need to parse pids anymore, process name should be completed instead
# should be extended to offer actual process count numbers
# there does not seem to be a way to pass an array
# (or any other way to pass parameters with spaces) to compgen
runtime_proc_count=${#runtime_processes[@]}
current_process=${cur#*=}
for ((i=0; $i<=$runtime_proc_count; i++)); do
[[ ${runtime_processes[i]} = "${current_process}"* ]] || {
unset runtime_processes[i]
}
done
COMPREPLY=( "${runtime_processes[@]}" )
if [[ ${#COMPREPLY[@]} = 1 ]]; then
# only one process left in the return array
[[ ${COMPREPLY[0]} =~ $runtime_processes_single ]] || {
# this will break if there's ever a process name that is a continuation of a "single" process
COMPREPLY+=( "${COMPREPLY[0]}", )
}
fi
return 0
fi
# if -c is passed, we grab the child pids for the pid from the pidfile. Include directive is not supported
# if -c is not yet passed, we should find out the default config file path for the used binary, but that is not possible yet
[[ -f "$config_path" ]] && {
pid_file=$(awk -F= '/^ *PidFile/ {pidfile=$2} END{print pidfile}' "$config_path")
[[ -f $pid_file ]] && {
parent_pid=$(cat "$pid_file")
}
}
[[ $parent_pid ]] && {
# currently log level changing is not supported for the main process
# $pids should include $parent_pid once that is fixed
ps_args=( --ppid $parent_pid )
} || {
# if the parent pid was not found, fall back to all processes by name
# for example, config file might be unreadable for the current user when using sudo
ps_args=( -C ${process##*/} )
}
if [[ ${cur} =~ , ]]; then
process_part=${cur#*=}
process_name=${process_part%,*}
process_name_unescaped=${process_name/\\/}
store_IFS=$IFS
IFS=$'\n'
process_count=$(ps ${ps_args[@]} -o args= | awk -F"[:#[]" '{print $2}' | grep -c "^ $process_name_unescaped $")
[[ $process_count -gt 0 ]] && {
# completion proposals will have process name prepended before the number. the only way around
# this seems to be set COMP_WORDBREAKS globally, which is not a good idea (it would affect
# other completions). setting COMP_WORDBREAKS at any level in a single completion does not seem
# to work.
COMPREPLY=( $(compgen -P "${process_name}," -W "{1..$process_count}" -- ${cur#*,}) )
}
IFS=$store_IFS
return 0
fi
pids=$(ps ${ps_args[@]} -o pid=)
# passing empty string to compgen makes it exit with code 1 and array assignment fails
pidarray=( $(compgen -W "${pids}" -- ${cur#*=}) )
[[ ${cur} =~ =$ ]] && {
COMPREPLY=( "${pidarray[@]}" "${runtime_processes[@]}" )
} || {
COMPREPLY=( "${pidarray[@]}" )
}
return 0
fi
fi
if [[ $cur =~ "diaginfo=" ]]; then
COMPREPLY=( $(compgen -W "${diaginfo_params}" -- ${cur#*=}) )
return 0
fi
COMPREPLY=( $(compgen -W "${runtime_options}" -- ${cur}) )
if [[ $runtime_loglevel_options =~ ${COMPREPLY[@]} ]]; then
compopt -o nospace
if [[ ${#COMPREPLY[@]} = 1 ]]; then
# only one of increase/decrease left in the return array
# quite hackish
COMPREPLY+=( "${COMPREPLY[0]}"= )
fi
fi
if [[ "diaginfo" == ${COMPREPLY[0]} ]]; then
compopt -o nospace
COMPREPLY+=( "${COMPREPLY[0]}"= )
fi
}