add log flood prevention
paulfantom committed
Aug 14, 2016 at 13:34 UTC
17d80a1ef354d6938d2844ed52d89b1cf3e22669
3 files changed
+41
-3
conf.d/python.d.conf
+9
@@ -9,6 +9,13 @@
9
# Enable / disable the whole python.d.plugin (all its modules)
10
enabled: yes
11
12
+# Prevent log flood
13
+# Define how many log messages can be written to log file in one log_interval
14
+logs_per_interval: 200
15
+
16
+# Define how long is one logging interval (in seconds)
17
+log_interval: 3600
18
+
19
# ----------------------------------------------------------------------
20
# Enable / Disable python.d.plugin modules
21
#
@@ -18,10 +25,12 @@ enabled: yes
25
# apache: yes
26
# apache_cache: yes
27
# cpufreq: yes
28
+# dovecot: yes
29
example: no
30
# exim: yes
31
# hddtemp: yes
32
# ipfs: yes
33
+# memcached: yes
34
# mysql: yes
35
# nginx: yes
36
# phpfpm: yes
plugins.d/python.d.plugin
+10
@@ -495,6 +495,14 @@ def run():
495
DEBUG_FLAG = conf['debug']
496
except (KeyError, TypeError):
497
pass
498
+ try:
499
+ log_counter = conf['logs_per_interval']
500
+ except (KeyError, TypeError):
501
+ log_counter = 200
502
+ try:
503
+ log_interval = conf['log_interval']
504
+ except (KeyError, TypeError):
505
+ log_interval = 3600
506
for k, v in conf.items():
507
if k in ("update_every", "debug", "enabled"):
508
continue
@@ -504,6 +512,8 @@ def run():
512
# parse passed command line arguments
513
modules = parse_cmdline(MODULES_DIR, *sys.argv)
514
msg.DEBUG_FLAG = DEBUG_FLAG
515
+ msg.LOG_COUNTER = log_counter
516
+ msg.LOG_INTERVAL = log_interval
517
msg.info("MODULES_DIR='" + MODULES_DIR +
518
"', CONFIG_DIR='" + CONFIG_DIR +
519
"', UPDATE_EVERY=" + str(BASE_CONFIG['update_every']) +
python.d/python_modules/msg.py
+22
-3
@@ -2,9 +2,16 @@
2
# Description: logging for netdata python.d modules
3
4
import sys
5
+from time import time
6
7
DEBUG_FLAG = False
8
PROGRAM = ""
9
+LOG_COUNTER = 2
10
+LOG_INTERVAL = 5
11
+NEXT_CHECK = 0
12
+
13
+WRITE = sys.stderr.write
14
+FLUSH = sys.stderr.flush
15
16
17
def log_msg(msg_type, *args):
@@ -12,10 +19,22 @@ def log_msg(msg_type, *args):
19
Print message on stderr.
20
:param msg_type: str
21
"""
15
- msg = "%s %s: %s" % (PROGRAM, str(msg_type), " ".join(args))
22
+ global LOG_COUNTER
23
+ if not DEBUG_FLAG:
24
+ LOG_COUNTER -= 1
25
+ now = time()
26
+ if LOG_COUNTER >= 0:
27
+ msg = "%s %s: %s" % (PROGRAM, str(msg_type), " ".join(args))
28
+ WRITE(msg + "\n")
29
+ FLUSH()
30
17
- sys.stderr.write(msg + "\n")
18
- sys.stderr.flush()
31
+ global NEXT_CHECK
32
+ if NEXT_CHECK <= now:
33
+ NEXT_CHECK = now - (now % LOG_INTERVAL) + LOG_INTERVAL
34
+ if LOG_COUNTER < 0:
35
+ msg = "Prevented %s log messages from displaying" % str(0 - LOG_COUNTER)
36
+ WRITE(msg + "\n")
37
+ FLUSH()
38
39
40
def debug(*args):