]> arthur.barton.de Git - netdata.git/commitdiff
add log flood prevention
authorpaulfantom <paulfantom@gmail.com>
Sun, 14 Aug 2016 11:34:16 +0000 (13:34 +0200)
committerpaulfantom <paulfantom@gmail.com>
Sun, 14 Aug 2016 11:34:16 +0000 (13:34 +0200)
conf.d/python.d.conf
plugins.d/python.d.plugin
python.d/python_modules/msg.py

index 18558fbd2294f1ae33308ede95949d0a4583b319..a0c809776ca5838180a5dbf02b5e0891e520ce74 100644 (file)
@@ -9,6 +9,13 @@
 # Enable / disable the whole python.d.plugin (all its modules)
 enabled: yes
 
+# Prevent log flood
+# Define how many log messages can be written to log file in one log_interval
+logs_per_interval: 200
+
+# Define how long is one logging interval (in seconds)
+log_interval: 3600
+
 # ----------------------------------------------------------------------
 # Enable / Disable python.d.plugin modules
 #
@@ -18,10 +25,12 @@ enabled: yes
 # apache: yes
 # apache_cache: yes
 # cpufreq: yes
+# dovecot: yes
 example: no
 # exim: yes
 # hddtemp: yes
 # ipfs: yes
+# memcached: yes
 # mysql: yes
 # nginx: yes
 # phpfpm: yes
index 1301777bee9debbd70108b093a4116297b8d459c..5a8768d42321c95656dfb1a52335007d9b16a9d3 100755 (executable)
@@ -495,6 +495,14 @@ def run():
             DEBUG_FLAG = conf['debug']
         except (KeyError, TypeError):
             pass
+        try:
+            log_counter = conf['logs_per_interval']
+        except (KeyError, TypeError):
+            log_counter = 200
+        try:
+            log_interval = conf['log_interval']
+        except (KeyError, TypeError):
+            log_interval = 3600
         for k, v in conf.items():
             if k in ("update_every", "debug", "enabled"):
                 continue
@@ -504,6 +512,8 @@ def run():
     # parse passed command line arguments
     modules = parse_cmdline(MODULES_DIR, *sys.argv)
     msg.DEBUG_FLAG = DEBUG_FLAG
+    msg.LOG_COUNTER = log_counter
+    msg.LOG_INTERVAL = log_interval
     msg.info("MODULES_DIR='" + MODULES_DIR +
              "', CONFIG_DIR='" + CONFIG_DIR +
              "', UPDATE_EVERY=" + str(BASE_CONFIG['update_every']) +
index 966962d8002d422fb205d7c834ac6c2c17027323..c1421605d90ed2f94d38cdafb1b8810a3b3a3a72 100644 (file)
@@ -2,9 +2,16 @@
 # Description: logging for netdata python.d modules
 
 import sys
+from time import time
 
 DEBUG_FLAG = False
 PROGRAM = ""
+LOG_COUNTER = 2
+LOG_INTERVAL = 5
+NEXT_CHECK = 0
+
+WRITE = sys.stderr.write
+FLUSH = sys.stderr.flush
 
 
 def log_msg(msg_type, *args):
@@ -12,10 +19,22 @@ def log_msg(msg_type, *args):
     Print message on stderr.
     :param msg_type: str
     """
-    msg = "%s %s: %s" % (PROGRAM, str(msg_type), " ".join(args))
+    global LOG_COUNTER
+    if not DEBUG_FLAG:
+        LOG_COUNTER -= 1
+    now = time()
+    if LOG_COUNTER >= 0:
+        msg = "%s %s: %s" % (PROGRAM, str(msg_type), " ".join(args))
+        WRITE(msg + "\n")
+        FLUSH()
 
-    sys.stderr.write(msg + "\n")
-    sys.stderr.flush()
+    global NEXT_CHECK
+    if NEXT_CHECK <= now:
+        NEXT_CHECK = now - (now % LOG_INTERVAL) + LOG_INTERVAL
+        if LOG_COUNTER < 0:
+            msg = "Prevented %s log messages from displaying" % str(0 - LOG_COUNTER)
+            WRITE(msg + "\n")
+            FLUSH()
 
 
 def debug(*args):