-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathnodebb-errors
executable file
·164 lines (134 loc) · 4.24 KB
/
nodebb-errors
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python3
# -*- coding: utf-8; py-indent-offset: 4 -*-
#
# Author: Linuxfabrik GmbH, Zurich, Switzerland
# Contact: info (at) linuxfabrik (dot) ch
# https://www.linuxfabrik.ch/
# License: The Unlicense, see LICENSE file.
# https://github.com/Linuxfabrik/monitoring-plugins/blob/main/CONTRIBUTING.rst
"""See the check's README for more details.
"""
import argparse # pylint: disable=C0413
import sys # pylint: disable=C0413
import lib.args # pylint: disable=C0413
import lib.base # pylint: disable=C0413
import lib.nodebb # pylint: disable=C0413
import lib.lftest # pylint: disable=C0413
from lib.globals import (STATE_CRIT, STATE_OK, # pylint: disable=C0413
STATE_UNKNOWN, STATE_WARN)
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
__version__ = '2025021501'
DESCRIPTION = """Get NodeBB server-side errors."""
DEFAULT_INSECURE = False
DEFAULT_NO_PROXY = False
DEFAULT_SERVERITY = 'warn'
DEFAULT_TIMEOUT = 3
DEFAULT_URL = 'http://localhost:4567/forum'
def parse_args():
"""Parse command line arguments using argparse.
"""
parser = argparse.ArgumentParser(description=DESCRIPTION)
parser.add_argument(
'-V', '--version',
action='version',
version='%(prog)s: v{} by {}'.format(__version__, __author__)
)
parser.add_argument(
'--always-ok',
help='Always returns OK.',
dest='ALWAYS_OK',
action='store_true',
default=False,
)
parser.add_argument(
'--insecure',
help='This option explicitly allows to perform "insecure" SSL connections. '
'Default: %(default)s',
dest='INSECURE',
action='store_true',
default=DEFAULT_INSECURE,
)
parser.add_argument(
'--no-proxy',
help='Do not use a proxy. '
'Default: %(default)s',
dest='NO_PROXY',
action='store_true',
default=DEFAULT_NO_PROXY,
)
parser.add_argument(
'--severity',
help='Severity for alerts that do not depend on thresholds. One of "warn" or "crit". '
'Default: %(default)s',
dest='SEVERITY',
default=DEFAULT_SERVERITY,
choices=['warn', 'crit'],
)
parser.add_argument(
'--test',
help='For unit tests. Needs "path-to-stdout-file,path-to-stderr-file,expected-retc".',
dest='TEST',
type=lib.args.csv,
)
parser.add_argument(
'--timeout',
help='Network timeout in seconds. '
'Default: %(default)s (seconds)',
dest='TIMEOUT',
type=int,
default=DEFAULT_TIMEOUT,
)
parser.add_argument(
'-p', '--token',
help='NodeBB API Bearer token.',
dest='TOKEN',
required=True,
)
parser.add_argument(
'--url',
help='NodeBB API URL. '
'Default: %(default)s',
dest='URL',
default=DEFAULT_URL,
)
return parser.parse_args()
def main():
"""The main function. Hier spielt die Musik.
"""
# parse the command line, exit with UNKNOWN if it fails
try:
args = parse_args()
except SystemExit:
sys.exit(STATE_UNKNOWN)
# fetch data
if args.TEST is None:
result = lib.nodebb.get_data(args, '/api/admin/advanced/errors')
else:
# do not call the command, put in test data
import json
stdout, stderr, retc = lib.lftest.test(args.TEST)
result = json.loads(stdout)
# init some vars
msg = ''
state = STATE_OK
perfdata = ''
# analyze data - analytics
err404 = result['analytics']['not-found'][-1]
err503 = result['analytics']['toobusy'][-1]
if err503:
state = lib.base.str2state(args.SEVERITY)
perfdata += lib.base.get_perfdata('err404', err404, 'c', None, None, 0, None)
perfdata += lib.base.get_perfdata('err503', err503, 'c', None, None, 0, None)
# build the message
msg += 'HTTP Status today: {}x 503 too busy{}, {}x 404 not found\n'.format(
err503,
lib.base.state2str(state, prefix=' '),
err404,
)
# over and out
lib.base.oao(msg, state, perfdata, always_ok=args.ALWAYS_OK)
if __name__ == '__main__':
try:
main()
except Exception: # pylint: disable=W0703
lib.base.cu()