systemd_service_check 0.0.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e1234d2b332ca9947a0bdcbd5b0f324e7ec74b7187a3c68788301bf9a407c244
4
- data.tar.gz: 243a66e0b11c851c66095da7830929565430dd22a553ff5c6d53adbe271dd167
3
+ metadata.gz: 14d00810a361024a249ae6211e3805e1383badf662431804174abfc4b86e842e
4
+ data.tar.gz: d68c217324a1a19a507e97cfa1c790f3e2ad18981d2496dba23a4be437220f30
5
5
  SHA512:
6
- metadata.gz: 3e12d33080afded09c38c8f0539390f47efb308250f8b1f6efea0dad513511c473bd0f4c35232f58a38577d1eeb6c1179757372dc3eafdd3e182dcf7118cfec1
7
- data.tar.gz: 52e2fca32dd71fa29b09a7308d9319e80fb36bf9b8ae786261e402456d65f9cd6a45cbbe1e843328f4fe72b77af616c09639fd77d153f82b0f62159644db8d6f
6
+ metadata.gz: 5c0f87e7ee79969bad99bf4f77f8be010217b0e75c9fc6b7a93541eaec69b6ed1bd5de78d9ebfc0c248add2f421151730fee9519c83b15869679733cef730995
7
+ data.tar.gz: c215f24bb497793765217e66e58df8c711d7218a40584ebce380cf0983f43289b3b36a5cb75b2b44d893feefc906a9cfedd576d295dee4c468f405e0dcea6fd4
data/.rubocop.yml CHANGED
@@ -5,6 +5,7 @@ AllCops:
5
5
  Exclude:
6
6
  - Vagrantfile
7
7
  - vendor/**/*
