-
Notifications
You must be signed in to change notification settings - Fork 1
/
README
238 lines (171 loc) · 6.42 KB
/
README
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
General zabbix tools on top of zabbix_api
Developed with aim to bring full power of zabbix_api to command line interface.
This currently based on gescheit version of api implemetation
https://github.com/gescheit/scripts/blob/master/zabbix/zabbix_api.py
>> 6893ab06cb33acbb3d15159e6f8db2b1cb72551e
zrc - example config file to placed under $HOME/.zrc or /etc/zrc
z - general wrapper
z.complete - provide completetion service, put under /etc/bash_completion.d
General syntax
z <func> <obj> <--filter|--search|--data> key=val[:key=val...] --show key[,key] [options]
FUNC
Func is the name of operation you want to preform, avaliable operations
you can see with
z [TAB][TAB]
OBJ
Obj is the name of object of FUNC, avaliable objects with current FUNC
z <func> [TAB][TAB]
General filtering
Depending on selected func you can use --filter or --search or --data
keyword to define array of parameters for selected FUNC
You can use [TAB] to get reasonable keys to define for selected FUNC
EXAMPLE: get details on host with name "zabbix"
z get host --filter host=zabbix
Options
There are many of them.
Most of avaliable options passed directly to zabbix_api. Some useful
options described here.
--search <array>
Search by any field of item. Take comma separated list of key=val
--filter <array>
Filter by any field of item. Take comma separated list of key=val
--show <list>
By default script will display output in json tree format. With this
define comma separated list of fields to output only selected fields.
--limit <num>
Select not more than <num> objects.
-d|--delimeter <char>
Char that will be used when parsing arrays like --search, filter, or
--data. Default is comma.
This need when comma is used in parameter passed to array
EXAMPLES
Example 1
Assume we want to disable "Cron not running" trigger on host "zabbix"
First we need triggerid of this trigger
bash# z get trigger --filter host=zabbix --search description=Cron \
--show description,status,value,triggerid
Return for me:
status = 0
description = Cron not running
value = 1
triggerid = 7411
status=0 mean trigger enabled and value=1 mean trigger is active
now set status=1 to disable trigger
bash# z update trigger --data triggerid=7411,status=1
Return for me:
{
"triggerids": [
"7411"
]
}
Done.
Example 2
Assume now we have bunch of item in the host that has wrong key
bash# z get item --filter host=hamster --search key_=privmm \
--show itemid,key_
Return for me:
itemid = 18072
key_ = vz.monitor[108,privmmpages,limit]
itemid = 18076
key_ = vz.monitor[109,privmmpages,limit]
... and there are many of them
There "privmmpages" must be replaced by "privvmpages"
and it is a bit tricky case we need kep right VEID (108,109 etc.) so i can not
use massupdate.
First lets get raw itemids to loop them later:
bash# BADITEM=`z get item --filter host=hamster --search key_=privmm \
--show itemid,key_ | grep itemid | awk '{ print $3 }' | xargs`
bash# echo $BADITEM
Return for me:
18072 18076 18080 18084 18095 18111 18127 18143 18159 18175 18191 18207 18223 18239 18255 18271
Than i define func to get VEID for given itemid
bash# get_key () { ./z get item --filter host=hamster,itemid=$1 --show key_ | \
cut -d "[" -f 2 | cut -d "," -f 1 | tail -1; }
bash# get_key 18072
Return for me:
108
Look it working :) cool!
Now func to change wrong key_ to good key_
bash# set_key () { KEY=`get_key $1`; z update item -d ":" \
--data itemid=$1:key_=vz.monitor[$KEY,privvmpages,limit]; }
bash# set_key 18072
There i set up alternate delimeter with -d option, this need to avoid
conflict when parsing key_ in --data
At this point we must have item updated, let's check
bash# BADITEM=`z get item --filter host=hamster --search key_=privmm \
--show itemid,key_ | grep itemid | awk '{ print $3 }' | xargs`
bash# echo $BADITEM
Return for me:
18076 18080 18084 18095 18111 18127 18143 18159 18175 18191 18207 18223 18239 18255 18271
bash# z get item --filter itemid=18076 --show key_
Return for me:
key_ = vz.monitor[109,privvmpages,limit]
Item 18072 fixed and it`s not more in my badlist and it`s key is OK now;
Now just loop other in $BADLIST
bash# for ITEM in $BADLIMITS; do set_key $ITEM; done
Return for me:
{
"itemids": [
"18076"
]
}
{
"itemids": [
"18080"
]
}
{
"itemids": [
"18084"
]
}
And so on...
bash# BADITEM=`z get item --filter host=hamster --search key_=privmm \
--show itemid,key_ | grep itemid | awk '{ print $3 }' | xargs`
bash# echo $BADITEM
Return for me:
Nohting
Job done.
Example 3
Create bunch of item.
Have an items
bash# z get item --search key_=failcnt --show itemid,key_,hostid --limit 1
Return for me:
itemid = 17228
hostid = 219
key_ = vz.monitor[149,oomguarpages,failcnt]
All items on one template
bash# z get template --filter hostid=219 --show host
Return for me:
host = hamster_ve
First of all make a list of items for final loop
bash# ITEMS=`z get item --search key_=failcnt --show itemid | grep itemid \
| awk '{ print $3 }' | xargs`
Then define two functions, parsing item key to get tokens for description
bash# get_type () { z get item --filter itemid=$1 --show key_ |\
tail -1 | cut -d"," -f2 ; }
bash# get_veid () { z get item --filter itemid=$1 --show key_ |\
tail -1 | cut -d"[" -f2 | cut -d"," -f1; }
bash# get_key () { z get item --filter itemid=$1 key_=failcnt \
--show itemid,description,key_ | grep key_ | awk '{ print $3 }'; }
bash# echo "KEY=`get_key 17228`; VEID=`get_veid 17228`; TYPE=`get_type 17228`"
Return for me:
KEY=vz.monitor[149,oomguarpages,failcnt]; VEID=149; TYPE=oomguarpages
And one function that actually create trigger
bash# add_trigger () { KEY=`get_key $1`; VEID=`get_veid $1`; TYPE=`get_type $1`; \
z create trigger -d "#" --data description=${VEID}_${TYPE}_fail#\
expression="{hamster_ve:${KEY}.abschange(0)} > 0"#\
status=0#\
priority=4; }
Two moments here. First, # is used as delimeter to avoid conflict with
expression. Second, spaces in expression are important. In web you can
define expression like this
"{hamster_ve:${KEY}.abschange(0)}>0"
and it will work, but via API there are ^^^ must be spaces
So now just loop all items
bash# for ITEM in $ITEMS; do add_trigger $ITEM; done
Done.
TODO
- Not supported usermacro for now in case of outdated api.
- Not supported discovery
- Not supported mass-update, use bash cickles