Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for exporting to influxdb v2. #217

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions glances/DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ influxdb:
prefix: localhost
interval: 60
ssl: false
influxdb2:
enabled: false
host: my.influxdb.host
port: 8086
token: "!secret glances_influxdbv2_token"
bucket: glances
org: myorg
interval: 60
ssl: false
```

**Note**: _This is just an example, don't copy and paste it! Create your own!_
Expand Down Expand Up @@ -147,6 +156,57 @@ Adding this option will allow SSL to be used on the InfluxDB connection. If not
set will default to `false` which is the required setting for the Community
InfluxDB add-on.

### Option group `influxdb2`

These options apply to data export to an InfluxDB v2.x database.

---

The following options are for the option group: `influxdb2`. These settings
only apply to the Glances InfluxDB v2.x data export.

#### Option `influxdb2`: `enabled`

Enables/Disables the Glances data export to InfluxDB.

#### Option `influxdb2`: `host`

The hostname where InfluxDB is running.

#### Option `influxdb2`: `port`

The port on which InfluxDB is listening.

#### Option `influxdb2`: `token`

An InfluxDB token with permissions to write to the given bucket. This should
look like `t9iHPiGQyg0ds4K1IlBrCyBsNGh71dkdR6u8Y9eeR37UzfGuFukFCdbMI4YA9EtKb4zr5coFXKw67tbBEP7CPw==`

#### Option `influxdb2`: `bucket`

The name of the bucket to store all Glances information into.

**Note**: _It is strongly recommended to create a separate bucket for glances
and not store this in the same bucket as Home Assistant._

#### Option `influxdb2`: `organization`

The InfluxDB organization that owns the given bucket.

#### Option `prefix`: `localhost`

The hostname to append for exported data.

#### Option `influxdb2`: `interval`

Defines the interval (in seconds) on how often Glances exports data to InfluxDB.

#### Option `influxdb2`: `ssl`

Adding this option will allow SSL to be used on the InfluxDB connection. If not
set will default to `false` which is the required setting for the Community
InfluxDB add-on.

## Adding Glances as a sensor into Home Assistant

The Home Assistant Glances sensor platform is consuming the system information
Expand Down
21 changes: 21 additions & 0 deletions glances/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@
"database": "glances",
"prefix": "localhost",
"interval": 60
},
"influxdb2": {
"enabled": false,
"host": "",
"port": 8086,
"token": "",
"bucket": "glances",
"organization": "",
"prefix": "localhost",
"interval": 60
}
},
"schema": {
Expand All @@ -60,6 +70,17 @@
"interval": "int",
"ssl": "bool?"
},
"influxdb2": {
"enabled": "bool",
"host": "str",
"port": "port",
"token": "str",
"bucket": "str",
"organization": "str",
"prefix": "str",
"interval": "int",
"ssl": "bool?"
},
"leave_front_door_open": "bool?"
},
"environment": {
Expand Down
1 change: 1 addition & 0 deletions glances/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ bottle==0.12.19
docker==5.0.3
glances==3.2.3.1
influxdb==5.3.1
influxdb-client==1.21.0
netifaces==0.11.0
paho-mqtt==1.5.1
psutil==5.8.0
Expand Down
19 changes: 18 additions & 1 deletion glances/rootfs/etc/cont-init.d/glances.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ else
fi

# Export Glances data to InfluxDB
# See https://glances.readthedocs.io/en/latest/gw/influxdb.html for
# configuration docs
if bashio::config.true 'influxdb.enabled'; then
protocol='http'
if bashio::config.true 'influxdb.ssl'; then
protocol='https'
fi
# Modify the configuration
{
echo "[influxdb]"
echo "host=$(bashio::config 'influxdb.host')"
Expand All @@ -35,3 +36,19 @@ if bashio::config.true 'influxdb.enabled'; then
echo "protocol=${protocol}"
} >> /etc/glances.conf
fi
if bashio::config.true 'influxdb2.enabled'; then
protocol='http'
if bashio::config.true 'influxdb2.ssl'; then
protocol='https'
fi
{
echo "[influxdb2]"
echo "host=$(bashio::config 'influxdb2.host')"
echo "port=$(bashio::config 'influxdb2.port')"
echo "token=$(bashio::config 'influxdb2.token')"
echo "bucket=$(bashio::config 'influxdb2.bucket')"
echo "org=$(bashio::config 'influxdb2.organization')"
echo "prefix=$(bashio::config 'influxdb.prefix')"
echo "protocol=${protocol}"
} >> /etc/glances.conf
fi
43 changes: 28 additions & 15 deletions glances/rootfs/etc/services.d/influxdb/run
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,40 @@
# ==============================================================================
declare -a options

if bashio::config.false 'influxdb.enabled'; then
if bashio::config.false 'influxdb.enabled' && bashio::config.false 'influxdb2.enabled'; then
# This script is re-run by systemd whenever it terminates, so sleep for a
# while to avoid a continual loop.
exec sleep 86400
fi

options+=(-C /etc/glances.conf)
options+=(--export influxdb)
options+=(--quiet)
function glances_export() {
influxdb_version="$1"
sleep_interval="$2"

options+=(--time "$(bashio::config 'refresh_time')")
options+=(-C /etc/glances.conf)
options+=(--export "$influxdb_version")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If both options are enabled, the influxdb2 process will export both v1 & v2, while v1 is also active.

options+=(--quiet)

if bashio::config.false 'process_info'; then
options+=(--disable-process)
fi
options+=(--time "$(bashio::config 'refresh_time')")

if bashio::debug; then
options+=(--debug)
fi
if bashio::config.false 'process_info'; then
options+=(--disable-process)
fi

# Interval
sleep "$(bashio::config 'influxdb.interval')"
if bashio::debug; then
options+=(--debug)
fi
Comment on lines +24 to +30
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are now added twice, when both influxdb1 & 2 are enabled


# Run Glances
exec glances "${options[@]}"
sleep "$sleep_interval"
glances "${options[@]}"
}

# Run the export processes in the background, then wait for both to complete
# (otherwise we delay one export by the other's interval).
if bashio::config.true 'influxdb.enabled'; then
glances_export "influxdb" "$(bashio::config 'influxdb.interval')" &
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not drop things in the background.

fi
if bashio::config.true 'influxdb2.enabled'; then
glances_export "influxdb2" "$(bashio::config 'influxdb2.interval')" &
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above.

fi
wait