livestatus-client 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +80 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/exe/livestatus +4 -0
- data/lib/livestatus-client.rb +147 -0
- data/lib/livestatus-client/version.rb +3 -0
- data/livestatus-client.gemspec +29 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ee718d293c12df414dd19bfc044acad024e2c084
|
4
|
+
data.tar.gz: fef5cab8aae86823270f487ae5e5e7b98e7e50d9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 70845d3b93ee369510be5b1ebf219768cd7cb16d17f2f34bd37044d89446775cdf28600d301d04b94692a4f86af99b4473c564f606f75cae15750c4df2c70b2b
|
7
|
+
data.tar.gz: 857a0fe3c730df07bb5cafe777bd77a6e87313d0170f8440902a3f37f0c80c4d43733d894f24d99cbdd9dfcc544298926fca862b505ddaec0bce8e7bd8d37ddf
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Eric Herot
|
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,80 @@
|
|
1
|
+
# LivestatusClient
|
2
|
+
|
3
|
+
This is a very "lightweight" command line client (written in Ruby) for the [MK Livestatus](http://mathias-kettner.de/checkmk_livestatus.html) Nagios module.
|
4
|
+
|
5
|
+
It makes simple queries and commands against the Livestatus API and returns slightly parsed JSON responses. Filters and complex queries (beyond just requesting particular data types and columns) are not yet supported.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Install it yourself as:
|
10
|
+
|
11
|
+
$ gem install livestatus-client
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
Run the `livestatus` executable:
|
16
|
+
|
17
|
+
```bash
|
18
|
+
$ livestatus --help
|
19
|
+
Options:
|
20
|
+
-h, --host=<s> Livestatus host
|
21
|
+
-p, --port=<s> Livestatus port
|
22
|
+
-q, --query=<s> The query string
|
23
|
+
-l, --columns=<s> Which columns to display (comma separated)
|
24
|
+
-i, --list List available columns
|
25
|
+
-c, --command=<s> Command to send
|
26
|
+
-a, --arguments=<s> Command argumenets (comma separated)
|
27
|
+
-e, --help Show this message
|
28
|
+
```
|
29
|
+
|
30
|
+
### Listing hosts with any configured downtimes
|
31
|
+
|
32
|
+
```bash
|
33
|
+
$ livestatus -h 10.99.0.1 -p 50000 -q hosts -l downtimes
|
34
|
+
```
|
35
|
+
|
36
|
+
This should return something like this:
|
37
|
+
```json
|
38
|
+
{
|
39
|
+
"default-ubuntu-1404": {
|
40
|
+
"downtimes": [
|
41
|
+
|
42
|
+
]
|
43
|
+
},
|
44
|
+
"stage-api": {
|
45
|
+
"downtimes": [
|
46
|
+
|
47
|
+
]
|
48
|
+
},
|
49
|
+
"test-dns": {
|
50
|
+
"downtimes": [
|
51
|
+
1483559124159689
|
52
|
+
]
|
53
|
+
}
|
54
|
+
}
|
55
|
+
```
|
56
|
+
|
57
|
+
### Configuring a service downtime
|
58
|
+
|
59
|
+
```bash
|
60
|
+
$ livestatus \
|
61
|
+
-h 10.99.0.1 -p 50000 \
|
62
|
+
-c SCHEDULE_HOST_DOWNTIME \
|
63
|
+
-a 'server-ubuntu-1404,1483644522,1483644523,1,0,0,yourname,Some downtime coment'
|
64
|
+
```
|
65
|
+
|
66
|
+
[Valid Nagios external commands](https://old.nagios.org/developerinfo/externalcommands/commandlist.php) (Click on individual commands for the argument syntax. Note that the script replaces `,` with `;` to prevent conflicts with the shell)
|
67
|
+
|
68
|
+
### To get a list of valid columns for a data type
|
69
|
+
|
70
|
+
```bash
|
71
|
+
$ livestatus -h 10.99.0.1 -p 50000 -q hosts --list
|
72
|
+
```
|
73
|
+
|
74
|
+
## Contributing
|
75
|
+
|
76
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/evertrue/livestatus-client.
|
77
|
+
|
78
|
+
## License
|
79
|
+
|
80
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "livestatus/client"
|
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
|
data/bin/setup
ADDED
data/exe/livestatus
ADDED
@@ -0,0 +1,147 @@
|
|
1
|
+
require 'livestatus-client/version'
|
2
|
+
require 'trollop'
|
3
|
+
require 'yaml'
|
4
|
+
require 'socket'
|
5
|
+
require 'byebug'
|
6
|
+
require 'json'
|
7
|
+
|
8
|
+
class LivestatusClient
|
9
|
+
def run
|
10
|
+
if opts[:query]
|
11
|
+
if opts[:list]
|
12
|
+
display_columns
|
13
|
+
else
|
14
|
+
puts JSON.pretty_generate query opts[:query]
|
15
|
+
end
|
16
|
+
elsif opts[:command]
|
17
|
+
command
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def display_columns
|
22
|
+
printf "%-30s %-9s %s\n", 'column', '(type)', 'description'
|
23
|
+
120.times { print '-' }
|
24
|
+
puts
|
25
|
+
query('columns').select { |col| col[2] == opts[:query] }.each do |col|
|
26
|
+
printf "%-30s %-9s %s\n", col[1], "(#{col[3]})", col[0]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def command
|
31
|
+
args = [opts[:command]] + opts[:arguments].split(',')
|
32
|
+
cmd_string = "COMMAND [#{Time.now.to_i}] #{args.join(';')}\n" \
|
33
|
+
"ResponseHeader: fixed16\n" \
|
34
|
+
"OutputFormat: json\n"
|
35
|
+
|
36
|
+
socket.write cmd_string + "\n"
|
37
|
+
response = read
|
38
|
+
socket.close
|
39
|
+
puts response
|
40
|
+
end
|
41
|
+
|
42
|
+
def key
|
43
|
+
key_map = {
|
44
|
+
'downtimes' => 'id',
|
45
|
+
'services' => 'description',
|
46
|
+
'servicesbygroup' => 'description',
|
47
|
+
'servicesbyhostgroup' => 'description',
|
48
|
+
'comments' => 'service_description'
|
49
|
+
}
|
50
|
+
|
51
|
+
key_map[opts[:query]] || 'name'
|
52
|
+
end
|
53
|
+
|
54
|
+
def query(keyword)
|
55
|
+
query_string = "GET #{keyword}\n" \
|
56
|
+
"ResponseHeader: fixed16\n" \
|
57
|
+
"OutputFormat: json\n"
|
58
|
+
query_string += "Columns: #{columns.join ' '}\n" unless opts[:list]
|
59
|
+
|
60
|
+
socket.write query_string + "\n"
|
61
|
+
response = read
|
62
|
+
socket.close
|
63
|
+
return response unless opts[:columns]
|
64
|
+
parse response
|
65
|
+
end
|
66
|
+
|
67
|
+
def parse(data)
|
68
|
+
# Outputs a somewhat more useful data structure that looks like this:
|
69
|
+
#
|
70
|
+
# {
|
71
|
+
# "item1": {
|
72
|
+
# "column1": [
|
73
|
+
# "value1",
|
74
|
+
# "value2"
|
75
|
+
# ],
|
76
|
+
# "column2": [
|
77
|
+
# "value1",
|
78
|
+
# "value2"
|
79
|
+
# ]
|
80
|
+
# },
|
81
|
+
# "item2": {
|
82
|
+
# "column1": [
|
83
|
+
# ],
|
84
|
+
# "column2": [
|
85
|
+
# "value1"
|
86
|
+
# ]
|
87
|
+
# }
|
88
|
+
# }
|
89
|
+
|
90
|
+
data.each_with_object({}) do |item, memo|
|
91
|
+
item_name = item.shift
|
92
|
+
# columns[1..-1] because we assume the first value is a name or id tag
|
93
|
+
memo[item_name] = columns[1..-1].each_with_object({}) do |col, cols_memo|
|
94
|
+
cols_memo[col] = item.shift
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def read
|
100
|
+
header = socket.read 16
|
101
|
+
|
102
|
+
response = {
|
103
|
+
code: header[0..2].to_i,
|
104
|
+
length: header[4..14].chomp.to_i
|
105
|
+
}
|
106
|
+
|
107
|
+
fail "Received error response #{response[:code]}" unless response[:code] == 200
|
108
|
+
|
109
|
+
JSON.parse socket.read response[:length]
|
110
|
+
end
|
111
|
+
|
112
|
+
def socket
|
113
|
+
return @socket unless !@socket || @socket.closed?
|
114
|
+
@socket = TCPSocket.open opts[:host], opts[:port]
|
115
|
+
end
|
116
|
+
|
117
|
+
# def conf
|
118
|
+
# @conf ||= begin
|
119
|
+
# return {} unless File.exist? '/etc/livestatus-client.yaml'
|
120
|
+
# YAML.load_file '/etc/livestatus-client.yaml'
|
121
|
+
# end
|
122
|
+
# end
|
123
|
+
|
124
|
+
def columns
|
125
|
+
return [key] unless opts[:columns]
|
126
|
+
[key] + opts[:columns].split(',')
|
127
|
+
end
|
128
|
+
|
129
|
+
def opts
|
130
|
+
@opts ||= begin
|
131
|
+
opts = Trollop.options do
|
132
|
+
version "livestatus-client #{LivestatusClient::VERSION} (c) 2017 Evertrue"
|
133
|
+
opt :host, 'Livestatus host', short: 'h', type: String
|
134
|
+
opt :port, 'Livestatus port', short: 'p', type: String
|
135
|
+
opt :query, 'The query string', short: 'q', type: String
|
136
|
+
opt :columns, 'Which columns to display (comma separated)', short: 'l', type: String
|
137
|
+
opt :list, 'List available columns'
|
138
|
+
opt :command, 'Command to send', short: 'c', type: String
|
139
|
+
opt :arguments, 'Command argumenets (comma separated)', short: 'a', type: String
|
140
|
+
end
|
141
|
+
|
142
|
+
fail 'Either a query or a command must be specified' unless opts[:query] || opts[:command]
|
143
|
+
|
144
|
+
opts
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'livestatus-client/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "livestatus-client"
|
8
|
+
spec.version = LivestatusClient::VERSION
|
9
|
+
spec.authors = ["Eric Herot"]
|
10
|
+
spec.email = ["eric.github@herot.com"]
|
11
|
+
|
12
|
+
spec.summary = 'This is a very "lightweight" command line client ' \
|
13
|
+
'(written in Ruby) for the MK Livestatus Nagios module.'
|
14
|
+
spec.homepage = "https://github.com/evertrue/livestatus-client"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_dependency 'trollop'
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: livestatus-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Eric Herot
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-01-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: trollop
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.13'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.13'
|
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: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- eric.github@herot.com
|
72
|
+
executables:
|
73
|
+
- livestatus
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rspec"
|
79
|
+
- ".travis.yml"
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- bin/console
|
85
|
+
- bin/setup
|
86
|
+
- exe/livestatus
|
87
|
+
- lib/livestatus-client.rb
|
88
|
+
- lib/livestatus-client/version.rb
|
89
|
+
- livestatus-client.gemspec
|
90
|
+
homepage: https://github.com/evertrue/livestatus-client
|
91
|
+
licenses:
|
92
|
+
- MIT
|
93
|
+
metadata: {}
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 2.5.1
|
111
|
+
signing_key:
|
112
|
+
specification_version: 4
|
113
|
+
summary: This is a very "lightweight" command line client (written in Ruby) for the
|
114
|
+
MK Livestatus Nagios module.
|
115
|
+
test_files: []
|