@cryptotaxi247 / netdata-1 / commits / 333348f5d

Better handling of env variables

paulfantom committed Jun 20, 2016 at 13:31 UTC 333348f5d9734abfd86d3cf3b82abe462c07c509
1 file changed +24 -32
plugins.d/python.d.plugin
+24 -32
@@ -1,9 +1,26 @@
1 #!/usr/bin/env python
2
3 +# Description: netdata python modules supervisor
4 +# Author: Pawel Krupa (paulfantom)
5 +
6 import os
7 import sys
8 import time
9
10 +# setup environment
11 +# https://github.com/firehol/netdata/wiki/External-Plugins#environment-variables
12 +MODULES_DIR = os.getenv('NETDATA_PLUGINS_DIR',
13 + os.path.abspath(__file__).strip("python.d.plugin.py").replace("plugins.d", "python.d"))
14 +CONFIG_DIR = os.getenv('NETDATA_CONFIG_DIR', "/etc/netdata/")
15 +INTERVAL = os.getenv('NETDATA_UPDATE_EVERY', 1)
16 +# directories should end with '/'
17 +if MODULES_DIR[-1] != "/":
18 + MODULES_DIR += "/"
19 +if CONFIG_DIR[-1] != "/":
20 + CONFIG_DIR += "/"
21 +sys.path.append(MODULES_DIR + "python_modules")
22 +
23 +
24 try:
25 assert sys.version_info >= (3, 1)
26 import importlib.machinery
@@ -22,7 +39,7 @@ except (AssertionError, ImportError):
39 # __builtin__.PY_VERSION = 2
40 PY_VERSION = 2
41 sys.stderr.write('python.d.plugin: Using python 2\n')
25 - except (AssertionError, ImportError):
42 + except ImportError:
43 sys.stderr.write('python.d.plugin: Cannot start. No importlib.machinery on python3 or lack of imp on python2\n')
44 sys.stdout.write('DISABLE\n')
45 sys.exit(1)
@@ -482,19 +499,12 @@ def run():
499 """
500 global PROGRAM, DEBUG_FLAG
501 PROGRAM = sys.argv[0].split('/')[-1].split('.plugin')[0]
485 - # parse env variables
486 - # https://github.com/firehol/netdata/wiki/External-Plugins#environment-variables
487 - main_dir = os.getenv('NETDATA_PLUGINS_DIR',
488 - os.path.abspath(__file__).strip("python.d.plugin.py"))
489 - config_dir = os.getenv('NETDATA_CONFIG_DIR', "/etc/netdata/")
490 - interval = os.getenv('NETDATA_UPDATE_EVERY', None)
502
503 # read configuration file
504 disabled = []
494 - if config_dir[-1] != '/':
495 - config_dir += '/'
496 - configfile = config_dir + "python.d.conf"
505 + configfile = CONFIG_DIR + "python.d.conf"
506
507 + interval = INTERVAL
508 conf = read_config(configfile)
509 if conf is not None:
510 try:
@@ -504,14 +514,6 @@ def run():
514 sys.exit(1)
515 except (KeyError, TypeError):
516 pass
507 - try:
508 - modules_conf = conf['plugins_config_dir']
509 - except (KeyError, TypeError):
510 - modules_conf = config_dir + "python.d/" # default configuration directory
511 - try:
512 - modules_dir = conf['plugins_dir']
513 - except (KeyError, TypeError):
514 - modules_dir = main_dir.replace("plugins.d", "python.d")
517 try:
518 interval = conf['interval']
519 except (KeyError, TypeError):
@@ -521,31 +523,21 @@ def run():
523 except (KeyError, TypeError):
524 pass
525 for k, v in conf.items():
524 - if k in ("plugins_config_dir", "plugins_dir", "interval", "debug"):
526 + if k in ("interval", "debug", "enable"):
527 continue
528 if v is False:
529 disabled.append(k)
530 else:
529 - modules_conf = config_dir + "python.d/"
530 - modules_dir = main_dir.replace("plugins.d", "python.d")
531 -
532 - # directories should end with '/'
533 - if modules_dir[-1] != '/':
534 - modules_dir += "/"
535 - if modules_conf[-1] != '/':
536 - modules_conf += "/"
531 + modules_conf = CONFIG_DIR + "python.d/"
532
533 # parse passed command line arguments
539 - out = parse_cmdline(modules_dir, *sys.argv)
534 + out = parse_cmdline(MODULES_DIR, *sys.argv)
535 modules = out['modules']
536 if out['interval'] is not None:
537 interval = out['interval']
538
544 - # configure environment to run modules
545 - sys.path.append(modules_dir + "python_modules") # append path to directory with modules dependencies
546 -
539 # run plugins
548 - charts = PythonCharts(interval, modules, modules_dir, modules_conf, disabled)
540 + charts = PythonCharts(interval, modules, MODULES_DIR, modules_conf, disabled)
541 charts.check()
542 charts.create()
543 charts.update()