mirror of
https://github.com/netdata/netdata.git
synced 2025-05-18 07:01:20 +00:00
pandas collector add read_sql()
support (#14563)
* import sqlalchemy and os to support read_sql
This commit is contained in:
parent
cac836070f
commit
df9cb39a81
3 changed files with 39 additions and 1 deletions
collectors/python.d.plugin/pandas
|
@ -28,6 +28,12 @@ This collector depends on some Python (Python 3 only) packages that can usually
|
||||||
sudo pip install pandas requests
|
sudo pip install pandas requests
|
||||||
```
|
```
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo pip install 'sqlalchemy<2.0' psycopg2-binary
|
||||||
|
```
|
||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
|
|
||||||
Below is an example configuration to query some json weather data from [Open-Meteo](https://open-meteo.com),
|
Below is an example configuration to query some json weather data from [Open-Meteo](https://open-meteo.com),
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
# Author: Andrew Maguire (andrewm4894)
|
# Author: Andrew Maguire (andrewm4894)
|
||||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
|
import os
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -11,6 +12,12 @@ try:
|
||||||
except ImportError:
|
except ImportError:
|
||||||
HAS_REQUESTS = False
|
HAS_REQUESTS = False
|
||||||
|
|
||||||
|
try:
|
||||||
|
from sqlalchemy import create_engine
|
||||||
|
HAS_SQLALCHEMY = True
|
||||||
|
except ImportError:
|
||||||
|
HAS_SQLALCHEMY = False
|
||||||
|
|
||||||
from bases.FrameworkServices.SimpleService import SimpleService
|
from bases.FrameworkServices.SimpleService import SimpleService
|
||||||
|
|
||||||
ORDER = []
|
ORDER = []
|
||||||
|
@ -48,6 +55,9 @@ class Service(SimpleService):
|
||||||
if not HAS_REQUESTS:
|
if not HAS_REQUESTS:
|
||||||
self.warn('requests library could not be imported')
|
self.warn('requests library could not be imported')
|
||||||
|
|
||||||
|
if not HAS_SQLALCHEMY:
|
||||||
|
self.warn('sqlalchemy library could not be imported')
|
||||||
|
|
||||||
if not self.chart_configs:
|
if not self.chart_configs:
|
||||||
self.error('chart_configs must be defined')
|
self.error('chart_configs must be defined')
|
||||||
|
|
||||||
|
|
|
@ -188,4 +188,26 @@ update_every: 5
|
||||||
# df_steps: >
|
# df_steps: >
|
||||||
# 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')|
|
# 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')|
|
||||||
# df.rename(columns={'value': 'dublin'})|
|
# df.rename(columns={'value': 'dublin'})|
|
||||||
# df[['dublin']]|
|
# df[['dublin']]|
|
||||||
|
|
||||||
|
# example showing a read_sql from a postgres database using sqlalchemy.
|
||||||
|
# note: example assumes a running postgress db on localhost with a netdata users and password netdata.
|
||||||
|
# sql:
|
||||||
|
# name: "sql"
|
||||||
|
# update_every: 5
|
||||||
|
# chart_configs:
|
||||||
|
# - name: "sql"
|
||||||
|
# title: "SQL Example"
|
||||||
|
# family: "sql.example"
|
||||||
|
# context: "example"
|
||||||
|
# type: "line"
|
||||||
|
# units: "percent"
|
||||||
|
# df_steps: >
|
||||||
|
# pd.read_sql_query(
|
||||||
|
# sql='\
|
||||||
|
# select \
|
||||||
|
# random()*100 as metric_1, \
|
||||||
|
# random()*100 as metric_2 \
|
||||||
|
# ',
|
||||||
|
# con=create_engine('postgresql://localhost/postgres?user=netdata&password=netdata')
|
||||||
|
# );
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue