Building clouddata.dustcloud.org
We discuss what is behind the clouddata.dustcloud.org server, and how exactly the server is built.
- 1 Requirements
- 2 Architecture
- 3 Installation Steps
- 3.1 Ubuntu
- 3.2 influxdb
- 3.2.1 install database
- 3.2.2 create database
- 3.3 grafana
- 3.3.1 install
- 3.3.2 change admin username and password
- 3.3.3 connect to database
- 3.3.4 create dashboard
- 3.3.5 enable anonymous access
- 3.3.6 set as default dashboard
- 3.4 clouddata_server script
- 3.4.1 install Python
- 3.4.2 install clouddata_server script
- 3.4.3 run at startup
- 3.5 NGINX
- 3.5.1 installation
- 3.5.2 configuration
- 3.6 iptables
- 3.6.1 configure
- 3.6.2 verify
- 3.6.3 run at boot
- 4 Testing
Requirements
Goal
clouddata.dustcloud.org allows a customer to publish sensor data to the Internet less than 5 min after having opened a SmartMesh IP starter kit
This translates into the following requirements:
clouddata.dustcloud.org must be able to receive sensor data, and store data as timelines for 12 hours.
clouddata.dustcloud.org must offer a web interface to visualize the data.
Architecture
The solution consists of the PublishToWeb SmartMesh SDK program and the clouddata.dustcloud.org server.
PublishToWeb Smartmesh SDK application
This application is part of the default SmartMesh SDK, and runs on the user's computer. It:
connects to the SmartMesh IP Manager
subscribes to data notifications
forwards all the OAP temperature data readings produced by the SmartMesh IP motes to clouddata.dustcloud.org.
clouddata.dusctloud.org server
The clouddata.dusctloud.org server is built on Ubuntu Server. It has the following internal architecture:
the iptables firewall filters out all external traffic not going to TCP port 80
the NGINX load balancer redirects:
HTTP requests to http://clouddata.dustcloud.org/api/v1 to the
clouddata_serverscript running on TCP port 8080HTTP requests to http://clouddata.dustcloud.org/ to the grafana frontend running on TCP port 3000
the
clouddata_serverscript is part of the SmartMesh SDK. It receives data from the PublishToWeb.py application and inserts it into the database.the influxdb database efficiently stores time series data. In this example, the database is configured to automatically drop data after 12 hours.
the grafana data visualization tools offer a clean dashboard for visualizing the data
Installation Steps
Vagrant setup files for the step-by-step guide below are maintained at https://github.com/dustcloud/clouddata-server.
Ubuntu
At the time of writing, we have installed Ubuntu Server 16.04 LTS.
If you are installing the server on a virtual machine using VirtualBox for testing purposes:
configure two network adapters:
Adapter 1 as "NAT" (this allows your VM to access the web)
Adaptor 2 as "Host-only Adapter" (this allows your host OS to access the web server running your VM)
after booting your VM, edit
/etc/network/interfacesto add the last lines corresponding to the second network interface# This file describes the network interfaces available on your system # and how to activate them. For more information, see interfaces(5). source /etc/network/interfaces.d/* # The loopback network interface auto lo iface lo inet loopback # The primary network interface (NAT) auto enp0s3 iface enp0s3 inet dhcp # The secondary network interface (Host-only adapter) auto enp0s8 iface enp0s8 inet dhcprestart the networking services:
sudo service networking restart
Once installed, log into the server.
Update
sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgradeThe commands above update your entire OS. This can take 10-15min.
SSH
Install the OpenSSH server:
sudo apt-get install openssh-serverAdd your public key to the server:
mkdir .ssh
touch .ssh/authorized_keys
echo "ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIBx8jcskAQwgmw2ZR18K1cyW4NyQDFhLYiva4WLDHyVgxBuo95ndyeYHoc1lk6FpRpV9jdvTCD4rGx8OT28dFyVFvDSiNxbwm/qMHhvY9Vtu7842h0Hkelb5w2DU8Qvp33OQ67frQNKcvnYOP2MQxkfKVRInP/pfzuux0NiErcFxQ== rsa-key-thomas" > .ssh/authorized_keys
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chown $USER:$USER ~/.ssh -REdit /etc/ssh/sshd_config so it contains the following (un-commented) lines:
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile %h/.ssh/authorized_keysRestart the SSH server:
sudo service ssh restartinfluxdb
install database
Install and start InfluxDB automatically:
curl -sL https://repos.influxdata.com/influxdb.key | sudo apt-key add -
source /etc/lsb-release
echo "deb https://repos.influxdata.com/${DISTRIB_ID,,} ${DISTRIB_CODENAME} stable" | sudo tee /etc/apt/sources.list.d/influxdb.list
sudo apt-get update && sudo apt-get install influxdb
sudo systemctl start influxdb
sudo systemctl enable influxdbYou should see the InfluxDB admin panel at http://192.168.56.101:8083/.
By default, the HTTP API of InfluxDB runs on TCP port 8086.
create database
Use the influx client to create a database "grafana" and associate a retention policy so datapoints are automatically deleted after 12 hours:
user@ubuntu:~$ influx
Visit https://enterprise.influxdata.com to register for updates, InfluxDB server management, and monitoring.
Connected to http://localhost:8086 version 0.10.0
InfluxDB shell 0.10.0
> CREATE DATABASE grafana
> CREATE RETENTION POLICY twelve_h_only ON grafana DURATION 12h REPLICATION 1 DEFAULT
> exit
user@ubuntu:~$Recent versions of the influxdb package include the command-line client. If your distribution has an older package, you may need to install the client separately.
sudo apt-get install influxdb-clientgrafana
install
The following commands install grafana, start it, and ensure it also starts on the next boot:
wget https://grafanarel.s3.amazonaws.com/builds/grafana_3.0.4-1464167696_amd64.deb
sudo apt-get install -y adduser libfontconfig
sudo dpkg -i grafana_3.0.4-1464167696_amd64.deb
sudo systemctl daemon-reload
sudo systemctl start grafana-server
systemctl status grafana-server
sudo systemctl enable grafana-server.serviceYou should now be able to see the Grafana front end running at http://192.168.56.101:3000/.
We are not using authentication between Grafana and InfluxDB, but you still have to provide a username/password in the "InfluxDB Details" section below.
change admin username and password
Connect to the Grafana front end with username "admin" and password "admin". Though the profile page, change both the username and password associated with the Grafana administrator.
connect to database
Through the admin interface, add InfluxDB as a data source. From the drop down menu in the upper left, choose Data Sources and click on Add data source.
create dashboard
Through the admin interface, create a new dashboard named Temperature. From the drop down menu in the upper left, select Dashboards → New. The first row is already created, indicated by the vertical line on the left edge. Hovering over that indicator will reveal a menu for editing. Select Add Panel → Graph to create a graph of the temperature data. Under Metrics, select Toggle edit mode under the drop down menu (on the right next to the eye icon) to change the input mode so you can paste in the following query:
SELECT "value" from "temperature" WHERE $timeFilter GROUP BY "mac"To verify everything works, on the Admin interface of InfluxDB (http://192.168.56.101:8083/), select the "grafana" database, click "Write Data" and enter:
temperature,mac=11-11-11-11-11-11-11-11 value=29
temperature,mac=22-22-22-22-22-22-22-22 value=21enable anonymous access
Change the /etc/grafana/grafana.ini file so it contains the following lines:
#################################### Anonymous Auth ##########################
[auth.anonymous]
# enable anonymous access
enabled = trueset as default dashboard
log into Grafana as admin
star the dashboard you create
in the dropdown menu, edit the setting for the default organization, and set the dashboard as default home dashboard