-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_graylog_lag
executable file
·137 lines (119 loc) · 5.03 KB
/
check_graylog_lag
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python
# coding: utf-8
# Copyright Ilya Margolin 2015
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Check graylog lag (and general availability), by searching for recent messages and inspecting the
timestamp of latest message.
"""
import datetime
import sys
import socket
import argparse
import logging
import requests
import nagiosplugin
import dateutil.parser
from nagiosplugin.state import Critical
HOSTNAME = socket.gethostname()
SEARCH_URL_TEMPLATE = 'http://%s:12900/search/universal/relative?query=*&range=%i&limit=1' # noqa
_log = logging.getLogger('nagiosplugin')
class ConnectionContext(nagiosplugin.context.ScalarContext):
def evaluate(self, metric, resource):
if resource.connection_error:
return nagiosplugin.result.Result(Critical, str(resource.connection_error.message))
else:
return super(ConnectionContext, self).evaluate(metric, resource)
class Graylog(nagiosplugin.Resource):
def __init__(self):
self.horizont = None
self.graylog = None
self.auth = None
self.connection_error = None
self.timeout = None
self.connection_errors_are_critical = None
def probe(self):
return [nagiosplugin.Metric('lag', self.get_lag(), 's', context='lag')]
def get_lag(self):
search_url = SEARCH_URL_TEMPLATE % (self.graylog, self.horizont)
_log.info('GET %s', search_url)
_log.info(' with auth: %s', self.auth)
try:
r = requests.get(
search_url,
auth=self.auth,
headers={'Accept': 'application/json'},
timeout=self.timeout
)
except IOError, e:
if self.connection_errors_are_critical:
self.connection_error = e
return float('inf')
else:
raise
_log.info('Response status code: %s', r.status_code)
_log.info('Response content: %s', r.content)
r.raise_for_status()
r.encoding = 'utf-8'
try:
json = r.json()
except TypeError: # requests < 1.0.0 compatibility
json = r.json
messages = json['messages']
if not messages:
_log.warn('no messages found (searched in last %s seconds)', self.horizont)
return self.horizont
ts = messages[0]['message']['timestamp']
parsed_ts = dateutil.parser.parse(ts)
now_tz = datetime.datetime.now(parsed_ts.tzinfo)
td = (now_tz - parsed_ts)
# total_seconds python 2.6 compatibility
total_seconds = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10 ** 6) / 10 ** 6
lag = int(total_seconds)
return lag
@nagiosplugin.guarded
def main():
argp = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.ArgumentDefaultsHelpFormatter)
argp.add_argument('-w', '--warning', metavar='RANGE', default='0:300',
help='return warning if lag is outside RANGE')
argp.add_argument('-c', '--critical', metavar='RANGE', default='0:600',
help='return critical if lag is outside RANGE')
argp.add_argument('-H', '--horizont', '--horizon', metavar='seconds', type=int, default=3600,
help='How far back to search for messages')
argp.add_argument('-g', '--graylog', metavar='hostname', default=HOSTNAME,
help='graylog hostname to check')
argp.add_argument('-a', '--auth', default=None,
help='username:password, basic auth')
argp.add_argument('-v', '--verbose', action='count', default=0,
help='increase output verbosity (use up to 3 times)')
argp.add_argument('--connection-errors-are-critical', action='count', default=0,
help='Return CRITICAL for timeouts and connection errors')
argp.add_argument('-t', '--timeout', type=float, metavar='seconds', default=10,
help='Connection timeout')
args = argp.parse_args()
resource = Graylog()
resource.graylog = args.graylog
resource.horizont = args.horizont
resource.timeout = args.timeout
resource.connection_errors_are_critical = args.connection_errors_are_critical
if args.auth:
resource.auth = tuple(args.auth.split(':'))
check = nagiosplugin.Check(
resource,
ConnectionContext('lag', args.warning, args.critical)
)
check.main(verbose=args.verbose)
if __name__ == '__main__':
main()