-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathserver.js
215 lines (176 loc) · 5.13 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
* kurento-monitor (c) 2016-2017 Mario Gasparoni Junior
*
* Freely distributed under the MIT license
*/
const config = require('config');
const console_stamp = require('console-stamp');
const kurento = require('kurento-client');
const fs = require('fs');
const file_output = config.get('file_output');
const graph_only = config.get('graph_only');
var logger = null;
var logger_file_writer = null;
if (file_output) {
logger_file_writer = getFileWriter();
logger = new console.Console(logger_file_writer, logger_file_writer);
const logger_options = {
pattern: "dd-mm HH:MM:ss",
label: false,
stdout: logger_file_writer
};
console_stamp(logger, logger_options);
}
if (!graph_only && config.get('log_prefix') !== false) {
const console_stamp_options = {
pattern: config.get('log_prefix') || 'dd-mm HH:MM:ss',
label: false
};
console_stamp(console, console_stamp_options);
}
const spacesNum = config.get('space_width');
const info_interval = config.get('info_interval') || 2000;
const keep_monitoring = config.get('keep_monitoring');
const pipelines_only = config.get('pipelines_only');
process.env.NODE_TLS_REJECT_UNAUTHORIZED =
config.get('kurento.reject_self_signed');
var kurentoClient = null;
function getKurentoClient(callback) {
var wsUri = process.argv[2] || config.get('kurento.server.uri');
kurento(wsUri, function(err, _kurentoClient) {
if (err) {
console.log("Could not find media server at address " + wsUri);
return callback(err);
}
callback(null, _kurentoClient);
});
}
function getPipelinesInfo(server, callback) {
if (!server) {
return callback('error - failed to find server');
}
var _pipelines = {};
server.getPipelines(function(error,pipelines){
if (error) {
return callback(error);
}
if (pipelines && (pipelines.length < 1)) {
return callback(null,_pipelines);
}
var childsCounter = 0;
pipelines.forEach(function(p,index,array){
p.getChilds(function(error,elements){
//add child elements to pipeline
this.childs = elements;
//append pipeline+childs to _pipelines
_pipelines[childsCounter] = this
childsCounter++;
if(childsCounter == array.length) {
//last child got, return
return callback(null,_pipelines);
}
})
})
})
}
function output(data) {
console.log(data);
if (file_output && logger) {
logger.log(data);
}
}
function getInfo(server, callback) {
if (!server) {
return callback('error - failed to find server');
}
server.getInfo(function(error,serverInfo) {
if (error) {
return callback(error);
}
getPipelinesInfo(server, function( error, pipelinesInfo ) {
if (error) {
return callback(error);
}
var pipelinesNumber = Object.keys(pipelinesInfo).length;
if (pipelines_only) {
return callback(pipelinesNumber);
} else {
//add pipeline info to server info
serverInfo.pipelinesNumber = pipelinesNumber;
serverInfo.pipelines = pipelinesInfo;
return callback(JSON.stringify(serverInfo,null,spacesNum));
}
});
})
}
function getGraph(server, callback){
if (!server) {
return callback('error - failed to find server');
}
server.getPipelines(function (error, pipelines) {
if (error) {
return callback('error - failed to get pipelines');
}
if (pipelines.length > 0) {
var pipeline = pipelines[0];
pipeline.getGstreamerDot('SHOW_CAPS_DETAILS', function(error, dotGraph) {
if (error) {
return callback('error - failed to get graph');
}
return callback(dotGraph);
});
} else { return callback('no pipelines'); }
});
}
function exit (code) {
process.exit(code);
}
function getFileWriter() {
var date = new Date();
var year = date.getFullYear();
var day = (date.getDate() < 10) ? '0' + date.getDate() : date.getDate() ;
var month = (date.getMonth() < 10) ?
'0' + (date.getMonth() + 1) : (date.getMonth() + 1);
var hours = (date.getHours() < 10) ? '0' + date.getHours() : date.getHours();
var minutes = (date.getMinutes() < 10) ?
'0' + date.getMinutes() : date.getMinutes();
var dateFormat = ''+ year + day + month + hours + minutes;
return fs.createWriteStream('./kurento-monitor-' +
dateFormat + '.out');
}
function stop (error) {
if (kurentoClient) {
kurentoClient.close();
}
if (file_output && logger_file_writer) {
logger_file_writer.end();
}
exit(0);
}
process.on('SIGINT', stop);
function start () {
getKurentoClient(function(err, _kurentoClient) {
if (err) {
console.log('Failed load kurento client. ' + err);
exit(1);
}
kurentoClient = _kurentoClient;
_kurentoClient.getServerManager(function (error,server) {
if (error) {
console.log(error);
exit(1);
}
var info = graph_only ? getGraph : getInfo ;
info(server, function(data) {
output(data);
if (keep_monitoring) {
setInterval(info, info_interval, server, output);
} else {
stop();
}
});
})
});
}
//start
start();