diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..bb1e930 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +* +!dist +!server +!package*.json diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..5f16d88 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM node:carbon + +WORKDIR /usr/src/app + +COPY . . +RUN npm install + +EXPOSE 3333 +CMD ["npm", "run", "server"] diff --git a/README.md b/README.md index 936a4e3..11eb137 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,19 @@ Allows MongoDB to be used as a data source for Grafana by providing a proxy to c * **Grafana** > 3.x.x * **MongoDB** > 3.4.x +## Demo in Docker + +* Bootstraps [Grafana](https://github.com/grafana/grafana-docker) with mongodb proxy plugin +* Launches mongodb proxy service +* Imports preconfigured Dashboards + +Grafana will be available on `3000` port + +``` +git clone https://github.com/JamesOsgood/mongodb-grafana.git +docker-compose up +``` + ## Installation ### Install the Grafana plugin components @@ -47,7 +60,7 @@ Clicking on the title of the graph allows you to see the aggregation query being The query here is ```javascript -db.sensor_value.aggregate ( [ +db.sensor_value.aggregate ( [ { "$match" : { "sensor_type" : "$sensor", "host_name" : "$host", "ts" : { "$gte" : "$from", "$lte" : "$to" } } }, {"$sort" : {"ts" : 1}}, {"$project" : { "name" : "value", "value" : "$sensor_value", "ts" : "$ts", "_id" : 0} } ]) @@ -73,10 +86,10 @@ db.sensor_value.aggregate ( [ Grafana tells the backend server the date range along with the size of the buckets that should be used to calculate points. Therefore it's possible to use the MongoDB aggregation operator [$bucketAuto](https://docs.mongodb.com/manual/reference/operator/aggregation/bucketAuto/) to automatically bucket the data points into display points. To support this the backend provides the `$dateBucketCount` macro so that queries such as the one below can be written ```javascript -db.sensor_value.aggregate( [ +db.sensor_value.aggregate( [ { "$match" : { "sensor_type" : "$sensor", "host_name" : "$host" , "ts" : { "$gte" : "$from", "$lt" : "$to" }}}, { "$bucketAuto" : { "groupBy" : "$ts", - "buckets" : "$dateBucketCount", + "buckets" : "$dateBucketCount", "output" : { "maxValue" : { "$max" : "$sensor_value" } } } }, { "$project" : { "name" : "value", "value" : "$maxValue", "ts" : "$_id.min", "_id" : 0 } } ] ) ``` @@ -91,13 +104,3 @@ The dashboard in `examples\RPI MongoDB Bucket - Atlas.json` shows this. * run `launchctl load mongodb-grafana-proxy` from ~/Library/LaunchAgents This launch ctrl plist runs the node script via forever. To check it's running, use `forever list`. Logs go into /usr/local/var/lib/grafana/plugins/mongodb-grafana/dist/server - - - - - - - - - - diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..57efd67 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,14 @@ +version: '2.2' +services: + graphana: + image: grafana/grafana:latest + ports: + - "3000:3000" + volumes: + - ".:/var/lib/grafana/plugins/grafana-mongodb-datasource" + - "./examples/docker-demo/datasources:/etc/grafana/provisioning/datasources" + - "./examples/docker-demo/dashboards:/etc/grafana/provisioning/dashboards" + - "./examples:/var/lib/grafana/dashboards" + gf-mdb-proxy: + build: . + image: jamesosgood/mongodb-grafana diff --git a/examples/RPI MongoDB - Atlas.json b/examples/RPI MongoDB - Atlas.json index 5a5223a..673cfa1 100644 --- a/examples/RPI MongoDB - Atlas.json +++ b/examples/RPI MongoDB - Atlas.json @@ -1,34 +1,16 @@ { - "__inputs": [ - { - "name": "DS_RPI_- ATLAS", - "label": "RPI - Atlas", - "description": "", - "type": "datasource", - "pluginId": "grafana-mongodb-datasource", - "pluginName": "MongoDB" - }, - { - "name": "DS_RPI_- LOCAL", - "label": "RPI - Local", - "description": "", - "type": "datasource", - "pluginId": "grafana-mongodb-datasource", - "pluginName": "MongoDB" - } - ], "__requires": [ { "type": "grafana", "id": "grafana", "name": "Grafana", - "version": "4.6.3" + "version": "" }, { "type": "datasource", "id": "grafana-mongodb-datasource", "name": "MongoDB", - "version": "1.3.5" + "version": "" }, { "type": "panel", @@ -67,7 +49,7 @@ "bars": false, "dashLength": 10, "dashes": false, - "datasource": "${DS_RPI_- ATLAS}", + "datasource": "DS_RPI_ATLAS", "fill": 1, "id": 1, "legend": { @@ -164,7 +146,7 @@ { "allValue": null, "current": {}, - "datasource": "${DS_RPI_- LOCAL}", + "datasource": "DS_RPI_LOCAL", "hide": 0, "includeAll": false, "label": "", @@ -184,7 +166,7 @@ { "allValue": null, "current": {}, - "datasource": "${DS_RPI_- LOCAL}", + "datasource": "DS_RPI_LOCAL", "hide": 0, "includeAll": false, "label": "sensor", @@ -235,4 +217,4 @@ "timezone": "", "title": "RPI Mongo - Atlas", "version": 4 -} \ No newline at end of file +} diff --git a/examples/RPI MongoDB Bucket - Atlas.json b/examples/RPI MongoDB Bucket - Atlas.json index 4be775f..28844c1 100644 --- a/examples/RPI MongoDB Bucket - Atlas.json +++ b/examples/RPI MongoDB Bucket - Atlas.json @@ -1,22 +1,4 @@ { - "__inputs": [ - { - "name": "DS_RPI_- ATLAS", - "label": "RPI - Atlas", - "description": "", - "type": "datasource", - "pluginId": "grafana-mongodb-datasource", - "pluginName": "MongoDB" - }, - { - "name": "DS_RPI_- LOCAL", - "label": "RPI - Local", - "description": "", - "type": "datasource", - "pluginId": "grafana-mongodb-datasource", - "pluginName": "MongoDB" - } - ], "__requires": [ { "type": "grafana", @@ -67,7 +49,7 @@ "bars": false, "dashLength": 10, "dashes": false, - "datasource": "${DS_RPI_- ATLAS}", + "datasource": "DS_RPI_ATLAS", "fill": 1, "id": 1, "legend": { @@ -168,7 +150,7 @@ { "allValue": null, "current": {}, - "datasource": "${DS_RPI_- LOCAL}", + "datasource": "DS_RPI_LOCAL", "hide": 0, "includeAll": false, "label": "", @@ -188,7 +170,7 @@ { "allValue": null, "current": {}, - "datasource": "${DS_RPI_- LOCAL}", + "datasource": "DS_RPI_LOCAL", "hide": 0, "includeAll": false, "label": "sensor", @@ -239,4 +221,4 @@ "timezone": "", "title": "RPI Mongo Bucket - Atlas", "version": 3 -} \ No newline at end of file +} diff --git a/examples/docker-demo/README.md b/examples/docker-demo/README.md new file mode 100644 index 0000000..bfa19c4 --- /dev/null +++ b/examples/docker-demo/README.md @@ -0,0 +1,6 @@ +Grafana Configuration Files +=========================== + +### Documentation references +* [Datasources](http://docs.grafana.org/administration/provisioning/#datasources) +* [Dashboards ](http://docs.grafana.org/administration/provisioning/#dashboards) diff --git a/examples/docker-demo/dashboards/custom.yaml b/examples/docker-demo/dashboards/custom.yaml new file mode 100644 index 0000000..3a6d23a --- /dev/null +++ b/examples/docker-demo/dashboards/custom.yaml @@ -0,0 +1,10 @@ +apiVersion: 1 + +providers: +- name: 'default' + orgId: 1 + folder: '' + type: file + disableDeletion: false + options: + path: /var/lib/grafana/dashboards diff --git a/examples/docker-demo/datasources/custom.yaml b/examples/docker-demo/datasources/custom.yaml new file mode 100644 index 0000000..535b975 --- /dev/null +++ b/examples/docker-demo/datasources/custom.yaml @@ -0,0 +1,19 @@ +apiVersion: 1 + +datasources: +- name: DS_RPI_LOCAL + type: grafana-mongodb-datasource + access: proxy + url: http://gf-mdb-proxy:3333 + jsonData: + mongodb_db: rpi + mongodb_url: mongodb://rpiread:rpiread@rpi-sensorreadings-shard-00-00-fgscn.mongodb.net:27017,rpi-sensorreadings-shard-00-01-fgscn.mongodb.net:27017,rpi-sensorreadings-shard-00-02-fgscn.mongodb.net:27017/test?ssl=true&replicaSet=RPI-SensorReadings-shard-0&authSource=admin + editable: true +- name: DS_RPI_ATLAS + type: grafana-mongodb-datasource + access: proxy + url: http://gf-mdb-proxy:3333 + jsonData: + mongodb_db: rpi + mongodb_url: mongodb://rpiread:rpiread@rpi-sensorreadings-shard-00-00-fgscn.mongodb.net:27017,rpi-sensorreadings-shard-00-01-fgscn.mongodb.net:27017,rpi-sensorreadings-shard-00-02-fgscn.mongodb.net:27017/test?ssl=true&replicaSet=RPI-SensorReadings-shard-0&authSource=admin + editable: true