zabbixapi_mgx 5.0.0.pre.alpha1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +58 -0
- data/LICENSE.md +20 -0
- data/README.md +98 -0
- data/lib/zabbixapi/basic/basic_alias.rb +37 -0
- data/lib/zabbixapi/basic/basic_func.rb +103 -0
- data/lib/zabbixapi/basic/basic_init.rb +46 -0
- data/lib/zabbixapi/basic/basic_logic.rb +181 -0
- data/lib/zabbixapi/classes/actions.rb +41 -0
- data/lib/zabbixapi/classes/applications.rb +43 -0
- data/lib/zabbixapi/classes/configurations.rb +42 -0
- data/lib/zabbixapi/classes/drules.rb +55 -0
- data/lib/zabbixapi/classes/errors.rb +28 -0
- data/lib/zabbixapi/classes/events.rb +17 -0
- data/lib/zabbixapi/classes/graphs.rb +111 -0
- data/lib/zabbixapi/classes/hostgroups.rb +24 -0
- data/lib/zabbixapi/classes/hosts.rb +80 -0
- data/lib/zabbixapi/classes/httptests.rb +54 -0
- data/lib/zabbixapi/classes/items.rb +83 -0
- data/lib/zabbixapi/classes/maintenance.rb +17 -0
- data/lib/zabbixapi/classes/mediatypes.rb +109 -0
- data/lib/zabbixapi/classes/problems.rb +101 -0
- data/lib/zabbixapi/classes/proxies.rb +48 -0
- data/lib/zabbixapi/classes/roles.rb +114 -0
- data/lib/zabbixapi/classes/screens.rb +94 -0
- data/lib/zabbixapi/classes/scripts.rb +35 -0
- data/lib/zabbixapi/classes/server.rb +16 -0
- data/lib/zabbixapi/classes/templates.rb +106 -0
- data/lib/zabbixapi/classes/triggers.rb +105 -0
- data/lib/zabbixapi/classes/unusable.rb +8 -0
- data/lib/zabbixapi/classes/usergroups.rb +73 -0
- data/lib/zabbixapi/classes/usermacros.rb +228 -0
- data/lib/zabbixapi/classes/users.rb +64 -0
- data/lib/zabbixapi/classes/valuemaps.rb +50 -0
- data/lib/zabbixapi/client.rb +167 -0
- data/lib/zabbixapi/version.rb +3 -0
- data/lib/zabbixapi.rb +194 -0
- data/zabbixapi.gemspec +29 -0
- metadata +124 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f437f7bf85358d0f384168dde81bcd827feba26256ee92b06dd6e8255210ab9b
|
4
|
+
data.tar.gz: d17b09bba91cfa99ea5a0084ac7a97640ebceb53dd560d4f39fd2a1e10857b6a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ef66203b442560d4eb89452ed0740c2f4153c679a91d369ee306224d6f627a103c30727dbf1793e5537c9b2f920cd8696fbfcc5ead4eb977a5ac2ad6734e5208
|
7
|
+
data.tar.gz: 296d273f3ac1ed420652444f6651237088f58e6116bc8660807cfcf0c558268a8e5cc7832f537e1b56a8024bf9bc80ccf2b5ba3c7fed168f56ceb859a7180d3c
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
#CHANGELOG
|
2
|
+
|
3
|
+
## 4.2.0
|
4
|
+
[PR #102](https://github.com/express42/zabbixapi/pull/102) Add events get method method. Fixed all testing failures. (Thanks @kevin-j-smith)
|
5
|
+
|
6
|
+
## 4.1.2
|
7
|
+
[PR #99](https://github.com/express42/zabbixapi/pull/99) Patch addressing log error function not found
|
8
|
+
|
9
|
+
## 4.1.1
|
10
|
+
|
11
|
+
[PR #97](https://github.com/express42/zabbixapi/pull/97) Bumped version
|
12
|
+
|
13
|
+
[PR #98](https://github.com/express42/zabbixapi/pull/98) Removed strict zabbix versioning error
|
14
|
+
|
15
|
+
## 4.1.0
|
16
|
+
|
17
|
+
[PR #82](https://github.com/express42/zabbixapi/pull/82) Add logout method (Thanks @baurmatt)
|
18
|
+
|
19
|
+
[PR #86](https://github.com/express42/zabbixapi/pull/86) Feature: add support for Discovery Rules (`drule` API) (Thanks @lehn-etracker)
|
20
|
+
|
21
|
+
## 4.0.0
|
22
|
+
|
23
|
+
[PR #90](https://github.com/express42/zabbixapi/pull/90) Add 4.0 support (Thanks @svdasein)
|
24
|
+
|
25
|
+
[PR #85](https://github.com/express42/zabbixapi/pull/85) Add 3.4 support (Thanks @ottok)
|
26
|
+
|
27
|
+
## 3.2.0
|
28
|
+
|
29
|
+
[PR #77](https://github.com/express42/zabbixapi/pull/77) Add support for Valuemap (Thanks @julienlevasseur)
|
30
|
+
|
31
|
+
[PR #80](https://github.com/express42/zabbixapi/pull/80) Hide password then raising error (Thanks @bbaugher)
|
32
|
+
|
33
|
+
## 3.1.0
|
34
|
+
|
35
|
+
[PR #68](https://github.com/express42/zabbixapi/pull/68) Add support for scripts (Thanx @jrbeilke)
|
36
|
+
|
37
|
+
## 2.4.9
|
38
|
+
|
39
|
+
[PR #55](https://github.com/express42/zabbixapi/pull/55) Add 3.2.x support (Thanx @jrbeilke)
|
40
|
+
|
41
|
+
[PR #54](https://github.com/express42/zabbixapi/pull/54) Add web scenarios support (Thanx @jrbeilke)
|
42
|
+
|
43
|
+
|
44
|
+
## 2.4.7
|
45
|
+
|
46
|
+
[PR #45](https://github.com/express42/zabbixapi/pull/45) Add support for zabbix 3.0.x (Thanx @wandenberg)
|
47
|
+
|
48
|
+
## 2.4.6
|
49
|
+
|
50
|
+
[PR #39](https://github.com/express42/zabbixapi/issues/39) Add support for maintenance API
|
51
|
+
|
52
|
+
## 2.4.5
|
53
|
+
|
54
|
+
[PR #33](https://github.com/express42/zabbixapi/issues/33) Typed exceptions
|
55
|
+
|
56
|
+
## 2.4.4
|
57
|
+
|
58
|
+
[Issue #32](https://github.com/express42/zabbixapi/issues/32): Force update of objects in zabbix
|
data/LICENSE.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015-2018 Express 42
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
# Ruby Zabbix Api Module
|
2
|
+
|
3
|
+
[![Gem Version](http://img.shields.io/gem/v/zabbixapi.svg)][gem]
|
4
|
+
[![Build Status](https://github.com/anapsix/zabbixapi/workflows/CI/badge.svg)][github-ci]
|
5
|
+
|
6
|
+
[gem]: https://rubygems.org/gems/zabbixapi
|
7
|
+
[github-ci]: https://github.com/express42/zabbixapi/actions?query=workflow%3ACI
|
8
|
+
|
9
|
+
Simple and lightweight ruby module for working with [Zabbix][Zabbix] via the [Zabbix API][Zabbix API]
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
```sh
|
13
|
+
# latest
|
14
|
+
gem install zabbixapi
|
15
|
+
|
16
|
+
# specific version
|
17
|
+
gem install zabbixapi -v 4.2.0
|
18
|
+
```
|
19
|
+
|
20
|
+
## Documentation
|
21
|
+
[http://rdoc.info/gems/zabbixapi][documentation]
|
22
|
+
|
23
|
+
[documentation]: http://rdoc.info/gems/zabbixapi
|
24
|
+
|
25
|
+
## Examples
|
26
|
+
[https://github.com/express42/zabbixapi/tree/master/examples][examples]
|
27
|
+
|
28
|
+
[examples]: https://github.com/express42/zabbixapi/tree/master/examples
|
29
|
+
|
30
|
+
## Version Policy
|
31
|
+
|
32
|
+
**NOTE:** `master` branch is used for ongoing development on Zabbix API 5.x (5.0 and 5.2).
|
33
|
+
|
34
|
+
We support only two last versions of zabbix (5.0 and 5.2), so you should consider all previous versions deprecated.
|
35
|
+
|
36
|
+
* Zabbix 1.8.2 (api version 1.2) | zabbixapi 0.6.x | [branch zabbix1.8](https://github.com/express42/zabbixapi/tree/zabbix1.8)
|
37
|
+
* Zabbix 1.8.9 (api version 1.3) | zabbixapi 0.6.x | [branch zabbix1.8](https://github.com/express42/zabbixapi/tree/zabbix1.8)
|
38
|
+
* Zabbix 2.0.x (api version 1.4 -> 2.0.10) | zabbixapi 2.0.x | [branch zabbix2.0](https://github.com/express42/zabbixapi/tree/zabbix2.0)
|
39
|
+
* Zabbix 2.2.x (api version 2.2.x) | zabbixapi 2.2.x | [branch zabbix2.2](https://github.com/express42/zabbixapi/tree/zabbix2.2)
|
40
|
+
* Zabbix 2.4.x (api version 2.2.x) | zabbixapi 2.4.x | [branch zabbix2.4](https://github.com/express42/zabbixapi/tree/zabbix2.4)
|
41
|
+
* Zabbix 3.0.x (api version 3.0.x) | zabbixapi 3.0.x | [branch zabbix3.0](https://github.com/express42/zabbixapi/tree/zabbix3.0)
|
42
|
+
* Zabbix 3.2.x (api version 3.2.x) | zabbixapi 3.2.x | [branch zabbix3.2](https://github.com/express42/zabbixapi/tree/zabbix3.2)
|
43
|
+
* Zabbix 4.0.x (api version 4.0.x) | zabbixapi 4.1.x | [branch zabbix4.0](https://github.com/express42/zabbixapi/tree/zabbix4.0)
|
44
|
+
* Zabbix 4.2.x (api version 4.2.x) | zabbixapi 4.1.x | [branch zabbix4.0](https://github.com/express42/zabbixapi/tree/zabbix4.0)
|
45
|
+
* Zabbix 4.4.x (api version 4.4.x) | zabbixapi 4.2.x | [branch zabbix4.2](https://github.com/express42/zabbixapi/tree/zabbix4.2)
|
46
|
+
|
47
|
+
## Supported Ruby Versions
|
48
|
+
This library aims to support and is [tested against][github-ci] the following Ruby
|
49
|
+
versions:
|
50
|
+
|
51
|
+
* Ruby 2.5
|
52
|
+
* Ruby 2.6
|
53
|
+
* Ruby 2.7
|
54
|
+
* JRuby 9.2.10.0
|
55
|
+
|
56
|
+
If something doesn't work on one of these versions, it's a bug.
|
57
|
+
|
58
|
+
This library may inadvertently work (or seem to work) on other Ruby versions,
|
59
|
+
however support will only be provided for the versions listed above.
|
60
|
+
|
61
|
+
If you would like this library to support another Ruby version or
|
62
|
+
implementation, you may volunteer to be a maintainer. Being a maintainer
|
63
|
+
entails making sure all tests run and pass on that implementation. When
|
64
|
+
something breaks on your implementation, you will be responsible for providing
|
65
|
+
patches in a timely fashion. If critical issues for a particular implementation
|
66
|
+
exist at the time of a major release, support for that Ruby version may be
|
67
|
+
dropped.
|
68
|
+
|
69
|
+
## Dependencies
|
70
|
+
|
71
|
+
* net/http
|
72
|
+
* json
|
73
|
+
|
74
|
+
## Contributing
|
75
|
+
|
76
|
+
* Fork the project.
|
77
|
+
* Base your work on the master branch.
|
78
|
+
* Make your feature addition or bug fix, write tests, write documentation/examples.
|
79
|
+
* Commit, do not mess with rakefile, version.
|
80
|
+
* Make a pull request.
|
81
|
+
|
82
|
+
## Zabbix documentation
|
83
|
+
|
84
|
+
* [Zabbix Project Homepage][Zabbix]
|
85
|
+
* [Zabbix API docs][Zabbix API]
|
86
|
+
|
87
|
+
[Zabbix]: https://www.zabbix.com
|
88
|
+
[Zabbix API]: https://www.zabbix.com/documentation/5.2/manual/api
|
89
|
+
|
90
|
+
## Copyright
|
91
|
+
|
92
|
+
- Copyright (c) 2021 [contributors]
|
93
|
+
- Copyright (c) 2015-2018 Express 42 and [contributors]
|
94
|
+
|
95
|
+
See [LICENSE] for details.
|
96
|
+
|
97
|
+
[LICENSE]: LICENSE.md
|
98
|
+
[contributors]: https://github.com/express42/zabbixapi/graphs/contributors
|
@@ -0,0 +1,37 @@
|
|
1
|
+
class ZabbixApi
|
2
|
+
class Basic
|
3
|
+
# Get Zabbix object data from API by id
|
4
|
+
#
|
5
|
+
# @param data [Hash] Should include object's id field name (identify) and id value
|
6
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
7
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
8
|
+
# @return [Hash]
|
9
|
+
def get(data)
|
10
|
+
get_full_data(data)
|
11
|
+
end
|
12
|
+
|
13
|
+
# Add new Zabbix object using API create
|
14
|
+
#
|
15
|
+
# @param data [Hash]
|
16
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
17
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
18
|
+
# @return [Integer] The object id if a single object is created
|
19
|
+
# @return [Boolean] True/False if multiple objects are created
|
20
|
+
def add(data)
|
21
|
+
create(data)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Destroy Zabbix object using API delete
|
25
|
+
#
|
26
|
+
# @param data [Hash] Should include object's id field name (identify) and id value
|
27
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
28
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
29
|
+
# @return [Integer] The object id if a single object is deleted
|
30
|
+
# @return [Boolean] True/False if multiple objects are deleted
|
31
|
+
def destroy(data)
|
32
|
+
delete(data)
|
33
|
+
end
|
34
|
+
|
35
|
+
def method_name; end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
class ZabbixApi
|
2
|
+
class Basic
|
3
|
+
# Log messages to stdout when debugging
|
4
|
+
#
|
5
|
+
# @param message [String]
|
6
|
+
def log(message)
|
7
|
+
puts message.to_s if @client.options[:debug]
|
8
|
+
end
|
9
|
+
|
10
|
+
# Compare two hashes for equality
|
11
|
+
#
|
12
|
+
# @param first_hash [Hash]
|
13
|
+
# @param second_hash [Hash]
|
14
|
+
# @return [Boolean]
|
15
|
+
def hash_equals?(first_hash, second_hash)
|
16
|
+
normalized_first_hash = normalize_hash(first_hash)
|
17
|
+
normalized_second_hash = normalize_hash(second_hash)
|
18
|
+
|
19
|
+
hash1 = normalized_first_hash.merge(normalized_second_hash)
|
20
|
+
hash2 = normalized_second_hash.merge(normalized_first_hash)
|
21
|
+
hash1 == hash2
|
22
|
+
end
|
23
|
+
|
24
|
+
# Convert all hash/array keys to symbols
|
25
|
+
#
|
26
|
+
# @param object [Array, Hash]
|
27
|
+
# @return [Array, Hash]
|
28
|
+
def symbolize_keys(object)
|
29
|
+
if object.is_a?(Array)
|
30
|
+
object.each_with_index do |val, index|
|
31
|
+
object[index] = symbolize_keys(val)
|
32
|
+
end
|
33
|
+
elsif object.is_a?(Hash)
|
34
|
+
object.keys.each do |key|
|
35
|
+
object[key.to_sym] = symbolize_keys(object.delete(key))
|
36
|
+
end
|
37
|
+
end
|
38
|
+
object
|
39
|
+
end
|
40
|
+
|
41
|
+
# Normalize all hash values to strings
|
42
|
+
#
|
43
|
+
# @param hash [Hash]
|
44
|
+
# @return [Hash]
|
45
|
+
def normalize_hash(hash)
|
46
|
+
result = hash.dup
|
47
|
+
|
48
|
+
result.delete(:hostid) # TODO: remove to logig. TemplateID and HostID has different id
|
49
|
+
|
50
|
+
result.each do |key, value|
|
51
|
+
result[key] = value.is_a?(Array) ? normalize_array(value) : value.to_s
|
52
|
+
end
|
53
|
+
|
54
|
+
result
|
55
|
+
end
|
56
|
+
|
57
|
+
# Normalize all array values to strings
|
58
|
+
#
|
59
|
+
# @param array [Array]
|
60
|
+
# @return [Array]
|
61
|
+
def normalize_array(array)
|
62
|
+
result = []
|
63
|
+
|
64
|
+
array.each do |e|
|
65
|
+
if e.is_a?(Array)
|
66
|
+
result.push(normalize_array(e))
|
67
|
+
elsif e.is_a?(Hash)
|
68
|
+
result.push(normalize_hash(e))
|
69
|
+
else
|
70
|
+
result.push(e.to_s)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
result
|
75
|
+
end
|
76
|
+
|
77
|
+
# Parse a data hash for id key or boolean to return
|
78
|
+
#
|
79
|
+
# @param data [Hash]
|
80
|
+
# @return [Integer] The object id if a single object hash is provided with key
|
81
|
+
# @return [Boolean] True/False if multiple class object hash is provided
|
82
|
+
def parse_keys(data)
|
83
|
+
case data
|
84
|
+
when Hash
|
85
|
+
data.empty? ? nil : data[keys][0].to_i
|
86
|
+
when TrueClass
|
87
|
+
true
|
88
|
+
when FalseClass
|
89
|
+
false
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
# Merge two hashes into a single new hash
|
94
|
+
#
|
95
|
+
# @param first_hash [Hash]
|
96
|
+
# @param second_hash [Hash]
|
97
|
+
# @return [Hash]
|
98
|
+
def merge_params(first_hash, second_hash)
|
99
|
+
new = first_hash.dup
|
100
|
+
new.merge(second_hash)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
class ZabbixApi
|
2
|
+
class Basic
|
3
|
+
# Initializes a new Basic object with ZabbixApi Client
|
4
|
+
#
|
5
|
+
# @param client [ZabbixApi::Client]
|
6
|
+
# @return [ZabbixApi::Client]
|
7
|
+
def initialize(client)
|
8
|
+
@client = client
|
9
|
+
end
|
10
|
+
|
11
|
+
# Placeholder for inherited objects to provide object-specific method name
|
12
|
+
#
|
13
|
+
# @raise [ApiError] Basic object does not directly support method_name
|
14
|
+
def method_name
|
15
|
+
raise ApiError.new("Can't call method_name here")
|
16
|
+
end
|
17
|
+
|
18
|
+
# Placeholder for inherited objects to provide default options
|
19
|
+
#
|
20
|
+
# @return [Hash]
|
21
|
+
def default_options
|
22
|
+
{}
|
23
|
+
end
|
24
|
+
|
25
|
+
# Returns the object's plural id field name (identify) based on key
|
26
|
+
#
|
27
|
+
# @return [String]
|
28
|
+
def keys
|
29
|
+
key + 's'
|
30
|
+
end
|
31
|
+
|
32
|
+
# Returns the object's id field name (identify) based on method_name + id
|
33
|
+
#
|
34
|
+
# @return [String]
|
35
|
+
def key
|
36
|
+
method_name + 'id'
|
37
|
+
end
|
38
|
+
|
39
|
+
# Placeholder for inherited objects to provide object-specific id field name
|
40
|
+
#
|
41
|
+
# @raise [ApiError] Basic object does not directly support identify
|
42
|
+
def identify
|
43
|
+
raise ApiError.new("Can't call identify here")
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,181 @@
|
|
1
|
+
class ZabbixApi
|
2
|
+
class Basic
|
3
|
+
# Create new Zabbix object using API (with defaults)
|
4
|
+
#
|
5
|
+
# @param data [Hash]
|
6
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
7
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
8
|
+
# @return [Integer] The object id if a single object is created
|
9
|
+
# @return [Boolean] True/False if multiple objects are created
|
10
|
+
def create(data)
|
11
|
+
log "[DEBUG] Call create with parameters: #{data.inspect}"
|
12
|
+
|
13
|
+
data_with_default = default_options.empty? ? data : merge_params(default_options, data)
|
14
|
+
data_create = [data_with_default]
|
15
|
+
result = @client.api_request(method: "#{method_name}.create", params: data_create)
|
16
|
+
parse_keys result
|
17
|
+
end
|
18
|
+
|
19
|
+
# Delete Zabbix object using API
|
20
|
+
#
|
21
|
+
# @param data [Hash] Should include object's id field name (identify) and id value
|
22
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
23
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
24
|
+
# @return [Integer] The object id if a single object is deleted
|
25
|
+
# @return [Boolean] True/False if multiple objects are deleted
|
26
|
+
def delete(data)
|
27
|
+
log "[DEBUG] Call delete with parameters: #{data.inspect}"
|
28
|
+
|
29
|
+
data_delete = [data]
|
30
|
+
result = @client.api_request(method: "#{method_name}.delete", params: data_delete)
|
31
|
+
parse_keys result
|
32
|
+
end
|
33
|
+
|
34
|
+
# Create or update Zabbix object using API
|
35
|
+
#
|
36
|
+
# @param data [Hash] Should include object's id field name (identify) and id value
|
37
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
38
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
39
|
+
# @return [Integer] The object id if a single object is created
|
40
|
+
# @return [Boolean] True/False if multiple objects are created
|
41
|
+
def create_or_update(data)
|
42
|
+
log "[DEBUG] Call create_or_update with parameters: #{data.inspect}"
|
43
|
+
|
44
|
+
id = get_id(identify.to_sym => data[identify.to_sym])
|
45
|
+
id ? update(data.merge(key.to_sym => id.to_s)) : create(data)
|
46
|
+
end
|
47
|
+
|
48
|
+
# Update Zabbix object using API
|
49
|
+
#
|
50
|
+
# @param data [Hash] Should include object's id field name (identify) and id value
|
51
|
+
# @param force [Boolean] Whether to force an object update even if provided data matches Zabbix
|
52
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
53
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
54
|
+
# @return [Integer] The object id if a single object is created
|
55
|
+
# @return [Boolean] True/False if multiple objects are created
|
56
|
+
def update(data, force = false)
|
57
|
+
log "[DEBUG] Call update with parameters: #{data.inspect}"
|
58
|
+
dump = {}
|
59
|
+
dump_by_id(key.to_sym => data[key.to_sym]).each do |item|
|
60
|
+
dump = symbolize_keys(item) if item[key].to_i == data[key.to_sym].to_i
|
61
|
+
end
|
62
|
+
if hash_equals?(dump, data) && !force
|
63
|
+
log "[DEBUG] Equal keys #{dump} and #{data}, skip update"
|
64
|
+
data[key.to_sym].to_i
|
65
|
+
else
|
66
|
+
data_update = [data]
|
67
|
+
result = @client.api_request(method: "#{method_name}.update", params: data_update)
|
68
|
+
parse_keys result
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
# Get full/extended Zabbix object data from API
|
73
|
+
#
|
74
|
+
# @param data [Hash] Should include object's id field name (identify) and id value
|
75
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
76
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
77
|
+
# @return [Hash]
|
78
|
+
def get_full_data(data)
|
79
|
+
log "[DEBUG] Call get_full_data with parameters: #{data.inspect}"
|
80
|
+
|
81
|
+
@client.api_request(
|
82
|
+
method: "#{method_name}.get",
|
83
|
+
params: {
|
84
|
+
filter: {
|
85
|
+
identify.to_sym => data[identify.to_sym]
|
86
|
+
},
|
87
|
+
output: 'extend'
|
88
|
+
}
|
89
|
+
)
|
90
|
+
end
|
91
|
+
|
92
|
+
# Get raw Zabbix object data from API
|
93
|
+
#
|
94
|
+
# @param data [Hash]
|
95
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
96
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
97
|
+
# @return [Hash]
|
98
|
+
def get_raw(data)
|
99
|
+
log "[DEBUG] Call get_raw with parameters: #{data.inspect}"
|
100
|
+
|
101
|
+
@client.api_request(
|
102
|
+
method: "#{method_name}.get",
|
103
|
+
params: data
|
104
|
+
)
|
105
|
+
end
|
106
|
+
|
107
|
+
# Dump Zabbix object data by key from API
|
108
|
+
#
|
109
|
+
# @param data [Hash] Should include desired object's key and value
|
110
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
111
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
112
|
+
# @return [Hash]
|
113
|
+
def dump_by_id(data)
|
114
|
+
log "[DEBUG] Call dump_by_id with parameters: #{data.inspect}"
|
115
|
+
|
116
|
+
@client.api_request(
|
117
|
+
method: "#{method_name}.get",
|
118
|
+
params: {
|
119
|
+
filter: {
|
120
|
+
key.to_sym => data[key.to_sym]
|
121
|
+
},
|
122
|
+
output: 'extend'
|
123
|
+
}
|
124
|
+
)
|
125
|
+
end
|
126
|
+
|
127
|
+
# Get full/extended Zabbix data for all objects of type/class from API
|
128
|
+
#
|
129
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
130
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
131
|
+
# @return [Array<Hash>] Array of matching objects
|
132
|
+
def all
|
133
|
+
result = {}
|
134
|
+
@client.api_request(method: "#{method_name}.get", params: { output: 'extend' }).each do |item|
|
135
|
+
result[item[identify]] = item[key]
|
136
|
+
end
|
137
|
+
result
|
138
|
+
end
|
139
|
+
|
140
|
+
# Get Zabbix object id from API based on provided data
|
141
|
+
#
|
142
|
+
# @param data [Hash]
|
143
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call or missing object's id field name (identify).
|
144
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
145
|
+
# @return [Integer] Zabbix object id
|
146
|
+
def get_id(data)
|
147
|
+
log "[DEBUG] Call get_id with parameters: #{data.inspect}"
|
148
|
+
# symbolize keys if the user used string keys instead of symbols
|
149
|
+
data = symbolize_keys(data) if data.key?(identify)
|
150
|
+
# raise an error if identify name was not supplied
|
151
|
+
name = data[identify.to_sym]
|
152
|
+
raise ApiError.new("#{identify} not supplied in call to get_id") if name.nil?
|
153
|
+
|
154
|
+
result = @client.api_request(
|
155
|
+
method: "#{method_name}.get",
|
156
|
+
params: {
|
157
|
+
filter: data,
|
158
|
+
output: [key, identify]
|
159
|
+
}
|
160
|
+
)
|
161
|
+
id = nil
|
162
|
+
result.each { |item| id = item[key].to_i if item[identify] == data[identify.to_sym] }
|
163
|
+
id
|
164
|
+
end
|
165
|
+
|
166
|
+
# Get or Create Zabbix object using API
|
167
|
+
#
|
168
|
+
# @param data [Hash] Should include object's id field name (identify) and id value
|
169
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
170
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
171
|
+
# @return [Integer] Zabbix object id
|
172
|
+
def get_or_create(data)
|
173
|
+
log "[DEBUG] Call get_or_create with parameters: #{data.inspect}"
|
174
|
+
|
175
|
+
unless (id = get_id(identify.to_sym => data[identify.to_sym]))
|
176
|
+
id = create(data)
|
177
|
+
end
|
178
|
+
id
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
class ZabbixApi
|
2
|
+
class Actions < Basic
|
3
|
+
# The method name used for interacting with Actions via Zabbix API
|
4
|
+
#
|
5
|
+
# @return [String]
|
6
|
+
def method_name
|
7
|
+
'action'
|
8
|
+
end
|
9
|
+
|
10
|
+
# The id field name used for identifying specific Action objects via Zabbix API
|
11
|
+
#
|
12
|
+
# @return [String]
|
13
|
+
def identify
|
14
|
+
'name'
|
15
|
+
end
|
16
|
+
|
17
|
+
# Get full/extended Action object data from API
|
18
|
+
#
|
19
|
+
# @param data [Hash] Should include object's id field name (identify) and id value
|
20
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
21
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
22
|
+
# @return [Hash]
|
23
|
+
def get_full_data(data)
|
24
|
+
log "[DEBUG] Call get_full_data with parameters: #{data.inspect}"
|
25
|
+
|
26
|
+
@client.api_request(
|
27
|
+
method: "#{method_name}.get",
|
28
|
+
params: {
|
29
|
+
filter: {
|
30
|
+
identify.to_sym => data[identify.to_sym]
|
31
|
+
},
|
32
|
+
output: 'extend',
|
33
|
+
selectOperations: "extend",
|
34
|
+
selectRecoveryOperations: "extend",
|
35
|
+
selectAcknowledgeOperations: "extend",
|
36
|
+
selectFilter: "extend",
|
37
|
+
}
|
38
|
+
)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
class ZabbixApi
|
2
|
+
class Applications < Basic
|
3
|
+
# The method name used for interacting with Applications via Zabbix API
|
4
|
+
#
|
5
|
+
# @return [String]
|
6
|
+
def method_name
|
7
|
+
'application'
|
8
|
+
end
|
9
|
+
|
10
|
+
# The id field name used for identifying specific Application objects via Zabbix API
|
11
|
+
#
|
12
|
+
# @return [String]
|
13
|
+
def identify
|
14
|
+
'name'
|
15
|
+
end
|
16
|
+
|
17
|
+
# Get or Create Application object using Zabbix API
|
18
|
+
#
|
19
|
+
# @param data [Hash] Needs to include name and hostid to properly identify Applications via Zabbix API
|
20
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
21
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
22
|
+
# @return [Integer] Zabbix object id
|
23
|
+
def get_or_create(data)
|
24
|
+
log "[DEBUG] Call get_or_create with parameters: #{data.inspect}"
|
25
|
+
|
26
|
+
unless (id = get_id(name: data[:name], hostid: data[:hostid]))
|
27
|
+
id = create(data)
|
28
|
+
end
|
29
|
+
id
|
30
|
+
end
|
31
|
+
|
32
|
+
# Create or update Application object using Zabbix API
|
33
|
+
#
|
34
|
+
# @param data [Hash] Needs to include name and hostid to properly identify Applications via Zabbix API
|
35
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
36
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
37
|
+
# @return [Integer] Zabbix object id
|
38
|
+
def create_or_update(data)
|
39
|
+
applicationid = get_id(name: data[:name], hostid: data[:hostid])
|
40
|
+
applicationid ? update(data.merge(applicationid: applicationid)) : create(data)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
class ZabbixApi
|
2
|
+
class Configurations < Basic
|
3
|
+
# @return [Boolean]
|
4
|
+
def array_flag
|
5
|
+
true
|
6
|
+
end
|
7
|
+
|
8
|
+
# The method name used for interacting with Configurations via Zabbix API
|
9
|
+
#
|
10
|
+
# @return [String]
|
11
|
+
def method_name
|
12
|
+
'configuration'
|
13
|
+
end
|
14
|
+
|
15
|
+
# The id field name used for identifying specific Configuration objects via Zabbix API
|
16
|
+
#
|
17
|
+
# @return [String]
|
18
|
+
def identify
|
19
|
+
'host'
|
20
|
+
end
|
21
|
+
|
22
|
+
# Export configuration data using Zabbix API
|
23
|
+
#
|
24
|
+
# @param data [Hash]
|
25
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
26
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
27
|
+
# @return [Hash]
|
28
|
+
def export(data)
|
29
|
+
@client.api_request(method: 'configuration.export', params: data)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Import configuration data using Zabbix API
|
33
|
+
#
|
34
|
+
# @param data [Hash]
|
35
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
36
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
37
|
+
# @return [Hash]
|
38
|
+
def import(data)
|
39
|
+
@client.api_request(method: 'configuration.import', params: data)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|