iteh-zabbixapi 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,101 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'yaml'
5
+ require 'getopt/std'
6
+ require 'zabbixapi'
7
+
8
+ opt = Getopt::Std.getopts("g:E:")
9
+
10
+ group_name = opt["g"]
11
+ zabbix_env = opt["E"]
12
+
13
+ template_name = "TMPL_Load_average"
14
+
15
+ # read config
16
+ config = YAML::load(open('./config.yml'))
17
+
18
+ api_url = config[zabbix_env]["api_url"]
19
+ api_login = config[zabbix_env]["api_login"]
20
+ api_password = config[zabbix_env]["api_password"]
21
+
22
+ # Esablish new connection
23
+ zbx = Zabbix::ZabbixApi.new(api_url, api_login, api_password)
24
+
25
+ # Create new template
26
+ p " * Creating template #{template_name}."
27
+ g_id = zbx.get_group_id(group_name)
28
+
29
+ options = {
30
+ 'groups' => [ g_id.to_i ],
31
+ 'host' => template_name
32
+ }
33
+
34
+ t_id = zbx.add_template(options)
35
+
36
+ # Create application #{app_name}
37
+ app_name = "Load_average"
38
+ p " ** Create application #{app_name}."
39
+ application = {
40
+ 'hostid' => t_id.to_i,
41
+ 'name' => app_name
42
+ }
43
+ a_id = zbx.add_application(application)
44
+
45
+ # 'Load Average 1'
46
+ options = {
47
+ 'description' => "Load Average 1",
48
+ 'key_' => "system.cpu.load[,avg1]",
49
+ 'hostid' => t_id.to_i,
50
+ 'applications' => [ a_id.to_i ],
51
+ 'history' => 7,
52
+ 'trends' => 30,
53
+ 'delay' => 60,
54
+ 'value_type' => 0
55
+ }
56
+ p " ** Add 'Load Average 1' to #{template_name}."
57
+
58
+ i_id = zbx.add_item(options)
59
+
60
+ # 'Load Average 5'
61
+ options = {
62
+ 'description' => "Load Average 5",
63
+ 'key_' => "system.cpu.load[,avg5]",
64
+ 'hostid' => t_id.to_i,
65
+ 'applications' => [ a_id.to_i ],
66
+ 'history' => 7,
67
+ 'trends' => 30,
68
+ 'delay' => 300,
69
+ 'value_type' => 0
70
+ }
71
+ p " ** Add 'Load Average 5' to #{template_name}."
72
+
73
+ i_id = zbx.add_item(options)
74
+
75
+ # TRIGGERS
76
+
77
+ options = {
78
+ 'description' => "Load Average",
79
+ 'expression' => "{#{template_name}:system.cpu.load[,avg5].last(0)}>5",
80
+ 'priority' => 2, # warning
81
+ 'templateid' => 0,
82
+ 'comments' => "Load Average (warning)",
83
+ 'type' => 0,
84
+ 'status' => '0'
85
+ }
86
+
87
+ p " ** Add 'Load Average warning trigger'"
88
+ tr_id = zbx.add_trigger(options)
89
+
90
+ options = {
91
+ 'description' => "Load Average",
92
+ 'expression' => "{#{template_name}:system.cpu.load[,avg5].last(0)}>10",
93
+ 'priority' => 5, # disaster
94
+ 'templateid' => 0,
95
+ 'comments' => "Load Average (disaster)",
96
+ 'type' => 0,
97
+ 'status' => '0'
98
+ }
99
+
100
+ p " ** Add 'Load Average disaster trigger'"
101
+ tr_id = zbx.add_trigger(options)
@@ -0,0 +1,276 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'yaml'
5
+ require 'getopt/std'
6
+ require 'zabbixapi'
7
+
8
+ opt = Getopt::Std.getopts("g:E:")
9
+
10
+ group_name = opt["g"]
11
+ zabbix_env = opt["E"]
12
+
13
+ template_name = "TMPL_Memory"
14
+
15
+ # read config
16
+ config = YAML::load(open('./config.yml'))
17
+
18
+ api_url = config[zabbix_env]["api_url"]
19
+ api_login = config[zabbix_env]["api_login"]
20
+ api_password = config[zabbix_env]["api_password"]
21
+
22
+ # Esablish new connection
23
+ zbx = Zabbix::ZabbixApi.new(api_url, api_login, api_password)
24
+
25
+ # Create new template
26
+ p " * Creating template #{template_name}."
27
+ g_id = zbx.get_group_id(group_name)
28
+
29
+ options = {
30
+ 'groups' => [ g_id.to_i ],
31
+ 'host' => template_name
32
+ }
33
+
34
+ t_id = zbx.add_template(options)
35
+
36
+ # Create application #{app_name}
37
+ app_name = "Memory"
38
+ p " ** Create application #{app_name}."
39
+ application = {
40
+ 'hostid' => t_id.to_i,
41
+ 'name' => app_name
42
+ }
43
+ a_id = zbx.add_application(application)
44
+
45
+ # 'Buffers memory'
46
+ options = {
47
+ 'description' => "Buffers memory",
48
+ 'key_' => "vm.memory.size[buffers]",
49
+ 'hostid' => t_id.to_i,
50
+ 'applications' => [ a_id.to_i ],
51
+ 'units' => 'B',
52
+ 'history' => 7,
53
+ 'trends' => 30,
54
+ 'value_type' => 0
55
+ }
56
+ p " ** Add 'Buffers memory' to #{template_name}."
57
+
58
+ buffers_in_b_item_id = zbx.add_item(options)
59
+
60
+ # 'Cached memory'
61
+ options = {
62
+ 'description' => "Cached memory",
63
+ 'key_' => "vm.memory.size[cached]",
64
+ 'hostid' => t_id.to_i,
65
+ 'applications' => [ a_id.to_i ],
66
+ 'units' => 'B',
67
+ 'history' => 7,
68
+ 'trends' => 30,
69
+ 'value_type' => 0
70
+ }
71
+ p " ** Add 'Cached memory' to #{template_name}."
72
+
73
+ cached_in_b_item_id = zbx.add_item(options)
74
+
75
+ # 'Total memory'
76
+ options = {
77
+ 'description' => "Total memory",
78
+ 'key_' => "vm.memory.size[total]",
79
+ 'hostid' => t_id.to_i,
80
+ 'applications' => [ a_id.to_i ],
81
+ 'delay' => '300',
82
+ 'units' => 'B',
83
+ 'history' => 7,
84
+ 'trends' => 30,
85
+ 'value_type' => 0
86
+ }
87
+ p " ** Add 'Total memory' to #{template_name}."
88
+
89
+ total_in_b_item_id = zbx.add_item(options)
90
+
91
+ # 'Used memory with buffers and cache'
92
+ options = {
93
+ 'description' => "Used memory",
94
+ 'key_' => "vm.memory.size[used]",
95
+ 'hostid' => t_id.to_i,
96
+ 'applications' => [ a_id.to_i ],
97
+ 'delay' => '300',
98
+ 'units' => 'B',
99
+ 'history' => 7,
100
+ 'trends' => 30,
101
+ 'value_type' => 0
102
+ }
103
+ p " ** Add 'Used memory' to #{template_name}."
104
+
105
+ used_in_b_item_id = zbx.add_item(options)
106
+
107
+ # 'Free memory'
108
+ options = {
109
+ 'description' => "Free memory",
110
+ 'key_' => "vm.memory.size[free]",
111
+ 'hostid' => t_id.to_i,
112
+ 'applications' => [ a_id.to_i ],
113
+ 'units' => 'B',
114
+ 'history' => 7,
115
+ 'trends' => 30,
116
+ 'value_type' => 0
117
+ }
118
+ p " ** Add 'Free memory' to #{template_name}."
119
+
120
+ i_id = zbx.add_item(options)
121
+
122
+ # 'Free mmeory with buffers and cache'
123
+ options = {
124
+ 'description' => "Free memory with buffers and cache",
125
+ 'key_' => "vm.memory.size[free_with_buffer_cache]",
126
+ 'params' => "last(\"vm.memory.size[free]\") + last(\"vm.memory.size[cached]\") + last(\"vm.memory.size[buffers]\")",
127
+ 'hostid' => t_id.to_i,
128
+ 'type' => '15', #calculated
129
+ 'applications' => [ a_id.to_i ],
130
+ 'units' => 'B',
131
+ 'history' => 7,
132
+ 'trends' => 30,
133
+ 'value_type' => 0
134
+ }
135
+ p " ** Add 'Free memory with buffers and cache' to #{template_name}."
136
+
137
+ i_id = zbx.add_item(options)
138
+
139
+ # 'Swap in all'
140
+ options = {
141
+ 'description' => "Swap in all",
142
+ 'key_' => "system.swap.in[]",
143
+ 'hostid' => t_id.to_i,
144
+ 'applications' => [ a_id.to_i ],
145
+ 'multiplier' => 1,
146
+ 'formula' => 512,
147
+ 'units' => 'B',
148
+ 'history' => 7,
149
+ 'trends' => 30,
150
+ 'value_type' => 0
151
+ }
152
+ p " ** Add 'Swap in all' to #{template_name}."
153
+
154
+ i_id = zbx.add_item(options)
155
+
156
+ # 'Swap out all'
157
+ options = {
158
+ 'description' => "Swap out all",
159
+ 'key_' => "system.swap.out[]",
160
+ 'hostid' => t_id.to_i,
161
+ 'applications' => [ a_id.to_i ],
162
+ 'multiplier' => 1,
163
+ 'formula' => 512,
164
+ 'units' => 'B',
165
+ 'history' => 7,
166
+ 'trends' => 30,
167
+ 'value_type' => 0
168
+ }
169
+ p " ** Add 'Swap out all' to #{template_name}."
170
+
171
+ i_id = zbx.add_item(options)
172
+
173
+ # TRIGGERS
174
+ options = {
175
+ 'description' => "Free memory without cache, %",
176
+ 'expression' => "{#{template_name}:vm.memory.size[free].last(0)}<15",
177
+ 'priority' => 2, # warning
178
+ 'templateid' => 0,
179
+ 'comments' => "Free memory without cache, % warning trigger",
180
+ 'type' => 0,
181
+ 'status' => '0'
182
+ }
183
+
184
+ p " ** Add 'Free memory without cache, %'"
185
+ tr_id = zbx.add_trigger(options)
186
+
187
+ options = {
188
+ 'description' => "Free memory without cache, %",
189
+ 'expression' => "{#{template_name}:vm.memory.size[free].last(0)}<10",
190
+ 'priority' => 5, # disaster
191
+ 'templateid' => 0,
192
+ 'commenty' => "Free memory without cache, %",
193
+ 'type' => 0,
194
+ 'status' => '0'
195
+ }
196
+
197
+ p " ** Add 'Free memory without cache, % disaster trigger'"
198
+ tr_id = zbx.add_trigger(options)
199
+
200
+ options = {
201
+ 'description' => "Swap usage in, B",
202
+ 'expression' => "{#{template_name}:system.swap.in[].last(0)}#0",
203
+ 'priority' => 2, # warning
204
+ 'templateid' => 0,
205
+ 'comments' => "Swap usage in, B",
206
+ 'type' => 0,
207
+ 'status' => '0'
208
+ }
209
+
210
+ p " ** Add 'Swap usage in, B warning trigger'"
211
+ tr_id = zbx.add_trigger(options)
212
+
213
+ options = {
214
+ 'description' => "Swap usage out, B",
215
+ 'expression' => "{#{template_name}:system.swap.out[].last(0)}#0",
216
+ 'priority' => 2, # warning
217
+ 'templateid' => 0,
218
+ 'comments' => "Swap usage out, B",
219
+ 'type' => 0,
220
+ 'status' => '0'
221
+ }
222
+
223
+ p " ** Add 'Swap usage out, B warning trigger'"
224
+ tr_id = zbx.add_trigger(options)
225
+
226
+ options = {
227
+ 'gitems' => [
228
+ {
229
+ "itemid" => total_in_b_item_id,
230
+ "drawtype" => "0",
231
+ "sortorder" => "0",
232
+ "color" => "AA0000",
233
+ "yaxisside" => "0",
234
+ "calc_fnc" => "2",
235
+ "type" => "0",
236
+ "periods_cnt" => "5"
237
+ },
238
+ {
239
+ "itemid" => used_in_b_item_id,
240
+ "drawtype" => "0",
241
+ "sortorder" => "0",
242
+ "color" => "009900",
243
+ "yaxisside" => "0",
244
+ "calc_fnc" => "2",
245
+ "type" => "0",
246
+ "periods_cnt" => "5"
247
+ },
248
+ {
249
+ "itemid" => cached_in_b_item_id,
250
+ "drawtype" => "0",
251
+ "sortorder" => "0",
252
+ "color" => "0000AA",
253
+ "yaxisside" => "0",
254
+ "calc_fnc" => "2",
255
+ "type" => "0",
256
+ "periods_cnt" => "5"
257
+ },
258
+ {
259
+ "itemid" => buffers_in_b_item_id,
260
+ "drawtype" => "0",
261
+ "sortorder" => "0",
262
+ "color" => "AA00AA",
263
+ "yaxisside" => "0",
264
+ "calc_fnc" => "2",
265
+ "type" => "0",
266
+ "periods_cnt" => "5"
267
+ }
268
+ ],
269
+ "show_triggers" => "1",
270
+ "name" => "Memory usage",
271
+ "width" => "900",
272
+ "height" => "200",
273
+ "templateid" => "0"
274
+ }
275
+
276
+ g_id = zbx.add_graph(options)
@@ -0,0 +1,82 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'getopt/std'
5
+ require 'yaml'
6
+ require 'zabbixapi'
7
+
8
+ opt = Getopt::Std.getopts("g:i:E:")
9
+
10
+ group_name = opt["g"]
11
+ interface_name = opt["i"]
12
+ zabbix_env = opt["E"]
13
+
14
+ template_name = "TMPL_Net_#{interface_name}"
15
+ app_name = "Net_#{interface_name}"
16
+
17
+
18
+ # read config
19
+ config = YAML::load(open('./config.yml'))
20
+
21
+ api_url = config[zabbix_env]["api_url"]
22
+ api_login = config[zabbix_env]["api_login"]
23
+ api_password = config[zabbix_env]["api_password"]
24
+
25
+ # Esablish new connection
26
+ zbx = Zabbix::ZabbixApi.new(api_url, api_login, api_password)
27
+
28
+ # Create new template
29
+ p " * Creating template #{template_name}."
30
+ g_id = zbx.get_group_id(group_name)
31
+
32
+ options = {
33
+ 'groups' => [ g_id.to_i ],
34
+ 'host' => template_name
35
+ }
36
+
37
+ t_id = zbx.add_template(options)
38
+
39
+ # Create application #{app_name}
40
+ p " ** Create application #{app_name}."
41
+ application = {
42
+ 'hostid' => t_id.to_i,
43
+ 'name' => app_name
44
+ }
45
+ a_id = zbx.add_application(application)
46
+
47
+ # 'Net #{interface_name} incoming traffic'
48
+ options = {
49
+ 'description' => "Net #{interface_name} incoming, B",
50
+ 'key_' => "net.if.in[#{interface_name}, bytes]",
51
+ 'hostid' => t_id.to_i,
52
+ 'applications' => [ a_id.to_i ],
53
+ 'history' => 7,
54
+ 'trends' => 30,
55
+ 'delay' => 60,
56
+ 'value_type' => 0,
57
+ 'units' => 'Bps',
58
+ 'value_type' => '3', # Numeric (unsigned)
59
+ 'delta' => 1 # Store as delta (Speed per second)
60
+ }
61
+ p " ** Add 'Net #{interface_name} incoming, B' to #{template_name}."
62
+
63
+ i_id = zbx.add_item(options)
64
+
65
+ # 'Net #{interface_name} outgoing traffic'
66
+ options = {
67
+ 'description' => "Net #{interface_name} outgoing, B",
68
+ 'key_' => "net.if.out[#{interface_name}, bytes]",
69
+ 'hostid' => t_id.to_i,
70
+ 'applications' => [ a_id.to_i ],
71
+ 'history' => 7,
72
+ 'trends' => 30,
73
+ 'delay' => 60,
74
+ 'value_type' => 0,
75
+ 'units' => 'Bps',
76
+ 'value_type' => '3', # Numeric (unsigned)
77
+ 'delta' => 1 # Store as delta (Speed per second)
78
+
79
+ }
80
+ p " ** Add 'Net #{interface_name} outgoing, B' to #{template_name}."
81
+
82
+ i_id = zbx.add_item(options)