]> arthur.barton.de Git - netdata.git/commitdiff
Merge pull request #595 from ktsaou/master
authorCosta Tsaousis <costa@tsaousis.gr>
Wed, 22 Jun 2016 10:17:25 +0000 (13:17 +0300)
committerGitHub <noreply@github.com>
Wed, 22 Jun 2016 10:17:25 +0000 (13:17 +0300)
more python.d.plugin debugging

.gitignore
build/subst.inc
plugins.d/python.d.plugin
python.d/Makefile.am
python.d/pip_install.sh [deleted file]
python.d/python-modules-installer.sh.in [new file with mode: 0755]

index 6267fd10a742f08f7a28ff8727b32cc1bbd450b4..c90442ce4016726dd6f43919966d95c064f7fd90 100644 (file)
@@ -62,6 +62,7 @@ system/netdata-lsb
 system/netdata-openrc
 system/netdata-init-d
 system/netdata.logrotate
+python.d/python-modules-installer.sh
 
 netdata-installer.log
 netdata-uninstaller.sh
index 99cac7f4f56b4c96dcc8d91be7aac9046e3bfe71..9682cf882f6f8380abc74f5a7c5ba7ab1cfd6cf9 100644 (file)
@@ -3,6 +3,7 @@
                -e 's#[@]localstatedir_POST@#$(localstatedir)#g' \
                -e 's#[@]sbindir_POST@#$(sbindir)#g' \
                -e 's#[@]sysconfdir_POST@#$(sysconfdir)#g' \
+               -e 's#[@]pythondir_POST@#$(pythondir)#g' \
                $< > $@.tmp; then \
                mv "$@.tmp" "$@"; \
        else \
index a16e1b8ccfcdeece46becdc4498ea8984ba9c044..f0ff63f1cc7aecb68eea876a2dd8796ab431ab78 100755 (executable)
@@ -167,7 +167,7 @@ class PythonCharts(object):
             else:
                 return imp.load_source(name, path)
         except Exception as e:
-            debug("Problem loading", name, str(e))
+            error("Problem loading", name, str(e))
             return None
 
     def _load_modules(self, path, modules, disabled):
