ruzbx 0.0.1

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
+ SHA256:
3
+ metadata.gz: f334640e1a01102b009102285d67532026a71dfbe8be96f328dd52e962892405
4
+ data.tar.gz: 2c151d3919c9461feacbbbcd2da8289d9b7af63115266621008318a2d5b917b0
5
+ SHA512:
6
+ metadata.gz: 00031b55eb0bcc3e5794af35aae407cd51433c5264b3efce65f098d39ff1cea3b3c38b69f9af571b84e8c7662044ea538644f27f823e80a367e36054e3077a5f
7
+ data.tar.gz: 83b579842846f5ee8fb0326aad6ac915cb127a8e49eb29e5dc6a80f695285784d421d4f34b1fbdb28c15bd0879336a2469476e823df127cdc008dd7c66cabb39
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-04-10
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders 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, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at i@itmage.ru. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Andrey Semenov
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,22 @@
1
+ # Ruzbx
2
+
3
+ ## Running
4
+
5
+ RUZBX_DEBUG=true RUZBX_URL=http://localhost:8080 RUZBX_TOKEN=<ZABBIX_ACCESS_TOKEN> bundle exec ./bin/console
6
+
7
+ ## Usage in the code
8
+
9
+ ## Rake tasks
10
+
11
+ rake zabbix:create -- \
12
+ '--name=example' \
13
+ '--templates=Linux by Zabbix agent,Website certificate by Zabbix agent 2' \
14
+ '--ip=127.0.0.1' \
15
+ '--hostgroups=Linux servers'
16
+
17
+ ## Testing
18
+
19
+ ### Run the instance of jira
20
+
21
+ docker compose up -d
22
+ open http://localhost:8080
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ $LOAD_PATH.unshift File.expand_path('lib', __dir__)
7
+ require 'ruzbx'
8
+ require 'ruzbx/tasks/zabbix'
9
+
10
+ task default: %i[test]
11
+
12
+ Rake::TestTask.new do |t|
13
+ t.libs << 'test'
14
+ t.test_files = FileList['test/test*.rb']
15
+ t.verbose = false
16
+ end
17
+
18
+ Ruzbx::Tasks::Jira.new
19
+
20
+ task :version do
21
+ puts Ruzbx::VERSION
22
+ end
data/compose.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ version: '3'
3
+ services:
4
+ jira:
5
+ restart: always
6
+ image: atlassian/jira-software
7
+ environment: {}
8
+ # JVM_SUPPORT_RECOMMENDED_ARGS: "-Datlassian.recovery.password=<PASSWORD>"
9
+ ports:
10
+ - 8080:8080
11
+ volumes:
12
+ - 'data:/var/atlassian/application-data/jira'
13
+
14
+ volumes:
15
+ data:
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ruzbx
4
+ module Api
5
+ # TODO
6
+ # https://www.zabbix.com/documentation/current/en/manual/api/reference/host/object
7
+ class Host < Item
8
+ # rubocop:disable Metrics/MethodLength
9
+ def self.create(opts, &block)
10
+ entity = Entity.build do
11
+ rpc_method 'host.create'
12
+ data params: {
13
+ host: opts[:name],
14
+ interfaces: [{ type: 1, main: 1, useip: 1, ip: opts[:ip],
15
+ dns: '', port: '10050' }],
16
+ groups: opts[:groups],
17
+ tags: [{ tag: 'creator', value: 'bot' }],
18
+ templates: opts[:templates],
19
+ macros: []
20
+ }
21
+ instance_eval(&block) if block_given?
22
+ end
23
+ new(entity.commit)
24
+ end
25
+ # rubocop:enable Metrics/MethodLength
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ruzbx
4
+ module Api
5
+ # TODO
6
+ # https://www.zabbix.com/documentation/current/en/manual/api/reference/hostgroup/object
7
+ class HostGroup < Item
8
+ def self.get(names, &block)
9
+ entity = Entity.build do
10
+ rpc_method 'hostgroup.get'
11
+ data params: { output: 'extend', filter: { name: names } }
12
+ instance_eval(&block) if block_given?
13
+ end
14
+ new(entity.commit)
15
+ end
16
+
17
+ def ids
18
+ @data['result'].map { |i| i['groupid'] } unless @data['result'].empty?
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ruzbx
4
+ module Api
5
+ # TODO
6
+ class Item
7
+ attr_reader :data
8
+
9
+ def initialize(data = nil)
10
+ @data = data
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ruzbx
4
+ module Api
5
+ # TODO
6
+ # https://www.zabbix.com/documentation/current/en/manual/api/reference/user/checkauthentication
7
+ class Myself < Item
8
+ def self.get
9
+ entity = Entity.build do
10
+ no_auth
11
+ rpc_method 'user.checkAuthentication'
12
+ data params: { token: Configuration.token }
13
+ end
14
+ new(entity.commit)
15
+ end
16
+
17
+ def name
18
+ @data
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ruzbx
4
+ module Api
5
+ # TODO
6
+ # https://www.zabbix.com/documentation/current/en/manual/api/reference/apiinfo/version
7
+ class ServerInfo < Item
8
+ def self.get
9
+ entity = Entity.build do
10
+ no_auth
11
+ rpc_method 'apiinfo.version'
12
+ data params: []
13
+ end
14
+ new(entity.commit)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ruzbx
4
+ module Api
5
+ # TODO
6
+ # https://www.zabbix.com/documentation/current/en/manual/api/reference/template/object
7
+ class Template < Item
8
+ def self.get(names, &block)
9
+ entity = Entity.build do
10
+ rpc_method 'template.get'
11
+ data params: { output: 'extend', filter: { host: names } }
12
+ instance_eval(&block) if block_given?
13
+ end
14
+ new(entity.commit)
15
+ end
16
+
17
+ def ids
18
+ @data['result'].map { |i| i['templateid'] } unless @data['result'].empty?
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ruzbx
4
+ # TODO
5
+ module Configuration
6
+ def self.username
7
+ return ENV['RUZBX_USERNAME'] if ENV.include?('RUZBX_USERNAME')
8
+
9
+ raise 'The environment variable RUZBX_USERNAME is not set'
10
+ end
11
+
12
+ def self.password
13
+ return ENV['RUZBX_PASSWORD'] if ENV.include?('RUZBX_PASSWORD')
14
+
15
+ raise 'The environment variable RUZBX_PASSWORD is not set'
16
+ end
17
+
18
+ def self.token
19
+ return ENV['RUZBX_TOKEN'] if ENV.include?('RUZBX_TOKEN')
20
+
21
+ raise 'The environment variable RUZBX_TOKEN is not set'
22
+ end
23
+
24
+ def self.url
25
+ return ENV['RUZBX_URL'] if ENV.include?('RUZBX_URL')
26
+
27
+ 'http://localhost:8080'
28
+ end
29
+
30
+ def self.debug
31
+ return false unless ENV.include?('RUZBX_DEBUG')
32
+
33
+ ENV['RUZBX_DEBUG'].match?(/^true$/)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ruzbx
4
+ # TODO
5
+ class Connection
6
+ def initialize
7
+ @token = Configuration.token
8
+ @options = {
9
+ url: Configuration.url
10
+ }
11
+ end
12
+
13
+ def run
14
+ Faraday.new(@options) do |builder|
15
+ builder.request :json
16
+ builder.response :json
17
+ builder.response :raise_error
18
+ builder.response :logger if Configuration.debug
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ruzbx
4
+ # TODO
5
+ class Entity
6
+ def initialize
7
+ @token = Configuration.token
8
+ @method = :POST
9
+ @rpc_method = nil
10
+ @params = {}
11
+ @headers = { 'Content-Type': 'application/json' }
12
+ @data = { "jsonrpc": '2.0', "id": rand(1000) }
13
+ @auth = { 'Authorization': "Bearer #{@token}" }
14
+ @rest_api = nil
15
+ end
16
+
17
+ def self.build(&block)
18
+ entity = new
19
+ return entity unless block_given?
20
+
21
+ entity.instance_eval(&block)
22
+
23
+ entity
24
+ end
25
+
26
+ def no_auth
27
+ @auth = {}
28
+ end
29
+
30
+ def params(params)
31
+ @params = params
32
+ end
33
+
34
+ def headers(headers)
35
+ @headers = headers
36
+ end
37
+
38
+ def method(method)
39
+ @method = method
40
+ end
41
+
42
+ def path(path = 'api_jsonrpc.php')
43
+ @path ||= "#{@rest_api}/#{path}"
44
+
45
+ return @path if @path
46
+
47
+ raise ArgumentError, "No argument to 'path' was given."
48
+ end
49
+
50
+ def rpc_method(method)
51
+ @rpc_method = { method: method }
52
+ end
53
+
54
+ def data(data = {})
55
+ @data.merge!(data)
56
+
57
+ return @data if @data
58
+
59
+ raise ArgumentError, "No argument to 'data' was given."
60
+ end
61
+
62
+ def commit
63
+ case @method
64
+ when :POST
65
+ post.body
66
+ end
67
+ end
68
+
69
+ private
70
+
71
+ def request
72
+ yield
73
+ rescue Faraday::Error => e
74
+ raise "Status #{e.response[:status]}: #{e.response[:body]}"
75
+ end
76
+
77
+ def post
78
+ request do
79
+ client.post path do |req|
80
+ req.headers = @headers.merge(@auth)
81
+ req.params = @params
82
+ req.body = @data.merge(@rpc_method)
83
+ end
84
+ end
85
+ end
86
+
87
+ def client
88
+ conn = Ruzbx::Connection.new
89
+ conn.run
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rake'
4
+ require 'rake/tasklib'
5
+ require 'json'
6
+
7
+ module Ruzbx
8
+ module Tasks
9
+ # TODO
10
+ class Jira
11
+ include ::Rake::DSL if defined?(::Rake::DSL)
12
+
13
+ def initialize
14
+ @options = {
15
+ issuetype: 'Task'
16
+ }
17
+ @parser = OptionParser.new
18
+ define
19
+ end
20
+
21
+ def parser
22
+ yield
23
+ args = @parser.order!(ARGV) {}
24
+ @parser.parse!(args)
25
+ end
26
+
27
+ def options(name)
28
+ @options[name]
29
+ end
30
+
31
+ # rubocop:disable Metrics/AbcSize
32
+ # rubocop:disable Metrics/MethodLength
33
+ def define
34
+ generate 'whoami' do
35
+ puts Ruzbx::Api::Myself.get.name
36
+ end
37
+ generate 'url' do
38
+ puts Ruzbx::Configuration.url
39
+ end
40
+ generate 'server_info' do
41
+ puts Ruzbx::Api::ServerInfo.get.data.to_json
42
+ end
43
+ generate 'template' do
44
+ parser do
45
+ @parser.banner = "Usage: rake zabbix:template -- '[options]'"
46
+ @parser.on('-n NAME', '--name=NAME') { |name| @options[:name] = name }
47
+ end
48
+
49
+ options = @options
50
+ result = Ruzbx::Api::Template.get options[:name]
51
+ result.data['result'].each { |i| puts JSON.pretty_generate(i) }
52
+ end
53
+ generate 'create' do
54
+ parser do
55
+ @parser.banner = "Usage: rake zabbix:create -- '[options]'"
56
+ @parser.on('-n NAME', '--name=NAME') { |name| @options[:name] = name.strip }
57
+ @parser.on('-i IP', '--ip=NAME') { |ip| @options[:ip] = ip }
58
+ @parser.on('-t NAME', '--templates=NAME') { |templates| @options[:templates] = templates.strip.split(',') }
59
+ @parser.on('-g NAME', '--hostgroups=NAME') do |hostgroups|
60
+ @options[:hostgroups] = hostgroups.strip.split(',')
61
+ end
62
+ end
63
+
64
+ options = @options
65
+ options[:templates] = Ruzbx::Api::Template.get(options[:templates]).ids.map do |i|
66
+ { templateid: i }
67
+ end
68
+ options[:groups] = Ruzbx::Api::HostGroup.get(options[:hostgroups]).ids.map do |i|
69
+ { groupid: i }
70
+ end
71
+ result = Ruzbx::Api::Host.create options
72
+ result.data['result'].each { |i| puts JSON.pretty_generate(i) }
73
+ end
74
+ end
75
+ # rubocop:enable Metrics/AbcSize
76
+ # rubocop:enable Metrics/MethodLength
77
+
78
+ def generate(name, &block)
79
+ fullname = "zabbix:#{name}"
80
+ desc "Run #{fullname}"
81
+
82
+ Rake::Task[fullname].clear if Rake::Task.task_defined?(fullname)
83
+ namespace :zabbix do
84
+ task name do
85
+ instance_eval(&block)
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ruzbx
4
+ VERSION = "0.0.1"
5
+ end
data/lib/ruzbx.rb ADDED
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+ require 'json'
5
+ require_relative 'ruzbx/version'
6
+ require_relative 'ruzbx/connection'
7
+ require_relative 'ruzbx/configuration'
8
+ require_relative 'ruzbx/entity'
9
+ require_relative 'ruzbx/api/item'
10
+ require_relative 'ruzbx/api/myself'
11
+ require_relative 'ruzbx/api/server_info'
12
+ require_relative 'ruzbx/api/host'
13
+ require_relative 'ruzbx/api/template'
14
+ require_relative 'ruzbx/api/host_group'
15
+
16
+ module Ruzbx
17
+ class Error < StandardError; end
18
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruzbx
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andrey Semenov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-06-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.9'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.9'
27
+ description: The library is under development
28
+ email:
29
+ - i@itmage.ru
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - CHANGELOG.md
35
+ - CODE_OF_CONDUCT.md
36
+ - LICENSE.txt
37
+ - README.md
38
+ - Rakefile
39
+ - compose.yaml
40
+ - lib/ruzbx.rb
41
+ - lib/ruzbx/api/host.rb
42
+ - lib/ruzbx/api/host_group.rb
43
+ - lib/ruzbx/api/item.rb
44
+ - lib/ruzbx/api/myself.rb
45
+ - lib/ruzbx/api/server_info.rb
46
+ - lib/ruzbx/api/template.rb
47
+ - lib/ruzbx/configuration.rb
48
+ - lib/ruzbx/connection.rb
49
+ - lib/ruzbx/entity.rb
50
+ - lib/ruzbx/tasks/zabbix.rb
51
+ - lib/ruzbx/version.rb
52
+ homepage: https://github.com/itmagelabs/ruzbx
53
+ licenses:
54
+ - MIT
55
+ metadata:
56
+ homepage_uri: https://github.com/itmagelabs/ruzbx
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 3.0.0
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubygems_version: 3.5.10
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: API for Zabbix in ruby
76
+ test_files: []