zabbix-action-status 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5276c42456588094e6702d1ce57ac1a6fc216037
4
+ data.tar.gz: b03d34bbbb7d905e6c1b2d30c23d26333e66ba60
5
+ SHA512:
6
+ metadata.gz: 10f62de84d4f40bf9fda017827dcfb3c2be6cc55e8a88d4c53a2a8d2a57a89a2e6e97ae036f6143f70a9c9975e4399d14712eafa3b3c181f14fad4ce33e3f055
7
+ data.tar.gz: da4d2dc614249f5ed83cd175a6a07fefc5666ebf97053f61da127e7d73e7d659f2c68d51f407162667fa8dd111c6017e80723bb01813802f8482d59650602d3f
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /vendor/bundle
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in zabbix-action-status.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,32 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ zabbix-action-status (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.2.5)
10
+ rake (10.4.2)
11
+ rspec (3.2.0)
12
+ rspec-core (~> 3.2.0)
13
+ rspec-expectations (~> 3.2.0)
14
+ rspec-mocks (~> 3.2.0)
15
+ rspec-core (3.2.0)
16
+ rspec-support (~> 3.2.0)
17
+ rspec-expectations (3.2.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.2.0)
20
+ rspec-mocks (3.2.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.2.0)
23
+ rspec-support (3.2.1)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 1.8)
30
+ rake (~> 10.0)
31
+ rspec
32
+ zabbix-action-status!
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 feedforce Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all 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,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # Zabbix::Action::Status
2
+
3
+ Toggle Zabbix Actions.
4
+
5
+ This gem is checked on Zabbix version 2.0.14.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'zabbix-action-status'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install zabbix-action-status
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ # 4 and 8 are action_ids
27
+ zabbix_action_status = Zabbix::Action::Status.new('http://example.com/api_jsonrpc.php', %w(4 8))
28
+
29
+ zabbix_action_status.disable
30
+ zabbix_action_status.enable
31
+ ```
32
+
33
+ ## Development
34
+
35
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
36
+
37
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
38
+
39
+ ## Contributing
40
+
41
+ 1. Fork it ( https://github.com/[my-github-username]/zabbix-action-status/fork )
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "zabbix/action/status"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,192 @@
1
+ require "zabbix/action/status/version"
2
+ require 'net/https'
3
+ require 'json'
4
+
5
+ class Zabbix
6
+ class Action
7
+ class Status
8
+ def initialize(zabbix_api_url, zabbix_action_ids)
9
+ @api_uri = URI.parse(zabbix_api_url)
10
+ @action_ids = zabbix_action_ids
11
+ end
12
+
13
+ # zabbixの通知をoffにする
14
+ def disable
15
+ auth_key = send_auth_request()
16
+ action_status_hash = get_target_actions_status(auth_key, @action_ids)
17
+ # ステータスを変える
18
+ @action_ids.each do |id|
19
+ result = update_zabbix_action_disable_status(auth_key, id) if action_status_hash[id] == "0"
20
+ check_error_json_response(result)
21
+ end
22
+
23
+ # 最終チェック
24
+ result_status = get_target_actions_status(auth_key, @action_ids)
25
+ @action_ids.each do |id|
26
+ raise 'ERROR:zabbix status error'+result_status.to_s if result_status[id] == "0"
27
+ end
28
+ end
29
+
30
+ # zabbixの通知をonにする
31
+ def enable
32
+ auth_key = send_auth_request()
33
+ action_status_hash = get_target_actions_status(auth_key, @action_ids)
34
+ # ステータスを変える
35
+ @action_ids.each do |id|
36
+ result = update_zabbix_action_enable_status(auth_key, id) if action_status_hash[id] == "1"
37
+ check_error_json_response(result)
38
+ end
39
+
40
+ # 最終チェック
41
+ result_status = get_target_actions_status(auth_key, @action_ids)
42
+ @action_ids.each do |id|
43
+ raise 'ERROR:zabbix status error' + result_status.to_s if result_status[id] == "1"
44
+ end
45
+ end
46
+
47
+ private
48
+
49
+ # 認証キーを取得する
50
+ def send_auth_request
51
+ authenticate_request = create_post_request(get_zabbix_authenticate_request_json)
52
+ authenticate_response = send_reqest(authenticate_request)
53
+ authenticatedhash = parse_response_hash(authenticate_response)
54
+ authenticatedhash['result']
55
+ end
56
+
57
+ # 対象actionIdのステータスをhashで取得する
58
+ def get_target_actions_status(auth_key, target_action_ids)
59
+ request_body = get_zabbix_status_check_request_json(auth_key, target_action_ids)
60
+ check_status_request = create_post_request(request_body)
61
+ check_status_response = send_reqest(check_status_request)
62
+ status_hash = parse_response_hash(check_status_response)
63
+
64
+ result_action_status_hash = Hash.new
65
+ status_hash['result'].each do |json|
66
+ result_action_status_hash.store(json['actionid'],json['status'] )
67
+ end
68
+ result_action_status_hash
69
+ end
70
+
71
+ # zabbixに対してauth認証用をリクエストするjsonを取得する
72
+ def get_zabbix_authenticate_request_json
73
+ {
74
+ jsonrpc: '2.0',
75
+ method: 'user.login',
76
+ params: {
77
+ user: 'admin',
78
+ password: 'passzabbixpass'
79
+ },
80
+ id: 1
81
+ }.to_json
82
+ end
83
+
84
+ # zabbixに対して対象のactionIdのステータスを取得するjsonを取得する
85
+ def get_zabbix_status_check_request_json(auth_str, target_action_ids)
86
+ {
87
+ auth: auth_str,
88
+ jsonrpc:"2.0",
89
+ method:"action.get",
90
+ params:{
91
+ output: "extend",
92
+ filter: {
93
+ actionid:target_action_ids
94
+ }
95
+ },
96
+ id: 1
97
+ }.to_json
98
+ end
99
+
100
+ # postリクエストを作成します
101
+ def create_post_request(body_desc)
102
+ request = Net::HTTP::Post.new(@api_uri.request_uri, initheader = {'Content-Type' =>'application/json'})
103
+ request.body = body_desc
104
+ request
105
+ end
106
+
107
+ # requestを元にリクエストしてresponse を受け取る
108
+ def send_reqest(request)
109
+ response = nil
110
+ http = create_http_connection
111
+ http.start do |h|
112
+ response = h.request(request)
113
+ end
114
+
115
+ case response
116
+ when Net::HTTPSuccess
117
+ response
118
+ else
119
+ raise [uri.to_s, response.value].join(' : ').to_s
120
+ end
121
+ end
122
+
123
+ # httpのコネクションを作成する
124
+ def create_http_connection
125
+ http = Net::HTTP.new(@api_uri.host, @api_uri.port)
126
+ http.use_ssl = true
127
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
128
+ # debug 用
129
+ # http.set_debug_output $stderr
130
+ http
131
+ end
132
+
133
+ # response をパースして hashオブジェクトに変換
134
+ def parse_response_hash(response)
135
+ responseParsedJson = JSON.parser.new(response.body)
136
+ responseParsedJson.parse
137
+ end
138
+
139
+ # 返ってきたレスポンスのJSONの中身がエラーかどうか
140
+ def check_error_json_response(response)
141
+ if response
142
+ status_hash = parse_response_hash(response)
143
+ if status_hash['error']
144
+ raise status_hash['error'].to_s
145
+ end
146
+ end
147
+ end
148
+
149
+ # 対象のacitonIdの通知ステータスをOFFにする
150
+ def update_zabbix_action_disable_status(auth_key, target_action_id)
151
+ request_body = get_zabbix_action_status_disable_json(auth_key, target_action_id)
152
+ authenticate_request = create_post_request(request_body)
153
+ send_reqest(authenticate_request)
154
+ end
155
+
156
+ # 対象のacitonIdの通知ステータスをONにする
157
+ def update_zabbix_action_enable_status(auth_key, target_action_id)
158
+ request_body = get_zabbix_action_status_enable_json(auth_key, target_action_id)
159
+ authenticate_request = create_post_request(request_body)
160
+ send_reqest(authenticate_request)
161
+ end
162
+
163
+ # zabbixに対して通知をOFFにするJsonを取得する
164
+ def get_zabbix_action_status_disable_json(auth_key, target_action_id)
165
+ {
166
+ auth: auth_key,
167
+ jsonrpc:"2.0",
168
+ method:"action.update",
169
+ params:{
170
+ actionid:target_action_id,
171
+ status:1
172
+ },
173
+ id: 1
174
+ }.to_json
175
+ end
176
+
177
+ # zabbixに対して通知をONにするJsonを取得する
178
+ def get_zabbix_action_status_enable_json(auth_key, target_action_id)
179
+ {
180
+ auth: auth_key,
181
+ jsonrpc:"2.0",
182
+ method:"action.update",
183
+ params:{
184
+ actionid:target_action_id,
185
+ status:0
186
+ },
187
+ id: 1
188
+ }.to_json
189
+ end
190
+ end
191
+ end
192
+ end
@@ -0,0 +1,7 @@
1
+ class Zabbix
2
+ class Action
3
+ class Status
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'zabbix/action/status/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "zabbix-action-status"
8
+ spec.version = Zabbix::Action::Status::VERSION
9
+ spec.authors = ["Shota Miyamoto", "Takashi Masuda"]
10
+ spec.email = ["miyamoto@feedforce.jp", "masutaka@feedforce.jp"]
11
+
12
+ spec.summary = %q{Toggle zabbix actions.}
13
+ spec.description = %q{You can toggle zabbix actions}
14
+ spec.homepage = "https://github.com/feedforce/zabbix-action-status"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.8"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zabbix-action-status
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Shota Miyamoto
8
+ - Takashi Masuda
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-02-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.8'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.8'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ description: You can toggle zabbix actions
57
+ email:
58
+ - miyamoto@feedforce.jp
59
+ - masutaka@feedforce.jp
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - ".travis.yml"
67
+ - CODE_OF_CONDUCT.md
68
+ - Gemfile
69
+ - Gemfile.lock
70
+ - LICENSE.txt
71
+ - README.md
72
+ - Rakefile
73
+ - bin/console
74
+ - bin/setup
75
+ - lib/zabbix/action/status.rb
76
+ - lib/zabbix/action/status/version.rb
77
+ - zabbix-action-status.gemspec
78
+ homepage: https://github.com/feedforce/zabbix-action-status
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.4.5
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Toggle zabbix actions.
102
+ test_files: []