checkson 0.5 → 0.6
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 +4 -4
- data/lib/checkson.rb +1 -0
- data/lib/checkson/apiclient.rb +75 -0
- data/lib/checkson/checks/packagemanagers/abstractpkgmgr.rb +23 -0
- data/lib/checkson/checks/packages.rb +25 -0
- data/lib/checkson/checks/process.rb +1 -3
- data/lib/checkson/ui.rb +32 -11
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8884f708c7fbe9c64243ce90b8942d822ee0bf3b664f1198e97198c7af339b1d
|
4
|
+
data.tar.gz: 9e1289046c0f46788c918b3b611f6424a2c15fe632523487d7f6750faa2c3c06
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aacb646e006f6af49070ad5f7e299bdb546c8cfea6c6b3d99fb92aea3d9f361f13b523f3f3546951d77b9a2ccba7b2a50ac19661b7326a061c05224aaa1e53e2
|
7
|
+
data.tar.gz: 76fde610194ed4e82fc4d653b18bed353dc0d269e1d99079faada485f29806c5e27531e2b601c360bb6af9145f65d4c70db9027a2509227ec397ced2de0f6d61
|
data/lib/checkson.rb
CHANGED
@@ -0,0 +1,75 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'net/http'
|
4
|
+
require 'json'
|
5
|
+
require 'uri'
|
6
|
+
require 'erb'
|
7
|
+
require 'socket'
|
8
|
+
require 'digest'
|
9
|
+
require 'logger'
|
10
|
+
|
11
|
+
module Checkson
|
12
|
+
class APIClient
|
13
|
+
attr_reader :apiaddr
|
14
|
+
attr_reader :outfile
|
15
|
+
def initialize(apiaddr, outfile)
|
16
|
+
@apiaddr = apiaddr
|
17
|
+
@outfile = outfile
|
18
|
+
end
|
19
|
+
|
20
|
+
def getchecks
|
21
|
+
template = <<-TEMPLATE
|
22
|
+
<%@checks.each do |check| %>
|
23
|
+
check '<%=check["description"]%>' do
|
24
|
+
using <%=check["check"]%>
|
25
|
+
<%check["sets"].each do |key,value| %>
|
26
|
+
set :<%=key%>, "<%=value%>"
|
27
|
+
<%end%>
|
28
|
+
<%unless check["helps"].eql?(nil)%>
|
29
|
+
<%check["helps"].each do |help| %>
|
30
|
+
help '<%= help %>'
|
31
|
+
<%end%>
|
32
|
+
<%end%>
|
33
|
+
end
|
34
|
+
<% end %>
|
35
|
+
TEMPLATE
|
36
|
+
querydata = get
|
37
|
+
@checks = querydata['checks'] if querydata['status'].eql? 'ok'
|
38
|
+
begin
|
39
|
+
remove_empty_lines(ERB.new(template).result(binding))
|
40
|
+
rescue StandardError
|
41
|
+
warn querydata['error']
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def writechecks
|
46
|
+
File.open(@outfile, 'w') do |f|
|
47
|
+
f.write(getchecks)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def remove_empty_lines(string)
|
54
|
+
regex = /^[\s]*$\n/
|
55
|
+
string.gsub(regex, '')
|
56
|
+
end
|
57
|
+
|
58
|
+
def get
|
59
|
+
token = ENV['CHECKSON_API_KEY']
|
60
|
+
token ||= 'empty'
|
61
|
+
hostname = Socket.gethostname
|
62
|
+
query("#{keyhash(token)}/get/#{hostname}")
|
63
|
+
end
|
64
|
+
|
65
|
+
def query(command)
|
66
|
+
@ep = "#{@apiaddr}/api/v1"
|
67
|
+
route = "#{@ep}/#{command}"
|
68
|
+
JSON.parse(Net::HTTP.get(URI.parse(route)))
|
69
|
+
end
|
70
|
+
|
71
|
+
def keyhash(token)
|
72
|
+
Digest::SHA512.hexdigest(token)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Checkson
|
4
|
+
module Check
|
5
|
+
module Pkganager
|
6
|
+
class AbstractPkgmanager
|
7
|
+
def self.descendants
|
8
|
+
ObjectSpace.each_object(Class).select { |klass| klass < self }
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class Pkgmanager < AbstractPkgmanager
|
13
|
+
def self.adapters
|
14
|
+
AbstractPkgmanager.descendants - [self]
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.adapter
|
18
|
+
@adapter ||= adapters.find { |adapter| adapter.supports_os? Facter['osfamily'].value.downcase }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Checkson
|
4
|
+
module Check
|
5
|
+
class Shell < Checkson::Check::Base
|
6
|
+
def initialize(opts = {})
|
7
|
+
@opts = (@opts || {}).merge(opts)
|
8
|
+
super()
|
9
|
+
end
|
10
|
+
|
11
|
+
def check
|
12
|
+
raise ArgumentError, 'No code given' unless @opts[:code]
|
13
|
+
|
14
|
+
execute(@opts[:code])
|
15
|
+
failed! unless $?.exitstatus.zero?
|
16
|
+
end
|
17
|
+
|
18
|
+
protected
|
19
|
+
|
20
|
+
def execute(command)
|
21
|
+
`sh -c "#{command.gsub(/"/, '\"')}"`
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -7,9 +7,7 @@ module Checkson
|
|
7
7
|
class Process < Checkson::Check::Shell
|
8
8
|
def initialize(opts = {})
|
9
9
|
@opts = opts
|
10
|
-
unless @opts[:pidfile] || @opts[:name]
|
11
|
-
raise ArgumentError, 'Neither pidfile nor process name given'
|
12
|
-
end
|
10
|
+
raise ArgumentError, 'Neither pidfile nor process name given' unless @opts[:pidfile] || @opts[:name]
|
13
11
|
|
14
12
|
super()
|
15
13
|
end
|
data/lib/checkson/ui.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'optparse'
|
4
|
+
require 'logger'
|
5
|
+
require 'uri'
|
4
6
|
|
5
7
|
module Checkson
|
6
8
|
# Simple user interface for checkson
|
@@ -11,15 +13,15 @@ module Checkson
|
|
11
13
|
start_checks
|
12
14
|
end
|
13
15
|
|
14
|
-
|
15
|
-
|
16
|
-
def winsize
|
17
|
-
require 'io/console'
|
18
|
-
IO.console.winsize
|
19
|
-
end
|
16
|
+
private
|
20
17
|
|
21
18
|
def start_checks
|
22
|
-
|
19
|
+
if @opts[:datasource].eql? :api
|
20
|
+
client = Checkson::APIClient.new(@opts[:endpoint], '/tmp/checkson.rb')
|
21
|
+
client.writechecks
|
22
|
+
@opts[:config_file] = client.outfile
|
23
|
+
end
|
24
|
+
@config = Checkson::Config.new(@opts[:config_file])
|
23
25
|
|
24
26
|
@num_checks = @config.checks.length
|
25
27
|
@num_ok = 0
|
@@ -35,8 +37,10 @@ module Checkson
|
|
35
37
|
|
36
38
|
def set_default_options
|
37
39
|
@opts = {
|
38
|
-
|
39
|
-
verbose: false
|
40
|
+
datasource: :file,
|
41
|
+
verbose: false,
|
42
|
+
config_file: '/etc/checkson.rb',
|
43
|
+
endpoint: 'https://checkson.fsrv.services:8080'
|
40
44
|
}
|
41
45
|
end
|
42
46
|
|
@@ -46,14 +50,26 @@ module Checkson
|
|
46
50
|
opts.on('-v', '--[no-]verbose', 'Run verbosely') do |v|
|
47
51
|
@opts[:verbose] = v
|
48
52
|
end
|
49
|
-
opts.on('-
|
53
|
+
opts.on('-d', '--datasource [TYPE]', %i[file api], 'Set datasource type', '(file, api)') do |d|
|
54
|
+
@opts[:datasource] = d
|
55
|
+
end
|
56
|
+
opts.on('-f', '--file [FILE]', 'Config file to use') do |file|
|
50
57
|
if File.exist?(file)
|
51
|
-
@opts[:
|
58
|
+
@opts[:config_file] = file
|
52
59
|
else
|
53
60
|
die("Config file #{file} does not exist")
|
54
61
|
end
|
55
62
|
end
|
63
|
+
opts.on('-e', '--endpoint [ADDR]', 'Config api to use') do |ep|
|
64
|
+
if ep =~ URI::DEFAULT_PARSER.make_regexp
|
65
|
+
@opts[:endpoint] = ep
|
66
|
+
else
|
67
|
+
die('URL of endpoint not valid')
|
68
|
+
end
|
69
|
+
end
|
56
70
|
end.parse!
|
71
|
+
logger = Logger.new(STDOUT)
|
72
|
+
logger.info "Using datasource `#{@opts[:datasource]}`"
|
57
73
|
end
|
58
74
|
|
59
75
|
def output(sym, check)
|
@@ -69,6 +85,11 @@ module Checkson
|
|
69
85
|
end
|
70
86
|
end
|
71
87
|
|
88
|
+
def winsize
|
89
|
+
require 'io/console'
|
90
|
+
IO.console.winsize
|
91
|
+
end
|
92
|
+
|
72
93
|
def die(msg)
|
73
94
|
warn(msg)
|
74
95
|
exit 1
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: checkson
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.6'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Florian Bauer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-04-13 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A simple framework for checking node facts
|
14
14
|
email: florian@fsrv.xyz
|
@@ -20,8 +20,11 @@ files:
|
|
20
20
|
- README.md
|
21
21
|
- bin/checkson
|
22
22
|
- lib/checkson.rb
|
23
|
+
- lib/checkson/apiclient.rb
|
23
24
|
- lib/checkson/checks/base.rb
|
24
25
|
- lib/checkson/checks/dns.rb
|
26
|
+
- lib/checkson/checks/packagemanagers/abstractpkgmgr.rb
|
27
|
+
- lib/checkson/checks/packages.rb
|
25
28
|
- lib/checkson/checks/process.rb
|
26
29
|
- lib/checkson/checks/shell.rb
|
27
30
|
- lib/checkson/config.rb
|