Add system stats and basic auth to the health endpoint (#105)

* Add system stats and basic auth to the health endpoint
* get all system stats in parallel
diff --git a/package.json b/package.json
index 7eaeff6..e70f4a6 100644
--- a/package.json
+++ b/package.json
@@ -7,7 +7,6 @@
   },
   "dependencies": {
     "body-parser": "^1.12.0",
-    "agentkeepalive": "^2.2.0",
     "express": "^4.12.2",
     "winston": "^2.1.1",
     "moment": "^2.11.1",
@@ -18,6 +17,7 @@
     "http-status-codes": "^1.0.5",
     "request-promise": "^1.0.2",
     "redis":"^2.7.1",
-    "bluebird": "^3.5.0"
+    "bluebird": "^3.5.0",
+    "systeminformation": "^3.19.0"
   }
 }
\ No newline at end of file
diff --git a/provider/app.js b/provider/app.js
index 75a8883..8195183 100644
--- a/provider/app.js
+++ b/provider/app.js
@@ -34,7 +34,7 @@
 var dbPrefix = process.env.DB_PREFIX;
 var databaseName = dbPrefix + constants.TRIGGER_DB_SUFFIX;
 var redisUrl = process.env.REDIS_URL;
-var ddname = '_design/triggers';
+var ddname = '_design/' + constants.DESIGN_DOC_NAME;
 
 // Create the Provider Server
 var server = http.createServer(app);
@@ -148,7 +148,7 @@
             app.get(providerRAS.endPoint, providerRAS.ras);
 
             // Health Endpoint
-            app.get(providerHealth.endPoint, providerHealth.health);
+            app.get(providerHealth.endPoint, providerUtils.authorize, providerHealth.health);
 
             // Activation Endpoint
             app.get(providerActivation.endPoint, providerUtils.authorize, providerActivation.active);
diff --git a/provider/lib/constants.js b/provider/lib/constants.js
index 810ea62..2f2eca1 100644
--- a/provider/lib/constants.js
+++ b/provider/lib/constants.js
@@ -3,11 +3,16 @@
 const RETRY_ATTEMPTS = 12;
 const RETRY_DELAY = 1000; //in milliseconds
 const REDIS_KEY = 'active';
+const DESIGN_DOC_NAME = 'triggers';
+const FILTER_FUNCTION = 'only_triggers_by_worker';
+
 
 module.exports = {
     TRIGGER_DB_SUFFIX: TRIGGER_DB_SUFFIX,
     DEFAULT_MAX_TRIGGERS: DEFAULT_MAX_TRIGGERS,
     RETRY_ATTEMPTS: RETRY_ATTEMPTS,
     RETRY_DELAY: RETRY_DELAY,
-    REDIS_KEY: REDIS_KEY
+    REDIS_KEY: REDIS_KEY,
+    DESIGN_DOC_NAME: DESIGN_DOC_NAME,
+    FILTER_FUNCTION: FILTER_FUNCTION
 };
diff --git a/provider/lib/health.js b/provider/lib/health.js
index 808295d..2c56e2b 100644
--- a/provider/lib/health.js
+++ b/provider/lib/health.js
@@ -1,11 +1,34 @@
-module.exports = function(providerUtils) {
+var si = require('systeminformation');
 
-  // Health Endpoint
-  this.endPoint = '/health';
+module.exports = function(utils) {
 
-  // Health Logic
-  this.health = function (req, res) {
-      res.send({triggerCount: Object.keys(providerUtils.triggers).length});
-  };
+    // Health Endpoint
+    this.endPoint = '/health';
+    var stats = {triggerCount: Object.keys(utils.triggers).length};
+
+    // Health Logic
+    this.health = function (req, res) {
+
+        // get all system stats in parallel
+        Promise.all([
+            si.mem(),
+            si.currentLoad(),
+            si.fsSize(),
+            si.networkStats(),
+            si.inetLatency(utils.routerHost)
+        ])
+        .then(results => {
+            stats.memory = results[0];
+            stats.cpu = results[1];
+            stats.disk = results[2];
+            stats.network = results[3];
+            stats.apiHostLatency = results[4];
+            res.send(stats);
+        })
+        .catch(error => {
+            stats.error = error;
+            res.send(stats);
+        });
+    };
 
 };
diff --git a/provider/lib/utils.js b/provider/lib/utils.js
index 68b9d9c..e184f4d 100644
--- a/provider/lib/utils.js
+++ b/provider/lib/utils.js
@@ -19,10 +19,9 @@
     this.redisHash = triggerDB.config.db + '_' + this.worker;
     this.redisKey = constants.REDIS_KEY;
 
-    this.retryAttempts = constants.RETRY_ATTEMPTS;
-
-    var ddname = 'triggers';
-    var filter = 'only_triggers_by_worker';
+    var retryAttempts = constants.RETRY_ATTEMPTS;
+    var ddname = constants.DESIGN_DOC_NAME;
+    var filter = constants.FILTER_FUNCTION;
 
     var utils = this;
 
@@ -220,7 +219,7 @@
                             reject('Disabled trigger ' + dataTrigger.id + ' due to status code: ' + response.statusCode);
                         }
                         else {
-                            if (retryCount < utils.retryAttempts ) {
+                            if (retryCount < retryAttempts ) {
                                 var timeout = response && response.statusCode === 429 && retryCount === 0 ? 60000 : 1000 * Math.pow(retryCount + 1, 2);
                                 logger.info(method, 'attempting to fire trigger again', dataTrigger.id, 'Retry Count:', (retryCount + 1));
                                 setTimeout(function () {
diff --git a/tests/src/test/scala/system/redundancy/CloudantRedundancyTests.scala b/tests/src/test/scala/system/redundancy/CloudantRedundancyTests.scala
index 6cc69f8..5279d8d 100644
--- a/tests/src/test/scala/system/redundancy/CloudantRedundancyTests.scala
+++ b/tests/src/test/scala/system/redundancy/CloudantRedundancyTests.scala
@@ -28,7 +28,7 @@
 /**
  * These tests verify that a cloudant redundancy (master/slave) configuration
  * works as expected.  They will only run properly in an environment with two
- * alarms containers running concurrently and env var ACTIVE set to true in
+ * cloudant containers running concurrently and env var ACTIVE set to true in
  * one container and false in the other.  This test also assumes that redis and
  * the active endpoint authorization are configured.  For the auth set the
  * ENDPOINT_AUTH env var in your containers to match the testing.auth property