icinga2 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,118 @@
1
+ # ruby-icinga2
2
+
3
+ Small Ruby Class for the Icinga2 API
4
+
5
+
6
+ ## Requirements
7
+
8
+ gem install rest-client --no-rdoc --no-ri
9
+ gem install json --no-rdoc --no-ri
10
+
11
+ ## usage
12
+
13
+ create an instance and get information about the Icinga2 Server
14
+
15
+ config = {
16
+ :icinga => {
17
+ :host => icingaHost,
18
+ :api => {
19
+ :port => icingaApiPort,
20
+ :user => icingaApiUser,
21
+ :pass => icingaApiPass
22
+ },
23
+ :cluster => icingaCluster,
24
+ :satellite => icingaSatellite,
25
+ }
26
+ }
27
+
28
+ i = Icinga::Client.new( config )
29
+ puts i.applicationData()
30
+
31
+ ## Status
32
+
33
+ supports the following API Calls:
34
+
35
+ - [Users](doc/users.md)
36
+ * addUser
37
+ * deleteUser
38
+ * listUsers
39
+ * existsUser
40
+ - [Usergroups](doc/usergroups.md)
41
+ * addUsergroup
42
+ * deleteUsergroup
43
+ * listUsergroups
44
+ * existsUsergroup
45
+ - [Hosts](doc/hosts.md)
46
+ * addHost
47
+ * deleteHost
48
+ * listHosts
49
+ * existsHost
50
+ - [Hostgroups](doc/hostgroups.md)
51
+ * addHostgroup
52
+ * deleteHostgroup
53
+ * listHostgroups
54
+ * existsHostgroup
55
+ - [Services](doc/services.md)
56
+ * addService
57
+ * deleteService
58
+ * listServices
59
+ * existsService
60
+ - [Servicegroups](doc/servicegroups.md)
61
+ * addServicegroup
62
+ * deleteServicegroup
63
+ * listServicegroups
64
+ * existsServicegroup
65
+ - [Downtimes](doc/downtimes.md)
66
+ * addDowntime
67
+ * listDowntimes
68
+ - [Notifications](doc/notifications.md)
69
+ * enableHostNotification
70
+ * disableHostNotification
71
+ * enableServiceNotification
72
+ * disableServiceNotification
73
+ * enableHostgroupNotification
74
+ * disableHostgroupNotification
75
+ * listNotifications
76
+
77
+
78
+ ## create a own gem File
79
+
80
+ #$ gem build icinga2.gemspec
81
+ Successfully built RubyGem
82
+ Name: icinga2
83
+ Version: 1.4.9
84
+ File: icinga2-1.4.9.gem
85
+
86
+ ## install gem
87
+
88
+ #$ gem install ./icinga2-1.4.9.gem
89
+ Successfully installed icinga2-1.4.9
90
+ 1 gem installed
91
+
92
+ ## test via CLI
93
+
94
+ #$ export ICINGA_HOST=localhost ; export ICINGA_API_USER=root ; export ICINGA_API_PASSWORD=icinga
95
+ #$ irb
96
+ 2.3.0 :001 > require 'icinga2'
97
+ => true
98
+ 2.3.0 :002 > config = { :icinga => { :host => 'localhost', :api => { :user => 'root', :pass => 'icinga' } } }
99
+ => {:icinga=>{:host=>"localhost", :api=>{:user=>"root", :pass=>"icinga"}}}
100
+ 2.3.0 :003 > i = Icinga2::Client.new( config )
101
+
102
+ ## test via example
103
+ #$ export ICINGA_HOST=localhost ; export ICINGA_API_USER=root ; export ICINGA_API_PASSWORD=icinga
104
+ #$ ruby examples/test.rb
105
+
106
+
107
+
108
+ ## Contributing
109
+
110
+ 1. Fork it
111
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
112
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
113
+ 4. Push to the branch (`git push origin my-new-feature`)
114
+ 5. Create new Pull Request
115
+
116
+
117
+
118
+
@@ -0,0 +1,7 @@
1
+ # Icinga2 - Downtimes
2
+
3
+ ## add
4
+ addDowntime()
5
+
6
+ # list
7
+ listDowntimes()
@@ -0,0 +1,122 @@
1
+
2
+ To add hostgroup :
3
+
4
+ curl -k -s -u icingaadmin:icinga 'https://localhost:5665/v1/objects/hostgroups/testgrp' \
5
+ -X PUT -d '{ "attrs": { "name" : "testgrp" ,"display_name" : "testgrp" , "state_loaded" :true }}'
6
+
7
+ To add host :
8
+
9
+ curl -k -s -u icingaadmin:icinga 'https://localhost:5665/v1/objects/hosts/8.8.8.8' \
10
+ -X PUT -d '{ "templates": [ "generic-host" ], "attrs": { "address": "8.8.8.8" , "groups" : [ "testgrp" ]} }'
11
+
12
+
13
+ https://localhost:5665/v1/objects/users
14
+ https://localhost:5665/v1/objects/usergroups
15
+
16
+
17
+ https://localhost:5665/v1/objects/hostgroups
18
+ https://localhost:5665/v1/objects/servicegroups
19
+
20
+ https://localhost:5665/v1/objects/hosts
21
+ https://localhost:5665/v1/objects/services
22
+
23
+ # list service from host
24
+ curl -k -u root:icinga -H 'Accept: application/json' -X GET 'https://localhost:5665/v1/objects/services/pandora-17-01!Runlevel-master-live-server'
25
+
26
+
27
+ https://localhost:5665/v1/objects/notifications
28
+
29
+ # get usergroups:
30
+ curl -k -s -u root:icinga -H 'Accept: application/json' -X GET 'https://localhost:5665/v1/objects/usergroups'
31
+
32
+ # add usergroup:
33
+ curl -k -s -u root:icinga -H 'Accept: application/json' -X PUT 'https://localhost:5665/v1/objects/usergroups/foo' --data '{ "attrs": {"display_name": "Foo Bar" } }'
34
+
35
+ # delete usergroup:
36
+ curl -k -s -u root:icinga -H 'Accept: application/json' -H 'X-HTTP-Method-Override: DELETE' -X POST 'https://localhost:5665/v1/objects/usergroups/foo'
37
+
38
+
39
+ # delete user:
40
+ curl -k -s -u root:icinga -H 'Accept: application/json' -H 'X-HTTP-Method-Override: DELETE' -X POST 'https://localhost:5665/v1/objects/users/foo'
41
+
42
+ # add user:
43
+ curl -k -s -u root:icinga -H 'Accept: application/json' -X PUT 'https://localhost:5665/v1/objects/users/foo' --data '{ "attrs": {"display_name": "Foo Bar", "email": "foo.bar@coremedia.com","enable_notifications": false, "groups": [ "coreme
44
+ dia" ] } }'
45
+
46
+
47
+
48
+ # add notification
49
+ curl -k -s -u root:icinga -H 'Accept: application/json' \
50
+ -X PUT 'https://localhost:5665/v1/objects/notifications/debian7.example.com!jabber' -d '{
51
+ "attrs": {
52
+ "command":"mail-service-notification",
53
+ "service_name":"apt",
54
+ "user_groups": ["icingaadmins"] } }'
55
+
56
+
57
+ # delete notification
58
+ curl -k -s -u root:icinga -H 'Accept: application/json' \
59
+ -H 'X-HTTP-Method-Override: DELETE' \
60
+ -X POST 'https://localhost:5665/v1/objects/notifications/debian7.example.com!apt!jabber?cascade=1'
61
+
62
+
63
+
64
+
65
+
66
+ # enable notification für host:
67
+ curl -k -s -u root:icinga -H 'Accept: application/json' -X POST 'https://localhost:5665/v1/objects/hosts/api_dummy_host_1' --data '{ "attrs": { "enable_notifications": true } }'
68
+
69
+ # disable notification für host:
70
+ curl -k -s -u root:icinga -H 'Accept: application/json' -X POST 'https://localhost:5665/v1/objects/hosts/api_dummy_host_1' --data '{ "attrs": { "enable_notifications": false } }'
71
+
72
+ # enable notification für service x von host:
73
+ curl -k -s -u root:icinga -H 'Accept: application/json' -X POST 'https://localhost:5665/v1/objects/services' --data '{ "filter": "host.name==\"api_dummy_host_1\"", "attrs": { "enable_notifications": true } }'
74
+
75
+ # disable notification für service x von host:
76
+ curl -k -s -u root:icinga -H 'Accept: application/json' -X POST 'https://localhost:5665/v1/objects/services' --data '{ "filter": "host.name==\"api_dummy_host_1\"", "attrs": { "enable_notifications": false } }'
77
+
78
+ # enable notification für hostgroup:
79
+ curl -k -s -u root:icinga -H 'Accept: application/json' -X POST 'https://localhost:5665/v1/objects/services' --data '{ "filter": "\"api_dummy_hostgroup\" in host.groups", "attrs": { "enable_notifications": true } }'
80
+
81
+ # disable notification für hostgroup:
82
+ curl -k -s -u root:icinga -H 'Accept: application/json' -X POST 'https://localhost:5665/v1/objects/services' --data '{ "filter": "\"api_dummy_hostgroup\" in host.groups", "attrs": { "enable_notifications": false } }'
83
+
84
+
85
+ # get downtimes
86
+ curl -k -s -u root:icinga -H 'Accept: application/json' -X GET 'https://localhost:5665/v1/objects/downtimes'
87
+
88
+ # delete named downtime
89
+ curl -k -s -u root:icinga -H 'Accept: application/json' -H 'X-HTTP-Method-Override: DELETE' -X POST 'https://localhost:5665/v1/objects/downtimes/icinga2-master!load!icinga2-master-1496045418-0'
90
+
91
+
92
+ # schedule downtime for a host
93
+
94
+ # current_time=$(date +%s)
95
+ # 1449057010
96
+ # current_time_add_30_second=$(date +%s --date="+30 seconds")
97
+ # 1449057040
98
+ curl -k -s -u root:icinga -H 'Accept: application/json' -X POST 'https://localhost:5665/v1/actions/schedule-downtime' \
99
+ --data '{ "type": "Host", "filter": "host.name==\"api_dummy_host_1\"", "start_time": 1449057685, "end_time": 1449057715, "author": "api_user", "comment": "api_comment", "fixed": true, "duration": 30 }'
100
+
101
+ # schedule downtime for all services of a host - change the timestamps accordingly
102
+ curl -k -s -u root:icinga -H 'Accept: application/json' -X POST 'https://localhost:5665/v1/actions/schedule-downtime' \
103
+ --data '{ "type": "Service", "filter": "host.name==\"api_dummy_host_1\"", "start_time": 1449064981, "end_time": 1449065129, "author": "api_user", "comment": "api_comment", "fixed": true, "duration": 30 }'
104
+
105
+
106
+ # schedule downtime for all hosts and services in a hostgroup - change the timestamps accordingly
107
+ curl -k -s -u root:icinga -H 'Accept: application/json' -X POST 'https://localhost:5665/v1/actions/schedule-downtime' \
108
+ --data '{ "type": "Host", "filter": "\"api_dummy_hostgroup\" in host.groups", "start_time": 1449065680, "end_time": 1449065823, "author": "api_user", "comment": "api_comment", "duration": 120, "fixed": true, "duration": 30 }'
109
+
110
+ curl -k -s -u root:icinga -H 'Accept: application/json' -X POST 'https://localhost:5665/v1/actions/schedule-downtime' \
111
+ --data '{ "type": "Service", "filter": "\"api_dummy_hostgroup\" in host.groups)", "start_time": 1449065680, "end_time": 1449065823, "author": "api_user", "comment": "api_comment", "duration": 120, "fixed": true, "duration": 30 }'
112
+
113
+
114
+
115
+
116
+
117
+
118
+
119
+
120
+
121
+
122
+
@@ -0,0 +1,19 @@
1
+ # Icinga2 - Hostgroups
2
+
3
+ ## check
4
+ existsHostgroup()
5
+
6
+ ## add
7
+ addHostgroup()
8
+
9
+ ## list
10
+
11
+ ### named
12
+ listHostgroups()
13
+
14
+ ### all
15
+ listHostgroups()
16
+
17
+ ## delete
18
+ deleteHostgroup()
19
+
@@ -0,0 +1,22 @@
1
+ # Icinga2 - Hosts
2
+
3
+ ## check
4
+ existsHost()
5
+
6
+ ## add
7
+ vars = {
8
+ 'aws' => false
9
+ }
10
+
11
+ addHost( 'foo-bar.lan', vars )
12
+
13
+ ## list
14
+
15
+ ### named
16
+ listHosts( 'foo-bar.lan' )
17
+
18
+ ### all
19
+ listHosts()
20
+
21
+ ## delete
22
+ deleteHost( 'foo-bar.lan' )
@@ -0,0 +1,15 @@
1
+ # Icinga2 - Notifications
2
+
3
+ enableHostNotification()
4
+
5
+ disableHostNotification()
6
+
7
+ enableServiceNotification()
8
+
9
+ disableServiceNotification()
10
+
11
+ enableHostgroupNotification()
12
+
13
+ disableHostgroupNotification()
14
+
15
+ listNotifications()
@@ -0,0 +1,19 @@
1
+ # Icinga2 - Servicegroups
2
+
3
+ ## check
4
+ existsServicegroup()
5
+
6
+ ## add
7
+ addServicegroup()
8
+
9
+ ## list
10
+
11
+ ### named
12
+ listServicegroups()
13
+
14
+ ### all
15
+ listServicegroups()
16
+
17
+ ## delete
18
+ deleteServicegroup()
19
+
@@ -0,0 +1,26 @@
1
+ # Icinga2 - Services
2
+
3
+
4
+ ## check
5
+ existsService()
6
+
7
+ ## add
8
+ services = {
9
+ 'service-heap-mem' => {
10
+ 'display_name' => 'Tomcat - Heap Memory',
11
+ 'check_command' => 'tomcat-heap-memory',
12
+ }
13
+ }
14
+
15
+ i.addServices( 'foo-bar.lan', services )
16
+
17
+ ## list
18
+
19
+ ### named
20
+ listServices( 'foo-bar.lan' )
21
+
22
+ ### all
23
+ listServices()
24
+
25
+ ## delete
26
+ **not yet implemented**
@@ -0,0 +1,12 @@
1
+ # Icinga2 - Usergroups
2
+
3
+ existsUsergroup()
4
+
5
+ listUsergroups()
6
+
7
+ addUsergroup()
8
+
9
+ deleteUsergroup()
10
+
11
+
12
+
@@ -0,0 +1,41 @@
1
+ # Icinga2 - Users
2
+
3
+ ## check, if User exists
4
+
5
+ existsUser?( $USERNAME )
6
+
7
+ return `true` or `false`
8
+
9
+ ## add User
10
+
11
+ var = {
12
+ :name => 'foo',
13
+ :display_name => 'FOO',
14
+ :email => 'foo@bar.com',
15
+ :pager => '0000',
16
+ :groups => ['icingaadmins']
17
+ }
18
+
19
+ addUser( var )
20
+
21
+ return `Hash`
22
+
23
+ ## list User
24
+
25
+ ### named User
26
+
27
+ listUsers( { :name => $USERNAME } )
28
+
29
+ return `Hash`
30
+
31
+ ### all Users
32
+
33
+ listUsers()
34
+
35
+ return `Hash`
36
+
37
+ ## delete User
38
+
39
+ deleteUser( { :name => $USERNAME } )
40
+
41
+ return `Hash`
@@ -0,0 +1,150 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # 23.01.2017 - Bodo Schulz
4
+ #
5
+ #
6
+ # v0.9.2
7
+
8
+ # -----------------------------------------------------------------------------
9
+
10
+ require_relative '../lib/icinga2'
11
+
12
+ # -----------------------------------------------------------------------------
13
+
14
+ icingaHost = ENV.fetch( 'ICINGA_HOST' , 'icinga2' )
15
+ icingaApiPort = ENV.fetch( 'ICINGA_API_PORT' , 5665 )
16
+ icingaApiUser = ENV.fetch( 'ICINGA_API_USER' , 'admin' )
17
+ icingaApiPass = ENV.fetch( 'ICINGA_API_PASSWORD' , nil )
18
+ icingaCluster = ENV.fetch( 'ICINGA_CLUSTER' , false )
19
+ icingaSatellite = ENV.fetch( 'ICINGA_CLUSTER_SATELLITE', nil )
20
+
21
+ # convert string to bool
22
+ icingaCluster = icingaCluster.to_s.eql?('true') ? true : false
23
+
24
+ config = {
25
+ :icinga => {
26
+ :host => icingaHost,
27
+ :api => {
28
+ :port => icingaApiPort,
29
+ :user => icingaApiUser,
30
+ :password => icingaApiPass
31
+ },
32
+ :cluster => icingaCluster,
33
+ :satellite => icingaSatellite,
34
+ }
35
+ }
36
+
37
+ # ---------------------------------------------------------------------------------------
38
+
39
+ i = Icinga2::Client.new( config )
40
+
41
+ if( i != nil )
42
+
43
+ # run tests ...
44
+ #
45
+ #
46
+
47
+ puts "Information about Icinga2:"
48
+ puts i.applicationData()
49
+ puts i.CIBData()
50
+ puts i.apiListener()
51
+ puts ""
52
+
53
+ puts "check if Host 'icinga2-master' exists:"
54
+ puts i.existsHost?( 'icinga2-master' ) ? 'true' : 'false'
55
+ puts "get host Objects from 'icinga2-master'"
56
+ puts i.hostObjects()
57
+ puts "Host problems:"
58
+ puts i.hostProblems()
59
+ puts "Problem Hosts:"
60
+ puts i.problemHosts()
61
+
62
+ puts "list named Hosts:"
63
+ puts i.listHosts( { :name => 'icinga2-master' } )
64
+ puts "list all Hosts:"
65
+ puts i.listHosts()
66
+ puts ""
67
+
68
+ puts "check if Hostgroup 'linux-servers' exists:"
69
+ puts i.existsHostgroup?( 'linux-servers' ) ? 'true' : 'false'
70
+ puts "add hostgroup 'foo'"
71
+ puts i.addHostgroup( { :name => 'foo', :display_name => 'FOO' } )
72
+ puts "list named Hostgroup 'foo'"
73
+ puts i.listHostgroups( { :name => 'foo' } )
74
+ puts "list all Hostgroups:"
75
+ puts i.listHostgroups()
76
+ puts "delete Hostgroup 'foo'"
77
+ puts i.deleteHostgroup( { :name => 'foo' } )
78
+ puts ""
79
+
80
+ puts "check if service 'users' on host 'icinga2-master' exists:"
81
+ puts i.existsService?( { :host => 'icinga2-master', :service => 'users' } ) ? 'true' : 'false'
82
+
83
+ puts "get service Objects"
84
+ puts i.serviceObjects()
85
+ puts "Service problems:"
86
+ puts i.serviceProblems()
87
+ puts "Problem Services:"
88
+ puts i.problemServices()
89
+
90
+ puts "list named Service 'ping4' from Host 'icinga2-master'"
91
+ puts i.listServices( { :host => 'icinga2-master', :service => 'ping4' } )
92
+ puts "list all Services:"
93
+ puts i.listServices()
94
+ puts ""
95
+
96
+ puts "check if Servicegroup 'disk' exists:"
97
+ puts i.existsServicegroup?( 'disk' ) ? 'true' : 'false'
98
+ puts "add Servicegroup 'foo'"
99
+ puts i.addServicegroup( { :name => 'foo', :display_name => 'FOO' } )
100
+ puts "list named Servicegroup 'foo'"
101
+ puts i.listServicegroups( { :name => 'foo' } )
102
+ puts "list all Servicegroup:"
103
+ puts i.listServicegroups()
104
+ puts "delete Servicegroup 'foo'"
105
+ puts i.deleteServicegroup( { :name => 'foo' } )
106
+ puts ""
107
+
108
+ puts "check if Usergroup 'icingaadmins' exists:"
109
+ puts i.existsUsergroup?( 'icingaadmins' ) ? 'true' : 'false'
110
+ puts "add Usergroup 'foo'"
111
+ puts i.addUsergroup( { :name => 'foo', :display_name => 'FOO' } )
112
+ puts "list named Usergroup 'foo'"
113
+ puts i.listUsergroups( { :name => 'foo' } )
114
+ puts "list all Usergroup:"
115
+ puts i.listUsergroups()
116
+ puts "delete Usergroup 'foo'"
117
+ puts i.deleteUsergroup( { :name => 'foo' } )
118
+ puts ""
119
+
120
+ puts "check if User 'icingaadmin' exists:"
121
+ puts i.existsUser?( 'icingaadmin' ) ? 'true' : 'false'
122
+ puts "add User 'foo'"
123
+ puts i.addUser( { :name => 'foo', :display_name => 'FOO', :email => 'foo@bar.com', :pager => '0000', :groups => ['icingaadmins'] } )
124
+ puts "list named User 'foo'"
125
+ puts i.listUsers( { :name => 'foo' } )
126
+ puts "list all User:"
127
+ puts i.listUsers()
128
+ puts "delete User 'foo'"
129
+ puts i.deleteUser( { :name => 'foo' } )
130
+ puts ""
131
+
132
+ puts "add Downtime 'test':"
133
+ puts i.addDowntime( { :name => 'test', :type => 'service', :host => 'icinga2-master', :comment => 'test downtime', :author => 'icingaadmin', :start_time => Time.now.to_i, :end_time => Time.now.to_i + 20 } )
134
+ puts "list all Downtimes:"
135
+ puts i.listDowntimes()
136
+
137
+ puts "list all Notifications:"
138
+ puts i.listNotifications()
139
+
140
+ puts i.enableHostNotification( 'icinga2-master' )
141
+ puts i.disableHostNotification( 'icinga2-master' )
142
+
143
+ puts i.disableServiceNotification( 'icinga2-master' )
144
+
145
+
146
+ end
147
+
148
+ # -----------------------------------------------------------------------------
149
+
150
+ # EOF