@@ -254,8 +254,8 @@ class PythonCharts(object):
         """
         # get default values
         defaults = {}
+        debug(module.__name__ + ": reading configuration")
         for key in BASE_CONFIG:
-            debug(module.__name__ + ": reading configuration")
             try:
                 # get defaults from module config
                 defaults[key] = int(config.pop(key))
@@ -364,7 +364,7 @@ class PythonCharts(object):
                 if not job.check():
                     self._stop(job, "failed check")
                 else:
-                    debug(job.name, ": check succeeded")
+                    debug(job.chart_name, ": check succeeded")
                     i += 1
             except AttributeError:
                 self._stop(job, "no check")
@@ -418,13 +418,14 @@ class PythonCharts(object):
         t_start = time.time()
         # check if it is time to execute job update() function
         if job.timetable['next'] > t_start:
-            debug("it is not a time to invoke update on", job.chart_name)
+            debug(job.chart_name + " will be run in " + str(int((job.timetable['next'] - t_start) * 1000)) + " ms")
             return True
         try:
             if self.first_run:
                 since_last = 0
             else:
                 since_last = int((t_start - job.timetable['last']) * 1000000)
+                debug(job.chart_name + " ready to run, after " + str(int((t_start - job.timetable['last']) * 1000)) + " ms (update_every: " + str(job.timetable['freq'] * 1000) + " ms, latency: " + str(int((t_start - job.timetable['next']) * 1000)) + " ms)")
             if not job.update(since_last):
                 if job.retries_left <= 0:
                     self._stop(job, "update failed")
@@ -442,7 +443,7 @@ class PythonCharts(object):
         job.timetable['next'] = t_end - (t_end % job.timetable['freq']) + job.timetable['freq']
         # draw performance graph
         run_time = str(int((t_end - t_start) * 1000))
-        debug(job.chart_name, "updated in", run_time)
+        debug(job.chart_name, "updated in", run_time, "ms")
         sys.stdout.write("BEGIN netdata.plugin_pythond_" + job.chart_name + " " + str(since_last) + '\n')
         sys.stdout.write("SET run_time = " + run_time + '\n')
         sys.stdout.write("END\n")
@@ -516,8 +517,9 @@ def parse_cmdline(directory, *commands):
         else:
             try:
                 BASE_CONFIG['update_every'] = int(cmd)
-                OVERRIDE_UPDATE_EVERY = True
-                debug(PROGRAM, "overriding update interval to", str(int(cmd)))
+                if DEBUG_FLAG:
+                    OVERRIDE_UPDATE_EVERY = True
+                    debug(PROGRAM, "overriding update interval to", str(int(cmd)))
             except ValueError:
                 pass
 
index 955e2176ae357a01efe9cc33e0ac69ffb9385b23..eef6594bb2d056f27a758647dd9305678f24ac7f 100644 (file)
@@ -1,8 +1,13 @@
 MAINTAINERCLEANFILES= $(srcdir)/Makefile.in
 
+include $(top_srcdir)/build/subst.inc
+
+SUFFIXES = .in
+
 dist_python_SCRIPTS = \
        example.chart.py \
        mysql.chart.py \
+       python-modules-installer.sh \
        $(NULL)
 
 dist_python_DATA = \
diff --git a/python.d/pip_install.sh b/python.d/pip_install.sh
deleted file mode 100644 (file)
index 84ebb1d..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-#!/bin/bash
-
-PIP=`which pip`
-
-${PIP} install --target="python_modules" yaml
-
-${PIP} install --target="python_modules" MySQL-python || echo "You need to install libmysqlclient-dev and python-dev"
diff --git a/python.d/python-modules-installer.sh.in b/python.d/python-modules-installer.sh.in
new file mode 100755 (executable)
index 0000000..955762d
--- /dev/null
@@ -0,0 +1,138 @@
+#!/bin/bash
+
+umask 022
+
+dir="@pythondir_POST@"
+target="${dir}/python_modules"
+pv="$(python -V 2>&1)"
+
+# parse parameters
+while [ ! -z "${1}" ]
+do
+    case "${1}" in
+    -p|--python)
+        pv="Python ${2}"
+        shift 2
+        ;;
+
+    -d|--dir)
+        dir="${2}"
+        target="${dir}/python_modules"
+        echo >&2 "Will install python modules in: '${target}'"
+        shift 2
+        ;;
+
+    -s|--system)
+        target=
+        echo >&2 "Will install python modules system-wide"
+        shift
+        ;;
+
+    -h|--help)
+        echo "${0} [--dir netdata-python.d-path] [--system]"
+        exit 0
+        ;;
+
+    *)
+        echo >&2 "Cannot understand parameter: ${1}"
+        exit 1
+        ;;
+    esac
+done
+
+
+if [ ! -z "${target}" -a ! -d "${target}" ]
+then
+    echo >&2 "Cannot find directory: '${target}'"
+    exit 1
+fi
+
+if [[ "${pv}" =~ ^Python\ 2.* ]]
+then
+    pv=2
+    pip="$(which pip2 2>/dev/null)"
+elif [[ "${pv}" =~ ^Python\ 3.* ]]
+then
+    pv=3
+    pip="$(which pip3 2>/dev/null)"
+else
+    echo >&2 "Cannot detect python version"
+    exit 1
+fi
+
+[ -z "${pip}" ] && pip="$(which pip 2>/dev/null)"
+if [ -z "${pip}" ]
+then
+    echo >& "pip command is required to install python v${pv} modules"
+    exit 1
+fi
+
+echo >&2 "Working for python version ${pv} (pip command: '${pip}')"
+echo >&2 "Installing netdata python modules in: '${target}'"
+
+run() {
+    printf "Running command:\n# "
+    printf "%q " "${@}"
+    printf "\n"
+    "${@}"
+}
+
+# try to install all the python modules given as parameters
+# until the first that succeeds
+failed=""
+installed=""
+errors=0
+pip_install() {
+    local ret x
+
+    echo >&2
+    echo >&2
+    echo >&2 "Installing one of: ${*}"
+
+    for x in "${@}"
+    do
+        echo >&2
+        echo >&2 "attempting to install: ${x}"
+        if [ ! -z "${target}" ]
+        then
+            run "${pip}" install --target "${target}" "${x}"
+            ret=$?
+        else
+            run "${pip}" install "${x}"
+            ret=$?
+        fi
+        [ ${ret} -eq 0 ] && break
+        echo >&2 "failed to install: ${x}"
+    done
+
+    if [ ${ret} -ne 0 ]
+    then
+        echo >&2 "ERROR: could not install any of: ${*}"
+        errors=$(( errors + 1 ))
+        failed="${failed}|${*}"
+    else
+        echo >&2 "SUCCESS: we have: ${x}"
+        installed="${installed} ${x}"
+    fi
+    return ${ret}
+}
+
+if [ "${pv}" = "2" ]
+then
+    pip_install pyyaml yaml
+    pip_install mysqlclient mysql-python pymysql
+else
+    pip_install yaml pyyaml
+    pip_install mysql-python mysqlclient pymysql
+fi
+
+echo >&2
+echo >&2
+if [ ${errors} -ne 0 ]
+then
+    echo >&2 "Failed to install ${errors} modules: ${failed}"
+    exit 1
+else
+    echo >&2 "All done. We have: ${installed}"
+    exit 0
+fi