zabbixapi 0.1.4 → 0.1.5a1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,11 +1,15 @@
1
1
  = Ruby Zabbix Api Module.
2
2
 
3
- Simple and lightweight ruby module for work with zabbix api version 1.8.2
3
+ Simple and lightweight ruby module for work with zabbix api version 1.8.x
4
4
 
5
5
  You can:
6
6
  * Create host/template/application/items/triggers and screens;
7
7
  * Get info about all zabbix essences;
8
8
 
9
+ == Installation
10
+
11
+ gem install zabbixapi
12
+
9
13
  == Get Start.
10
14
 
11
15
  * Get hostid from zabbix api:
@@ -21,8 +25,17 @@ You can:
21
25
  * net/https
22
26
  * json
23
27
 
28
+ == Use examples
29
+
30
+ * zabbix_la - LoadAverage template
31
+
32
+ cd examples
33
+ ruby zabbix_la -E development -g Templates
34
+
35
+ * -E - env from examples/config.yml (like RAILS_ENV)
36
+ * -g - group in zabbix for templates
37
+
24
38
  == Zabbix documentation
25
39
 
26
40
  * Zabbix Project Homepage -> http://zabbix.com/
27
- * Zabbix Api Draft docs -> http://www.zabbix.com/documentation/1.8/api
28
-
41
+ * Zabbix Api docs -> http://www.zabbix.com/documentation/1.8/api
@@ -0,0 +1,4 @@
1
+ development:
2
+ api_url: "http://zabbix.local/api_jsonrpc.php"
3
+ api_login: admin
4
+ api_password: zabbix
@@ -0,0 +1,73 @@
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:E:")
9
+
10
+ group_name = opt["g"]
11
+ zabbix_env = opt["E"]
12
+
13
+ template_name = "TMPL_Availability"
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
+
23
+ # Esablish new connection
24
+ zbx = Zabbix::ZabbixApi.new(api_url, api_login, api_password)
25
+
26
+ # Create new template
27
+ p " * Creating template #{template_name}."
28
+ g_id = zbx.get_group_id(group_name)
29
+
30
+ options = {
31
+ 'groups' => [ g_id.to_i ],
32
+ 'host' => template_name
33
+ }
34
+
35
+ t_id = zbx.add_template(options)
36
+
37
+ # Create application #{app_name}
38
+ app_name = "Availability"
39
+ p " ** Create application #{app_name}."
40
+ application = {
41
+ 'hostid' => t_id.to_i,
42
+ 'name' => app_name
43
+ }
44
+ a_id = zbx.add_application(application)
45
+
46
+ # 'Ping.'
47
+ options = {
48
+ 'description' => "Ping",
49
+ 'key_' => "agent.ping",
50
+ 'hostid' => t_id.to_i,
51
+ 'applications' => [ a_id.to_i ],
52
+ 'history' => 7,
53
+ 'trends' => 30,
54
+ 'delay' => 60,
55
+ 'value_type' => 0,
56
+ 'type' => '0'
57
+ }
58
+ p " ** Add 'Ping' to #{template_name}."
59
+ i_id = zbx.add_item(options)
60
+
61
+ # TRIGGERS
62
+ options = {
63
+ 'description' => "Host availability",
64
+ 'expression' => "{#{template_name}:agent.ping.nodata(61)}=1",
65
+ 'priority' => 5, #disaster
66
+ 'templateid' => 0,
67
+ 'comments' => "Host availability",
68
+ 'type' => 0,
69
+ 'status' => '0'
70
+ }
71
+
72
+ p " ** Add 'Host availability disaster trigger'"
73
+ tr_id = zbx.add_trigger(options)
@@ -0,0 +1,152 @@
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("d:g:E:")
9
+
10
+ group_name = opt["g"]
11
+ disk_name = opt["d"]
12
+ zabbix_env = opt["E"]
13
+
14
+ template_name = "TMPL_Disk_" + disk_name.gsub(/\//, "_")
15
+
16
+ # read config
17
+ config = YAML::load(open('./config.yml'))
18
+
19
+ api_url = config[zabbix_env]["api_url"]
20
+ api_login = config[zabbix_env]["api_login"]
21
+ api_password = config[zabbix_env]["api_password"]
22
+
23
+ # Esablish new connection
24
+ zbx = Zabbix::ZabbixApi.new(api_url, api_login, api_password)
25
+ zbx.debug = true
26
+
27
+ # Create new template
28
+ p " * Creating template #{template_name}."
29
+ g_id = zbx.get_group_id(group_name)
30
+
31
+ options = {
32
+ 'groups' => [ g_id.to_i ],
33
+ 'host' => template_name
34
+ }
35
+
36
+ t_id = zbx.add_template(options)
37
+
38
+ # Create application #{app_name}
39
+ app_name = "Disk_" + disk_name.gsub(/\//, "_")
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
+ # 'IOPS Read on #{disk_name} avg5'
48
+ options = {
49
+ 'description' => "IOPS Read on #{disk_name} avg5",
50
+ 'key_' => "vfs.dev.read[#{disk_name}, ops, avg5]",
51
+ 'hostid' => t_id.to_i,
52
+ 'applications' => [ a_id.to_i ],
53
+ 'units' => 'ops',
54
+ 'history' => 7,
55
+ 'trends' => 30,
56
+ 'delay' => 300,
57
+ 'value_type' => 0,
58
+ }
59
+ p " ** Add 'IOPS Read on #{disk_name} avg5' to #{template_name}."
60
+
61
+ i_id = zbx.add_item(options)
62
+
63
+ # 'IOPS Write on #{disk_name} avg5'
64
+ options = {
65
+ 'description' => "IOPS Write on #{disk_name} avg5",
66
+ 'key_' => "vfs.dev.write[#{disk_name}, ops, avg5]",
67
+ 'hostid' => t_id.to_i,
68
+ 'applications' => [ a_id.to_i ],
69
+ 'units' => 'ops',
70
+ 'history' => 7,
71
+ 'trends' => 30,
72
+ 'delay' => 300,
73
+ 'value_type' => 0,
74
+ }
75
+ p " ** Add 'IOPS Write on #{disk_name} avg5' to #{template_name}."
76
+
77
+ i_id = zbx.add_item(options)
78
+
79
+ # 'BPS Read on #{disk_name} avg5'
80
+ options = {
81
+ 'description' => "BPS Read on #{disk_name} avg5",
82
+ 'key_' => "vfs.dev.read[#{disk_name}, sps, avg5]",
83
+ 'hostid' => t_id.to_i,
84
+ 'applications' => [ a_id.to_i ],
85
+
86
+ # convert sectors to bytes
87
+ 'multiplier' => 1,
88
+ 'formula' => 512,
89
+
90
+ 'value_type' => 0,
91
+ 'units' => 'Bps',
92
+ 'history' => 7,
93
+ 'trends' => 30,
94
+ 'delay' => 300,
95
+ }
96
+ p " ** Add 'BPS Read on #{disk_name} avg5' to #{template_name}."
97
+
98
+ bps_r_avg5_id = zbx.add_item(options)
99
+
100
+ # 'BPS Write on #{disk_name}'
101
+ options = {
102
+ 'description' => "BPS Write on #{disk_name} avg5",
103
+ 'key_' => "vfs.dev.write[#{disk_name}, sps, avg5]",
104
+ 'hostid' => t_id.to_i,
105
+ 'applications' => [ a_id.to_i ],
106
+
107
+ # convert sectors to bytes
108
+ 'multiplier' => 1,
109
+ 'formula' => 512,
110
+
111
+ 'value_type' => 0,
112
+ 'units' => 'Bps',
113
+ 'history' => 7,
114
+ 'trends' => 30,
115
+ 'delay' => 300
116
+ }
117
+ p " ** Add 'BPS Write on #{disk_name} avg5' to #{template_name}."
118
+
119
+ bps_w_avg5_id = zbx.add_item(options)
120
+
121
+ # Create graph 'Disk usage #{disk_name}'
122
+ options = {
123
+ 'gitems' => [
124
+ {
125
+ "itemid" => bps_w_avg5_id,
126
+ "drawtype" => "0",
127
+ "sortorder" => "0",
128
+ "color" => "AA0000",
129
+ "yaxisside" => "0",
130
+ "calc_fnc" => "2",
131
+ "type" => "0",
132
+ "periods_cnt" => "5"
133
+ },
134
+ {
135
+ "itemid" => bps_r_avg5_id,
136
+ "drawtype" => "0",
137
+ "sortorder" => "0",
138
+ "color" => "009900",
139
+ "yaxisside" => "0",
140
+ "calc_fnc" => "2",
141
+ "type" => "0",
142
+ "periods_cnt" => "5"
143
+ }
144
+ ],
145
+ "show_triggers" => "1",
146
+ "name" => "Disk usage #{disk_name}",
147
+ "width" => "900",
148
+ "height" => "200",
149
+ "templateid" => "0"
150
+ }
151
+ p " ** Add 'Disk usage #{disk_name} graph'"
152
+ g_id = zbx.add_graph(options)
@@ -0,0 +1,249 @@
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("m:g:E:")
9
+
10
+ mount_point = opt["m"]
11
+ group_name = opt["g"]
12
+ zabbix_env = opt["E"]
13
+
14
+ if ( mount_point == "/" )
15
+ template_name = "TMPL_Filesystem_root"
16
+ app_name = "Filesystem_root"
17
+ else
18
+ mount_point = mount_point.sub(/\/$/, "")
19
+ template_name = "TMPL_Filesystem" + mount_point.gsub(/\//, "_")
20
+ app_name = "Filesystem" + mount_point.gsub(/\//, "_")
21
+ end
22
+
23
+ # read config
24
+ config = YAML::load(open('./config.yml'))
25
+
26
+ api_url = config[zabbix_env]["api_url"]
27
+ api_login = config[zabbix_env]["api_login"]
28
+ api_password = config[zabbix_env]["api_password"]
29
+
30
+ # Esablish new connection
31
+ zbx = Zabbix::ZabbixApi.new(api_url, api_login, api_password)
32
+
33
+ # Create new template
34
+ p " * Creating template #{template_name}."
35
+ g_id = zbx.get_group_id(group_name)
36
+
37
+ options = {
38
+ 'groups' => [ g_id.to_i ],
39
+ 'host' => template_name
40
+ }
41
+
42
+ t_id = zbx.add_template(options)
43
+
44
+ # Create application #{app_name}
45
+ p " ** Create application #{app_name}."
46
+ application = {
47
+ 'hostid' => t_id.to_i,
48
+ 'name' => app_name
49
+ }
50
+
51
+ a_id = zbx.add_application(application)
52
+
53
+ # Total disk space on #{mount_point}
54
+ options = {
55
+ 'description' => "Total disk space on #{mount_point}",
56
+ 'key_' => "vfs.fs.size[#{mount_point},total]",
57
+ 'hostid' => t_id.to_i,
58
+ 'applications' => [ a_id.to_i ],
59
+ 'units' => 'B',
60
+ 'history' => 7,
61
+ 'trends' => 30,
62
+ 'delay' => 600,
63
+ 'value_type' => 0
64
+ }
65
+
66
+ p " ** Add 'Total disk space on #{mount_point}' to #{template_name}."
67
+ total_in_b_item_id = zbx.add_item(options)
68
+
69
+ # Used disk space on #{mount_point}
70
+ options = {
71
+ 'description' => "Used disk space on #{mount_point}",
72
+ 'key_' => "vfs.fs.size[#{mount_point},used]",
73
+ 'hostid' => t_id.to_i,
74
+ 'applications' => [ a_id.to_i ],
75
+ 'units' => 'B',
76
+ 'history' => 7,
77
+ 'trends' => 30,
78
+ 'delay' => 60,
79
+ 'value_type' => 0
80
+ }
81
+
82
+ p " ** Add 'Used disk space on #{mount_point}' to #{template_name}."
83
+ used_in_b_item_id = zbx.add_item(options)
84
+
85
+ # Use disk space on #{mount_point} in %
86
+ options = {
87
+ 'description' => "Used disk space on #{mount_point} in %",
88
+ 'key_' => "vfs.fs.size[#{mount_point},pused]",
89
+ 'hostid' => t_id.to_i,
90
+ 'applications' => [ a_id.to_i ],
91
+ 'history' => 7,
92
+ 'trends' => 30,
93
+ 'delay' => 60,
94
+ 'value_type' => 0
95
+ }
96
+
97
+ p " ** Add 'Used disk space on #{mount_point} in %' to #{template_name}."
98
+ i_id = zbx.add_item(options)
99
+
100
+ # Free disk space on #{mount_point}
101
+ options = {
102
+ 'description' => "Free disk space on #{mount_point}",
103
+ 'type' => '15', # calculated
104
+ 'key_' => "vfs.fs.size[#{mount_point},free]",
105
+ 'params' => "last(\"vfs.fs.size[#{mount_point},total]\") - last(\"vfs.fs.size[#{mount_point},used]\")",
106
+ 'hostid' => t_id.to_i,
107
+ 'applications' => [ a_id.to_i ],
108
+ 'units' => 'B',
109
+ 'history' => 7,
110
+ 'trends' => 30,
111
+ 'delay' => 60,
112
+ 'value_type' => 0
113
+ }
114
+
115
+ p " ** Add 'Free disk space on #{mount_point}' to #{template_name}."
116
+ i_id = zbx.add_item(options)
117
+
118
+ # Free disk space on #{mount_point} in %
119
+ options = {
120
+ 'description' => "Free disk space on #{mount_point} in %",
121
+ 'type' => '15', # calculated
122
+ 'key_' => "vfs.fs.size[#{mount_point},pfree]",
123
+ 'params' => "100 - last(\"vfs.fs.size[#{mount_point},pused]\")",
124
+ 'hostid' => t_id.to_i,
125
+ 'applications' => [ a_id.to_i ],
126
+ 'history' => 7,
127
+ 'trends' => 30,
128
+ 'delay' => 60,
129
+ 'value_type' => 0
130
+ }
131
+
132
+ p " ** Add 'Free disk space on #{mount_point} in %' to #{template_name}."
133
+ i_id = zbx.add_item(options)
134
+
135
+ # Free number of inodes on #{mount_point}
136
+ options = {
137
+ 'description' => "Free number of inodes on #{mount_point}",
138
+ 'key_' => "vfs.fs.inode[#{mount_point},free]",
139
+ 'hostid' => t_id.to_i,
140
+ 'applications' => [ a_id.to_i ],
141
+ 'units' => '',
142
+ 'history' => 7,
143
+ 'trends' => 30,
144
+ 'delay' => 60,
145
+ 'value_type' => 0
146
+ }
147
+
148
+ p " ** Add 'Free number of inodes on #{mount_point}' to #{template_name}."
149
+ i_id = zbx.add_item(options)
150
+
151
+ # Free number of inodes on #{mount_point} in %
152
+ options = {
153
+ 'description' => "Free number of inodes on #{mount_point} in %",
154
+ 'key_' => "vfs.fs.inode[#{mount_point},pfree]",
155
+ 'hostid' => t_id.to_i,
156
+ 'applications' => [ a_id.to_i ],
157
+ 'history' => 7,
158
+ 'trends' => 30,
159
+ 'delay' => 60,
160
+ 'value_type' => 0
161
+ }
162
+
163
+ p " ** Add 'Free number of inodes on #{mount_point} in %' to #{template_name}."
164
+ i_id = zbx.add_item(options)
165
+
166
+ ## TRIGGERS
167
+ options = {
168
+ 'description' => "Free disk space on #{mount_point}",
169
+ 'expression' => "{#{template_name}:vfs.fs.size[#{mount_point},pfree].last(0)}<15",
170
+ 'priority' => 2, # warning
171
+ 'templateid' => 0,
172
+ 'comments' => "Free disk space on #{mount_point} (warning)",
173
+ 'type' => 0,
174
+ 'status' => '0'
175
+ }
176
+
177
+ p " ** Add 'Free disk space on #{mount_point} warning trigger'"
178
+ tr_id = zbx.add_trigger(options)
179
+
180
+ options = {
181
+ 'description' => "Free disk space on #{mount_point}",
182
+ 'expression' => "{#{template_name}:vfs.fs.size[#{mount_point},pfree].last(0)}<10",
183
+ 'priority' => 5, # Disaster
184
+ 'templateid' => 0,
185
+ 'comments' => "Free disk space on #{mount_point} (disaster)",
186
+ 'type' => 0,
187
+ 'status' => 0
188
+ }
189
+
190
+ p " ** Add 'Free disk space on #{mount_point} disaster trigger'"
191
+ tr_id = zbx.add_trigger(options)
192
+
193
+ options = {
194
+ 'description' => "Free inodes on #{mount_point}",
195
+ 'expression' => "{#{template_name}:vfs.fs.inode[#{mount_point},pfree].last(0)}<15",
196
+ 'priority' => 2, # warning
197
+ 'templateid' => '0',
198
+ 'comments' => "Free disk inodes on #{mount_point} (warning)",
199
+ 'type' => 0,
200
+ 'status' => '0'
201
+ }
202
+
203
+ p " ** Add 'Free inodes on #{mount_point} warning trigger'"
204
+ tr_id = zbx.add_trigger(options)
205
+
206
+ options = {
207
+ 'description' => "Free inodes on #{mount_point}",
208
+ 'expression' => "{#{template_name}:vfs.fs.inode[#{mount_point},pfree].last(0)}<10",
209
+ 'priority' => 5, #disaster
210
+ 'templateid' => '0',
211
+ 'comments' => "Free disk inodes on #{mount_point} (disaster)",
212
+ 'type' => 0,
213
+ 'status' => '0'
214
+ }
215
+
216
+ p " ** Add 'Free inodes on #{mount_point} disaster trigger'"
217
+ tr_id = zbx.add_trigger(options)
218
+
219
+ options = {
220
+ 'gitems' => [
221
+ {
222
+ "itemid" => total_in_b_item_id,
223
+ "drawtype" => "0",
224
+ "sortorder" => "0",
225
+ "color" => "AA0000",
226
+ "yaxisside" => "0",
227
+ "calc_fnc" => "2",
228
+ "type" => "0",
229
+ "periods_cnt" => "5"
230
+ },
231
+ {
232
+ "itemid" => used_in_b_item_id,
233
+ "drawtype" => "0",
234
+ "sortorder" => "0",
235
+ "color" => "009900",
236
+ "yaxisside" => "0",
237
+ "calc_fnc" => "2",
238
+ "type" => "0",
239
+ "periods_cnt" => "5"
240
+ }
241
+ ],
242
+ "show_triggers" => "1",
243
+ "name" => "Disk space on #{mount_point}",
244
+ "width" => "900",
245
+ "height" => "200",
246
+ "templateid" => "0"
247
+ }
248
+ p " ** Add 'Disk space on #{mount_point} graph'"
249
+ g_id = zbx.add_graph(options)
@@ -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)