sensu-plugins-docker-swarm 3.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +179 -0
- data/LICENSE +22 -0
- data/README.md +40 -0
- data/bin/check-container-logs.rb +199 -0
- data/bin/check-container.rb +93 -0
- data/bin/check-docker-container.rb +108 -0
- data/bin/check-service-logs.rb +199 -0
- data/bin/check-service.rb +102 -0
- data/bin/metrics-docker-container.rb +127 -0
- data/bin/metrics-docker-info.rb +65 -0
- data/bin/metrics-docker-stats.rb +219 -0
- data/lib/sensu-plugins-docker.rb +1 -0
- data/lib/sensu-plugins-docker/client_helpers.rb +60 -0
- data/lib/sensu-plugins-docker/version.rb +9 -0
- metadata +271 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f7aa1313a3ad4fccbc1b55e0528da2a29113f94503d65fa8ec4b214cf1c6f514
|
4
|
+
data.tar.gz: b62b1d57e984f3f347d25f95105edeb517476677b5eced33a6284c7bae7ac6ac
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3c6238ef3415a0e440b484a182514cc902ff807f75cac05ba5d19a1971cab7631d2f4f1a410a14a9bd3e4cd9304fe6bdbbc5af161b35de391d323e7fc046f66b
|
7
|
+
data.tar.gz: 5d61e3439df34a494d65dc8f966c32c0dd808ae173c101c55251a001006b56ec53de8b04ee322ae324edaefc7e936a26c59fa13cc55ad12f94542d2cdd75157e
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,179 @@
|
|
1
|
+
# Change Log
|
2
|
+
This project adheres to [Semantic Versioning](http://semver.org/).
|
3
|
+
|
4
|
+
This CHANGELOG follows the format listed at [Our CHANGELOG Guidelines ](https://github.com/sensu-plugins/community/blob/master/HOW_WE_CHANGELOG.md).
|
5
|
+
Which is based on [Keep A Changelog](http://keepachangelog.com/)
|
6
|
+
|
7
|
+
## [Unreleased]
|
8
|
+
### Added
|
9
|
+
- check-service.rb: a new check similar to check-cotainer but for docker services when running in swarm mode
|
10
|
+
- check-service-logs.rb: a new check mirroring check-container-logs.rb but for docker services
|
11
|
+
|
12
|
+
## [3.2.0] - 2018-11-22
|
13
|
+
### Fixed
|
14
|
+
- metrics-docker-stats.rb: fix #16 -n option causes metrics-docker-stats.rb to fail, in case containers are linked together
|
15
|
+
|
16
|
+
## [3.1.1] - 2018-06-29
|
17
|
+
### Fixed
|
18
|
+
- check-container-logs.rb: fix nil.gsub condition on empty log lines
|
19
|
+
|
20
|
+
## [3.1.0] - 2018-05-07
|
21
|
+
### Added
|
22
|
+
- intergration test for container check (@antonidabek)
|
23
|
+
- check-container.rb: -x (--allow-exited) to avoid raising alerts when container exited without error, useful for task oriented containers monitoring. (@antonidabek)
|
24
|
+
|
25
|
+
## [3.0.0] - 2018-02-17
|
26
|
+
### Breaking Changes
|
27
|
+
- Default docker host defined by DockerApi Class ( ENV[DOCKER_URL] => ENV[DOCKER_HOST] => /var/run/docker.sock )
|
28
|
+
- check-container-logs.rb: -N (--container-name) instead of -n for container name. Now a 'CRITICAL' is trigger if a container doesn't exist (previously, a 'OK' was trigger)
|
29
|
+
- check-docker-container.rb: -H (--docker-host) instead of -h (--host) for docker Host
|
30
|
+
- check-container.rb: -H (--docker-host) for docker Host instead of -h (--host) for docker host, -N (--container-name) instead of -c (--container) for container name
|
31
|
+
- metrics-docker-stats.rb: -N (--container-name) instead of -c (--container) for Container name
|
32
|
+
|
33
|
+
### Added
|
34
|
+
- client_helpers.rb: Add a simple DockerApi class. Add parse_json method.
|
35
|
+
- metrics-docker-container.rb: Friendly names option added
|
36
|
+
- check-container-logs.rb: Add an option to check logs from stopped containers if all containers are checked. Add an option to don't check stderr logs. Add an option to don't check stdout logs. Add timestamp in logs output. Add the possibility to use -n multiple times to check multiple containers at once.
|
37
|
+
|
38
|
+
### Changed
|
39
|
+
- metrics-docker-stats.rb: Make use of DockerApi class. Default docker_host defined by DockerApi class. Remove docker_api method.
|
40
|
+
- metrics-docker-info.rb: Make use of DockerApi class. Default docker_host defined by DockerApi class. Remove docker_api method.
|
41
|
+
- metrics-docker-container.rb: Make use of DockerApi class. Re-enable rubocop for container_metrics method.
|
42
|
+
- check-container.rb: Make use of DockerApi class.
|
43
|
+
- check-docker-container.rb: Make use of DockerApi class.
|
44
|
+
- check-container-logs.rb: Make use of DockerApi class. Check only logs generated with the 8 bits control to prevent to check logs generated in interactive mode. Check the newest logs rows first instead the oldest. Option -n is not required anymore, if -n option is not provided, the check will be applied to all running containers. Changed the messages displayed with ok and critical
|
45
|
+
|
46
|
+
### Fixed
|
47
|
+
- metrics-docker-stats.rb: Remove trailing / in name value.
|
48
|
+
|
49
|
+
### Removed
|
50
|
+
- Remove unnecessary `docker_api` dependency
|
51
|
+
- check-container-logs.rb: Logs generated in interactive mode are not checked anymore
|
52
|
+
- metrics-docker-stats.rb: option -p (--protocol) have been removed because new DockerApi don't use it
|
53
|
+
- metrics-docker-info.rb: option -p (--protocol) have been removed because new DockerApi don't use it
|
54
|
+
|
55
|
+
## [2.0.0] - 2017-11-06
|
56
|
+
### Fixed
|
57
|
+
- metrics-docker-stats.rb:: Fix gsub on nil docker environment variable (@epierotto)
|
58
|
+
|
59
|
+
### Breaking Change
|
60
|
+
- bumped dependency of `sensu-plugin` to 2.x you can read about it [here](https://github.com/sensu-plugins/sensu-plugin/blob/master/CHANGELOG.md#v145---2017-03-07) (@majormoses)
|
61
|
+
|
62
|
+
## [1.5.0] - 2017-09-09
|
63
|
+
### Added
|
64
|
+
- metrics-docker-stats.rb: Include metric with cpu usage percentage calculated based on docker stats (@alcasim)
|
65
|
+
|
66
|
+
### Changed
|
67
|
+
- updated CHANGELOG guidelines location (@majormoses)
|
68
|
+
|
69
|
+
## [1.4.0] - 2017-08-08
|
70
|
+
### Added
|
71
|
+
- metrics-docker-info.rb: general information metrics from docker (@alcasim)
|
72
|
+
- metrics-docker-stats.rb: Added to include environment variables values as part of the metric. Added I/O stats option (@alcasim)
|
73
|
+
|
74
|
+
### Added
|
75
|
+
- Ruby 2.4.1 testing
|
76
|
+
|
77
|
+
## [1.3.1] - 2017-06-12
|
78
|
+
### Fixed
|
79
|
+
- check-container.rb: fixes to work with docker >= 1.17 (@israelriibeiro)
|
80
|
+
|
81
|
+
## [1.3.0] - 2017-06-04
|
82
|
+
### Added
|
83
|
+
- metrics-docker-stats.rb: Add an option to ouput a portion of the docker
|
84
|
+
container name using by splitting on a delimiter of the users choice.
|
85
|
+
|
86
|
+
### Fixed
|
87
|
+
- check-container.rb: Fix support for docker versions >= 1.18. The key-pair [State][Status] was replaced with [State][Running] and also logic was updated (#45)
|
88
|
+
|
89
|
+
## [1.2.0] - 2017-02-08
|
90
|
+
### Added
|
91
|
+
- check-container.rb: add an option to test image's tag (@obazoud)
|
92
|
+
|
93
|
+
## [1.1.5] - 2016-11-26
|
94
|
+
### Changed
|
95
|
+
- Loosen `sensu-plugin` dependency to `~> 1.2` (#44)
|
96
|
+
|
97
|
+
## [1.1.4] - 2016-11-26
|
98
|
+
### Changed
|
99
|
+
- metrics-docker-stats.rb: Fix JSON parse error if there is more than one chunk
|
100
|
+
of response data needed to result in valid JSON because of large datasets.
|
101
|
+
(#18)
|
102
|
+
|
103
|
+
## [1.1.3] - 2016-08-11
|
104
|
+
### Changed
|
105
|
+
- dependencies: use net\_http\_unix = 0.2.2
|
106
|
+
|
107
|
+
## [1.1.2] - 2016-06-20
|
108
|
+
### Changed
|
109
|
+
- dependencies: use sensu-plugin ~> 1.2.0, docker-api = 1.21.0
|
110
|
+
|
111
|
+
## [1.1.1] - 2016-06-10
|
112
|
+
### Fixed
|
113
|
+
- metrics-docker-stats.rb: Fix error from trying to collect stats with multiple values. Stats that return array values are now excluded. (#29)
|
114
|
+
|
115
|
+
### Changed
|
116
|
+
- improved help messages
|
117
|
+
- check-container.rb: issue a critical event if container state != running
|
118
|
+
|
119
|
+
## [1.1.0] - 2016-06-03
|
120
|
+
### Added
|
121
|
+
- check-container-logs.rb: added `-s|--seconds-ago` option to be able to set time interval more precisely
|
122
|
+
|
123
|
+
## [1.0.0] - 2016-05-24
|
124
|
+
Note: this release changes how connections are made to the Docker API and also
|
125
|
+
changes some options. Review your check commands before deploying this version.
|
126
|
+
|
127
|
+
### Added
|
128
|
+
- Added check-container-logs.rb to check docker logs for matching strings
|
129
|
+
- Support for Ruby 2.3.0
|
130
|
+
- metrics-docker-container.rb: add option to override the default path to cgroup.proc
|
131
|
+
|
132
|
+
### Removed
|
133
|
+
- Support for Ruby 1.9.3
|
134
|
+
|
135
|
+
### Changed
|
136
|
+
- check-docker-container.rb: output the number of running containers
|
137
|
+
- Refactor to connect to the Docker API socket directly instead of using the `docker` or `docker-api` gems
|
138
|
+
- Update to rubocop 0.40 and cleanup
|
139
|
+
|
140
|
+
## [0.0.4] - 2015-08-10
|
141
|
+
### Changed
|
142
|
+
- updated dependencies (added missing dependency `sys-proctable`)
|
143
|
+
- added docker metrics using docker api
|
144
|
+
|
145
|
+
## [0.0.3] - 2015-07-14
|
146
|
+
### Changed
|
147
|
+
- updated sensu-plugin gem to 1.2.0
|
148
|
+
|
149
|
+
## [0.0.2] - 2015-06-02
|
150
|
+
### Fixed
|
151
|
+
- added binstubs
|
152
|
+
|
153
|
+
### Changed
|
154
|
+
- removed cruft from /lib
|
155
|
+
|
156
|
+
## 0.0.1 - 2015-04-30
|
157
|
+
### Added
|
158
|
+
- initial release
|
159
|
+
|
160
|
+
[Unreleased]: https://github.com/sensu-plugins/sensu-plugins-docker/compare/3.1.1...HEAD
|
161
|
+
[3.1.1]: https://github.com/sensu-plugins/sensu-plugins-docker/compare/3.1.0...3.1.1
|
162
|
+
[3.1.0]: https://github.com/sensu-plugins/sensu-plugins-docker/compare/3.0.0...3.1.0
|
163
|
+
[3.0.0]: https://github.com/sensu-plugins/sensu-plugins-docker/compare/2.0.0...3.0.0
|
164
|
+
[2.0.0]: https://github.com/sensu-plugins/sensu-plugins-docker/compare/1.5.0...2.0.0
|
165
|
+
[1.5.0]: https://github.com/sensu-plugins/sensu-plugins-docker/compare/1.4.0..1.5.0
|
166
|
+
[1.4.0]: https://github.com/sensu-plugins/sensu-plugins-docker/compare/1.3.1...1.4.0
|
167
|
+
[1.3.1]: https://github.com/sensu-plugins/sensu-plugins-docker/compare/1.3.0...1.3.1
|
168
|
+
[1.3.0]: https://github.com/sensu-plugins/sensu-plugins-docker/compare/1.2.0...1.3.0
|
169
|
+
[1.2.0]: https://github.com/sensu-plugins/sensu-plugins-docker/compare/1.1.5...1.2.0
|
170
|
+
[1.1.5]: https://github.com/sensu-plugins/sensu-plugins-docker/compare/1.1.4...1.1.5
|
171
|
+
[1.1.4]: https://github.com/sensu-plugins/sensu-plugins-docker/compare/1.1.3...1.1.4
|
172
|
+
[1.1.3]: https://github.com/sensu-plugins/sensu-plugins-docker/compare/1.1.2...1.1.3
|
173
|
+
[1.1.2]: https://github.com/sensu-plugins/sensu-plugins-docker/compare/1.1.1...1.1.2
|
174
|
+
[1.1.1]: https://github.com/sensu-plugins/sensu-plugins-docker/compare/1.1.0...1.1.1
|
175
|
+
[1.1.0]: https://github.com/sensu-plugins/sensu-plugins-docker/compare/1.0.0...1.1.0
|
176
|
+
[1.0.0]: https://github.com/sensu-plugins/sensu-plugins-docker/compare/0.0.4...1.0.0
|
177
|
+
[0.0.4]: https://github.com/sensu-plugins/sensu-plugins-docker/compare/0.0.3...0.0.4
|
178
|
+
[0.0.3]: https://github.com/sensu-plugins/sensu-plugins-docker/compare/0.0.2...0.0.3
|
179
|
+
[0.0.2]: https://github.com/sensu-plugins/sensu-plugins-docker/compare/0.0.1...0.0.2
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Sensu-Plugins
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
## Sensu-Plugins-docker
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/sensu-plugins/sensu-plugins-docker.svg?branch=master)](https://travis-ci.org/sensu-plugins/sensu-plugins-docker)
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/sensu-plugins-docker.svg)](http://badge.fury.io/rb/sensu-plugins-docker)
|
5
|
+
[![Code Climate](https://codeclimate.com/github/sensu-plugins/sensu-plugins-docker/badges/gpa.svg)](https://codeclimate.com/github/sensu-plugins/sensu-plugins-docker)
|
6
|
+
[![Test Coverage](https://codeclimate.com/github/sensu-plugins/sensu-plugins-docker/badges/coverage.svg)](https://codeclimate.com/github/sensu-plugins/sensu-plugins-docker)
|
7
|
+
[![Dependency Status](https://gemnasium.com/sensu-plugins/sensu-plugins-docker.svg)](https://gemnasium.com/sensu-plugins/sensu-plugins-docker)
|
8
|
+
|
9
|
+
## Functionality
|
10
|
+
This check supports docker versions >= 1.18. Check docker-engine API for more information
|
11
|
+
|
12
|
+
The check-service.rb and check-service-logs.rb are only supported for docker when run in swarm mode
|
13
|
+
|
14
|
+
## Files
|
15
|
+
* check-container.rb
|
16
|
+
* check-service.rb
|
17
|
+
* check-container-logs.rb
|
18
|
+
* check-service-logs.rb
|
19
|
+
* check-docker-container.rb
|
20
|
+
* metrics-docker-container.rb
|
21
|
+
* metrics-docker-stats.rb
|
22
|
+
* metrics-docker-info.rb
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
### Default docker host
|
27
|
+
By default, all the checks will try to use a default docker host if a specific docker host is not provided to the check on the command line (-H <docker_host> / --docker-host <docker_host>).
|
28
|
+
|
29
|
+
Those paramaters will be tried in this order as default docker host :
|
30
|
+
|
31
|
+
DOCKER_URL environnement variable
|
32
|
+
DOCKER_HOST environnement variable
|
33
|
+
/var/run/docker.sock file
|
34
|
+
|
35
|
+
## Installation
|
36
|
+
|
37
|
+
[Installation and Setup](http://sensu-plugins.io/docs/installation_instructions.html)
|
38
|
+
|
39
|
+
## Notes
|
40
|
+
[docker-engine API](https://docs.docker.com/engine/api/v1.29/#section/Versioning)
|
@@ -0,0 +1,199 @@
|
|
1
|
+
#! /usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# check-container-logs
|
4
|
+
#
|
5
|
+
# DESCRIPTION:
|
6
|
+
# Checks docker logs for specified strings
|
7
|
+
# with the option to ignore lines if they contain specified substrings.
|
8
|
+
#
|
9
|
+
# OUTPUT:
|
10
|
+
# plain text
|
11
|
+
#
|
12
|
+
# PLATFORMS:
|
13
|
+
# Linux
|
14
|
+
#
|
15
|
+
# DEPENDENCIES:
|
16
|
+
# gem: sensu-plugin
|
17
|
+
# gem: net_http_unix
|
18
|
+
#
|
19
|
+
# USAGE:
|
20
|
+
# # Check only one container
|
21
|
+
# check-container-logs.rb -H /tmp/docker.sock -N logspout -r 'problem sending' -r 'i/o timeout' -i 'Remark:' -i 'The configuration is'
|
22
|
+
# => 1 container running = OK
|
23
|
+
# => 4 container running = CRITICAL
|
24
|
+
#
|
25
|
+
# # Check multiple containers
|
26
|
+
# check-container-logs.rb -H /tmp/docker.sock -N logspout -N logtest -r 'problem sending' -r 'i/o timeout' -i 'Remark:' -i 'The configuration is'
|
27
|
+
# => 1 container running = OK
|
28
|
+
# => 4 container running = CRITICAL
|
29
|
+
#
|
30
|
+
# # Check all containers
|
31
|
+
# check-container-logs.rb -H /tmp/docker.sock -r 'problem sending' -r 'i/o timeout' -i 'Remark:' -i 'The configuration is'
|
32
|
+
# => 1 containers running = OK
|
33
|
+
# => 4 containers running = CRITICAL
|
34
|
+
#
|
35
|
+
# NOTES:
|
36
|
+
# The API parameter required to use the limited lookback (-t) was introduced
|
37
|
+
# the Docker server API version 1.19. This check may still work on older API
|
38
|
+
# versions if you don't want to limit the timestamps of logs.
|
39
|
+
#
|
40
|
+
# LICENSE:
|
41
|
+
# Author: Nathan Newman <newmannh@gmail.com>, Kel Cecil <kelcecil@praisechaos.com>
|
42
|
+
# Released under the same terms as Sensu (the MIT license); see LICENSE
|
43
|
+
# for details.
|
44
|
+
#
|
45
|
+
|
46
|
+
require 'sensu-plugin/check/cli'
|
47
|
+
require 'sensu-plugins-docker/client_helpers'
|
48
|
+
|
49
|
+
class ContainerLogChecker < Sensu::Plugin::Check::CLI
|
50
|
+
option :docker_host,
|
51
|
+
description: 'Docker API URI. https://host, https://host:port, http://host, http://host:port, host:port, unix:///path',
|
52
|
+
short: '-H DOCKER_HOST',
|
53
|
+
long: '--docker-host DOCKER_HOST'
|
54
|
+
|
55
|
+
option :container,
|
56
|
+
description: 'name of container; can be used multiple times. /!\ All running containers will be check if this options is not provided',
|
57
|
+
short: '-N CONTAINER',
|
58
|
+
long: '--container-name CONTAINER',
|
59
|
+
default: [],
|
60
|
+
proc: proc { |flag| (@options[:container][:accumulated] ||= []).push(flag) }
|
61
|
+
|
62
|
+
option :red_flags,
|
63
|
+
description: 'String whose presence (case-insensitive by default) in a log line indicates an error; can be used multiple times',
|
64
|
+
short: '-r ERR_STRING',
|
65
|
+
long: '--red-flag ERR_STRING',
|
66
|
+
default: [],
|
67
|
+
proc: proc { |flag| (@options[:red_flags][:accumulated] ||= []).push(flag) }
|
68
|
+
|
69
|
+
option :ignore_list,
|
70
|
+
description: 'String whose presence (case-insensitive by default) in a log line indicates the line should be ignored; can be used multiple times',
|
71
|
+
short: '-i IGNSTR',
|
72
|
+
long: '--ignore-lines-with IGNSTR',
|
73
|
+
default: [],
|
74
|
+
proc: proc { |flag| (@options[:ignore_list][:accumulated] ||= []).push(flag) }
|
75
|
+
|
76
|
+
option :case_sensitive,
|
77
|
+
description: 'indicates all red_flag and ignore_list substring matching should be case-sensitive instead of the default case-insensitive',
|
78
|
+
short: '-c',
|
79
|
+
long: '--case-sensitive',
|
80
|
+
boolean: true
|
81
|
+
|
82
|
+
option :hours_ago,
|
83
|
+
description: 'Amount of time in hours to look back for log strings',
|
84
|
+
short: '-t HOURS',
|
85
|
+
long: '--hours-ago HOURS',
|
86
|
+
required: false
|
87
|
+
|
88
|
+
option :seconds_ago,
|
89
|
+
description: 'Amount of time in seconds to look back for log strings',
|
90
|
+
short: '-s SECONDS',
|
91
|
+
long: '--seconds-ago SECONDS',
|
92
|
+
required: false
|
93
|
+
|
94
|
+
option :check_all,
|
95
|
+
description: 'If all containers are checked (no container name provided with -n) , check offline containers too',
|
96
|
+
short: '-a',
|
97
|
+
long: '--all',
|
98
|
+
default: false,
|
99
|
+
boolean: true
|
100
|
+
|
101
|
+
option :disable_stdout,
|
102
|
+
description: 'Disable the check on STDOUT logs. By default both STDERR and STDOUT are checked',
|
103
|
+
short: '-1',
|
104
|
+
long: '--no-stdout',
|
105
|
+
default: true,
|
106
|
+
boolean: true,
|
107
|
+
proc: proc { false } # used to negate the false(default)->true boolean option behaviour to true(default)->false
|
108
|
+
|
109
|
+
option :disable_stderr,
|
110
|
+
description: 'Disable the check on STDERR logs. By default both STDERR and STDOUT are checked',
|
111
|
+
short: '-2',
|
112
|
+
long: '--no-stderr',
|
113
|
+
default: true,
|
114
|
+
boolean: true,
|
115
|
+
proc: proc { false } # used to negate the false(default)->true boolean option behaviour to true(default)->false
|
116
|
+
|
117
|
+
def calculate_timestamp(seconds_ago = nil)
|
118
|
+
seconds_ago = yield if block_given?
|
119
|
+
(Time.now - seconds_ago).to_i
|
120
|
+
end
|
121
|
+
|
122
|
+
def process_docker_logs(container_name)
|
123
|
+
path = "/containers/#{container_name}/logs?stdout=#{config[:disable_stdout]}&stderr=#{config[:disable_stderr]}×tamps=true"
|
124
|
+
if config.key? :hours_ago
|
125
|
+
timestamp = calculate_timestamp { config[:hours_ago].to_i * 3600 }
|
126
|
+
elsif config.key? :seconds_ago
|
127
|
+
timestamp = calculate_timestamp config[:seconds_ago].to_i
|
128
|
+
end
|
129
|
+
path = "#{path}&since=#{timestamp}"
|
130
|
+
response = @client.call(path, false)
|
131
|
+
if response.code.to_i == 404
|
132
|
+
critical "Container '#{container_name}' not found on #{@client.uri}"
|
133
|
+
end
|
134
|
+
yield remove_headers response.read_body
|
135
|
+
end
|
136
|
+
|
137
|
+
def remove_headers(raw_logs)
|
138
|
+
lines = raw_logs.split("\n")
|
139
|
+
lines.map! do |line|
|
140
|
+
# Check only logs generated with the 8 bits control
|
141
|
+
if !line.nil? && line.bytesize > 8 && /^(0|1|2)000$/ =~ line.byteslice(0, 4).unpack('C*').join('')
|
142
|
+
# Remove the first 8 bits and ansii colors too
|
143
|
+
line.byteslice(8, line.bytesize).gsub(/\x1b\[[\d;]*?m/, '')
|
144
|
+
end
|
145
|
+
end
|
146
|
+
# We want the most recent logs lines first
|
147
|
+
lines.compact.reverse.join("\n")
|
148
|
+
end
|
149
|
+
|
150
|
+
def includes_any?(str, array_of_substrings)
|
151
|
+
array_of_substrings.each do |substring|
|
152
|
+
return true if str.include? substring
|
153
|
+
end
|
154
|
+
false
|
155
|
+
end
|
156
|
+
|
157
|
+
def detect_problem(logs)
|
158
|
+
whiteflags = config[:ignore_list]
|
159
|
+
redflags = config[:red_flags]
|
160
|
+
unless config[:case_sensitive]
|
161
|
+
logs = logs.downcase
|
162
|
+
whiteflags.map!(&:downcase)
|
163
|
+
redflags.map!(&:downcase)
|
164
|
+
end
|
165
|
+
|
166
|
+
logs.split("\n").each do |line|
|
167
|
+
return line if !includes_any?(line, whiteflags) && includes_any?(line, redflags)
|
168
|
+
end
|
169
|
+
nil
|
170
|
+
end
|
171
|
+
|
172
|
+
def run
|
173
|
+
@client = DockerApi.new(config[:docker_host])
|
174
|
+
problem = []
|
175
|
+
problem_string = nil
|
176
|
+
path = "/containers/json?all=#{config[:check_all]}"
|
177
|
+
containers = config[:container]
|
178
|
+
if config[:container].none?
|
179
|
+
warn_msg = %(
|
180
|
+
Collecting logs from all containers is dangerous and could lead to sensu client hanging depending on volume of logs.
|
181
|
+
This not recommended for production environments.
|
182
|
+
).gsub(/\s+/, ' ').strip
|
183
|
+
message warn_msg
|
184
|
+
end
|
185
|
+
containers = @client.parse(path).map { |p| p['Names'][0].delete('/') } if containers.none?
|
186
|
+
critical 'Check all containers was asked but no containers was found' if containers.none?
|
187
|
+
containers.each do |container|
|
188
|
+
process_docker_logs container do |log_chunk|
|
189
|
+
problem_string = detect_problem(log_chunk)
|
190
|
+
break unless problem_string.nil?
|
191
|
+
end
|
192
|
+
problem << "\tError found inside container : '#{container}'\n\t\t#{problem_string}" unless problem_string.nil?
|
193
|
+
end
|
194
|
+
problem_string = problem.join("\n")
|
195
|
+
critical "Container(s) logs indicate problems :\n#{problem_string}" unless problem.none?
|
196
|
+
containers_string = containers.join(', ')
|
197
|
+
ok "No errors detected from logs inside container(s) : \n#{containers_string}"
|
198
|
+
end
|
199
|
+
end
|