add tor python module (#4546)
* tor: python tor module init version * tor: minor * tor: minor * tor: minor * tor: user super in __init__ * tor: do not suggest to use password by default * tor: add module to python.d.conf
Ilya Mashchenko committed
Nov 5, 2018 at 22:38 UTC
bc91d1624354fb63d54471ada1835f3a4d469ef8
6 files changed
+248
collectors/python.d.plugin/Makefile.am
+1
@@ -95,6 +95,7 @@ include spigotmc/Makefile.inc
95
include springboot/Makefile.inc
96
include squid/Makefile.inc
97
include tomcat/Makefile.inc
98
+include tor/Makefile.inc
99
include traefik/Makefile.inc
100
include unbound/Makefile.inc
101
include uwsgi/Makefile.inc
collectors/python.d.plugin/python.d.conf
+1
@@ -90,6 +90,7 @@ nginx_log: no
90
# springboot: yes
91
# squid: yes
92
# tomcat: yes
93
+# tor: yes
94
unbound: no
95
# uwsgi: yes
96
# varnish: yes
collectors/python.d.plugin/tor/Makefile.inc
new
+13
@@ -0,0 +1,13 @@
1
+# SPDX-License-Identifier: GPL-3.0-or-later
2
+
3
+# THIS IS NOT A COMPLETE Makefile
4
+# IT IS INCLUDED BY ITS PARENT'S Makefile.am
5
+# IT IS REQUIRED TO REFERENCE ALL FILES RELATIVE TO THE PARENT
6
+
7
+# install these files
8
+dist_python_DATA += tor/tor.chart.py
9
+dist_pythonconfig_DATA += tor/tor.conf
10
+
11
+# do not install these files, but include them in the distribution
12
+dist_noinst_DATA += tor/README.md tor/Makefile.inc
13
+
collectors/python.d.plugin/tor/README.md
new
+46
@@ -0,0 +1,46 @@
1
+# tor
2
+
3
+Module connects to tor control port to collect traffic statistics.
4
+
5
+**Requirements:**
6
+* `tor` program
7
+* `stem` python package
8
+
9
+It produces only one chart:
10
+
11
+1. **Traffic**
12
+ * read
13
+ * write
14
+
15
+### configuration
16
+
17
+Needs only `control_port`
18
+
19
+Here is an example for local server:
20
+
21
+```yaml
22
+update_every : 1
23
+priority : 60000
24
+
25
+local_tcp:
26
+ name: 'local'
27
+ control_port: 9051
28
+
29
+local_socket:
30
+ name: 'local'
31
+ control_port: '/var/run/tor/control'
32
+```
33
+
34
+### prerequisite
35
+
36
+Add to `/etc/tor/torrc`:
37
+
38
+```
39
+ControlPort 9051
40
+```
41
+
42
+For more options please read the manual.
43
+
44
+Without configuration, module attempts to connect to `127.0.0.1:9051`.
45
+
46
+---
collectors/python.d.plugin/tor/tor.chart.py
new
+108
@@ -0,0 +1,108 @@
1
+# -*- coding: utf-8 -*-
2
+# Description: adaptec_raid netdata python.d module
3
+# Author: Federico Ceratto <federico.ceratto@gmail.com>
4
+# Author: Ilya Mashchenko (l2isbad)
5
+# SPDX-License-Identifier: GPL-3.0-or-later
6
+
7
+
8
+from bases.FrameworkServices.SimpleService import SimpleService
9
+
10
+try:
11
+ import stem
12
+ import stem.connection
13
+ import stem.control
14
+ STEM_AVAILABLE = True
15
+except ImportError:
16
+ STEM_AVAILABLE = False
17
+
18
+
19
+DEF_PORT = 'default'
20
+
21
+ORDER = [
22
+ 'traffic',
23
+]
24
+
25
+CHARTS = {
26
+ 'traffic': {
27
+ 'options': [None, 'Tor Traffic', 'KB/s', 'traffic', 'tor.traffic', 'area'],
28
+ 'lines': [
29
+ ['read', 'read', 'incremental', 1, 1024],
30
+ ['write', 'write', 'incremental', 1, -1024],
31
+ ]
32
+ }
33
+}
34
+
35
+
36
+class Service(SimpleService):
37
+ """Provide netdata service for Tor"""
38
+ def __init__(self, configuration=None, name=None):
39
+ super(Service, self).__init__(configuration=configuration, name=name)
40
+ self.order = ORDER
41
+ self.definitions = CHARTS
42
+
43
+ self.port = self.configuration.get('control_port', DEF_PORT)
44
+ self.password = self.configuration.get('password')
45
+
46
+ self.use_socket = isinstance(self.port, str) and self.port != DEF_PORT and not self.port.isdigit()
47
+ self.conn = None
48
+ self.alive = False
49
+
50
+ def check(self):
51
+ if not STEM_AVAILABLE:
52
+ self.error('the stem library is missing')
53
+ return False
54
+
55
+ return self.connect()
56
+
57
+ def get_data(self):
58
+ if not self.alive and not self.reconnect():
59
+ return None
60
+
61
+ data = dict()
62
+
63
+ try:
64
+ data['read'] = self.conn.get_info('traffic/read')
65
+ data['write'] = self.conn.get_info('traffic/written')
66
+ except stem.ControllerError as error:
67
+ self.debug(error)
68
+ self.alive = False
69
+
70
+ return data or None
71
+
72
+ def authenticate(self):
73
+ try:
74
+ self.conn.authenticate(password=self.password)
75
+ except stem.connection.AuthenticationFailure as error:
76
+ self.error('authentication error: {0}'.format(error))
77
+ return False
78
+ return True
79
+
80
+ def connect_via_port(self):
81
+ try:
82
+ self.conn = stem.control.Controller.from_port(port=self.port)
83
+ except (stem.SocketError, ValueError) as error:
84
+ self.error(error)
85
+
86
+ def connect_via_socket(self):
87
+ try:
88
+ self.conn = stem.control.Controller.from_socket_file(path=self.port)
89
+ except (stem.SocketError, ValueError) as error:
90
+ self.error(error)
91
+
92
+ def connect(self):
93
+ if self.conn:
94
+ self.conn.close()
95
+ self.conn = None
96
+
97
+ if self.use_socket:
98
+ self.connect_via_socket()
99
+ else:
100
+ self.connect_via_port()
101
+
102
+ if self.conn and self.authenticate():
103
+ self.alive = True
104
+
105
+ return self.alive
106
+
107
+ def reconnect(self):
108
+ return self.connect()
collectors/python.d.plugin/tor/tor.conf
new
+79
@@ -0,0 +1,79 @@
1
+# netdata python.d.plugin configuration for tor
2
+#
3
+# This file is in YaML format. Generally the format is:
4
+#
5
+# name: value
6
+#
7
+# There are 2 sections:
8
+# - global variables
9
+# - one or more JOBS
10
+#
11
+# JOBS allow you to collect values from multiple sources.
12
+# Each source will have its own set of charts.
13
+#
14
+# JOB parameters have to be indented (using spaces only, example below).
15
+
16
+# ----------------------------------------------------------------------
17
+# Global Variables
18
+# These variables set the defaults for all JOBs, however each JOB
19
+# may define its own, overriding the defaults.
20
+
21
+# update_every sets the default data collection frequency.
22
+# If unset, the python.d.plugin default is used.
23
+# update_every: 1
24
+
25
+# priority controls the order of charts at the netdata dashboard.
26
+# Lower numbers move the charts towards the top of the page.
27
+# If unset, the default for python.d.plugin is used.
28
+# priority: 60000
29
+
30
+# retries sets the number of retries to be made in case of failures.
31
+# If unset, the default for python.d.plugin is used.
32
+# Attempts to restore the service are made once every update_every
33
+# and only if the module has collected values in the past.
34
+# retries: 60
35
+
36
+# autodetection_retry sets the job re-check interval in seconds.
37
+# The job is not deleted if check fails.
38
+# Attempts to start the job are made once every autodetection_retry.
39
+# This feature is disabled by default.
40
+# autodetection_retry: 0
41
+
42
+# ----------------------------------------------------------------------
43
+# JOBS (data collection sources)
44
+#
45
+# The default JOBS share the same *name*. JOBS with the same name
46
+# are mutually exclusive. Only one of them will be allowed running at
47
+# any time. This allows autodetection to try several alternatives and
48
+# pick the one that works.
49
+#
50
+# Any number of jobs is supported.
51
+#
52
+# All python.d.plugin JOBS (for all its modules) support a set of
53
+# predefined parameters. These are:
54
+#
55
+# job_name:
56
+# name: myname # the JOB's name as it will appear at the
57
+# # dashboard (by default is the job_name)
58
+# # JOBs sharing a name are mutually exclusive
59
+# update_every: 1 # the JOB's data collection frequency
60
+# priority: 60000 # the JOB's order on the dashboard
61
+# retries: 10 # the JOB's number of restoration attempts
62
+# autodetection_retry: 0 # the JOB's re-check interval in seconds
63
+#
64
+# Additionally to the above, tor plugin also supports the following:
65
+#
66
+# control_port: 'port' # tor control port
67
+# password: 'password' # tor control password
68
+#
69
+# ----------------------------------------------------------------------
70
+# AUTO-DETECTION JOBS
71
+# only one of them will run (they have the same name)
72
+#
73
+# local_tcp:
74
+# name: 'local'
75
+# control_port: 9051
76
+#
77
+# local_socket:
78
+# name: 'local'
79
+# control_port: '/var/run/tor/control'