systemd_service_check 0.0.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
+ SHA256:
3
+ metadata.gz: e1234d2b332ca9947a0bdcbd5b0f324e7ec74b7187a3c68788301bf9a407c244
4
+ data.tar.gz: 243a66e0b11c851c66095da7830929565430dd22a553ff5c6d53adbe271dd167
5
+ SHA512:
6
+ metadata.gz: 3e12d33080afded09c38c8f0539390f47efb308250f8b1f6efea0dad513511c473bd0f4c35232f58a38577d1eeb6c1179757372dc3eafdd3e182dcf7118cfec1
7
+ data.tar.gz: 52e2fca32dd71fa29b09a7308d9319e80fb36bf9b8ae786261e402456d65f9cd6a45cbbe1e843328f4fe72b77af616c09639fd77d153f82b0f62159644db8d6f
data/.gitignore ADDED
@@ -0,0 +1,53 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ # Gemfile.lock
46
+ # .ruby-version
47
+ # .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
51
+
52
+ .vagrant/
53
+ Gemfile.lock
data/.rubocop.yml ADDED
@@ -0,0 +1,26 @@
1
+ inherit_from: .rubocop_todo.yml
2
+
3
+ AllCops:
4
+ TargetRubyVersion: 2.5
5
+ Exclude:
6
+ - Vagrantfile
7
+ - vendor/**/*
8
+
9
+ Style/AsciiComments:
10
+ Enabled: false
11
+
12
+ Style/FrozenStringLiteralComment:
13
+ Enabled: false
14
+
15
+ Style/StringLiterals:
16
+ Enabled: false
17
+
18
+ Style/StringLiteralsInInterpolation:
19
+ Enabled: false
20
+
21
+ Metrics/LineLength:
22
+ Max: 90
23
+
24
+ Metrics/BlockLength:
25
+ Enabled: false
26
+
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,7 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2018-01-23 22:11:29 +0900 using RuboCop version 0.52.1.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.0
5
+ before_install: gem install bundler -v 1.16.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at key.luvless@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in systemd_service_check.gemspec
6
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 k-ta yamada
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 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,
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 THE
21
+ SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 k-ta-yamada
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,218 @@
1
+ # SystemdServiceCheck
2
+
3
+ [![Build Status](https://travis-ci.org/k-ta-yamada/systemd_service_check.svg?branch=master)](https://travis-ci.org/k-ta-yamada/systemd_service_check)
4
+
5
+ It is a script that checks `systemd service` on the server using net-ssh.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'systemd_service_check'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install systemd_service_check
22
+
23
+ ## Usage
24
+
25
+ ### configure yaml
26
+
27
+ sample file => `./lib/systemd_service_check.yml`
28
+
29
+ usage at require: `SystemdServiceCheck::Base.new(yaml: './path/to/setting.yml')`
30
+
31
+ usage as CLI: `ssc check -y ./path/to/setting.yml`
32
+
33
+ ### Usage at `require 'systemd_service_check'`
34
+
35
+ ```rb
36
+ require 'systemd_service_check'
37
+
38
+ ssc = SystemdServiceCheck::Base.new
39
+ ssc.run
40
+ puts ssc.to_json
41
+ ```
42
+
43
+ ### Usage as CLI `ssc`
44
+
45
+ ```sh
46
+ $ ssc help
47
+ Commands:
48
+ ssc check ENV [ENV...] options # check target ENV Servers. default option is `-t, [--table]`
49
+ ssc help [COMMAND] # Describe available commands or one specific command
50
+ ssc version # return SystemdServiceCheck::VERSION
51
+ ```
52
+
53
+ ```sh
54
+ $ ssc help check
55
+ Usage:
56
+ ssc check ENV [ENV...] options
57
+
58
+ Options:
59
+ -t, [--table], [--no-table] # Displaying results using table_print
60
+ -j, [--json], [--no-json] # Result display in json format
61
+ -a, [--awesome], [--no-awesome] # Displaying results using awesome_print
62
+ -y, [--yaml=./systemd_service_check.yml] # setting yaml file
63
+
64
+ check target ENV Servers.
65
+ default option is `-t, [--table]`
66
+ ```
67
+
68
+ ```sh
69
+ $ ssc check
70
+ ENV | IP | HOSTNAME | USER | SERVICE_NAME | LOAD_STATE | ACTIVE_STATE | SUB_STATE
71
+ ----|---------------|----------|------|----------------------------------------------------|--------------------|-------------------|-----------------
72
+ dev | 192.168.1.101 | centos7 | root | sshd.service | loaded | active | running
73
+ dev | 192.168.1.101 | centos7 | root | firewalld.service | loaded | inactive | dead
74
+ dev | 192.168.1.101 | centos7 | root | dummy.service | not-found | inactive | dead
75
+ dev | 192.168.1.101 | centos7 | root | rsyslog.service | loaded | active | running
76
+ dev | 192.168.1.101 | centos7 | root | 12345678901234567890123456789012345678901234567890 | not-found | inactive | dead
77
+ dev | 192.168.1.101 | centos7 | root | systemd-tmpfiles-clean.timer | loaded | active | waiting
78
+ ```
79
+
80
+ ```sh
81
+ $ ssc check -j | jq
82
+ [
83
+ {
84
+ "server": {
85
+ "env": "dev",
86
+ "ip": "192.168.1.101",
87
+ "user": "root",
88
+ "pass": "vagrant",
89
+ "services": [
90
+ "sshd.service",
91
+ "firewalld.service",
92
+ "dummy.service",
93
+ "rsyslog.service",
94
+ "12345678901234567890123456789012345678901234567890",
95
+ "systemd-tmpfiles-clean.timer"
96
+ ],
97
+ "hostname": "centos7"
98
+ },
99
+ "services": [
100
+ {
101
+ "service_name": "sshd.service",
102
+ "load_state": "loaded",
103
+ "active_state": "active",
104
+ "sub_state": "running"
105
+ },
106
+ {
107
+ "service_name": "firewalld.service",
108
+ "load_state": "loaded",
109
+ "active_state": "inactive",
110
+ "sub_state": "dead"
111
+ },
112
+ {
113
+ "service_name": "dummy.service",
114
+ "load_state": "not-found",
115
+ "active_state": "inactive",
116
+ "sub_state": "dead"
117
+ },
118
+ {
119
+ "service_name": "rsyslog.service",
120
+ "load_state": "loaded",
121
+ "active_state": "active",
122
+ "sub_state": "running"
123
+ },
124
+ {
125
+ "service_name": "12345678901234567890123456789012345678901234567890",
126
+ "load_state": "not-found",
127
+ "active_state": "inactive",
128
+ "sub_state": "dead"
129
+ },
130
+ {
131
+ "service_name": "systemd-tmpfiles-clean.timer",
132
+ "load_state": "loaded",
133
+ "active_state": "active",
134
+ "sub_state": "waiting"
135
+ }
136
+ ]
137
+ }
138
+ ]
139
+ ```
140
+
141
+ ```sh
142
+ $ ssc check -a
143
+ [
144
+ [0] #<Struct:SystemdServiceCheck::Base::Result:0x7fa4cec5aa10
145
+ server = #<Struct:SystemdServiceCheck::Base::Server:0x7fa4cdbfc088
146
+ env = "dev",
147
+ hostname = "centos7",
148
+ ip = "192.168.1.101",
149
+ pass = "vagrant",
150
+ services = [
151
+ [0] "sshd.service",
152
+ [1] "firewalld.service",
153
+ [2] "dummy.service",
154
+ [3] "rsyslog.service",
155
+ [4] "12345678901234567890123456789012345678901234567890",
156
+ [5] "systemd-tmpfiles-clean.timer"
157
+ ],
158
+ user = "root"
159
+ >,
160
+ services = [
161
+ [0] #<Struct:SystemdServiceCheck::Base::Service:0x7fa4cf05a9a8
162
+ active_state = "active",
163
+ load_state = "loaded",
164
+ service_name = "sshd.service",
165
+ sub_state = "running"
166
+ >,
167
+ [1] #<Struct:SystemdServiceCheck::Base::Service:0x7fa4cf049748
168
+ active_state = "inactive",
169
+ load_state = "loaded",
170
+ service_name = "firewalld.service",
171
+ sub_state = "dead"
172
+ >,
173
+ [2] #<Struct:SystemdServiceCheck::Base::Service:0x7fa4cdb534d8
174
+ active_state = "inactive",
175
+ load_state = "not-found",
176
+ service_name = "dummy.service",
177
+ sub_state = "dead"
178
+ >,
179
+ [3] #<Struct:SystemdServiceCheck::Base::Service:0x7fa4cdb497a8
180
+ active_state = "active",
181
+ load_state = "loaded",
182
+ service_name = "rsyslog.service",
183
+ sub_state = "running"
184
+ >,
185
+ [4] #<Struct:SystemdServiceCheck::Base::Service:0x7fa4cec7a180
186
+ active_state = "inactive",
187
+ load_state = "not-found",
188
+ service_name = "12345678901234567890123456789012345678901234567890",
189
+ sub_state = "dead"
190
+ >,
191
+ [5] #<Struct:SystemdServiceCheck::Base::Service:0x7fa4cec5ad80
192
+ active_state = "active",
193
+ load_state = "loaded",
194
+ service_name = "systemd-tmpfiles-clean.timer",
195
+ sub_state = "waiting"
196
+ >
197
+ ]
198
+ >
199
+ ]
200
+ ```
201
+
202
+ ## Development
203
+
204
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
205
+
206
+ 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`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
207
+
208
+ ## Contributing
209
+
210
+ Bug reports and pull requests are welcome on GitHub at https://github.com/k-ta-yamada/systemd_service_check. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
211
+
212
+ ## License
213
+
214
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
215
+
216
+ ## Code of Conduct
217
+
218
+ Everyone interacting in the SystemdServiceCheck project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/k-ta-yamada/systemd_service_check/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task default: :test
data/Vagrantfile ADDED
@@ -0,0 +1,72 @@
1
+ # -*- mode: ruby -*-
2
+ # vi: set ft=ruby :
3
+
4
+ # All Vagrant configuration is done below. The "2" in Vagrant.configure
5
+ # configures the configuration version (we support older styles for
6
+ # backwards compatibility). Please don't change it unless you know what
7
+ # you're doing.
8
+ Vagrant.configure("2") do |config|
9
+ # The most common configuration options are documented and commented below.
10
+ # For a complete reference, please see the online documentation at
11
+ # https://docs.vagrantup.com.
12
+
13
+ # Every Vagrant development environment requires a box. You can search for
14
+ # boxes at https://vagrantcloud.com/search.
15
+ config.vm.box = "centos/7"
16
+ config.vm.box_version = "1710.01"
17
+
18
+ config.vm.define :centos7 do |centos7|
19
+ centos7.vm.provider "virtualbox" do |v|
20
+ v.linked_clone = true
21
+ end
22
+
23
+ centos7.vm.hostname = 'centos7'
24
+
25
+ centos7.vm.network "private_network", ip: "192.168.1.101"
26
+
27
+ centos7.vm.provision "ssh:root", type: :shell do |s|
28
+ private_key = "/vagrant/.vagrant/machines/#{centos7.vm.hostname}/virtualbox/private_key"
29
+ s.inline = <<-SHELL
30
+ sudo su - root -c "mkdir -p /root/.ssh/"
31
+ sudo chmod 700 /root/.ssh/
32
+ ssh-keygen -yf #{private_key} > /root/.ssh/authorized_keys
33
+ sudo chmod 600 /root/.ssh/authorized_keys
34
+ SHELL
35
+ end
36
+ end
37
+
38
+ config.vm.provision "selinux:disabled", type: :shell do |s|
39
+ s.inline = "sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config"
40
+ end
41
+
42
+ config.vm.provision "sshd:PasswordAuthentication:yes", type: :shell do |s|
43
+ s.inline = "sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config"
44
+ end
45
+
46
+ config.vm.provision "timedatectl:set-timezone:Asia/Tokyo", type: :shell do |s|
47
+ s.inline = "timedatectl set-timezone Asia/Tokyo"
48
+ end
49
+
50
+ config.vm.provision "yum:install:tmux", type: :shell do |s|
51
+ s.inline = "yum install tmux -y"
52
+ end
53
+
54
+ config.vm.provision "yum:install:vim", type: :shell do |s|
55
+ s.inline = "yum install vim-enhanced -y"
56
+ end
57
+
58
+ if Vagrant.has_plugin?("vagrant-vbguest")
59
+ puts "-- has_plugin: vagrant-vbguest"
60
+ config.vm.synced_folder ".", "/vagrant", type: "virtualbox"
61
+ end
62
+
63
+ if Vagrant.has_plugin?("vagrant-proxyconf")
64
+ puts "-- has_plugin: vagrant-proxyconf"
65
+ puts " http_proxy is [#{ENV["http_proxy"]}]"
66
+ puts " https_proxy is [#{ENV["https_proxy"]}]"
67
+ puts " no_proxy is [#{ENV["no_proxy"]}]"
68
+ config.proxy.http = ENV["http_proxy"]
69
+ config.proxy.https = ENV["https_proxy"]
70
+ config.proxy.no_proxy = ENV["no_proxy"]
71
+ end
72
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "systemd_service_check"
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(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/bin/ssc ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "systemd_service_check/cli"
4
+ SystemdServiceCheck::CLI.start
@@ -0,0 +1,99 @@
1
+ require 'net/ssh'
2
+ require 'yaml'
3
+ require 'json'
4
+ require 'active_support'
5
+ require 'active_support/core_ext/string'
6
+ require 'pry' # forDebug
7
+
8
+ # SystemdServiceCheck
9
+ module SystemdServiceCheck
10
+ # Base
11
+ class Base
12
+ Server = Struct.new(:env, :ip, :user, :pass, :services, :hostname)
13
+ Service = Struct.new(:service_name, :load_state, :active_state, :sub_state)
14
+ Result = Struct.new(:server, :services)
15
+
16
+ ENVS = %w[dev stg prd].freeze
17
+ # TODO: systemctl show -p
18
+ STATES = %w[LoadState ActiveState SubState].freeze
19
+ # TODO: setting by yaml
20
+ SHOW_GREP = /env/i
21
+
22
+ attr_reader :argv, :servers, :target_env, :target_servers, :results
23
+
24
+ def initialize(argv: nil, yaml: nil)
25
+ @argv = argv || []
26
+ @servers = []
27
+ @target_env = []
28
+ @target_servers = []
29
+ @results = []
30
+ load_settings(yaml)
31
+ configure_target_servers
32
+ end
33
+
34
+ def run
35
+ @results = @target_servers.map { |server| ssh(server) }
36
+ end
37
+
38
+ def to_json
39
+ @results.map do |result|
40
+ { server: result.server.to_h,
41
+ services: result.services.map(&:to_h) }
42
+ end.to_json
43
+ end
44
+
45
+ private
46
+
47
+ def load_settings(filename)
48
+ yaml = YAML.load_file(filename(filename))
49
+ @servers = yaml[:servers].map { |s| Server.new(*s.values) }
50
+ end
51
+
52
+ def filename(filename)
53
+ if filename.nil?
54
+ File.expand_path('../../systemd_service_check.yml', __FILE__)
55
+ else
56
+ File.expand_path(filename)
57
+ end
58
+ end
59
+
60
+ def configure_target_servers
61
+ configure_target_env
62
+ @target_servers = @servers.select { |s| @target_env.include?(s[:env]) }
63
+ end
64
+
65
+ def configure_target_env
66
+ @target_env = %w[dev stg prd] & @argv
67
+ # If there is no argument, only `dev` is targeted
68
+ @target_env = %w[dev] if @target_env.empty? && @argv.empty?
69
+ # If there is only one argument and it is `all`, it targets all (dev, stg, prd)
70
+ @target_env = ENVS if @argv.size == 1 && argv.first == 'all'
71
+ end
72
+
73
+ def ssh(server)
74
+ services = []
75
+ Net::SSH.start(server[:ip], server[:user], password: server[:pass]) do |ssh|
76
+ server[:hostname] = ssh.exec!('hostname').chop
77
+ services = server[:services].map { |service_name| systemd(ssh, service_name) }
78
+
79
+ # TODO: store in Result object
80
+ # puts ssh.exec!("systemctl list-timers")
81
+ end
82
+ Result.new(server, services)
83
+ end
84
+
85
+ def systemd(ssh, service_name)
86
+ states = ssh.exec!("systemctl show #{service_name} -p #{STATES.join(",")}")
87
+ .split("\n")
88
+ .map { |v| v.split("=") }
89
+ .each_with_object({}) { |(k, v), memo| memo[k.underscore.to_sym] = v }
90
+
91
+ Service.new(
92
+ service_name,
93
+ states[:load_state],
94
+ states[:active_state],
95
+ states[:sub_state]
96
+ )
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,101 @@
1
+ require 'systemd_service_check'
2
+ require 'thor'
3
+ require 'awesome_print'
4
+ require 'table_print'
5
+ require 'highline'
6
+ require 'pry' # forDebug
7
+
8
+ module SystemdServiceCheck
9
+ # CLI
10
+ class CLI < Thor
11
+ class InvalidOption < StandardError; end
12
+
13
+ option :table,
14
+ type: :boolean,
15
+ default: false,
16
+ aliases: '-t',
17
+ desc: 'Displaying results using table_print'
18
+ option :json,
19
+ type: :boolean,
20
+ default: false,
21
+ aliases: '-j',
22
+ desc: 'Result display in json format'
23
+ option :awesome,
24
+ type: :boolean,
25
+ default: false,
26
+ aliases: '-a',
27
+ desc: 'Displaying results using awesome_print'
28
+ option :yaml,
29
+ type: :string,
30
+ aliases: '-y',
31
+ desc: 'setting yaml file',
32
+ banner: './systemd_service_check.yml'
33
+ # default: '../../lib/sample.yml'
34
+
35
+ description = <<~"STRING"
36
+ check target ENV Servers.
37
+ default option is `-t, [--table]`
38
+ STRING
39
+ desc "check ENV [ENV...] options", description
40
+ def check(*env)
41
+ raise InvalidOption if options.values.count(true) > 1
42
+ @ssc = Base.new(argv: env, yaml: options[:yaml])
43
+ @ssc.run
44
+ disp
45
+ rescue InvalidOption => e
46
+ puts "<#{e}>: Multiple display format options can not be specified"
47
+ puts " #{e.backtrace_locations.first}"
48
+ end
49
+
50
+ desc "version", "return SystemdServiceCheck::VERSION"
51
+ def version
52
+ puts VERSION
53
+ end
54
+
55
+ private
56
+
57
+ def disp
58
+ if options[:json] then disp_json
59
+ elsif options[:awesome] then disp_ap
60
+ else disp_table
61
+ end
62
+ end
63
+
64
+ def disp_json
65
+ puts @ssc.to_json
66
+ end
67
+
68
+ def disp_ap
69
+ ap @ssc.results
70
+ end
71
+
72
+ COLS = %i[env ip hostname user service_name load_state active_state sub_state].freeze
73
+ def disp_table # rubocop:disable Metrics/AbcSize
74
+ service_name_width =
75
+ @ssc.results.map(&:services).flatten.map(&:service_name).map(&:size).max
76
+
77
+ @ssc.results.each do |result|
78
+ services = decorate_ansi_color(result.services)
79
+ data = services.map { |s| result.server.to_h.merge(s.to_h) }
80
+
81
+ tp(data, COLS, service_name: { width: service_name_width })
82
+ end
83
+ end
84
+
85
+ def decorate_ansi_color(services)
86
+ services.map do |s|
87
+ s.class.new(
88
+ s.service_name,
89
+ color_state(s.load_state, :==, "loaded"),
90
+ color_state(s.active_state, :==, "active"),
91
+ color_state(s.sub_state, :!=, "dead")
92
+ )
93
+ end
94
+ end
95
+
96
+ def color_state(obj, method, arg)
97
+ green_or_red = obj.send(method, arg) ? :green : :red
98
+ HighLine.color(obj, green_or_red)
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,3 @@
1
+ module SystemdServiceCheck
2
+ VERSION = "0.0.0".freeze
3
+ end
@@ -0,0 +1,7 @@
1
+ # require 'pry';binding.pry
2
+ require "systemd_service_check/version"
3
+ require_relative "systemd_service_check/base"
4
+
5
+ # module SystemdServiceCheck
6
+ # # Your code goes here...
7
+ # end
@@ -0,0 +1,26 @@
1
+ # SERVICE TO BE CHECKED
2
+ :services: &default_services
3
+ - sshd.service
4
+ - firewalld.service
5
+ - dummy.service
6
+ - rsyslog.service
7
+ - "12345678901234567890123456789012345678901234567890"
8
+ - systemd-tmpfiles-clean.timer
9
+
10
+ # SERVERS TO BE CHECKED
11
+ :servers:
12
+ - :env: dev
13
+ :ip: 192.168.1.101
14
+ :user: root
15
+ :pass: vagrant
16
+ :services: *default_services
17
+ - :env: stg
18
+ :ip: centos7
19
+ :user: root
20
+ :pass: vagrant
21
+ :services: *default_services
22
+ - :env: prd
23
+ :ip: centos7
24
+ :user: root
25
+ :pass: vagrant
26
+ :services: *default_services
@@ -0,0 +1,52 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "systemd_service_check/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "systemd_service_check"
8
+ spec.version = SystemdServiceCheck::VERSION
9
+ spec.authors = ["k-ta-yamada"]
10
+ spec.email = ["key.luvless@gmail.com"]
11
+
12
+ spec.summary =
13
+ 'It is a script that checks systemd service on the server using net-ssh.'
14
+ spec.description =
15
+ 'It is a script that checks systemd service on the server using net-ssh.'
16
+ spec.homepage = "https://github.com/k-ta-yamada/systemd_service_check"
17
+ spec.license = "MIT"
18
+
19
+ # # Prevent pushing this gem to RubyGems.org.
20
+ # To allow pushes either set the 'allowed_push_host'
21
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
22
+ # if spec.respond_to?(:metadata)
23
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
24
+ # else
25
+ # raise "RubyGems 2.0 or newer is required to protect against " \
26
+ # "public gem pushes."
27
+ # end
28
+
29
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
30
+ f.match(%r{^(test|spec|features)/})
31
+ end
32
+ spec.bindir = "bin"
33
+ # spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
34
+ spec.executables = ['ssc']
35
+ spec.require_paths = ["lib"]
36
+
37
+ spec.add_development_dependency "bundler", "~> 1.16"
38
+ spec.add_development_dependency "minitest", "~> 5.0"
39
+ spec.add_development_dependency "rake", "~> 10.0"
40
+
41
+ spec.add_development_dependency 'pry-doc'
42
+ spec.add_development_dependency 'pry-theme'
43
+ spec.add_development_dependency 'rubocop'
44
+
45
+ spec.add_dependency 'activesupport'
46
+ spec.add_dependency 'awesome_print'
47
+ spec.add_dependency 'highline'
48
+ spec.add_dependency 'net-ssh'
49
+ spec.add_dependency 'pry'
50
+ spec.add_dependency 'table_print'
51
+ spec.add_dependency 'thor'
52
+ end
metadata ADDED
@@ -0,0 +1,247 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: systemd_service_check
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - k-ta-yamada
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-01-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry-doc
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-theme
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: activesupport
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: awesome_print
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: highline
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: net-ssh
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :runtime
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: pry
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :runtime
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: table_print
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :runtime
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ - !ruby/object:Gem::Dependency
182
+ name: thor
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ type: :runtime
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
195
+ description: It is a script that checks systemd service on the server using net-ssh.
196
+ email:
197
+ - key.luvless@gmail.com
198
+ executables:
199
+ - ssc
200
+ extensions: []
201
+ extra_rdoc_files: []
202
+ files:
203
+ - ".gitignore"
204
+ - ".rubocop.yml"
205
+ - ".rubocop_todo.yml"
206
+ - ".travis.yml"
207
+ - CODE_OF_CONDUCT.md
208
+ - Gemfile
209
+ - LICENSE
210
+ - LICENSE.txt
211
+ - README.md
212
+ - Rakefile
213
+ - Vagrantfile
214
+ - bin/console
215
+ - bin/setup
216
+ - bin/ssc
217
+ - lib/systemd_service_check.rb
218
+ - lib/systemd_service_check.yml
219
+ - lib/systemd_service_check/base.rb
220
+ - lib/systemd_service_check/cli.rb
221
+ - lib/systemd_service_check/version.rb
222
+ - systemd_service_check.gemspec
223
+ homepage: https://github.com/k-ta-yamada/systemd_service_check
224
+ licenses:
225
+ - MIT
226
+ metadata: {}
227
+ post_install_message:
228
+ rdoc_options: []
229
+ require_paths:
230
+ - lib
231
+ required_ruby_version: !ruby/object:Gem::Requirement
232
+ requirements:
233
+ - - ">="
234
+ - !ruby/object:Gem::Version
235
+ version: '0'
236
+ required_rubygems_version: !ruby/object:Gem::Requirement
237
+ requirements:
238
+ - - ">="
239
+ - !ruby/object:Gem::Version
240
+ version: '0'
241
+ requirements: []
242
+ rubyforge_project:
243
+ rubygems_version: 2.7.3
244
+ signing_key:
245
+ specification_version: 4
246
+ summary: It is a script that checks systemd service on the server using net-ssh.
247
+ test_files: []