Skip to content

Commit

Permalink
[monitoring] Updated files and corrected failing qa checks #274
Browse files Browse the repository at this point in the history
Fixes #274
  • Loading branch information
praptisharma28 committed Jun 11, 2024
1 parent d02db9c commit 0aa3c6d
Show file tree
Hide file tree
Showing 13 changed files with 579 additions and 841 deletions.
12 changes: 2 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,15 @@ RUN pip install -r /opt/openwisp/requirements.txt && \
pip install -r /opt/openwisp/requirements-test.txt && \
rm -rf /var/lib/apt/lists/* /root/.cache/pip/* /tmp/*

# Copy project files and install the project
ADD . /opt/openwisp
RUN pip install -U /opt/openwisp && \
rm -rf /var/lib/apt/lists/* /root/.cache/pip/* /tmp/*

# Set working directory
WORKDIR /opt/openwisp/tests/

# Set environment variables
ENV NAME=openwisp-monitoring \
PYTHONBUFFERED=1 \
INFLUXDB1_HOST=influxdb \
INFLUXDB2_HOST=influxdb2 \
INFLUXDB_HOST=influxdb \
REDIS_HOST=redis

# Expose the application port
CMD ["sh", "docker-entrypoint.sh"]
EXPOSE 8000

# Command to run the application
CMD ["sh", "docker-entrypoint.sh"]
163 changes: 113 additions & 50 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -326,20 +326,23 @@ Follow the setup instructions of `openwisp-controller
# Make sure you change them in production
# You can select one of the backends located in openwisp_monitoring.db.backends
TIMESERIES_DATABASE = {
INFLUXDB_1x_DATABASE = {
'BACKEND': 'openwisp_monitoring.db.backends.influxdb',
'USER': 'openwisp',
'PASSWORD': 'openwisp',
'NAME': 'openwisp2',
'HOST': 'localhost',
'HOST': 'influxdb',
'PORT': '8086',
'OPTIONS': {
# Specify additional options to be used while initializing
# database connection.
# Note: These options may differ based on the backend used.
'udp_writes': True,
'udp_port': 8089,
}
'OPTIONS': {'udp_writes': False, 'udp_port': 8089},
}
INFLUXDB_2x_DATABASE = {
'BACKEND': 'openwisp_monitoring.db.backends.influxdb2',
'TOKEN': 'my-super-secret-auth-token',
'ORG': 'openwisp',
'BUCKET': 'openwisp2',
'HOST': 'influxdb2',
'PORT': '9999',
}
``urls.py``:
Expand Down Expand Up @@ -1413,56 +1416,109 @@ Settings
| **default**: | see below |
+--------------+-----------+

Timeseries Database Configuration
---------------------------------

The ``TIMESERIES_DATABASE`` setting allows configuring the timeseries
database backend used by OpenWISP Monitoring. The configuration supports
both InfluxDB 1.x and 2.x versions.

Configuration for InfluxDB 1.x
------------------------------

.. code-block:: python
TIMESERIES_DATABASE = {
INFLUXDB_1x_DATABASE = {
'BACKEND': 'openwisp_monitoring.db.backends.influxdb',
'USER': 'openwisp',
'PASSWORD': 'openwisp',
'NAME': 'openwisp2',
'HOST': 'localhost',
'HOST': 'influxdb',
'PORT': '8086',
'OPTIONS': {
'udp_writes': False,
'udp_port': 8089,
}
'OPTIONS': {'udp_writes': False, 'udp_port': 8089},
}
The following table describes all keys available in ``TIMESERIES_DATABASE``
setting:

+---------------+--------------------------------------------------------------------------------------+
| **Key** | ``Description`` |
+---------------+--------------------------------------------------------------------------------------+
| ``BACKEND`` | The timeseries database backend to use. You can select one of the backends |
| | located in ``openwisp_monitoring.db.backends`` |
+---------------+--------------------------------------------------------------------------------------+
| ``USER`` | User for logging into the timeseries database |
+---------------+--------------------------------------------------------------------------------------+
| ``PASSWORD`` | Password of the timeseries database user |
+---------------+--------------------------------------------------------------------------------------+
| ``NAME`` | Name of the timeseries database |
+---------------+--------------------------------------------------------------------------------------+
| ``HOST`` | IP address/hostname of machine where the timeseries database is running |
+---------------+--------------------------------------------------------------------------------------+
| ``PORT`` | Port for connecting to the timeseries database |
+---------------+--------------------------------------------------------------------------------------+
| ``OPTIONS`` | These settings depends on the timeseries backend: |
| | |
| | +-----------------+----------------------------------------------------------------+ |
| | | ``udp_writes`` | Whether to use UDP for writing data to the timeseries database | |
| | +-----------------+----------------------------------------------------------------+ |
| | | ``udp_port`` | Timeseries database port for writing data using UDP | |
| | +-----------------+----------------------------------------------------------------+ |
+---------------+--------------------------------------------------------------------------------------+

**Note:** UDP packets can have a maximum size of 64KB. When using UDP for writing timeseries
data, if the size of the data exceeds 64KB, TCP mode will be used instead.

**Note:** If you want to use the ``openwisp_monitoring.db.backends.influxdb`` backend
with UDP writes enabled, then you need to enable two different ports for UDP
(each for different retention policy) in your InfluxDB configuration. The UDP configuration
section of your InfluxDB should look similar to the following:
Configuration for InfluxDB 2.x
------------------------------

.. code-block:: python
INFLUXDB_2x_DATABASE = {
'BACKEND': 'openwisp_monitoring.db.backends.influxdb2',
'TOKEN': 'my-super-secret-auth-token',
'ORG': 'openwisp',
'BUCKET': 'openwisp2',
'HOST': 'influxdb2',
'PORT': '9999',
}
Dynamic Configuration Based on Environment
------------------------------------------

You can dynamically switch between InfluxDB 1.x and 2.x configurations
using environment variables:

.. code-block:: python
import os
if os.environ.get('USE_INFLUXDB2', 'False') == 'True':
TIMESERIES_DATABASE = INFLUXDB_2x_DATABASE
else:
TIMESERIES_DATABASE = INFLUXDB_1x_DATABASE
if TESTING:
if os.environ.get('TIMESERIES_UDP', False):
TIMESERIES_DATABASE['OPTIONS'] = {'udp_writes': True, 'udp_port': 8091}
Explanation of Settings
-----------------------

+---------------+---------------------------------------------------------------+
| **Key** | **Description** |
+-------------------------------------------------------------------------------+
| ``BACKEND`` | The timeseries database backend to use. You can select one |
| | of the backends located in ``openwisp_monitoring.db.backends``|
+---------------+---------------------------------------------------------------+
| ``USER`` | User for logging into the timeseries database (only for |
| | InfluxDB 1.x) |
+---------------+---------------------------------------------------------------+
| ``PASSWORD`` | Password of the timeseries database user (only for InfluxDB |
| | 1.x) |
+---------------+---------------------------------------------------------------+
| ``NAME`` | Name of the timeseries database (only for InfluxDB 1.x) |
+---------------+---------------------------------------------------------------+
| ``TOKEN`` | Authentication token for InfluxDB 2.x |
+---------------+---------------------------------------------------------------+
| ``ORG`` | Organization name for InfluxDB 2.x |
+---------------+---------------------------------------------------------------+
| ``BUCKET`` | Bucket name for InfluxDB 2.x |
+---------------+---------------------------------------------------------------+
| ``HOST`` | IP address/hostname of machine where the timeseries |
| | database is running |
+---------------+---------------------------------------------------------------+
| ``PORT`` | Port for connecting to the timeseries database |
+---------------+---------------------------------------------------------------+
| ``OPTIONS`` | Additional options for the timeseries backend |
| | |
| | +-----------------+-----------------------------------------+ |
| | | ``udp_writes`` | Whether to use UDP for writing data | |
| | | | to the timeseries database | |
| | +-----------------+-----------------------------------------+ |
| | | ``udp_port`` | Timeseries database port for writing | |
| | | | data using UDP | |
| | +-----------------+-----------------------------------------+ |
+---------------+---------------------------------------------------------------+

UDP Configuration for InfluxDB 1.x
----------------------------------

If you want to use the ``openwisp_monitoring.db.backends.influxdb`` backend
with UDP writes enabled, you need to enable two different ports for UDP
(each for a different retention policy) in your InfluxDB configuration.

Here is an example of the UDP configuration section in your InfluxDB
configuration file:

.. code-block:: text
Expand All @@ -1479,6 +1535,13 @@ section of your InfluxDB should look similar to the following:
database = "openwisp2"
retention-policy = 'short'
**Note:** UDP packets can have a maximum size of 64KB. When using UDP for
writing timeseries data, if the size of the data exceeds 64KB, TCP mode
will be used instead.

Deploying with Ansible
----------------------

If you are using `ansible-openwisp2 <https://github.com/openwisp/ansible-openwisp2>`_
for deploying OpenWISP, you can set the ``influxdb_udp_mode`` ansible variable to ``true``
in your playbook, this will make the ansible role automatically configure the InfluxDB UDP listeners.
Expand Down
21 changes: 17 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ services:
dockerfile: Dockerfile
ports:
- "8000:8000"
- "8089:8089/udp"
- "8090:8090/udp"
- "8091:8091/udp"
- "8092:8092/udp"
depends_on:
- influxdb
- redis
Expand All @@ -32,6 +28,22 @@ services:
INFLUXDB_USER: openwisp
INFLUXDB_USER_PASSWORD: openwisp

influxdb2:
image: influxdb:2.0
container_name: influxdb2
ports:
# Map the 9086 port on host machine to 8086 in container
- "9086:8086"
environment:
DOCKER_INFLUXDB_INIT_MODE: setup
DOCKER_INFLUXDB_INIT_USERNAME: myuser
DOCKER_INFLUXDB_INIT_PASSWORD: mypassword
DOCKER_INFLUXDB_INIT_ORG: myorg
DOCKER_INFLUXDB_INIT_BUCKET: mybucket
DOCKER_INFLUXDB_INIT_RETENTION: 1w
volumes:
- influxdb-storage:/var/lib/influxdb2

redis:
image: redis:5.0-alpine
ports:
Expand All @@ -40,3 +52,4 @@ services:

volumes:
influxdb-data: {}
influxdb-storage:
16 changes: 12 additions & 4 deletions openwisp_monitoring/db/backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ def load_backend_module(backend_name=TIMESERIES_DB['BACKEND'], module=None):
assert 'USER' in TIMESERIES_DB, 'USER'
assert 'PASSWORD' in TIMESERIES_DB, 'PASSWORD'
assert 'NAME' in TIMESERIES_DB, 'NAME'
assert 'USER' in TIMESERIES_DB, 'USER'
assert 'PASSWORD' in TIMESERIES_DB, 'PASSWORD'
assert 'NAME' in TIMESERIES_DB, 'NAME'
assert 'HOST' in TIMESERIES_DB, 'HOST'
assert 'PORT' in TIMESERIES_DB, 'PORT'
if module:
Expand All @@ -67,7 +64,18 @@ def load_backend_module(backend_name=TIMESERIES_DB['BACKEND'], module=None):
"Try using 'openwisp_monitoring.db.backends.XXX', where XXX is one of:\n"
f"{builtin_backends}"
) from e
else:
raise e


if '2' in TIMESERIES_DB['BACKEND']:
timeseries_db = load_backend_module(module='client').DatabaseClient(
bucket=TIMESERIES_DB['BUCKET'],
org=TIMESERIES_DB['ORG'],
token=TIMESERIES_DB['TOKEN'],
url=f"http://{TIMESERIES_DB['HOST']}:{TIMESERIES_DB['PORT']}",
)
else:
timeseries_db = load_backend_module(module='client').DatabaseClient()

timeseries_db = load_backend_module(module='client').DatabaseClient()
timeseries_db.queries = load_backend_module(module='queries')
42 changes: 0 additions & 42 deletions openwisp_monitoring/db/backends/base.py

This file was deleted.

Empty file.
Loading

0 comments on commit 0aa3c6d

Please sign in to comment.