Skip to content

Commit fdcffe9

Browse files
committed
Implemented monitor page with kth-node-monitor
1 parent 0962b78 commit fdcffe9

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ docker-compose*.yml
3535

3636
# test coverage
3737
coverage
38+
yarn.lock

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
"debug": "cross-env NODE_ENV=development concurrently --kill-others \"npm run startDebug\" \"npm run nodeInspector\" \"npm run openDebugBrowser\""
2222
},
2323
"dependencies": {
24+
"bluebird": "^3.4.6",
2425
"body-parser": "^1.15.2",
2526
"co": "^4.6.0",
27+
"component-registry": "^0.2.0",
2628
"cookie-parser": "^1.4.3",
2729
"express": "^4.14.0",
2830
"express-handlebars": "^3.0.0",
@@ -31,10 +33,12 @@
3133
"kth-node-configuration": "KTH/kth-node-configuration.git#v1.0.1",
3234
"kth-node-log": "KTH/kth-node-log.git#v1.0.1",
3335
"kth-node-mongo": "KTH/kth-node-mongo.git#v1.0.3",
36+
"kth-node-monitor": "https://github.com/KTH/kth-node-monitor.git#v0.1.2",
3437
"kth-node-response": "KTH/kth-node-response.git#v1.0.0",
3538
"kth-node-server": "KTH/kth-node-server.git#v3.0.1",
3639
"mongoose": "^4.5.8",
3740
"passport": "^0.3.2",
41+
"safe-utils": "0.1.0",
3842
"swagger-ui": "^2.1.5"
3943
},
4044
"devDependencies": {

server/controllers/systemCtrl.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ const config = require('../init/configuration').full
55
const paths = require('../init/routing/paths')
66
const db = require('kth-node-mongo')
77

8+
const Promise = require('bluebird')
9+
const registry = require('component-registry').globalRegistry
10+
const { IHealthCheck } = require('kth-node-monitor').interfaces
11+
812
/**
913
* System controller for functions such as about and monitor.
1014
* Avoid making changes here in sub-projects.
@@ -45,8 +49,34 @@ function getAbout (req, res) {
4549
* Monitor page
4650
*/
4751
function getMonitor (req, res) {
48-
res.type('text').render('system/monitor', {
49-
dbStatus: db.isOk() ? 'OK' : 'ERROR'
52+
// Check MongoDB
53+
const mongodbHealthUtil = registry.getUtility(IHealthCheck, 'kth-node-mongodb')
54+
const subSystems = [mongodbHealthUtil.status(db, { required: true })]
55+
56+
// If we need local system checks, such as memory or disk, we would add it here.
57+
// Make sure it returns a promise which resolves with an object containing:
58+
// {statusCode: ###, message: '...'}
59+
// The property statusCode should be standard HTTP status codes.
60+
const localSystems = Promise.resolve({ statusCode: 200, message: 'OK' })
61+
62+
/* -- You will normally not change anything below this line -- */
63+
64+
// Determine system health based on the results of the checks above. Expects
65+
// arrays of promises as input. This returns a promise
66+
const systemHealthUtil = registry.getUtility(IHealthCheck, 'kth-node-system-check')
67+
const systemStatus = systemHealthUtil.status(localSystems, subSystems)
68+
69+
systemStatus.then((status) => {
70+
// Return the result either as JSON or text
71+
if (req.headers['accept'] === 'application/json') {
72+
let outp = systemHealthUtil.renderJSON(status)
73+
res.status(status.statusCode).json(outp)
74+
} else {
75+
let outp = systemHealthUtil.renderText(status)
76+
res.type('text').status(status.statusCode).send(outp)
77+
}
78+
}).catch((err) => {
79+
res.type('text').status(500).send(err)
5080
})
5181
}
5282

0 commit comments

Comments
 (0)