Skip to content

Commit 192ffc9

Browse files
authored
Merge pull request #3 from ParadigmHyperloop/feature/CL/influx-log-example
feature/cl/influx-log-example
2 parents fc6f87d + f45dc10 commit 192ffc9

File tree

4 files changed

+598
-0
lines changed

4 files changed

+598
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vscode
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Example for logging data to influxdb using python
2+
# Sourced from the influxdb python docs
3+
4+
import random
5+
import time
6+
from datetime import datetime
7+
from influxdb import InfluxDBClient
8+
9+
# Example data payload
10+
# Tags contain metadata about the measurement - always a string
11+
# Thinking we could have something like below for each measurement
12+
13+
def main():
14+
15+
telemetry = [
16+
{
17+
"measurement": "temperature",
18+
"tags": {
19+
"host": "server01",
20+
"region": "us-west"
21+
},
22+
#"time": f"{datetime.now()}",
23+
"fields": {
24+
"Celsius": 1.0,
25+
"Fahrenheit": 3,
26+
}
27+
}
28+
]
29+
30+
# Create client instance
31+
# host, port, user, pass, database
32+
client = InfluxDBClient('localhost', 8086, 'root', 'root', 'example')
33+
34+
# Create a new database in influx
35+
client.drop_database('example')
36+
client.create_database('example')
37+
38+
while 1:
39+
40+
print(f"C: {telemetry[0]['fields']['Celsius']}")
41+
print(f"F: {telemetry[0]['fields']['Fahrenheit']}")
42+
print()
43+
44+
# Writing the payload to the DB
45+
client.write_points(telemetry)
46+
47+
# Scramble data
48+
telemetry[0]['fields']['Celsius'] = random.random() * 100.0
49+
telemetry[0]['fields']['Fahrenheit'] = random.randint(0,100)
50+
51+
time.sleep(1)
52+
53+
# Print the results of the query
54+
result = client.query('select Celsius,Fahrenheit from temperature;')
55+
print(f"Inserted: {result}")
56+
print()
57+
58+
if __name__ == "__main__":
59+
main()
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Example - Logging to Influx
2+
3+
Example of logging to influx using python, as well as how to setup influxdb and grafana. Drawn from http://richardn.ca/2019/01/04/installing-influxdb-on-windows/
4+
5+
6+
7+
## Installing nssm
8+
nssm is the "non-sucking service manager", it is a valuable tool for creating, configuring, and deploying services on windows. It is simply a .exe that is downloaded and then added to the PATH before it can be used from the CLI (Command line interface).
9+
10+
* Download here: https://nssm.cc/download
11+
12+
* Make a directory in program files called nssm and move the nssm.exe there
13+
* Add C:/Program Files/nssm to your path
14+
* start -> "Edit system environment variables" -> environment variables path -> edit -> new -> path-to-nssm
15+
16+
* In console, enter `nssm` to ensure it is installed correctly
17+
18+
19+
## Setting Up Influxdb
20+
21+
### Windows
22+
Download the V1.7.9 windows binaries here: <https://portal.influxdata.com/downloads/>
23+
24+
Extract the contents of the zip to a folder called `influxdb`, and place that folder in program files (doesnt really matter where - I just used program files)
25+
26+
Create another directory anywhere you want to store the influx data, its recommended you do not place this folder within the same directory as the binaries. I called mine **`influx-data`**
27+
28+
Edit the following fields in the influx.conf file within the unzipped folder
29+
* [meta] - dir: C:/Path to influx-data/meta
30+
* [data] - dir: C:/Path to influx-data/data
31+
* [data] - wal-dir: C:/Path to influx-data/wal
32+
33+
Next, we have to configure influx to run as a service.
34+
In an admin console:
35+
* `nssm install`
36+
* Then configure the following settings with the correct paths (may differ from mine depending on where you put them) :
37+
* Path: c:\Program Files\influxdb\influxd.exe
38+
* Startup Directory: c:\Program Files\influxdb\
39+
* Arguments: -config "C:\Program Files\Iinfluxdb\influxdb.conf"
40+
* Service Name: influxdb
41+
42+
Finally, launch the service
43+
```
44+
nssm start influxdb
45+
```
46+
47+
If all has gone well, go to http://localhost:8086/query\
48+
And you should see something like:
49+
```
50+
{"error":"missing required parameter \"q\""}
51+
```
52+
You can now enter the influx interactive shell by navigating to the influxdb folder containing `influx.exe` and running it (enter `influx` in console or double click the exe)
53+
54+
55+
## Setting Up Grafana
56+
### Windows
57+
This pretty much describes everything you need to know in a concise and visual form, no need to reinvent the wheel.
58+
<https://devconnected.com/how-to-install-grafana-on-windows-8-10/>
59+
60+
Note: It is not necessary to use a custom .ini, the defaults are fine for most use cases! There will be a config file for the production grafana server that we will use, however for personal dev environments the defaults are fine.
61+
62+
After getting grafana running as a service, you can login and import the dashboard by uploading the ` dashboard.json` file in this directory.
63+
64+
65+
66+

0 commit comments

Comments
 (0)