pandas collector add `read_sql()` support (#14563)
* import sqlalchemy and os to support read_sql
Andrew Maguire committed
Feb 20, 2023 at 21:39 UTC
df9cb39a811c53ff36e7fe1dcb0f93ec2fde53ae
3 files changed
+39
-1
collectors/python.d.plugin/pandas/README.md
+6
@@ -28,6 +28,12 @@ This collector depends on some Python (Python 3 only) packages that can usually
28
sudo pip install pandas requests
29
```
30
31
+Note: If you would like to use [`pandas.read_sql`](https://pandas.pydata.org/docs/reference/api/pandas.read_sql.html) to query a database, you will need to install the below packages as well.
32
+
33
+```bash
34
+sudo pip install 'sqlalchemy<2.0' psycopg2-binary
35
+```
36
+
37
## Configuration
38
39
Below is an example configuration to query some json weather data from [Open-Meteo](https://open-meteo.com),
collectors/python.d.plugin/pandas/pandas.chart.py
+10
@@ -3,6 +3,7 @@
3
# Author: Andrew Maguire (andrewm4894)
4
# SPDX-License-Identifier: GPL-3.0-or-later
5
6
+import os
7
import pandas as pd
8
9
try:
@@ -11,6 +12,12 @@ try:
12
except ImportError:
13
HAS_REQUESTS = False
14
15
+try:
16
+ from sqlalchemy import create_engine
17
+ HAS_SQLALCHEMY = True
18
+except ImportError:
19
+ HAS_SQLALCHEMY = False
20
+
21
from bases.FrameworkServices.SimpleService import SimpleService
22
23
ORDER = []
@@ -48,6 +55,9 @@ class Service(SimpleService):
55
if not HAS_REQUESTS:
56
self.warn('requests library could not be imported')
57
58
+ if not HAS_SQLALCHEMY:
59
+ self.warn('sqlalchemy library could not be imported')
60
+
61
if not self.chart_configs:
62
self.error('chart_configs must be defined')
63
collectors/python.d.plugin/pandas/pandas.conf
+23
-1
@@ -188,4 +188,26 @@ update_every: 5
188
# df_steps: >
189
# pd.read_xml('http://metwdb-openaccess.ichec.ie/metno-wdb2ts/locationforecast?lat=54.7210798611;long=-8.7237392806', xpath='./product/time[1]/location/temperature', parser='etree')|
190
# df.rename(columns={'value': 'dublin'})|
191
-# df[['dublin']]|
\ No newline at end of file
191
+# df[['dublin']]|
192
+
193
+# example showing a read_sql from a postgres database using sqlalchemy.
194
+# note: example assumes a running postgress db on localhost with a netdata users and password netdata.
195
+# sql:
196
+# name: "sql"
197
+# update_every: 5
198
+# chart_configs:
199
+# - name: "sql"
200
+# title: "SQL Example"
201
+# family: "sql.example"
202
+# context: "example"
203
+# type: "line"
204
+# units: "percent"
205
+# df_steps: >
206
+# pd.read_sql_query(
207
+# sql='\
208
+# select \
209
+# random()*100 as metric_1, \
210
+# random()*100 as metric_2 \
211
+# ',
212
+# con=create_engine('postgresql://localhost/postgres?user=netdata&password=netdata')
213
+# );