-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathzaf
executable file
·469 lines (454 loc) · 12.6 KB
/
zaf
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#!/bin/sh
if [ -z "$secondstage" ]; then
ZAF_CFG_FILE="/etc/zaf.conf"
if [ -f $ZAF_CFG_FILE ]; then
. $ZAF_CFG_FILE
else
echo "Missing config file $ZAF_CFG_FILE! Exiting."
exit 2
fi
# Read options as config for ZAF
for pair in "$@"; do
if echo $pair | grep -qE '^ZAF\_(.*)='; then
option=$(echo $pair|cut -d '=' -f 1)
value=$(echo $pair|cut -d '=' -f 2-)
eval "${option}='$value'"
export secondstage=1
else
params="$params $pair"
fi
done
export $(set |grep ^ZAF_ | cut -d '=' -f 1)
# If some variables in cmd were stripped, rerun only with right arguments and exported variables
[ -n "$secondstage" ] && exec $0 $params
fi
if [ -n "$ZAF_PROXY" ]; then
export ALL_PROXY="$ZAF_PROXY"
export http_proxy="$ZAF_PROXY"
export https_proxy="$ZAF_PROXY"
fi
if [ -f ./lib/zaf.lib.sh ]; then
. ./lib/zaf.lib.sh
. ./lib/plugin.lib.sh
. ./lib/os.lib.sh
. ./lib/ctrl.lib.sh
. ./lib/cache.lib.sh
. ./lib/zbxapi.lib.sh
[ -f ./lib/zaf.${ZAF_OS}.sh ] && . ./lib/zaf.${ZAF_OS}.sh
else
. ${ZAF_LIB_DIR}/zaf.lib.sh
. ${ZAF_LIB_DIR}/plugin.lib.sh
. ${ZAF_LIB_DIR}/os.lib.sh
. ${ZAF_LIB_DIR}/ctrl.lib.sh
. ${ZAF_LIB_DIR}/cache.lib.sh
. ${ZAF_LIB_DIR}/zbxapi.lib.sh
[ -f ${ZAF_LIB_DIR}/zaf.${ZAF_OS}.sh ] && . ${ZAF_LIB_DIR}/zaf.${ZAF_OS}.sh
fi
zaf_debug_init stderr
zaf_tmp_init
zaf_cache_init
case $1 in
cache-clean)
zaf_cache_clean
;;
cache-list)
zaf_cache_list
;;
cache-test1)
for i in test1 test2 test3; do
echo "To cache:"
echo "$i" | zaf_tocache_stdin "test" 10
done
;;
cache-test2)
for i in test1 test2 test3; do
echo "From cache:"
zaf_fromcache "test"
done
;;
version)
echo "$ZAF_VERSION"
[ $ZAF_DEBUG -gt 1 ] && set |grep -E ^ZAF_
;;
###### Agent related commands
userparms)
for plugin in $(zaf_list_plugins); do
plugindir=$ZAF_PLUGINS_DIR/$plugin
control=$plugindir/control.zaf
zaf_ctrl_generate_items_cfg "${control}" "${plugin}" "noscripts" \
| zaf_far '{PLUGINDIR}' "${plugindir}"
done
;;
agent-config)
[ -n "$2" ] && force=yes
for p in $(zaf_list_plugins); do
if [ -f "$ZAF_PLUGINS_DIR/$p/postinst.need" ]; then
zaf_wrn "Calling postinstalation of plugin $p"
zaf_postinstall_plugin $p && rm -f "$ZAF_PLUGINS_DIR/$p/postinst.need"
force=yes
fi
done
zaf_wrn "Generating config files in $ZAF_AGENT_CONFIGD ..."
for plugin in $(zaf_list_plugins); do
plugindir=$ZAF_PLUGINS_DIR/$plugin
control=$plugindir/control.zaf
if ! [ -f "${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf" ] || [ "$control" -nt "${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf" ] || [ -n "$force" ]; then
zaf_dbg "Generating ${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf"
zaf_ctrl_generate_items_cfg "${control}" "${plugin}" \
| zaf_far '{PLUGINDIR}' "${plugindir}" >${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf
fi
done
;;
server-config)
zaf_wrn "Not implemented yet"
;;
###### Plugins related commands
update)
zaf_wrn "Updating repository ${ZAF_REPO_GITURL}..."
zaf_update_repo
;;
upgrade)
ZAF_DEBUG=$ZAF_DEBUG $0 reinstall $(zaf_list_plugins)
;;
show)
shift;
if [ -z "$1" ]; then
zaf_list_plugins | while read plugin; do
zaf_plugin_info $ZAF_PLUGINS_DIR/$plugin/control.zaf
done
else
if echo $1 | grep -vq "\."; then
if zaf_list_plugins | grep -q "^$1"; then
zaf_is_plugin $1 && zaf_plugin_info $ZAF_PLUGINS_DIR/$1/control.zaf || zaf_err "Plugin $1 not installed."
else
if echo $1 |grep -q ^http; then
zaf_prepare_plugin "$1" "$ZAF_TMP_DIR/plugin"
zaf_plugin_info "$ZAF_TMP_DIR/plugin/control.zaf"
else
zaf_err "Plugin $1 not installed."
fi
fi
else
zaf_item_info "$1"
fi
fi
;;
plugins)
zaf_list_plugins
;;
items)
shift
if [ -z "$1" ]; then
zaf_list_items
else
zaf_list_plugin_items "$1"
fi
;;
test)
[ "$USER" != "zabbix" ] && zaf_wrn "You are not zabbix user. Test will be run with your privileges and sudo access!"
shift
if echo $1|grep -q '\.'; then
zaf_test_item "$1"
exit
fi
if [ -n "$1" ]; then
plugins="$@"
else
plugins="$(zaf_list_plugins)"
fi
for p in $plugins; do
! zaf_is_plugin $p && zaf_err "Unknown plugin $p"
for i in $(zaf_list_plugin_items $p test); do
zaf_test_item $i
echo
done
done
;;
get)
shift
if echo $1|grep -q '\.'; then
zaf_get_item "$1"
exit
fi
if [ -n "$1" ]; then
plugins="$@"
else
plugins="$(zaf_list_plugins)"
fi
for p in $plugins; do
for i in $(zaf_list_plugin_items $p test); do
echo $i: $(zaf_get_item $i)
echo
done
done
;;
precache)
shift
for i in $*; do
if zaf_is_plugin $i; then
for j in $(zaf_list_plugin_items $i precache); do
item=$(echo $j | cut -d '[' -f 1)
params=$(echo $j | cut -d '[' -f 2 | cut -d ']' -f 1 | tr ',' ' ')
zaf_precache_item $params >/dev/null
done
else
item=$(echo $i | cut -d '[' -f 1)
params=$(echo $i | cut -d '[' -f 2 | cut -d ']' -f 1 | tr ',' ' ')
zaf_precache_item $params >/dev/null
fi
done
;;
install)
shift
[ -z "$1" ] && echo "$0 install plugin [plugin]..."
for p in $@; do
if zaf_is_plugin "$(basename $p)"; then
zaf_wrn "Plugin $(basename $p) already installed. Skipping installation."
continue
fi
zaf_install_plugin "$p"
installed=1
done
[ -n "$installed" ] && zaf_is_root && zaf_restart_agent
;;
reinstall)
shift
[ -z "$1" ] && echo "$0 reinstall plugin [plugin]..."
for p in $@; do
if zaf_is_plugin "$p"; then
zaf_remove_plugin "$(basename $p)"
reinstalled=1
fi
zaf_install_plugin "$p"
reinstalled=1
done
[ -n "$reinstalled" ] && zaf_is_root && zaf_restart_agent
;;
remove)
shift
[ -z "$1" ] && 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
;;
###### Zaf related commands
self-upgrade)
shift
[ -z "$1" ] && auto=auto
zaf_os_specific zaf_check_deps zaf && zaf_err "Zaf is installed as system package. Cannot self-upgrade."
if ! zaf_which curl >/dev/null;
then
zaf_err "Curl not found. Cannot continue. Please install it."
fi
zaf_fetch_url $ZAF_RAW_URL/$ZAF_GITBRANCH/install.sh | exec sh -s $auto "$@"
exit
;;
self-remove)
shift
zaf_os_specific zaf_check_deps zaf && zaf_err "Zaf is installed as system package. Cannot self-remove."
. /etc/zaf.conf
if [ "$1" = "force" ]; then
rm -rf /etc/zaf.conf ${ZAF_PLUGINS_DIR} ${ZAF_REPO_DIR} ${ZAF_LIB_DIR} \
${ZAF_BIN_DIR}/zaf ${ZAF_AGENT_CONFIGD}/zaf_*
else
echo "This will remove zaf from this computer and erase all configuration."
echo "This command will be executed:"
echo "rm -rf /etc/zaf.conf ${ZAF_PLUGINS_DIR} ${ZAF_REPO_DIR} ${ZAF_LIB_DIR} \
${ZAF_BIN_DIR}/zaf ${ZAF_AGENT_CONFIGD}/zaf_*"
echo
echo "To continue, please do $0 self-remove force"
fi
;;
###### API related commands
api)
zaf_zbxapi_login
case $2 in
get-*-id)
obj=$(echo $2|cut -d '-' -f 2)
[ -z "$3" ] && zaf_err "$0 $1 $2 <$obj>"
eval zaf_zbxapi_get${obj}id "$3"
;;
get-byid-*|get-id-*)
obj=$(echo $2|cut -d '-' -f 3)
[ -z "$3" ] && zaf_err "$0 $1 $2 <id>"
eval zaf_zbxapi_get${obj} "$3" "$4"
;;
get-host-ids)
if [ -n "$3" ]; then
gid=$(zaf_zbxapi_gethostgroupid "$3") || exit 1
zaf_dbg "Selecting all hosts in group $3($gid)"
zaf_zbxapi_gethostsingroup $gid
else
zaf_dbg "Selecting all hosts in system"
zaf_zbxapi_gethostids
fi
;;
get-inventory)
[ -z "$3" ] && zaf_err "$0 $1 field [host]. If host unspecified, use hostname"
if [ -z "$4" ]; then
host=$(hostname)
else
host="$4"
fi
zaf_zbxapi_gethostinventory $host $3
;;
get-template-ids)
if [ -n "$3" ]; then
gid=$(zaf_zbxapi_gethostgroupid "$3") || exit 1
zaf_dbg "Selecting all templates in group $3($gid)"
zaf_zbxapi_gettemplatesingroup $gid
else
zaf_dbg "Selecting all templates in system"
zaf_zbxapi_gettemplateids
fi
;;
get-map-ids)
zaf_zbxapi_getmapid
;;
export-hosts)
shift; shift
[ -z "$1" ] && zaf_err "$0 api export-hosts dir [hostgroup]"
dir="$1"
shift
if [ -n "$1" ]; then
gid=$(zaf_zbxapi_gethostgroupid "$1") || exit 1
shift
hosts=$(zaf_zbxapi_gethostsingroup $gid)
else
hosts=$(zaf_zbxapi_gethostids)
fi
zaf_wrn "Will backup this hosts: $hosts"
zaf_wrn "Output dir: $dir"
for h in $hosts; do
if zaf_bglimit 5; then
(
hn=$(zaf_zbxapi_gethost $h name)
zaf_wrn "Exporting host $hn($h)..."
zaf_zbxapi_export_host $h >"$dir/$hn.xml"
) &
else
hn=$(zaf_zbxapi_gethost $h name)
zaf_wrn "Exporting host $hn($h)..."
zaf_zbxapi_export_host $h >"$dir/$hn.xml"
fi
done
wait
;;
export-host)
shift; shift
hostid=$(zaf_zbxapi_gethostid "$1") || exit 1
zaf_wrn "Exporting host $3($hostid)..."
zaf_zbxapi_export_host $hostid
;;
export-template)
shift; shift
templateid=$(zaf_zbxapi_gettemplateid "$1") || exit 1
zaf_wrn "Exporting template $3($hostid)..."
zaf_zbxapi_export_template $templateid
;;
export-templates)
shift; shift
[ -z "$1" ] && zaf_err "$0 api export-templates dir"
dir="$1"
templates=$(zaf_zbxapi_gettemplateids)
zaf_wrn "Will backup this templates: $templates"
zaf_wrn "Output dir: $dir"
for t in $templates; do
if zaf_bglimit 5; then
(
tn=$(zaf_zbxapi_gettemplate $t name)
zaf_wrn "Exporting template $tn($t)..."
zaf_zbxapi_export_template $t >"$dir/$tn.xml"
) &
else
tn=$(zaf_zbxapi_gettemplate $t name)
zaf_wrn "Exporting template $tn($t)..."
zaf_zbxapi_export_template $t >"$dir/$tn.xml"
fi
done
wait
;;
import-template)
shift; shift
if zaf_is_plugin $1; then
if [ -f "$ZAF_PLUGINS_DIR/$1/template.xml" ]; then
template="$ZAF_PLUGINS_DIR/$1/template.xml"
zaf_wrn "Importing template $template"
zaf_zbxapi_import_config $template || zaf_err "Error importing template"
else
url="$(zaf_plugin_option $1 Template)"
if [ -n "$url" ]; then
zaf_fetch_url $url | zaf_zbxapi_import_config || zaf_err "Error importing template"
else
url="$(zaf_plugin_option $1 Url)"
zaf_fetch_url $url/template.xml | zaf_zbxapi_import_config || zaf_err "Error importing template"
fi
fi
else
if [ -f $1 ]; then
zaf_wrn "Importing template $1"
zaf_zbxapi_import_config $1 || zaf_err "Error importing template"
else
zaf_err "Unknown plugin $1!"
fi
fi
;;
*)
echo "$0 api command [parameters]"
for i in host template map; do
zaf_hlp "get-${i}-id $i" "Get $i id"
zaf_hlp "get-byid-${i} id [property]" "Get $i property from id. Leave empty property for JSON"
done
zaf_hlp "get-inventory host [fields]" "Get inventory fields [or all fields]"
zaf_hlp "get-host-ids [hg]" "Get all hostids or hostids in group hg"
zaf_hlp "get-template-ids [hg]" "Get all templateids or templateids in group hg"
zaf_hlp "get-map-ids" "Get all mapids"
zaf_hlp "export-hosts dir [hg]" "Backup all hosts [in group hg] (get their config from zabbix and save to dir/hostname.xml)"
zaf_hlp "export-host host" "Backup host (get config from zabbix to stdout)"
zaf_hlp "import-template {plugin|file}" "Import template for plugin or from file"
zaf_hlp "export-template name" "Export template to stdout"
zaf_hlp "export-templates dir" "Export all template to dir"
echo
[ -n "$2" ] && zaf_err "Bad API command '$2'!"
;;
esac
;;
*)
echo "$0 Version ${ZAF_VERSION}. Please use some of this commands:"
echo "$0 Cmd [ZAF_OPTION=value] [ZAF_CTRL_Option=value] [ZAF_CTRLI_Item_Option=value] ..."
echo "Plugin manipulation commands:"
zaf_hlp "$0 update" "To update repo (not plugins, similar to apt-get update)"
zaf_hlp "$0 upgrade" "To upgrade installed plugins from repo"
zaf_hlp "$0 install plugin" "To install plugin"
zaf_hlp "$0 remove plugin" "To remove plugin"
echo
echo "Plugin info commands:"
zaf_hlp "$0 plugins" "To list installed plugins"
zaf_hlp "$0 show [plugin]" "To show installed plugins or plugin info"
zaf_hlp "$0 items [plugin]" "To list all suported items [for plugin]"
echo
echo "Plugin diagnostic commands:"
zaf_hlp "$0 test [plugin[.item]]" "To test [all] suported items by zabbix_agentd [for plugin]"
zaf_hlp "$0 get [plugin[.item]]" "To test [all] suported items by zabbix_get [for plugin]"
zaf_hlp "$0 precache [plugin[.item]]" "To precache [all] suported items"
echo
echo "Zabbix API commands:"
zaf_hlp "$0 api" "To zabbix API functions. See $0 api for more info."
echo
echo "Agent config info commands:"
zaf_hlp "$0 userparms" "See userparms generated from zaf on stdout"
zaf_hlp "$0 agent-config [force]" "Reconfigure zabbix userparms in $ZAF_AGENT_CONFIGD"
echo
echo "Zaf related commands:"
zaf_hlp "$0 self-upgrade" "To self-upgrade zaf"
zaf_hlp "$0 self-remove" "To self-remove zaf and its config"
zaf_hlp "$0 cache-clean" "To remove all entries from cache"
zaf_hlp "$0 cache-list" "To show all entries in cache"
echo
[ -n "$1" ] && zaf_err "Bad command '$1'!"
;;
esac