naginata 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,35 @@
1
+ module Naginata
2
+ class Configuration
3
+ class Service
4
+ attr_reader :description, :hostname, :nagios
5
+
6
+ # @param [String] description
7
+ # @param [Hash] options
8
+ # @option options [String] :host hostname monitored by nagios
9
+ # @option options [String] :on nagios server name
10
+ def initialize(description, options = {})
11
+ ArgumentError if options[:host].nil?
12
+ ArgumentError if options[:on].nil?
13
+ @description = description
14
+ @hostname = options[:host]
15
+ @nagios = options[:on]
16
+ end
17
+
18
+ #def to_s
19
+ # @description
20
+ #end
21
+
22
+ end
23
+
24
+ class Host < Service
25
+
26
+ # @param [String] hostname
27
+ # @param [Hash] options
28
+ # @option options [String] :nagios nagios server name
29
+ def initialize(hostname, options = {})
30
+ super(:ping, options.merge(host: hostname))
31
+ end
32
+
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,8 @@
1
+ set_if_empty :format, :pretty
2
+ set_if_empty :log_level, :warn
3
+ set_if_empty :pty, false
4
+
5
+ set_if_empty :nagios_server_options, {
6
+ command_file: '/usr/local/nagios/var/rw/nagios.cmd',
7
+ status_file: '/usr/local/nagios/var/status.cmd',
8
+ }
@@ -0,0 +1,28 @@
1
+ require 'naginata/configuration'
2
+
3
+ module Naginata
4
+ module DSL
5
+
6
+ def nagios_server(name)
7
+ Configuration.env.nagios_server(name)
8
+ end
9
+
10
+ def fetch(key, default=nil, &block)
11
+ env.fetch(key, default, &block)
12
+ end
13
+
14
+ def env
15
+ Configuration.env
16
+ end
17
+
18
+ def set(key, value)
19
+ env.set(key, value)
20
+ end
21
+
22
+ def set_if_empty(key, value)
23
+ env.set_if_empty(key, value)
24
+ end
25
+
26
+ end
27
+ end
28
+
@@ -0,0 +1,39 @@
1
+ require 'naginata/dsl'
2
+
3
+ module Naginata
4
+ class Loader
5
+ #module_function
6
+
7
+ class << self
8
+ include Naginata::DSL
9
+
10
+ def load_configuration
11
+ instance_eval File.read(File.join(File.dirname(__FILE__), 'defaults.rb'))
12
+ instance_eval File.read 'Naginatafile'
13
+ end
14
+
15
+ def load_remote_objects
16
+ require 'naginata/cli/fetch'
17
+ # Refresh cached status.dat
18
+ CLI::Fetch.new.run
19
+
20
+ # Load status.dat
21
+ #
22
+ # @Note currently here reads all nagios servers' cache files. This
23
+ # means that users can not reduce nagios servers by --nagios= CLI
24
+ # option. Below loop may be moved into initialization phase of CLI
25
+ # class.
26
+ Configuration.env.nagios_servers.each do |nagios_server|
27
+ status = ::Naginata::Status.find(nagios_server.hostname)
28
+ status.service_items.group_by{ |section| section.host_name }.each do |host, sections|
29
+ services = sections.map { |s| s.service_description }
30
+ Configuration.env.host(host, services: services, on: nagios_server.hostname)
31
+ end
32
+ end
33
+
34
+ end
35
+
36
+ end
37
+ end
38
+ end
39
+
@@ -0,0 +1,36 @@
1
+ require 'naginata/configuration'
2
+ require 'naginata/configuration/filter'
3
+ require 'sshkit/coordinator'
4
+
5
+ module Naginata
6
+ class Runner
7
+ def self.run(&block)
8
+ nagios_servers = Configuration.env.filter(Configuration.env.nagios_servers)
9
+ services = Configuration.env.filter_service(Configuration.env.services)
10
+ user = Configuration.env.fetch(:nagios_server_options)[:run_command_as]
11
+
12
+ run_backend(nagios_servers) do |nagios_server|
13
+ svcs = Configuration::Filter.new(:nagios_server, nagios_server).filter_service(services)
14
+ as(user) do
15
+ yield self, nagios_server, svcs
16
+ end
17
+ end
18
+ end
19
+
20
+ def self.run_locally(&block)
21
+ nagios_servers = Configuration.env.filter(Configuration.env.nagios_servers)
22
+ services = Configuration.env.filter_service(Configuration.env.services)
23
+ nagios_servers.each do |nagios_server|
24
+ svcs = Configuration::Filter.new(:nagios_server, nagios_server).filter_service(services)
25
+ yield nagios_server, svcs
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def self.run_backend(nagios_servers, options = {}, &block)
32
+ SSHKit::Coordinator.new(nagios_servers).each(options, &block)
33
+ end
34
+ end
35
+ end
36
+
@@ -0,0 +1,82 @@
1
+ require 'nagios_analyzer'
2
+ require 'forwardable'
3
+ require 'tempfile'
4
+
5
+ module Naginata
6
+ class Status
7
+ extend Forwardable
8
+ def_delegators :@status, :service_items, :host_items, :items, :service_problems, :host_problems, :scopes
9
+ attr_accessor :status, :hostname
10
+
11
+ # Write status.dat into local disk
12
+ #
13
+ # @return [Naginata::Status] return self
14
+ # @return [false] if @status is nil
15
+ def save
16
+ return false if status.nil?
17
+ File.open(path, "w") do |f|
18
+ f.sync = true
19
+ Marshal.dump(status, f)
20
+ end
21
+ self
22
+ end
23
+
24
+ def purge_cache
25
+ if File.exist?(path)
26
+ FileUtils.rm path
27
+ end
28
+ end
29
+
30
+ # Path of marshal dumped status file
31
+ #
32
+ # @return [String] path of marshal dumped status file
33
+ def path
34
+ raise "@hostname must be set" if hostname.nil?
35
+ @path ||= File.join(self.class.cache_dir, "#{hostname}.status.dat")
36
+ end
37
+
38
+ # Create a new instance from raw status.dat string
39
+ #
40
+ # @param [String] string raw status.dat text
41
+ # @param [String] nagios_server_hostname hostname to identify status.data data
42
+ # @return [Naginata::Status]
43
+ def self.build(string, nagios_server_hostname)
44
+ instance = new
45
+ instance.hostname = nagios_server_hostname
46
+
47
+ # @Note NagiosAnalizer::Status.new requires filename, so here
48
+ # needs to write text data into tempfile.
49
+ Tempfile.open(["naginata-#{nagios_server_hostname}", ".status.dat"]) do |temp|
50
+ temp.sync = true
51
+ temp.write string
52
+ instance.status = ::NagiosAnalyzer::Status.new(temp.path, include_ok: true)
53
+ temp.close!
54
+ end
55
+ instance
56
+ end
57
+
58
+ # Return a directory path for status.dat caching
59
+ #
60
+ # @return [String] directory path for status.dat caching
61
+ def self.cache_dir
62
+ dir = (ENV['HOME'] && Dir.exist?(ENV['HOME'])) ? ENV['HOME'] : Dir.pwd
63
+ dir = File.join(dir, '.naginata/cache/status')
64
+ FileUtils.mkdir_p dir
65
+ return dir
66
+ end
67
+
68
+ # Find a instance from cache by nagios server hostname
69
+ #
70
+ # @return [Naginata::Status]
71
+ def self.find(nagios_server_hostname)
72
+ instance = new
73
+ instance.hostname = nagios_server_hostname
74
+ if File.exist?(instance.path)
75
+ File.open(instance.path) { |f| instance.status = Marshal.load(f) }
76
+ return instance
77
+ end
78
+ end
79
+
80
+ end
81
+ end
82
+
@@ -0,0 +1,6 @@
1
+ require 'naginata/ui/shell'
2
+
3
+ module Naginata
4
+ module UI
5
+ end
6
+ end
@@ -0,0 +1,39 @@
1
+ require 'thor/base'
2
+
3
+ module Naginata
4
+ module UI
5
+ class Shell
6
+ LEVELS = %w(error warn info debug)
7
+
8
+ def initialize
9
+ @shell = Thor::Base.shell.new
10
+ @level = ENV['DEBUG'] ? "debug" : "info"
11
+ end
12
+
13
+ def info(msg, newline = nil)
14
+ @shell.say(msg, nil, newline) if level("info")
15
+ end
16
+
17
+ def warn(msg, newline = nil)
18
+ @shell.say(msg, :yellow, newline) if level("warn")
19
+ end
20
+
21
+ def error(msg, newline = nil)
22
+ @shell.say(msg, :red, newline) if level("error")
23
+ end
24
+
25
+ def debug(msg, newline = nil)
26
+ @shell.say(msg, nil, newline) if level("debug")
27
+ end
28
+
29
+ def level(name = nil)
30
+ name ? LEVELS.index(name) <= LEVELS.index(@level) : @level
31
+ end
32
+
33
+ def yes?(msg)
34
+ @shell.yes?(msg)
35
+ end
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module Naginata
2
+ VERSION = "0.1.0"
3
+ end
data/naginata.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'naginata/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "naginata"
8
+ spec.version = Naginata::VERSION
9
+ spec.authors = ["Nobuhiro Nikushi"]
10
+ spec.email = ["deneb.ge@gmail.com"]
11
+
12
+ spec.summary = "Remote multi nagios server control tool"
13
+ spec.description = "Remote multi nagios server control tool over ssh connection"
14
+ spec.homepage = "https://github.com/niku4i/naginata"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "bin"
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency 'sshkit', '~> 1.7'
23
+ spec.add_dependency 'thor'
24
+ spec.add_dependency 'nagios_analyzer', '~> 0.0.5'
25
+ spec.add_development_dependency "bundler", "~> 1.9"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3"
28
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: naginata
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nobuhiro Nikushi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sshkit
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: thor
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: nagios_analyzer
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.0.5
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.0.5
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.9'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.9'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3'
97
+ description: Remote multi nagios server control tool over ssh connection
98
+ email:
99
+ - deneb.ge@gmail.com
100
+ executables:
101
+ - naginata
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".travis.yml"
108
+ - CHANGELOG.md
109
+ - Gemfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - bin/naginata
114
+ - integration-test/.gitignore
115
+ - integration-test/Naginatafile
116
+ - integration-test/Vagrantfile
117
+ - lib/naginata.rb
118
+ - lib/naginata/cli.rb
119
+ - lib/naginata/cli/fetch.rb
120
+ - lib/naginata/cli/notification.rb
121
+ - lib/naginata/command/external_command.rb
122
+ - lib/naginata/configuration.rb
123
+ - lib/naginata/configuration/filter.rb
124
+ - lib/naginata/configuration/nagios_server.rb
125
+ - lib/naginata/configuration/service.rb
126
+ - lib/naginata/defaults.rb
127
+ - lib/naginata/dsl.rb
128
+ - lib/naginata/loader.rb
129
+ - lib/naginata/runner.rb
130
+ - lib/naginata/status.rb
131
+ - lib/naginata/ui.rb
132
+ - lib/naginata/ui/shell.rb
133
+ - lib/naginata/version.rb
134
+ - naginata.gemspec
135
+ homepage: https://github.com/niku4i/naginata
136
+ licenses:
137
+ - MIT
138
+ metadata: {}
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubyforge_project:
155
+ rubygems_version: 2.4.5
156
+ signing_key:
157
+ specification_version: 4
158
+ summary: Remote multi nagios server control tool
159
+ test_files: []