8
+ - lib/table_print-patch-pr70/*
8
9
 
9
10
  Style/AsciiComments:
10
11
  Enabled: false
data/README.md CHANGED
@@ -1,8 +1,10 @@
1
1
  # SystemdServiceCheck
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/systemd_service_check.svg)](https://badge.fury.io/rb/systemd_service_check)
3
4
  [![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
 
5
- It is a script that checks `systemd service` on the server using net-ssh.
6
+ This gem provide `ssc` command to check multiple `systemd service`
7
+ etc on multiple remote servers using net-ssh.
6
8
 
7
9
  ## Installation
8
10
 
@@ -14,17 +16,21 @@ gem 'systemd_service_check'
14
16
 
15
17
  And then execute:
16
18
 
17
- $ bundle
19
+ ```sh
20
+ $ bundle
21
+ ```
18
22
 
19
23
  Or install it yourself as:
20
24
 
21
- $ gem install systemd_service_check
25
+ ```sh
26
+ $ gem install systemd_service_check
27
+ ```
22
28
 
23
29
  ## Usage
24
30
 
25
31
  ### configure yaml
26
32
 
27
- sample file => `./lib/systemd_service_check.yml`
33
+ sample file: [./systemd_service_check.sample.yml](https://github.com/k-ta-yamada/systemd_service_check/blob/master/systemd_service_check.sample.yml)
28
34
 
29
35
  usage at require: `SystemdServiceCheck::Base.new(yaml: './path/to/setting.yml')`
30
36
 
@@ -36,7 +42,7 @@ usage as CLI: `ssc check -y ./path/to/setting.yml`
36
42
  require 'systemd_service_check'
37
43
 
38
44
  ssc = SystemdServiceCheck::Base.new
39
- ssc.run
45
+
40
46
  puts ssc.to_json
41
47
  ```
42
48
 
@@ -45,7 +51,7 @@ puts ssc.to_json
45
51
  ```sh
46
52
  $ ssc help
47
53
  Commands:
48
- ssc check ENV [ENV...] options # check target ENV Servers. default option is `-t, [--table]`
54
+ ssc check ENV [ENV...] options # check target ENV Servers.
49
55
  ssc help [COMMAND] # Describe available commands or one specific command
50
56
  ssc version # return SystemdServiceCheck::VERSION
51
57
  ```
@@ -56,43 +62,50 @@ Usage:
56
62
  ssc check ENV [ENV...] options
57
63
 
58
64
  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
65
+ -f, [--format=table] # [t]able, [j]son, [a]wesome_print
66
+ # Default: table
67
+ -y, [--yaml=./systemd_service_check.sample.yml] # setting yaml file
68
+ # Default: ./systemd_service_check.sample.yml
63
69
 
64
70
  check target ENV Servers.
65
- default option is `-t, [--table]`
66
71
  ```
67
72
 
68
73
  ```sh
69
74
  $ 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
75
+ ENV | IP | HOSTNAME | USER | SERVICE_NAME | LOAD_STATE | ACTIVE_STATE | SUB_STATE | UNIT_FILE_STATE | TYPE
76
+ ----|---------------|----------|------|--------------------------------|------------|--------------|-----------|-----------------|--------
77
+ dev | 192.168.1.101 | centos7 | root | sshd.service | loaded | active | running | enabled | notify
78
+ dev | 192.168.1.101 | centos7 | root | firewalld.service | loaded | inactive | dead | disabled | dbus
79
+ dev | 192.168.1.101 | centos7 | root | rsyslog.service | loaded | active | running | enabled | notify
80
+ dev | 192.168.1.101 | centos7 | root | network.service | loaded | active | exited | bad | forking
81
+ dev | 192.168.1.101 | centos7 | root | systemd-tmpfiles-clean.timer | loaded | active | waiting | static | n/a
82
+ dev | 192.168.1.101 | centos7 | root | dummy_long_name_size_30_______ | not-found | inactive | dead | n/a | n/a
78
83
  ```
79
84
 
80
85
  ```sh
81
- $ ssc check -j | jq
86
+ $ ssc check --format=json | jq
82
87
  [
83
88
  {
84
89
  "server": {
85
90
  "env": "dev",
86
91
  "ip": "192.168.1.101",
87
92
  "user": "root",
88
- "pass": "vagrant",
93
+ "options": {
94
+ "password": "vagrant",
95
+ "keys": [
96
+ ""
97
+ ],
98
+ "logger": "#<Logger:0x00007fa52293b0b0>",
99
+ "password_prompt": "#<Net::SSH::Prompt:0x00007fa52293aa70>",
100
+ "user": "root"
101
+ },
89
102
  "services": [
90
103
  "sshd.service",
91
104
  "firewalld.service",
92
- "dummy.service",
93
105
  "rsyslog.service",
94
- "12345678901234567890123456789012345678901234567890",
95
- "systemd-tmpfiles-clean.timer"
106
+ "network.service",
107
+ "systemd-tmpfiles-clean.timer",
108
+ "dummy_long_name_size_30_______"
96
109
  ],
97
110
  "hostname": "centos7"
98
111
  },
@@ -101,37 +114,49 @@ $ ssc check -j | jq
101
114
  "service_name": "sshd.service",
102
115
  "load_state": "loaded",
103
116
  "active_state": "active",
104
- "sub_state": "running"
117
+ "sub_state": "running",
118
+ "unit_file_state": "enabled",
119
+ "type": "notify"
105
120
  },
106
121
  {
107
122
  "service_name": "firewalld.service",
108
123
  "load_state": "loaded",
109
124
  "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"
125
+ "sub_state": "dead",
126
+ "unit_file_state": "disabled",
127
+ "type": "dbus"
117
128
  },
118
129
  {
119
130
  "service_name": "rsyslog.service",
120
131
  "load_state": "loaded",
121
132
  "active_state": "active",
122
- "sub_state": "running"
133
+ "sub_state": "running",
134
+ "unit_file_state": "enabled",
135
+ "type": "notify"
123
136
  },
124
137
  {
125
- "service_name": "12345678901234567890123456789012345678901234567890",
126
- "load_state": "not-found",
127
- "active_state": "inactive",
128
- "sub_state": "dead"
138
+ "service_name": "network.service",
139
+ "load_state": "loaded",
140
+ "active_state": "active",
141
+ "sub_state": "exited",
142
+ "unit_file_state": "bad",
143
+ "type": "forking"
129
144
  },
130
145
  {
131
146
  "service_name": "systemd-tmpfiles-clean.timer",
132
147
  "load_state": "loaded",
133
148
  "active_state": "active",
134
- "sub_state": "waiting"
149
+ "sub_state": "waiting",
150
+ "unit_file_state": "static",
151
+ "type": "n/a"
152
+ },
153
+ {
154
+ "service_name": "dummy_long_name_size_30_______",
155
+ "load_state": "not-found",
156
+ "active_state": "inactive",
157
+ "sub_state": "dead",
158
+ "unit_file_state": "n/a",
159
+ "type": "n/a"
135
160
  }
136
161
  ]
137
162
  }
@@ -139,60 +164,80 @@ $ ssc check -j | jq
139
164
  ```
140
165
 
141
166
  ```sh
142
- $ ssc check -a
167
+ $ ssc check --format=awesome_print
143
168
  [
144
- [0] #<Struct:SystemdServiceCheck::Base::Result:0x7fa4cec5aa10
145
- server = #<Struct:SystemdServiceCheck::Base::Server:0x7fa4cdbfc088
169
+ [0] #<Struct:SystemdServiceCheck::Base::Result:0x7fabeca9ad40
170
+ server = #<Struct:SystemdServiceCheck::Base::Server:0x7fabec9c6ea0
146
171
  env = "dev",
147
172
  hostname = "centos7",
148
173
  ip = "192.168.1.101",
149
- pass = "vagrant",
174
+ options = {
175
+ :password => "vagrant",
176
+ :keys => [
177
+ [0] ""
178
+ ],
179
+ :logger => #<Logger:0x00007fabec9c6478 @level=4, @progname=nil, @default_formatter=#<Logger::Formatter:0x00007fabec9c63d8 @datetime_format=nil>, @formatter=nil, @logdev=#<Logger::LogDevice:0x00007fabec9c6360 @shift_period_suffix=nil, @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<IO:<STDERR>>, @mon_owner=nil, @mon_count=0, @mon_mutex=#<Thread::Mutex:0x00007fabec9c6248>>>,
180
+ :password_prompt => #<Net::SSH::Prompt:0x00007fabec9c61d0>,
181
+ :user => "root"
182
+ },
150
183
  services = [
151
184
  [0] "sshd.service",
152
185
  [1] "firewalld.service",
153
- [2] "dummy.service",
154
- [3] "rsyslog.service",
155
- [4] "12345678901234567890123456789012345678901234567890",
156
- [5] "systemd-tmpfiles-clean.timer"
186
+ [2] "rsyslog.service",
187
+ [3] "network.service",
188
+ [4] "systemd-tmpfiles-clean.timer",
189
+ [5] "dummy_long_name_size_30_______"
157
190
  ],
158
191
  user = "root"
159
192
  >,
160
193
  services = [
161
- [0] #<Struct:SystemdServiceCheck::Base::Service:0x7fa4cf05a9a8
194
+ [0] #<Struct:SystemdServiceCheck::Base::Service:0x7fabed259798
162
195
  active_state = "active",
163
196
  load_state = "loaded",
164
197
  service_name = "sshd.service",
165
- sub_state = "running"
198
+ sub_state = "running",
199
+ type = "notify",
200
+ unit_file_state = "enabled"
166
201
  >,
167
- [1] #<Struct:SystemdServiceCheck::Base::Service:0x7fa4cf049748
202
+ [1] #<Struct:SystemdServiceCheck::Base::Service:0x7fabecabaeb0
168
203
  active_state = "inactive",
169
204
  load_state = "loaded",
170
205
  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"
206
+ sub_state = "dead",
207
+ type = "dbus",
208
+ unit_file_state = "disabled"
178
209
  >,
179
- [3] #<Struct:SystemdServiceCheck::Base::Service:0x7fa4cdb497a8
210
+ [2] #<Struct:SystemdServiceCheck::Base::Service:0x7fabecab7648
180
211
  active_state = "active",
181
212
  load_state = "loaded",
182
213
  service_name = "rsyslog.service",
183
- sub_state = "running"
214
+ sub_state = "running",
215
+ type = "notify",
216
+ unit_file_state = "enabled"
184
217
  >,
185
- [4] #<Struct:SystemdServiceCheck::Base::Service:0x7fa4cec7a180
186
- active_state = "inactive",
187
- load_state = "not-found",
188
- service_name = "12345678901234567890123456789012345678901234567890",
189
- sub_state = "dead"
218
+ [3] #<Struct:SystemdServiceCheck::Base::Service:0x7fabeca9e3f0
219
+ active_state = "active",
220
+ load_state = "loaded",
221
+ service_name = "network.service",
222
+ sub_state = "exited",
223
+ type = "forking",
224
+ unit_file_state = "bad"
190
225
  >,
191
- [5] #<Struct:SystemdServiceCheck::Base::Service:0x7fa4cec5ad80
226
+ [4] #<Struct:SystemdServiceCheck::Base::Service:0x7fabed8c6db0
192
227
  active_state = "active",
193
228
  load_state = "loaded",
194
229
  service_name = "systemd-tmpfiles-clean.timer",
195
- sub_state = "waiting"
230
+ sub_state = "waiting",
231
+ type = "n/a",
232
+ unit_file_state = "static"
233
+ >,
234
+ [5] #<Struct:SystemdServiceCheck::Base::Service:0x7fabeca9b1f0
235
+ active_state = "inactive",
236
+ load_state = "not-found",
237
+ service_name = "dummy_long_name_size_30_______",
238
+ sub_state = "dead",
239
+ type = "n/a",
240
+ unit_file_state = "n/a"
196
241
  >
197
242
  ]
198
243
  >
@@ -0,0 +1,27 @@
1
+ # CHANGELOG
2
+
3
+ Please confirm details from the following.
4
+
5
+ https://github.com/k-ta-yamada/systemd_service_check/releases
6
+
7
+ ## v1.0.0
8
+
9
+ 2018-01-28 JST
10
+
11
+ - issues: https://github.com/k-ta-yamada/systemd_service_check/milestone/1?closed=1
12
+ - release: https://github.com/k-ta-yamada/systemd_service_check/releases/tag/v1.0.0
13
+ - commits: https://github.com/k-ta-yamada/systemd_service_check/compare/v0.0.0...v1.0.0
14
+
15
+ ## v0.0.0
16
+
17
+ 2018-01-25 JST
18
+
19
+ - release: https://github.com/k-ta-yamada/systemd_service_check/releases/tag/v0.0.0
20
+ - commits: https://github.com/k-ta-yamada/systemd_service_check/compare/v0.0.0-alpha...v0.0.0
21
+
22
+ ## v0.0.0-alpha
23
+
24
+ 2018-01-23 JST
25
+
26
+ - release: https://github.com/k-ta-yamada/systemd_service_check/releases/tag/v0.0.0-alpha
27
+ - commits: https://github.com/k-ta-yamada/systemd_service_check/compare/4e4adbd...v0.0.0-alpha
@@ -1,34 +1,43 @@
1
1
  require 'net/ssh'
2
+ require 'thor'
2
3
  require 'yaml'
3
4
  require 'json'
4
- require 'active_support'
5
- require 'active_support/core_ext/string'
6
5
  require 'pry' # forDebug
7
6
 
8
7
  # SystemdServiceCheck
9
8
  module SystemdServiceCheck
10
9
  # Base
11
10
  class Base
12
- Server = Struct.new(:env, :ip, :user, :pass, :services, :hostname)
13
- Service = Struct.new(:service_name, :load_state, :active_state, :sub_state)
11
+ class InvalidOption < StandardError; end
12
+
13
+ Server = Struct.new(:env, :ip, :user, :options, :services, :hostname)
14
+ Service = Struct.new(:service_name, :load_state, :active_state, :sub_state, :unit_file_state, :type)
14
15
  Result = Struct.new(:server, :services)
15
16
 
16
- ENVS = %w[dev stg prd].freeze
17
17
  # TODO: systemctl show -p
18
- STATES = %w[LoadState ActiveState SubState].freeze
18
+ STATES = %w[LoadState ActiveState SubState UnitFileState Type].freeze
19
19
  # TODO: setting by yaml
20
20
  SHOW_GREP = /env/i
21
21
 
22
- attr_reader :argv, :servers, :target_env, :target_servers, :results
22
+ attr_reader :argv, :env_names, :servers, :target_env, :target_servers, :results
23
23
 
24
- def initialize(argv: nil, yaml: nil)
25
- @argv = argv || []
26
- @servers = []
24
+ def initialize(argv, yaml)
25
+ @argv = argv || []
26
+ @env_names = []
27
+ @servers = []
27
28
  @target_env = []
28
29
  @target_servers = []
29
- @results = []
30
+ @results = []
31
+
32
+ raise InvalidOption if yaml.nil? || yaml.empty?
30
33
  load_settings(yaml)
31
34
  configure_target_servers
35
+ run
36
+ rescue InvalidOption => e
37
+ puts "<#{e}>",
38
+ " Argument yaml must not be nil or empty.",
39
+ " yaml.class: [#{yaml.class}]"
40
+ puts " #{e.backtrace_locations.first}"
32
41
  end
33
42
 
34
43
  def run
@@ -44,16 +53,14 @@ module SystemdServiceCheck
44
53
 
45
54
  private
46
55
 
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)
56
+ def load_settings(file) # rubocop:disable Metrics/AbcSize
57
+ yaml = JSON.parse(YAML.load_file(File.expand_path(file)).to_json,
58
+ symbolize_names: true)
59
+ @env_names = yaml[:servers].map { |s| s[:env] }.uniq
60
+ @servers = yaml[:servers].map do |s|
61
+ raise InvalidOption, "ENV: #{s[:env]}" if [s[:password], s[:key]].all?(nil)
62
+ options = { password: s[:password], keys: [s[:key] || ""] }
63
+ Server.new(s[:env], s[:ip], s[:user], options, s[:services])
57
64
  end
58
65
  end
59
66
 
@@ -63,19 +70,20 @@ module SystemdServiceCheck
63
70
  end
64
71
 
65
72
  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'
73
+ @target_env = @env_names & @argv
74
+
75
+ # If there is no argument.
76
+ @target_env = @env_names.first if @target_env.empty? && @argv.empty?
77
+
78
+ # If there is only one argument and it is `all`.
79
+ @target_env = @env_names if @argv.size == 1 && argv.first == 'all'
71
80
  end
72
81
 
73
82
  def ssh(server)
74
83
  services = []
75
- Net::SSH.start(server[:ip], server[:user], password: server[:pass]) do |ssh|
84
+ Net::SSH.start(server[:ip], server[:user], server[:options]) do |ssh|
76
85
  server[:hostname] = ssh.exec!('hostname').chop
77
86
  services = server[:services].map { |service_name| systemd(ssh, service_name) }
78
-
79
87
  # TODO: store in Result object
80
88
  # puts ssh.exec!("systemctl list-timers")
81
89
  end
@@ -86,13 +94,16 @@ module SystemdServiceCheck
86
94
  states = ssh.exec!("systemctl show #{service_name} -p #{STATES.join(",")}")
87
95
  .split("\n")
88
96
  .map { |v| v.split("=") }
89
- .each_with_object({}) { |(k, v), memo| memo[k.underscore.to_sym] = v }
97
+ .map { |v| [Thor::Util.snake_case(v[0]).to_sym, v[1]] }
98
+ .each_with_object({}) { |(k, v), memo| memo[k] = v }
90
99
 
91
100
  Service.new(
92
101
  service_name,
93
102
  states[:load_state],
94
103
  states[:active_state],
95
- states[:sub_state]
104
+ states[:sub_state],
105
+ states[:unit_file_state] || "n/a",
106
+ states[:type] || "n/a"
96
107
  )
97
108
  end
98
109
  end
@@ -2,48 +2,40 @@ require 'systemd_service_check'
2
2
  require 'thor'
3
3
  require 'awesome_print'
4
4
  require 'table_print'
5
- require 'highline'
6
5
  require 'pry' # forDebug
7
6
 
7
+ # ###############################################################################
8
+ # MEMO: This patch is a copy of https://github.com/arches/table_print/pull/70
9
+ # ###############################################################################
10
+ require 'table_print-patch-pr70/column'
11
+ require 'table_print-patch-pr70/formatter'
12
+
8
13
  module SystemdServiceCheck
9
14
  # CLI
10
15
  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'
16
+ class InvalidFormatOption < StandardError; end
17
+
18
+ option :format,
19
+ type: :string,
20
+ aliases: '-f',
21
+ desc: "[t]able, [j]son, [a]wesome_print",
22
+ banner: 'table',
23
+ default: 'table'
28
24
  option :yaml,
29
25
  type: :string,
30
26
  aliases: '-y',
31
27
  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
28
+ banner: './systemd_service_check.sample.yml',
29
+ default: './systemd_service_check.sample.yml'
30
+
31
+ desc "check ENV [ENV...] options", "check target ENV Servers."
40
32
  def check(*env)
41
- raise InvalidOption if options.values.count(true) > 1
42
- @ssc = Base.new(argv: env, yaml: options[:yaml])
43
- @ssc.run
33
+ raise InvalidFormatOption unless format_option_validate
34
+ @ssc = Base.new(env, options[:yaml])
44
35
  disp
45
- rescue InvalidOption => e
46
- puts "<#{e}>: Multiple display format options can not be specified"
36
+ rescue InvalidFormatOption => e
37
+ puts "<#{e}>",
38
+ " [#{options[:format]}] is invalid value."
47
39
  puts " #{e.backtrace_locations.first}"
48
40
  end
49
41
 
@@ -54,10 +46,15 @@ module SystemdServiceCheck
54
46
 
55
47
  private
56
48
 
49
+ def format_option_validate
50
+ %w[t table j json a awesome_print].include? options[:format]
51
+ end
52
+
57
53
  def disp
58
- if options[:json] then disp_json
59
- elsif options[:awesome] then disp_ap
60
- else disp_table
54
+ case options[:format]
55
+ when 't', 'table' then disp_table
56
+ when 'j', 'json' then disp_json
57
+ when 'a', 'awesome_print' then disp_ap
61
58
  end
62
59
  end
63
60
 
@@ -69,7 +66,7 @@ module SystemdServiceCheck
69
66
  ap @ssc.results
70
67
  end
71
68
 
72
- COLS = %i[env ip hostname user service_name load_state active_state sub_state].freeze
69
+ COLS = %i[env ip hostname user service_name load_state active_state sub_state unit_file_state type].freeze
73
70
  def disp_table # rubocop:disable Metrics/AbcSize
74
71
  service_name_width =
75
72
  @ssc.results.map(&:services).flatten.map(&:service_name).map(&:size).max
@@ -79,6 +76,13 @@ module SystemdServiceCheck
79
76
  data = services.map { |s| result.server.to_h.merge(s.to_h) }
80
77
 
81
78
  tp(data, COLS, service_name: { width: service_name_width })
79
+ puts
80
+ end
81
+
82
+ # ref: https://github.com/erikhuda/thor/blob/master/lib/thor/shell.rb#L14
83
+ if RbConfig::CONFIG["host_os"] =~ /mswin|mingw/ && !ENV["ANSICON"]
84
+ puts "-- For colors on windows, please setup `ANSICON`.",
85
+ "-- ANSICON: https://github.com/adoxa/ansicon"
82
86
  end
83
87
  end
84
88
 
@@ -86,16 +90,18 @@ module SystemdServiceCheck
86
90
  services.map do |s|
87
91
  s.class.new(
88
92
  s.service_name,
89
- color_state(s.load_state, :==, "loaded"),
90
- color_state(s.active_state, :==, "active"),
91
- color_state(s.sub_state, :!=, "dead")
93
+ color_state(s.load_state, :==, "loaded"),
94
+ color_state(s.active_state, :==, "active"),
95
+ color_state(s.sub_state, :!=, "dead"),
96
+ color_state(s.unit_file_state, :==, "enabled"),
97
+ s.type
92
98
  )
93
99
  end
94
100
  end
95
101
 
96
102
  def color_state(obj, method, arg)
97
103
  green_or_red = obj.send(method, arg) ? :green : :red
98
- HighLine.color(obj, green_or_red)
104
+ set_color(obj, green_or_red)
99
105
  end
100
106
  end
101
107
  end
@@ -1,3 +1,3 @@
1
1
  module SystemdServiceCheck
2
- VERSION = "0.0.0".freeze
2
+ VERSION = "1.0.0".freeze
3
3
  end
@@ -1,7 +1,6 @@
1
- # require 'pry';binding.pry
2
1
  require "systemd_service_check/version"
3
- require_relative "systemd_service_check/base"
2
+ require "systemd_service_check/base"
4
3
 
5
4
  # module SystemdServiceCheck
6
- # # Your code goes here...
5
+ # # # Your code goes here...
7
6
  # end
@@ -0,0 +1,27 @@
1
+ # ###############################################################################
2
+ # MEMO: This patch is a copy of https://github.com/arches/table_print/pull/70
3
+ # ###############################################################################
4
+ module TablePrint
5
+ class Column
6
+ def data_width
7
+ if multibyte_count
8
+ [
9
+ name.each_char.collect { |c| c.bytesize == 1 ? 1 : 2 }.inject(0, &:+),
10
+ Array(data).compact.collect { |s| escape_strip(s.to_s) }.collect { |m| m.each_char.collect { |n| n.bytesize == 1 ? 1 : 2 }.inject(0, &:+) }.max
11
+ ].compact.max || 0
12
+ else
13
+ [
14
+ name.length,
15
+ Array(data).compact.collect { |s| escape_strip(s.to_s) }.collect(&:length).max
16
+ ].compact.max || 0
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def escape_strip(string)
23
+ return string unless string.class == String
24
+ string.gsub(/\e\[([0-9]{1,2};){0,2}[0-9]{1,2}m/, '')
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,33 @@
1
+ # ###############################################################################
2
+ # MEMO: This patch is a copy of https://github.com/arches/table_print/pull/70
3
+ # ###############################################################################
4
+ module TablePrint
5
+ class FixedWidthFormatter
6
+ def format(value)
7
+ padding = width - length(escape_strip(value).to_s)
8
+ truncate(value) + (padding < 0 ? '' : " " * padding)
9
+ end
10
+
11
+ private
12
+
13
+ def truncate(value)
14
+ return "" unless value
15
+
16
+ value = value.to_s
17
+ value_stripped, stripped_stuff = escape_strip(value, true)
18
+ return value unless value_stripped.length > width
19
+ "#{value[0..(width + stripped_stuff.length) - 4]}..."
20
+ end
21
+
22
+ def escape_strip(string, return_stripped_stuff = false)
23
+ return string unless string.class == String
24
+ stripped_stuff = ''
25
+ string_stripped = string.gsub(/\e\[([0-9]{1,2};){0,2}[0-9]{1,2}m/) do |s|
26
+ stripped_stuff << s
27
+ s = ''
28
+ end
29
+ return string_stripped, stripped_stuff if return_stripped_stuff == true
30
+ string_stripped
31
+ end
32
+ end
33
+ end
@@ -10,9 +10,11 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["key.luvless@gmail.com"]
11
11
 
12
12
  spec.summary =
13
- 'It is a script that checks systemd service on the server using net-ssh.'
13
+ 'This gem provide `ssc` command to check multiple `systemd service` \
14
+ etc on multiple remote servers using net-ssh.'
14
15
  spec.description =
15
- 'It is a script that checks systemd service on the server using net-ssh.'
16
+ 'This gem provide `ssc` command to check multiple `systemd service` \
17
+ etc on multiple remote servers using net-ssh.'
16
18
  spec.homepage = "https://github.com/k-ta-yamada/systemd_service_check"
17
19
  spec.license = "MIT"
18
20
 
@@ -42,9 +44,7 @@ Gem::Specification.new do |spec|
42
44
  spec.add_development_dependency 'pry-theme'
43
45
  spec.add_development_dependency 'rubocop'
44
46
 
45
- spec.add_dependency 'activesupport'
46
47
  spec.add_dependency 'awesome_print'
47
- spec.add_dependency 'highline'
48
48
  spec.add_dependency 'net-ssh'
49
49
  spec.add_dependency 'pry'
50
50
  spec.add_dependency 'table_print'
@@ -0,0 +1,32 @@
1
+ # SERVICES TO BE CHECKED [Array]
2
+ services: &default_services
3
+ - sshd.service
4
+ - firewalld.service
5
+ - rsyslog.service
6
+ - network.service
7
+ - systemd-tmpfiles-clean.timer
8
+ - dummy_long_name_size_30_______
9
+
10
+ # SERVERS TO BE CHECKED [Array of Hash]
11
+ servers:
12
+ # only password
13
+ - env: dev
14
+ ip: 192.168.1.101
15
+ user: root
16
+ password: vagrant
17
+ # key: ./.vagrant/machines/centos7/virtualbox/private_key
18
+ services: *default_services
19
+ # only private key
20
+ - env: stg
21
+ ip: 192.168.1.101
22
+ user: root
23
+ # password: vagrant
24
+ key: ./.vagrant/machines/centos7/virtualbox/private_key
25
+ services: *default_services
26
+ # password and private key (private key will be used preferentially)
27
+ - env: prd
28
+ ip: 192.168.1.101
29
+ user: root
30
+ password: invalid-dummy-password
31
+ key: ./.vagrant/machines/centos7/virtualbox/private_key
32
+ services: *default_services
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: systemd_service_check
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - k-ta-yamada
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-25 00:00:00.000000000 Z
11
+ date: 2018-01-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -94,20 +94,6 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
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
97
  - !ruby/object:Gem::Dependency
112
98
  name: awesome_print
113
99
  requirement: !ruby/object:Gem::Requirement
@@ -122,20 +108,6 @@ dependencies:
122
108
  - - ">="
123
109
  - !ruby/object:Gem::Version
124
110
  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
111
  - !ruby/object:Gem::Dependency
140
112
  name: net-ssh
141
113
  requirement: !ruby/object:Gem::Requirement
@@ -192,7 +164,9 @@ dependencies:
192
164
  - - ">="
193
165
  - !ruby/object:Gem::Version
194
166
  version: '0'
195
- description: It is a script that checks systemd service on the server using net-ssh.
167
+ description: |-
168
+ This gem provide `ssc` command to check multiple `systemd service` \
169
+ etc on multiple remote servers using net-ssh.
196
170
  email:
197
171
  - key.luvless@gmail.com
198
172
  executables:
@@ -206,7 +180,6 @@ files:
206
180
  - ".travis.yml"
207
181
  - CODE_OF_CONDUCT.md
208
182
  - Gemfile
209
- - LICENSE
210
183
  - LICENSE.txt
211
184
  - README.md
212
185
  - Rakefile
@@ -215,11 +188,14 @@ files:
215
188
  - bin/setup
216
189
  - bin/ssc
217
190
  - lib/systemd_service_check.rb
218
- - lib/systemd_service_check.yml
191
+ - lib/systemd_service_check/CHANGELOG.md
219
192
  - lib/systemd_service_check/base.rb
220
193
  - lib/systemd_service_check/cli.rb
221
194
  - lib/systemd_service_check/version.rb
195
+ - lib/table_print-patch-pr70/column.rb
196
+ - lib/table_print-patch-pr70/formatter.rb
222
197
  - systemd_service_check.gemspec
198
+ - systemd_service_check.sample.yml
223
199
  homepage: https://github.com/k-ta-yamada/systemd_service_check
224
200
  licenses:
225
201
  - MIT
@@ -243,5 +219,6 @@ rubyforge_project:
243
219
  rubygems_version: 2.7.3
244
220
  signing_key:
245
221
  specification_version: 4
246
- summary: It is a script that checks systemd service on the server using net-ssh.
222
+ summary: This gem provide `ssc` command to check multiple `systemd service` \ etc
223
+ on multiple remote servers using net-ssh.
247
224
  test_files: []
data/LICENSE DELETED
@@ -1,21 +0,0 @@
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.
@@ -1,26 +0,0 @@
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