improve code readability (update_every issue)
paulfantom committed
Jun 21, 2016 at 12:27 UTC
46d5663bcb7565073a4dbdba5ae1f8c447216919
1 file changed
+26
-28
plugins.d/python.d.plugin
+26
-28
@@ -8,19 +8,29 @@ import os
8
import sys
9
import time
10
11
-
11
+# -----------------------------------------------------------------------------
12
+# globals & environment setup
13
+# https://github.com/firehol/netdata/wiki/External-Plugins#environment-variables
14
MODULE_EXTENSION = ".chart.py"
13
-BASE_CONFIG = {'update_every': 1,
15
+BASE_CONFIG = {'update_every': os.getenv('NETDATA_UPDATE_EVERY', 1),
16
'priority': 90000,
17
'retries': 10}
18
17
-# -----------------------------------------------------------------------------
18
-# logging functions
19
+MODULES_DIR = os.path.abspath(os.getenv('NETDATA_PLUGINS_DIR',
20
+ os.path.dirname(__file__)) + "/../python.d") + "/"
21
+CONFIG_DIR = os.getenv('NETDATA_CONFIG_DIR', "/etc/netdata/")
22
+# directories should end with '/'
23
+if CONFIG_DIR[-1] != "/":
24
+ CONFIG_DIR += "/"
25
+sys.path.append(MODULES_DIR + "python_modules")
26
27
PROGRAM = os.path.basename(__file__).replace(".plugin", "")
28
DEBUG_FLAG = False
29
+OVERRIDE_UPDATE_EVERY = False
30
31
32
+# -----------------------------------------------------------------------------
33
+# logging functions
34
def debug(*args):
35
"""
36
Print message on stderr.
@@ -70,20 +80,7 @@ def fatal(*args):
80
81
82
# -----------------------------------------------------------------------------
73
-# globals & python modules management
74
-
75
-# setup environment
76
-# https://github.com/firehol/netdata/wiki/External-Plugins#environment-variables
77
-MODULES_DIR = os.path.abspath(os.getenv('NETDATA_PLUGINS_DIR',
78
- os.path.dirname(__file__)) + "/../python.d") + "/"
79
-CONFIG_DIR = os.getenv('NETDATA_CONFIG_DIR', "/etc/netdata/")
80
-UPDATE_EVERY = os.getenv('NETDATA_UPDATE_EVERY', None)
81
-# directories should end with '/'
82
-if CONFIG_DIR[-1] != "/":
83
- CONFIG_DIR += "/"
84
-sys.path.append(MODULES_DIR + "python_modules")
85
-
86
-
83
+# third party and version specific python modules management
84
try:
85
assert sys.version_info >= (3, 1)
86
import importlib.machinery
@@ -115,7 +112,6 @@ class PythonCharts(object):
112
Main class used to control every python module.
113
"""
114
def __init__(self,
118
- update_every=None,
115
modules=None,
116
modules_path='../python.d/',
117
modules_configs='../conf.d/',
@@ -147,9 +143,9 @@ class PythonCharts(object):
143
self.jobs = self._create_jobs(configured_modules) # type: list
144
145
# enable timetable override like `python.d.plugin mysql debug 1`
150
- if DEBUG_FLAG and update_every is not None:
146
+ if DEBUG_FLAG and OVERRIDE_UPDATE_EVERY:
147
for job in self.jobs:
152
- job.create_timetable(update_every)
148
+ job.create_timetable(BASE_CONFIG['update_every'])
149
150
@staticmethod
151
def _import_module(path, name=None):
@@ -497,7 +493,8 @@ def parse_cmdline(directory, *commands):
493
:return: dict
494
"""
495
global DEBUG_FLAG
500
- global UPDATE_EVERY
496
+ global OVERRIDE_UPDATE_EVERY
497
+ global BASE_CONFIG
498
499
mods = []
500
for cmd in commands[1:]:
@@ -511,7 +508,8 @@ def parse_cmdline(directory, *commands):
508
mods.append(cmd.replace(".chart.py", ""))
509
else:
510
try:
514
- UPDATE_EVERY = int(cmd)
511
+ BASE_CONFIG['update_every'] = int(cmd)
512
+ OVERRIDE_UPDATE_EVERY = True
513
except ValueError:
514
pass
515
@@ -525,13 +523,12 @@ def run():
523
"""
524
Main program.
525
"""
528
- global DEBUG_FLAG
526
+ global DEBUG_FLAG, BASE_CONFIG
527
528
# read configuration file
529
disabled = []
530
configfile = CONFIG_DIR + "python.d.conf"
531
534
- update_every = UPDATE_EVERY
532
conf = read_config(configfile)
533
if conf is not None:
534
try:
@@ -541,7 +538,8 @@ def run():
538
except (KeyError, TypeError):
539
pass
540
try:
544
- update_every = conf['update_every']
541
+ for param in BASE_CONFIG:
542
+ BASE_CONFIG[param] = conf[param]
543
except (KeyError, TypeError):
544
pass # use default update_every from NETDATA_UPDATE_EVERY
545
try:
@@ -558,11 +556,11 @@ def run():
556
modules = parse_cmdline(MODULES_DIR, *sys.argv)
557
info("MODULES_DIR='" + MODULES_DIR +
558
"', CONFIG_DIR='" + CONFIG_DIR +
561
- "', UPDATE_EVERY=" + str(UPDATE_EVERY) +
559
+ "', UPDATE_EVERY=" + str(BASE_CONFIG['update_every']) +
560
", ONLY_MODULES=" + str(modules))
561
562
# run plugins
565
- charts = PythonCharts(update_every, modules, MODULES_DIR, CONFIG_DIR + "python.d/", disabled)
563
+ charts = PythonCharts(modules, MODULES_DIR, CONFIG_DIR + "python.d/", disabled)
564
charts.check()
565
charts.create()
566
charts.update()