zabbixapi 0.1.6.4 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,167 @@
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
+ zabbix_env = opt["E"]
12
+
13
+ template_name = "TMPL_nginx"
14
+ app_name = "Nginx"
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
+
26
+ # Create new template
27
+ p " * Creating template #{template_name}."
28
+ g_id = zbx.add_or_get_group(group_name)
29
+
30
+ options = {
31
+ 'groups' => [ g_id.to_i ],
32
+ 'host' => template_name
33
+ }
34
+
35
+ t_id = zbx.add_or_get_template(options)
36
+
37
+ # Create application #{app_name}
38
+ p " ** Create application #{app_name}."
39
+ application = {
40
+ 'hostid' => t_id.to_i,
41
+ 'name' => app_name
42
+ }
43
+ a_id = zbx.add_or_get_application(t_id, application)
44
+
45
+ # 'nginx service status'
46
+ options = {
47
+ 'description' => "nginx service status",
48
+ 'key_' => "runit_service[nginx]",
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 'nginx service status' to #{template_name}."
57
+ item_id = zbx.add_or_get_item(t_id, options)
58
+
59
+ store = Hash.new()
60
+ # 'nginx active_connections'
61
+ options = {
62
+ 'description' => "nginx active_connections",
63
+ 'key_' => "nginx_status[http://127.0.0.1:8010/status,active_connections]",
64
+ 'hostid' => t_id.to_i,
65
+ 'applications' => [ a_id.to_i ],
66
+ 'history' => 7,
67
+ 'trends' => 30,
68
+ 'delay' => 60,
69
+ 'value_type' => 0
70
+ }
71
+ p " ** Add 'nginx active_connections' to #{template_name}."
72
+ item_id = zbx.add_or_get_item(t_id, options)
73
+ store["active_connections"] = item_id
74
+
75
+ ["writing", "reading", "waiting"].each do |param|
76
+ # 'nginx #{param}'
77
+ options = {
78
+ 'description' => "nginx #{param}",
79
+ 'key_' => "nginx_status[http://127.0.0.1:8010/status,#{param}]",
80
+ 'hostid' => t_id.to_i,
81
+ 'applications' => [ a_id.to_i ],
82
+ 'history' => 7,
83
+ 'trends' => 30,
84
+ 'delay' => 60,
85
+ 'value_type' => 0
86
+ }
87
+ p " ** Add 'nginx #{param}' to #{template_name}."
88
+ item_id = zbx.add_or_get_item(t_id, options)
89
+
90
+ store[param] = item_id
91
+ end
92
+
93
+ ["handled", "accepts", "requests"].each do |param|
94
+ # 'nginx #{param}'
95
+ options = {
96
+ 'description' => "nginx #{param}",
97
+ 'key_' => "nginx_status[http://127.0.0.1:8010/status,#{param}]",
98
+ 'hostid' => t_id.to_i,
99
+ 'applications' => [ a_id.to_i ],
100
+ 'history' => 7,
101
+ 'trends' => 30,
102
+ 'delay' => 60,
103
+ 'value_type' => '3', # Numeric (unsigned)
104
+ 'delta' => 1 # Store as delta (Speed per second)
105
+ }
106
+ p " ** Add 'nginx #{param}' to #{template_name}."
107
+ item_id = zbx.add_or_get_item(t_id, options)
108
+
109
+ store[param] = item_id
110
+ end
111
+
112
+ # TRIGGERS
113
+ options = {
114
+ 'description' => "nginx service status",
115
+ 'expression' => "{#{template_name}:runit_service[nginx].last(0)}#0",
116
+ 'priority' => 5, # disaster
117
+ 'templateid' => 0,
118
+ 'comments' => "nginx service status (disaster)",
119
+ 'type' => 0,
120
+ 'status' => '0'
121
+ }
122
+
123
+ p " ** Add 'nginx service status' trigger"
124
+ tr_id = zbx.add_or_get_trigger(t_id, options)
125
+
126
+ # GRAPHS
127
+ options = {
128
+ 'gitems' => [
129
+ {
130
+ "itemid" => store["active_connections"],
131
+ "drawtype" => "0",
132
+ "sortorder" => "0",
133
+ "color" => "AA0000",
134
+ "yaxisside" => "0",
135
+ "calc_fnc" => "2",
136
+ "type" => "0",
137
+ "periods_cnt" => "5"
138
+ },
139
+ {
140
+ "itemid" => store["writing"],
141
+ "drawtype" => "0",
142
+ "sortorder" => "0",
143
+ "color" => "009900",
144
+ "yaxisside" => "0",
145
+ "calc_fnc" => "2",
146
+ "type" => "0",
147
+ "periods_cnt" => "5"
148
+ },
149
+ {
150
+ "itemid" => store["waiting"],
151
+ "drawtype" => "0",
152
+ "sortorder" => "0",
153
+ "color" => "0000AA",
154
+ "yaxisside" => "0",
155
+ "calc_fnc" => "2",
156
+ "type" => "0",
157
+ "periods_cnt" => "5"
158
+ }
159
+ ],
160
+ "show_triggers" => "1",
161
+ "name" => "Nginx status",
162
+ "width" => "900",
163
+ "height" => "200",
164
+ "templateid" => "0"
165
+ }
166
+
167
+ g_id = zbx.add_or_get_graph(t_id, options)
@@ -0,0 +1,112 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'getopt/std'
5
+ require 'yaml'
6
+ require 'zabbixapi'
7
+ require 'digest'
8
+
9
+ opt = Getopt::Std.getopts("g:i:E:")
10
+
11
+ group_name = opt["g"]
12
+ zabbix_env = opt["E"]
13
+
14
+ template_name = "TMPL_nginx_500"
15
+ app_name = "Nginx_500"
16
+
17
+ # read config
18
+ config = YAML::load(open('./config.yml'))
19
+
20
+ api_url = config[zabbix_env]["api_url"]
21
+ api_login = config[zabbix_env]["api_login"]
22
+ api_password = config[zabbix_env]["api_password"]
23
+
24
+ # Monitor all /var/log/nginx/#{access_log_files}.log
25
+ access_log_files = ["face-server-500", "api-server-500", "logger-face-500", "site-500"]
26
+ # Search in log last min
27
+ in_sec = 60 # in seconds
28
+ # Monitor delay
29
+ mon_delay_in_sec = 60 # in seconds
30
+ # Disaster value
31
+ disaster_value = 5
32
+
33
+ # Esablish new connection
34
+ zbx = Zabbix::ZabbixApi.new(api_url, api_login, api_password)
35
+
36
+ # Create new template
37
+ p " * Creating template #{template_name}."
38
+ g_id = zbx.add_or_get_group(group_name)
39
+
40
+ options = {
41
+ 'groups' => [ g_id.to_i ],
42
+ 'host' => template_name
43
+ }
44
+
45
+ t_id = zbx.add_or_get_template(options)
46
+
47
+ # Create application #{app_name}
48
+ p " ** Create application #{app_name}."
49
+ application = {
50
+ 'hostid' => t_id.to_i,
51
+ 'name' => app_name
52
+ }
53
+ a_id = zbx.add_or_get_application(t_id, application)
54
+
55
+ store = Hash.new()
56
+ # 'nginx_500 error rate'
57
+ access_log_files.each do |param|
58
+ # 'nginx #{param}'
59
+ options = {
60
+ 'description' => "nginx error rate #{param}.log",
61
+ 'key_' => "nginx_500[/var/log/nginx/#{param}.log,#{in_sec}]",
62
+ 'hostid' => t_id.to_i,
63
+ 'applications' => [ a_id.to_i ],
64
+ 'history' => 70,
65
+ 'trends' => 30,
66
+ 'delay' => "#{mon_delay_in_sec}",
67
+ 'value_type' => 0
68
+ }
69
+ p " ** Add 'nginx_500 #{param}' to #{template_name}."
70
+ item_id = zbx.add_or_get_item(t_id, options)
71
+
72
+ store["nginx_500_error_rate_log_#{param}"] = item_id
73
+ end
74
+
75
+ # TRIGGERS
76
+ access_log_files.each do |param|
77
+ # 'nginx #{param}'
78
+ options = {
79
+ 'description' => "nginx error log disaster for /var/log/nginx/#{param}.log",
80
+ 'expression' => "{#{template_name}:nginx_500[/var/log/nginx/#{param}.log,#{in_sec}].last(0)}>#{disaster_value}",
81
+ 'priority' => 5, # disaster
82
+ 'templateid' => 0,
83
+ 'comments' => "nginx error rate disaster for /var/log/nginx/#{param}.log errors > #{disaster_value} in last #{in_sec} min",
84
+ 'type' => 0,
85
+ 'status' => '0'
86
+ }
87
+ p " ** Add 'nginx service status' trigger for #{param}"
88
+ tr_id = zbx.add_or_get_trigger(t_id, options)
89
+
90
+ end
91
+
92
+ # GRAPHS
93
+ gitems = access_log_files.map do |param| {
94
+ "itemid" => store["nginx_500_error_rate_log_#{param}"],
95
+ "drawtype" => "0",
96
+ "sortorder" => "0",
97
+ "color" => Digest::MD5.hexdigest("#{param}").to_s[0..6],
98
+ "yaxisside" => "0",
99
+ "calc_fnc" => "2",
100
+ "type" => "0",
101
+ "periods_cnt" => "5"
102
+ }
103
+ end
104
+ options = {
105
+ 'gitems' => gitems,
106
+ "show_triggers" => "0",
107
+ "name" => "Nginx error rate",
108
+ "width" => "900",
109
+ "height" => "200"
110
+ }
111
+
112
+ g_id = zbx.add_or_get_graph(t_id, options)
@@ -0,0 +1,71 @@
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
+ zabbix_env = opt["E"]
12
+
13
+ template_name = "TMPL_ntp"
14
+ app_name = "NTP"
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
+
26
+ # Create new template
27
+ p " * Creating template #{template_name}."
28
+ g_id = zbx.add_or_get_group(group_name)
29
+
30
+ options = {
31
+ 'groups' => [ g_id.to_i ],
32
+ 'host' => template_name
33
+ }
34
+
35
+ t_id = zbx.add_or_get_template(options)
36
+
37
+ # Create application #{app_name}
38
+ p " ** Create application #{app_name}."
39
+ application = {
40
+ 'hostid' => t_id.to_i,
41
+ 'name' => app_name
42
+ }
43
+ a_id = zbx.add_or_get_application(t_id, application)
44
+
45
+ # 'ntpd process exists'
46
+ options = {
47
+ 'description' => "ntpd process exists",
48
+ 'key_' => "proc.num[ntpd]",
49
+ 'hostid' => t_id.to_i,
50
+ 'applications' => [ a_id.to_i ],
51
+ 'history' => 7,
52
+ 'trends' => 30,
53
+ 'delay' => 120,
54
+ 'value_type' => 0
55
+ }
56
+ p " ** Add 'ntpd process exists' to #{template_name}."
57
+ item_id = zbx.add_or_get_item(t_id, options)
58
+
59
+ # TRIGGERS
60
+ options = {
61
+ 'description' => "ntpd process exists",
62
+ 'expression' => "{#{template_name}:proc.num[ntpd].last(0)}<1",
63
+ 'priority' => 5, # disaster
64
+ 'templateid' => 0,
65
+ 'comments' => "ntpd process exists (disaster)",
66
+ 'type' => 0,
67
+ 'status' => '0'
68
+ }
69
+
70
+ p " ** Add 'ntpd process exists' trigger"
71
+ tr_id = zbx.add_or_get_trigger(t_id, options)
@@ -0,0 +1,111 @@
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_nv_gputemp_sensor"
14
+ app_name = "nv_gputemp_sensor"
15
+ high_temperature = '70'
16
+ crit_temperature = '80'
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.add_or_get_group(group_name)
31
+
32
+ options = {
33
+ 'groups' => [ g_id.to_i ],
34
+ 'host' => template_name
35
+ }
36
+
37
+ t_id = zbx.add_or_get_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_or_get_application(t_id, application)
46
+
47
+ # 'cpu temperature sensor'
48
+ options = {
49
+ 'description' => "Nvidia GPU Temperature sensor",
50
+ 'key_' => "nv-gputemp",
51
+ 'hostid' => t_id.to_i,
52
+ 'applications' => [ a_id.to_i ],
53
+ 'history' => 7,
54
+ 'trends' => 30,
55
+ 'delay' => 60,
56
+ 'value_type' => '3',
57
+ 'units' => 'C'
58
+ }
59
+ p " ** Add 'nv-gputemp' to #{template_name}."
60
+ sensor_item_id = zbx.add_or_get_item(t_id, options)
61
+
62
+ # TRIGGER
63
+ # nv-gputemp disaster (high threshold)
64
+ options = {
65
+ 'description' => "Nvidia GPU Temperature sensor warning",
66
+ 'expression' => "{#{template_name}:nv-gputemp.last(0)}>#{high_temperature}",
67
+ 'priority' => 2, # warning
68
+ 'templateid' => 0,
69
+ 'comments' => "Nvidia GPU temperature sensor (Warning)",
70
+ 'type' => 0,
71
+ 'status' => '0'
72
+ }
73
+ p " ** Add 'nv-gputemp sensor warning' trigger"
74
+ tr_id = zbx.add_or_get_trigger(t_id, options)
75
+
76
+ # TRIGGER
77
+ # cputemp disaster (high threshold)
78
+ options = {
79
+ 'description' => "Nvidia GPU temperature sensor disaster",
80
+ 'expression' => "{#{template_name}:nv-gputemp.last(0)}>#{crit_temperature}",
81
+ 'priority' => 5, # disaster
82
+ 'templateid' => 0,
83
+ 'comments' => "Nvidia GPU temperature sensor (Disaster)",
84
+ 'type' => 0,
85
+ 'status' => '0'
86
+ }
87
+ p " ** Add 'cputemp sensor disaster' trigger"
88
+ tr_id = zbx.add_or_get_trigger(t_id, options)
89
+
90
+ # GRAPH
91
+ options = {
92
+ 'gitems' => [
93
+ {
94
+ "itemid" => sensor_item_id,
95
+ "drawtype" => "0",
96
+ "sortorder" => "0",
97
+ "color" => "AA0000",
98
+ "yaxisside" => "0",
99
+ "calc_fnc" => "2",
100
+ "type" => "0",
101
+ "periods_cnt" => "5"
102
+ }
103
+ ],
104
+ "show_triggers" => "1",
105
+ "name" => "Nvidia GPU Temperature from nvidia-smi",
106
+ "width" => "900",
107
+ "height" => "200",
108
+ "templateid" => "0"
109
+ }
110
+
111
+ g_id = zbx.add_or_get_graph(t_id, options)