zabbixapi 0.1.6.4 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -5,7 +5,7 @@ Simple and lightweight ruby module for work with zabbix api version 1.8.x
5
5
  {<img src="https://travis-ci.org/vadv/zabbixapi.png">}[https://travis-ci.org/vadv/zabbixapi]
6
6
 
7
7
  You can:
8
- * Create and delete host/template/application/items/triggers and screens;
8
+ * Create and delete host/template/application/items/triggers/maintenance and screens;
9
9
  * Get info about all zabbix essences;
10
10
 
11
11
  == Installation
@@ -35,6 +35,12 @@ You can:
35
35
  puts zbx.get_group_id('some_group')
36
36
  end
37
37
 
38
+ * Debugging
39
+
40
+ For debug information on requests set
41
+
42
+ zbx.debug = true
43
+
38
44
  More see in spec please...
39
45
 
40
46
  == Dependencies
@@ -0,0 +1,30 @@
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
+ maintenance_name = "Test Maintenance"
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
+ zbx.debug = true
25
+ #Simply uses the api_login and api_password for basic auth
26
+ zbx.basic_auth = true
27
+
28
+ p " * Creating hostgroup #{group_name}."
29
+ g_id = zbx.add_or_get_group(group_name)
30
+ puts g_id
@@ -0,0 +1,55 @@
1
+ #
2
+ # gems
3
+ #
4
+ require 'zabbixapi'
5
+
6
+ #
7
+ # main
8
+ #
9
+ @yourZabbixHost = 'https://zabbix.example.com/api_jsonrpc.php'
10
+ @yourZabbixLogin = 'login'
11
+ @yourZabbixPassword = 'password'
12
+ @yourServerHost = 'examplehost'
13
+
14
+ # init
15
+ zbx = Zabbix::ZabbixApi.new(@yourZabbixHost, @yourZabbixLogin, @yourZabbixPassword)
16
+ zbx.debug = true
17
+
18
+ # find your host
19
+ host_id = zbx.get_host_id(@yourServerHost);
20
+ puts host_id
21
+
22
+ # create an 1hour maintenance period
23
+ # => true
24
+ time_now = Time.now.to_i
25
+ options = {
26
+ 'name' => 'maintenance example',
27
+ 'description' => 'maintenance example description ',
28
+ 'active_since' => time_now,
29
+ 'active_till' => time_now + 60*60
30
+ }
31
+ maintenance_id = zbx.create_maintenance(host_id,options)
32
+ puts maintenance_id
33
+
34
+ # is maintenance item available?
35
+ # => true
36
+ puts zbx.maintenance_exists?(maintenance_id)
37
+
38
+ # update name, description and extend period to 3h
39
+ # => true
40
+ options = {
41
+ 'name' => 'maintenance example update',
42
+ 'description' => 'maintenance example update description',
43
+ 'hostids' => [host_id],
44
+ 'active_since' => time_now,
45
+ 'active_till' => time_now + 180*60
46
+ }
47
+ puts zbx.update_maintenance(maintenance_id,options)
48
+
49
+ # delete it
50
+ # => true
51
+ puts zbx.delete_maintenance(maintenance_id)
52
+
53
+ # is maintenance item still available?
54
+ # => false
55
+ puts zbx.maintenance_exists?(maintenance_id)
@@ -0,0 +1,84 @@
1
+ #!/bin/bash
2
+
3
+ if [ $# -ne 1 ]; then
4
+ echo "usage: $(basename $0) ENVIRONMENT"
5
+ exit 1
6
+ fi
7
+
8
+ ENVIRONMENT=$1
9
+ CURRENT_DIR=$(dirname $0)
10
+
11
+ # Clear default
12
+ bundle exec ruby zabbix_clear_default -E $ENVIRONMENT
13
+
14
+ # Availability
15
+ bundle exec ruby zabbix_availability -E $ENVIRONMENT -g "Templates"
16
+
17
+ # Net
18
+ bundle exec ruby zabbix_net -E $ENVIRONMENT -g "Templates" -i eth0
19
+ bundle exec ruby zabbix_net -E $ENVIRONMENT -g "Templates" -i eth1
20
+ bundle exec ruby zabbix_net -E $ENVIRONMENT -g "Templates" -i eth2
21
+ bundle exec ruby zabbix_net -E $ENVIRONMENT -g "Templates" -i eth3
22
+
23
+ # LA
24
+ bundle exec ruby zabbix_la -E $ENVIRONMENT -g "Templates"
25
+
26
+ # Memory
27
+ bundle exec ruby zabbix_memory -E $ENVIRONMENT -g "Templates"
28
+
29
+ # Filesystems
30
+ for i in "/storage" "/" "/boot" "/mnt" "/var" "/home" "/tmp" "/backup" \
31
+ "/var/lib/postgresql"; do
32
+ bundle exec ruby zabbix_filesystem -E $ENVIRONMENT -g "Templates" -m "$i"
33
+ done
34
+
35
+ # SSH
36
+ bundle exec ruby zabbix_ssh -E $ENVIRONMENT -g Templates
37
+
38
+ # NTP
39
+ bundle exec ruby zabbix_ntp -E $ENVIRONMENT -g Templates
40
+
41
+ # HLSP
42
+ for i in {1..8}; do
43
+ bundle exec ruby zabbix_hlsp -E $ENVIRONMENT -g Templates -i ${i}
44
+ done
45
+
46
+ # Playout
47
+ bundle exec ruby zabbix_playout-t -E $ENVIRONMENT -g Templates
48
+
49
+ # MegaRAID
50
+ bundle exec ruby zabbix_megaraid -E $ENVIRONMENT -g Templates
51
+
52
+ # PGSQL
53
+ bundle exec ruby zabbix_pgsql -E $ENVIRONMENT -g Templates
54
+
55
+ # MDADM
56
+ for i in md0 md1 md2 md3; do
57
+ bundle exec ruby zabbix_mdadm -E $ENVIRONMENT -g Templates -n ${i}
58
+ done
59
+
60
+ # ipmi_systemp
61
+ bundle exec ruby zabbix_ipmi_systemp -E $ENVIRONMENT -g Templates
62
+
63
+ # IO
64
+ bundle exec ruby zabbix_iops -E $ENVIRONMENT -g Templates -n sda
65
+
66
+ # varnish
67
+ bundle exec ruby zabbix_varnish -E $ENVIRONMENT -g Templates
68
+
69
+ # nginx
70
+ bundle exec ruby zabbix_nginx -E $ENVIRONMENT -g Templates
71
+
72
+ # nginx_500
73
+ bundle exec ruby zabbix_nginx_500 -E $ENVIRONMENT -g Templates
74
+
75
+
76
+ bundle exec ruby zabbix_cpufan-sersor -E $ENVIRONMENT -g Templates
77
+ bundle exec ruby zabbix_cputemp-sersor -E $ENVIRONMENT -g Templates
78
+ bundle exec ruby zabbix_ipmi-cpufan-sersor -E $ENVIRONMENT -g Templates
79
+ bundle exec ruby zabbix_mailer -E $ENVIRONMENT -g Templates
80
+ bundle exec ruby zabbix_megaraid -E $ENVIRONMENT -g Templates
81
+ bundle exec ruby zabbix_mpeg2lander_signal -E $ENVIRONMENT -g Templates
82
+ bundle exec ruby zabbix_mpeg2lander_status -E $ENVIRONMENT -g Templates
83
+ bundle exec ruby zabbix_named -E $ENVIRONMENT -g Templates
84
+ bundle exec ruby zabbix_nv-gputemp -E $ENVIRONMENT -g Templates
@@ -25,23 +25,23 @@ zbx = Zabbix::ZabbixApi.new(api_url, api_login, api_password)
25
25
 
26
26
  # Create new template
27
27
  p " * Creating template #{template_name}."
28
- g_id = zbx.get_group_id(group_name)
28
+ g_id = zbx.add_or_get_group(group_name)
29
29
 
30
30
  options = {
31
31
  'groups' => [ g_id.to_i ],
32
32
  'host' => template_name
33
33
  }
34
34
 
35
- t_id = zbx.add_template(options)
35
+ t_id = zbx.add_or_get_template(options)
36
36
 
37
37
  # Create application #{app_name}
38
38
  app_name = "Availability"
39
39
  p " ** Create application #{app_name}."
40
40
  application = {
41
- 'hostid' => t_id.to_i,
41
+ 'hostid' => t_id,
42
42
  'name' => app_name
43
43
  }
44
- a_id = zbx.add_application(application)
44
+ a_id = zbx.add_or_get_application(t_id, application)
45
45
 
46
46
  # 'Ping.'
47
47
  options = {
@@ -51,12 +51,12 @@ options = {
51
51
  'applications' => [ a_id.to_i ],
52
52
  'history' => 7,
53
53
  'trends' => 30,
54
- 'delay' => 60,
54
+ 'delay' => 30,
55
55
  'value_type' => 0,
56
56
  'type' => '0'
57
57
  }
58
58
  p " ** Add 'Ping' to #{template_name}."
59
- i_id = zbx.add_item(options)
59
+ i_id = zbx.add_or_get_item(t_id, options)
60
60
 
61
61
  # TRIGGERS
62
62
  options = {
@@ -70,4 +70,4 @@ options = {
70
70
  }
71
71
 
72
72
  p " ** Add 'Host availability disaster trigger'"
73
- tr_id = zbx.add_trigger(options)
73
+ tr_id = zbx.add_or_get_trigger(t_id, options)
@@ -0,0 +1,36 @@
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
+ defaults = zbx.get_templates()
27
+
28
+ p " * Delete all template ^Template_."
29
+
30
+ defaults.each do |template|
31
+ if template[1].match('^Template_')
32
+ p " ** Delete template #{template[1]}"
33
+ zbx.delete_template(template[1])
34
+ end
35
+ end
36
+
@@ -0,0 +1,114 @@
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_cpufan_sensor"
14
+ app_name = "cpufan_sensor"
15
+ high_disaster_rpm = '5000'
16
+ low_disaster_rpm = '800'
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
+ app_name = "cpufan_sensor"
41
+ p " ** Create application #{app_name}."
42
+ application = {
43
+ 'hostid' => t_id.to_i,
44
+ 'name' => app_name
45
+ }
46
+ a_id = zbx.add_or_get_application(t_id, application)
47
+
48
+ # 'cpu fan speed'
49
+ options = {
50
+ 'description' => "CPU FAN Speed sensor",
51
+ 'key_' => "cpufansensor",
52
+ 'hostid' => t_id.to_i,
53
+ 'applications' => [ a_id.to_i ],
54
+ 'history' => 7,
55
+ 'trends' => 30,
56
+ 'delay' => 60,
57
+ 'templateid' => t_id,
58
+ 'value_type' => '3',
59
+ 'units' => 'rpm'
60
+ }
61
+ p " ** Add 'cpufansensor' to #{template_name}."
62
+ sensor_item_id = zbx.add_or_get_item(t_id, options)
63
+
64
+ # TRIGGER
65
+ # cpufan disaster (high threshold)
66
+ options = {
67
+ 'description' => "CPU FAN sensor high",
68
+ 'expression' => "{#{template_name}:cpufansensor.last(0)}>#{high_disaster_rpm}",
69
+ 'priority' => 5, # disaster
70
+ 'templateid' => 0,
71
+ 'comments' => "Cpufan sensor high threshold (Disaster)",
72
+ 'type' => 0,
73
+ 'status' => '0'
74
+ }
75
+ p " ** Add 'cpufan sensor high disaster' trigger"
76
+ zbx.debug = true
77
+ tr_id = zbx.add_or_get_trigger(t_id, options)
78
+
79
+ # TRIGGER
80
+ # cpufan disaster (high threshold)
81
+ options = {
82
+ 'description' => "CPU FAN sensor low",
83
+ 'expression' => "{#{template_name}:cpufansensor.last(0)}<#{low_disaster_rpm}",
84
+ 'priority' => 5, # disaster
85
+ 'templateid' => 0,
86
+ 'comments' => "Cpufan sensor low threshold (Disaster)",
87
+ 'type' => 0,
88
+ 'status' => '0'
89
+ }
90
+ p " ** Add 'cpufan sensor low disaster' trigger"
91
+ tr_id = zbx.add_or_get_trigger(t_id, options)
92
+
93
+ # GRAPH
94
+ options = {
95
+ 'gitems' => [
96
+ {
97
+ "itemid" => sensor_item_id,
98
+ "drawtype" => "0",
99
+ "sortorder" => "0",
100
+ "color" => "AA0000",
101
+ "yaxisside" => "0",
102
+ "calc_fnc" => "2",
103
+ "type" => "0",
104
+ "periods_cnt" => "5"
105
+ }
106
+ ],
107
+ "show_triggers" => "1",
108
+ "name" => "CPU FAN Speed sensor data from lm-sensors",
109
+ "width" => "900",
110
+ "height" => "200",
111
+ "templateid" => "0"
112
+ }
113
+
114
+ 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
+
8
+ opt = Getopt::Std.getopts("g:E:")
9
+
10
+ group_name = opt["g"]
11
+ zabbix_env = opt["E"]
12
+
13
+ template_name = "TMPL_cputemp_sensor"
14
+ app_name = "cputemp_sensor"
15
+ high_temperature = '60'
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
+ app_name = "cputemp_sensor"
41
+ p " ** Create application #{app_name}."
42
+ application = {
43
+ 'hostid' => t_id.to_i,
44
+ 'name' => app_name
45
+ }
46
+ a_id = zbx.add_or_get_application(t_id, application)
47
+
48
+ # 'cpu temperature sensor'
49
+ options = {
50
+ 'description' => "CPU Temperature sensor",
51
+ 'key_' => "cputempsensor",
52
+ 'hostid' => t_id.to_i,
53
+ 'applications' => [ a_id.to_i ],
54
+ 'history' => 7,
55
+ 'trends' => 30,
56
+ 'delay' => 60,
57
+ 'value_type' => '3',
58
+ 'units' => 'C'
59
+ }
60
+ p " ** Add 'cputempsensor' to #{template_name}."
61
+ sensor_item_id = zbx.add_or_get_item(t_id, options)
62
+
63
+ # TRIGGER
64
+ # cputemp disaster (high threshold)
65
+ options = {
66
+ 'description' => "CPU Temperature sensor warning",
67
+ 'expression' => "{#{template_name}:cputempsensor.last(0)}>#{high_temperature}",
68
+ 'priority' => 2, # warning
69
+ 'templateid' => 0,
70
+ 'comments' => "Cputemp sensor (Warning)",
71
+ 'type' => 0,
72
+ 'status' => '0'
73
+ }
74
+ p " ** Add 'cputemp sensor warning' trigger"
75
+ tr_id = zbx.add_or_get_trigger(t_id, options)
76
+
77
+ # TRIGGER
78
+ # cputemp disaster (high threshold)
79
+ options = {
80
+ 'description' => "CPU temperature sensor disaster",
81
+ 'expression' => "{#{template_name}:cputempsensor.last(0)}>#{crit_temperature}",
82
+ 'priority' => 5, # disaster
83
+ 'templateid' => 0,
84
+ 'comments' => "Cputemp sensor (Disaster)",
85
+ 'type' => 0,
86
+ 'status' => '0'
87
+ }
88
+ p " ** Add 'cputemp sensor disaster' trigger"
89
+ tr_id = zbx.add_or_get_trigger(t_id, options)
90
+
91
+ # GRAPH
92
+ options = {
93
+ 'gitems' => [
94
+ {
95
+ "itemid" => sensor_item_id,
96
+ "drawtype" => "0",
97
+ "sortorder" => "0",
98
+ "color" => "AA0000",
99
+ "yaxisside" => "0",
100
+ "calc_fnc" => "2",
101
+ "type" => "0",
102
+ "periods_cnt" => "5"
103
+ }
104
+ ],
105
+ "show_triggers" => "1",
106
+ "name" => "CPU Temperature from lm-sensors",
107
+ "width" => "900",
108
+ "height" => "200",
109
+ "templateid" => "0"
110
+ }
111
+
112
+ g_id = zbx.add_or_get_graph(t_id, options)