From 1827c0f0fb074baed3940af6518b7eccd9fdda31 Mon Sep 17 00:00:00 2001
From: Rousseau Vincent <vincent.rousseau@irstea.fr>
Date: Wed, 13 Feb 2019 11:20:03 +0100
Subject: [PATCH] Update for multi sensors

---
 Readme.md                                     |   8 +-
 docker-compose.yml                            |   2 +-
 .../dashboards/CPU Usage-1549886184778.json   | 175 ------------------
 .../dashboards/CPU Usage-1549886184778.json   |   4 +-
 grafana/provisioning/dashboards/dashboard.yml |   2 +-
 .../provisioning/datasources/datasource.yml   |   2 +-
 {sensor => sensors/cpu_load}/Dockerfile       |   4 +-
 .../cpu_load/cpu_load.py                      |   7 +-
 {sensor => sensors/cpu_load}/requirements.txt |   0
 9 files changed, 15 insertions(+), 189 deletions(-)
 delete mode 100644 grafana/dashboards/CPU Usage-1549886184778.json
 rename {sensor => sensors/cpu_load}/Dockerfile (65%)
 rename sensor/cpu_influx.py => sensors/cpu_load/cpu_load.py (61%)
 rename {sensor => sensors/cpu_load}/requirements.txt (100%)

diff --git a/Readme.md b/Readme.md
index c7d73a8..e75d7cd 100644
--- a/Readme.md
+++ b/Readme.md
@@ -30,11 +30,11 @@ http://docs.grafana.org/installation/debian/
 
 `git clone`
 
-`cd influxdb/sensor`
+`cd influxdb/sensors/sensor_driver`
 
 `python3 -m pip install -r requirements.txt`
 
-`python3 cpu_influx.py`
+`python3 sensor_driver.py`
 
 # Connect to Grafana
 
@@ -42,5 +42,5 @@ http://docs.grafana.org/installation/debian/
 
 * user : admin
 * pwd : admin
-* Ajouter une source de donnée influxdb avec comme nom de base de donnée "cpu-sense"
-* Ajouter un graphique pour afficher usage CPU dans le temps
\ No newline at end of file
+* Ajouter une source de donnée influxdb avec comme nom de base de donnée "multi-sense"
+* Ajouter un graphique pour afficher les données dans le temps
\ No newline at end of file
diff --git a/docker-compose.yml b/docker-compose.yml
index e5586ed..7ec1aab 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -16,7 +16,7 @@ services:
     env_file:
       - ./grafana/config.monitoring
   sensor:
-    build: ./sensor
+    build: ./sensors/cpu_load
     restart: always
     environment:
       - INFLUX_HOST=influxdb
diff --git a/grafana/dashboards/CPU Usage-1549886184778.json b/grafana/dashboards/CPU Usage-1549886184778.json
deleted file mode 100644
index 82a7677..0000000
--- a/grafana/dashboards/CPU Usage-1549886184778.json	
+++ /dev/null
@@ -1,175 +0,0 @@
-{
-  "annotations": {
-    "list": [
-      {
-        "builtIn": 1,
-        "datasource": "-- Grafana --",
-        "enable": true,
-        "hide": true,
-        "iconColor": "rgba(0, 211, 255, 1)",
-        "name": "Annotations & Alerts",
-        "type": "dashboard"
-      }
-    ]
-  },
-  "editable": true,
-  "gnetId": null,
-  "graphTooltip": 0,
-  "id": 1,
-  "links": [],
-  "panels": [
-    {
-      "aliasColors": {},
-      "bars": false,
-      "dashLength": 10,
-      "dashes": false,
-      "datasource": "Influxdb",
-      "fill": 1,
-      "gridPos": {
-        "h": 15,
-        "w": 18,
-        "x": 0,
-        "y": 0
-      },
-      "id": 2,
-      "legend": {
-        "avg": true,
-        "current": false,
-        "max": true,
-        "min": true,
-        "show": true,
-        "total": false,
-        "values": true
-      },
-      "lines": true,
-      "linewidth": 2,
-      "links": [],
-      "nullPointMode": "null",
-      "percentage": false,
-      "pointradius": 5,
-      "points": false,
-      "renderer": "flot",
-      "seriesOverrides": [],
-      "spaceLength": 10,
-      "stack": false,
-      "steppedLine": false,
-      "targets": [
-        {
-          "groupBy": [
-            {
-              "params": [
-                "10s"
-              ],
-              "type": "time"
-            },
-            {
-              "params": [
-                "null"
-              ],
-              "type": "fill"
-            }
-          ],
-          "measurement": "CPU Usage",
-          "orderByTime": "ASC",
-          "policy": "default",
-          "refId": "A",
-          "resultFormat": "time_series",
-          "select": [
-            [
-              {
-                "params": [
-                  "value"
-                ],
-                "type": "field"
-              },
-              {
-                "params": [],
-                "type": "mean"
-              }
-            ]
-          ],
-          "tags": []
-        }
-      ],
-      "thresholds": [],
-      "timeFrom": null,
-      "timeRegions": [],
-      "timeShift": null,
-      "title": "Panel Title",
-      "tooltip": {
-        "shared": true,
-        "sort": 0,
-        "value_type": "individual"
-      },
-      "type": "graph",
-      "xaxis": {
-        "buckets": null,
-        "mode": "time",
-        "name": null,
-        "show": true,
-        "values": []
-      },
-      "yaxes": [
-        {
-          "format": "short",
-          "label": null,
-          "logBase": 1,
-          "max": null,
-          "min": null,
-          "show": true
-        },
-        {
-          "format": "short",
-          "label": null,
-          "logBase": 1,
-          "max": null,
-          "min": null,
-          "show": true
-        }
-      ],
-      "yaxis": {
-        "align": false,
-        "alignLevel": null
-      }
-    }
-  ],
-  "schemaVersion": 16,
-  "style": "dark",
-  "tags": [],
-  "templating": {
-    "list": []
-  },
-  "time": {
-    "from": "now-6h",
-    "to": "now"
-  },
-  "timepicker": {
-    "refresh_intervals": [
-      "5s",
-      "10s",
-      "30s",
-      "1m",
-      "5m",
-      "15m",
-      "30m",
-      "1h",
-      "2h",
-      "1d"
-    ],
-    "time_options": [
-      "5m",
-      "15m",
-      "1h",
-      "6h",
-      "12h",
-      "24h",
-      "2d",
-      "7d",
-      "30d"
-    ]
-  },
-  "timezone": "",
-  "title": "CPU Usage",
-  "uid": "OwisVIuiz",
-  "version": 3
-}
diff --git a/grafana/provisioning/dashboards/CPU Usage-1549886184778.json b/grafana/provisioning/dashboards/CPU Usage-1549886184778.json
index 82a7677..d25aa77 100644
--- a/grafana/provisioning/dashboards/CPU Usage-1549886184778.json	
+++ b/grafana/provisioning/dashboards/CPU Usage-1549886184778.json	
@@ -69,7 +69,7 @@
               "type": "fill"
             }
           ],
