-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
116 lines (94 loc) · 3.24 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
'use strict';
const PORT = 8080;
const HOST = "0.0.0.0";
const ETCDENDPOINT = `https://${process.env.MACHINEID}.etcd.services.ruhmesmeile.local:2379`;
const PROJECTKEY = process.env.TYPO3_PROJECTKEY;
const STAGE = process.env.TYPO3_STAGE;
const SERVICES = process.env.TYPO3_SERVICES.split(',');
const REQUIRED_SERVICES = process.env.TYPO3_REQUIRED_SERVICES.split(',');
const STATUS = {
'stopped': 0,
'deploying': 1,
'deployed': 2,
'installing': 3,
'installed': 4,
'starting': 5,
'started': 6
}
const express = require('express');
const Prometheus = require('prom-client')
const Etcd = require('node-etcd');
const fs = require('fs');
var options = {
ca: fs.readFileSync('/etc/ssl/etcd/ca.pem'),
cert: fs.readFileSync('/etc/ssl/etcd/calculonc.pem'),
key: fs.readFileSync('/etc/ssl/etcd/calculonc-key.pem')
};
const etcd = new Etcd(ETCDENDPOINT, options);
const typo3CurrentStatus = new Prometheus.Gauge({
name: 'typo3_current_status',
help: 'Current status of TYPO3 system',
labelNames: ['service']
});
const typo3StatusCounter = new Prometheus.Counter({
name: 'typo3_status_counter',
help: 'Counter for number of status changes, timestamped',
labelNames: ['service','status']
});
var getStatusName = function getStatusName (name) {
var retVal;
if (name.indexOf('stopped') > -1) {
retVal = 'stopped';
} else if (name.indexOf('installing') > -1) {
retVal = 'installing';
} else {
retVal = name;
}
return retVal;
};
const app = express();
app.get('/metrics', (req, res) => {
res.set('Content-Type', Prometheus.register.contentType);
res.end(Prometheus.register.metrics());
});
REQUIRED_SERVICES.forEach(function (serviceName) {
etcd.set(`/ruhmesmeile/projects/typo3/${STAGE}/${PROJECTKEY}/status/${serviceName}/current`, 'stopped', {
prevExist: false
}, function (err, res) {
err ? console.log(err) : etcd.set(`/ruhmesmeile/projects/typo3/${STAGE}/${PROJECTKEY}/status/${serviceName}/stopped`, function (err, res) {
if (err) console.log(err);
});
});
});
var watcher;
var watchers = [];
SERVICES.forEach(function (serviceName) {
var handleEtcdResult = function handleEtcdResult (err, currentStatus) {
if (err) {
console.log(err);
} else {
typo3CurrentStatus.labels(serviceName).set(STATUS[getStatusName(currentStatus.node.value)]);
etcd.get(`/ruhmesmeile/projects/typo3/${STAGE}/${PROJECTKEY}/status/${serviceName}/${getStatusName(currentStatus.node.value)}`, function (err, timestamp) {
err ? console.log(err) : typo3StatusCounter.labels(serviceName, getStatusName(currentStatus.node.value)).inc(1);
})
}
};
watcher = etcd.watcher(`/ruhmesmeile/projects/typo3/${STAGE}/${PROJECTKEY}/status/${serviceName}/current`);
watcher.on("change", function (value) { handleEtcdResult(null, value); });
watchers.push(watcher);
etcd.get(`/ruhmesmeile/projects/typo3/${STAGE}/${PROJECTKEY}/status/${serviceName}/current`, handleEtcdResult);
});
app.listen(PORT, HOST);
console.log(`Metrics running on http://${HOST}:${PORT}/metrics`);
process.on('SIGTERM', () => {
server.close((err) => {
watchers.forEach(function (watcher) {
watcher.stop();
});
if (err) {
console.error(err)
process.exit(1)
}
process.exit(0)
})
});