-          "measurement": "CPU Usage",
+          "measurement": "cpu_load",
           "orderByTime": "ASC",
           "policy": "default",
           "refId": "A",
@@ -169,7 +169,7 @@
     ]
   },
   "timezone": "",
-  "title": "CPU Usage",
+  "title": "CPU Load",
   "uid": "OwisVIuiz",
   "version": 3
 }
diff --git a/grafana/provisioning/dashboards/dashboard.yml b/grafana/provisioning/dashboards/dashboard.yml
index 5ab58f4..d70d943 100644
--- a/grafana/provisioning/dashboards/dashboard.yml
+++ b/grafana/provisioning/dashboards/dashboard.yml
@@ -1,6 +1,6 @@
 apiVersion: 1
 providers:
-  - name: 'CPU usage'
+  - name: 'CPU load'
     orgId: 1
     folder: ''
     type: file
diff --git a/grafana/provisioning/datasources/datasource.yml b/grafana/provisioning/datasources/datasource.yml
index 49c544d..f045db6 100644
--- a/grafana/provisioning/datasources/datasource.yml
+++ b/grafana/provisioning/datasources/datasource.yml
@@ -3,7 +3,7 @@ datasources:
   - name: Influxdb
     type: influxdb
     access: proxy
-    database: cpu-sense
+    database: multi-sense
     user: root
     password: root
     url: http://influxdb:8086
diff --git a/sensor/Dockerfile b/sensors/cpu_load/Dockerfile
similarity index 65%
rename from sensor/Dockerfile
rename to sensors/cpu_load/Dockerfile
index 26f60ae..7a9e5cd 100644
--- a/sensor/Dockerfile
+++ b/sensors/cpu_load/Dockerfile
@@ -1,10 +1,10 @@
 FROM python:3
 
 RUN mkdir -p /usr/src/app
-COPY cpu_influx.py /usr/src/app
+COPY cpu_load.py /usr/src/app
 COPY requirements.txt /usr/src/app
 WORKDIR /usr/src/app
 
 RUN python3 -m pip install -r requirements.txt
 
-CMD ["python3", "/usr/src/app/cpu_influx.py"]
\ No newline at end of file
+CMD ["python3", "/usr/src/app/cpu_load.py"]
\ No newline at end of file
diff --git a/sensor/cpu_influx.py b/sensors/cpu_load/cpu_load.py
similarity index 61%
rename from sensor/cpu_influx.py
rename to sensors/cpu_load/cpu_load.py
index 1a1d146..1a1b371 100644
--- a/sensor/cpu_influx.py
+++ b/sensors/cpu_load/cpu_load.py
@@ -4,13 +4,14 @@ import psutil
 from influxdb import InfluxDBClient
 
 influx_host = os.getenv('INFLUX_HOST', 'localhost')
-influx_client = InfluxDBClient(host=influx_host, database='cpu-sense')
-influx_client.create_database('cpu-sense')
+influx_dbname = os.getenv('INFLUX_DBNAME', 'multi-sense')
+influx_client = InfluxDBClient(host=influx_host, database=influx_dbname)
+influx_client.create_database(influx_dbname)
 
 while True:
     measurement = [
         {
-            'measurement': 'CPU Usage',
+            'measurement': 'cpu_load',
             'fields': {
                 'value': float(psutil.cpu_percent(0))
             }
diff --git a/sensor/requirements.txt b/sensors/cpu_load/requirements.txt
similarity index 100%
rename from sensor/requirements.txt
rename to sensors/cpu_load/requirements.txt
-- 
GitLab