2 # ============LICENSE_START===============================================
3 # Copyright (C) 2020 Nordix Foundation. All rights reserved.
4 # ========================================================================
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 # ============LICENSE_END=================================================
20 // Sim mon server - query the agent and the simulators for counters and other data
21 // Presents a web page on localhost:9999/mon
23 var LOCALHOST="http://127.0.0.1:"
24 var LOCALHOSTSECURE="https://127.0.0.1:"
25 //This var may switch between LOCALHOST and LOCALHOSTSECURE
26 var SIM_LOCALHOST=LOCALHOST
27 var MRSTUB_PORT="3905"
30 var http = require('http');
31 var https = require('https');
33 var express = require('express');
40 app.get("/",function(req, res){
44 //Get parameter valuue from other server
45 function getSimCtr(httpx, url, index, cb) {
51 console.log("URL: "+ url + " - " + httpx)
53 http_type.get(url, (resp) => {
54 // A chunk of data has been recieved.
55 resp.on('data', (chunk) => {
59 // The whole response has been received.
60 resp.on('end', () => {
61 var code=resp.statusCode
62 if (code > 199 && code < 300) {
65 cb("not found", index);
69 }).on("error", (err) => {
70 console.log("Error: " + err.message);
71 cb("no response", index);
74 cb("no response", index);
79 //Format a comma separated list of data to a html-safe string with fixed fieldsizes
80 function formatDataRow(commaList) {
82 var tmp=commaList.split(',');
83 for(var i=0;i<tmp.length;i++) {
85 var len = fieldSize-data.length;
90 str=str+data+" ";
95 //Format a comma separated list of ids to a html-safe string with fixed fieldsizes
96 function formatIdRow(commaList) {
98 var tmp=commaList.split(',');
99 for(var i=0;i<tmp.length;i++) {
100 tmp[i] = tmp[i].trim();
101 var data="<"+tmp[i]+">";
102 var len = fieldSize+4-data.length;
104 data = data+" ";
107 str=str+data+" ";
112 //Format a list of ids to a html-safe string in compact format
113 function formatIdRowCompact(commaList) {
114 if (commaList == undefined) {
118 var tmp=commaList.split(',');
119 for(var i=0;i<tmp.length;i++) {
120 tmp[i] = tmp[i].trim();
121 var data="<"+tmp[i]+">";
122 str=str+data+" ";
127 //Pad a string upto a certain size using a pad string
128 function padding(val, fieldSize, pad) {
130 for(var i=s.length;i<fieldSize;i++) {
136 //Status variables, for parameters values fetched from other simulators
137 var mr1="", mr2="", mr3="", mr4="", mr5="", mr6="";
139 //Status variables for agent
145 //Status variables for callback receiver
151 //Container names and ports of the ric simulator
155 //Status variables for each ric simulator
162 //Counts the number of get request for the html page
165 var refreshInterval=4000
167 //Ignore self signed cert
168 process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;
170 var sim_http_type="http"
172 function fetchAllMetrics() {
175 console.log("Fetching all metics data")
176 if (refreshInterval < 20000) {
180 //Extract the port numbers from the running simulators, for every 3 calls
181 const { exec } = require('child_process');
182 exec('docker ps --filter "name=ricsim" --format "{{.Names}} {{.Ports}}" | sed s/0.0.0.0:// | cut -d \'>\' -f1 | sed \'s/[[-]]*$//\'', (err, stdout, stderr) => {
185 simulators=`${stdout}`.replace(/(\r\n|\n|\r)/gm," ");
186 simulators=simulators.trim();
187 var sims=simulators.split(" ")
190 for(i=0;i<sims.length;i=i+2) {
191 simnames[i/2]=sims[i]
192 simports[i/2]=sims[i+1]
198 //Get metric values from the simulators
199 for(var index=0;index<simnames.length;index++) {
202 // Check is simulator are running on http or https - no response assumes http
203 getSimCtr("https",LOCALHOSTSECURE+simports[index]+"/", index, function(data, index) {
205 console.log("Found https simulator - assuming all simulators using https" )
206 sim_http_type="https"
207 SIM_LOCALHOST=LOCALHOSTSECURE
209 console.log("No https simulator found - assuming all simulators using http" )
211 SIM_LOCALHOST=LOCALHOST
217 console.log("No https simulator found - assuming all simulators using http" )
219 SIM_LOCALHOST=LOCALHOST
221 getSimCtr(sim_http_type, SIM_LOCALHOST+simports[index]+"/counter/num_instances", index, function(data, index) {
222 simvar1[index] = data;
224 getSimCtr(sim_http_type, SIM_LOCALHOST+simports[index]+"/counter/num_types", index, function(data,index) {
225 simvar2[index] = data;
227 getSimCtr(sim_http_type, SIM_LOCALHOST+simports[index]+"/policytypes", index, function(data,index) {
228 data=data.replace(/\[/g,'');
229 data=data.replace(/\]/g,'');
230 data=data.replace(/ /g,'');
231 data=data.replace(/\"/g,'');
232 simvar3[index] = data;
234 getSimCtr(sim_http_type, SIM_LOCALHOST+simports[index]+"/counter/interface", index, function(data,index) {
235 simvar4[index] = data;
237 getSimCtr(sim_http_type, SIM_LOCALHOST+simports[index]+"/counter/remote_hosts", index, function(data,index) {
238 simvar5[index] = data;
242 //MR - get metrics values from the MR stub
243 getSimCtr("http", LOCALHOST+MRSTUB_PORT+"/counter/requests_submitted", 0, function(data, index) {
246 getSimCtr("http", LOCALHOST+MRSTUB_PORT+"/counter/requests_fetched", 0, function(data, index) {
249 getSimCtr("http", LOCALHOST+MRSTUB_PORT+"/counter/current_requests", 0, function(data, index) {
252 getSimCtr("http", LOCALHOST+MRSTUB_PORT+"/counter/responses_submitted", 0, function(data, index) {
255 getSimCtr("http", LOCALHOST+MRSTUB_PORT+"/counter/responses_fetched", 0, function(data, index) {
258 getSimCtr("http", LOCALHOST+MRSTUB_PORT+"/counter/current_responses", 0, function(data, index) {
262 //CR - get metrics values from the callbackreceiver
263 getSimCtr("http", LOCALHOST+CR_PORT+"/counter/received_callbacks", 0, function(data, index) {
266 getSimCtr("http", LOCALHOST+CR_PORT+"/counter/fetched_callbacks", 0, function(data, index) {
269 getSimCtr("http", LOCALHOST+CR_PORT+"/counter/current_messages", 0, function(data, index) {
273 //Agent - get metrics from the agent
274 getSimCtr("http", LOCALHOST+AGENT_PORT+"/status", 0, function(data, index) {
277 getSimCtr("http", LOCALHOST+AGENT_PORT+"/services", 0, function(data, index) {
280 var jd=JSON.parse(data);
282 if (ag2.length > 1) {
285 ag2=ag2+(jd[key]["serviceName"]).trim()
292 getSimCtr("http", LOCALHOST+AGENT_PORT+"/policy_types", 0, function(data, index) {
295 var jd=JSON.parse(data);
297 if (ag3.length > 0) {
300 ag3=ag3+jd[key].trim()
307 getSimCtr("http", LOCALHOST+AGENT_PORT+"/policy_ids", 0, function(data, index) {
310 var jd=JSON.parse(data);
327 console.log("Setting interval "+refreshInterval+"ms")
330 app.get("/mon",function(req, res){
336 var htmlStr = "<!DOCTYPE html>" +
339 "<meta http-equiv=\"refresh\" content=\"2\">"+ //2 sec auto refresh
340 "<title>Policy Agent and simulator monitor</title>"+
343 "<h3>Policy agent</h3>" +
344 "<font face=\"monospace\">" +
345 "Status:..............................." + formatDataRow(ag1) + "<br>" +
346 "Services:............................." + formatIdRowCompact(ag2) + "<br>" +
347 "Types:................................" + formatIdRowCompact(ag3) + "<br>" +
348 "Number of instances:.................." + formatDataRow(ag4) + "<br>" +
350 "<h3>MR Stub interface</h3>" +
351 "<font face=\"monospace\">"+
352 "Submitted requests:............................" + formatDataRow(mr1) + "<br>" +
353 "Fetched requests:.............................." + formatDataRow(mr2) + "<br>" +
354 "Current requests waiting:......................" + formatDataRow(mr3) + "<br>" +
355 "Submitted responses:..........................." + formatDataRow(mr4) + "<br>" +
356 "Fetched responses.............................." + formatDataRow(mr5) + "<br>" +
357 "Current responses waiting......................" + formatDataRow(mr6) + "<br>" +
359 "<h3>Callback receiver</h3>" +
360 "<font face=\"monospace\">" +
361 "Callbacks received:..................." + formatDataRow(cr1) + "<br>" +
362 "Callbacks fetched:...................." + formatDataRow(cr2) + "<br>" +
363 "Number of waiting callback messages:.." + formatDataRow(cr3) + "<br>" +
365 "<h3>Near-RT RIC Simulators</h3>" +
366 "<font face=\"monospace\">"
368 htmlStr=htmlStr+padding("Near-RT RIC Simulator name", 35," ")
369 htmlStr=htmlStr+padding("Types", 10," ")
370 htmlStr=htmlStr+padding("Instances", 10," ")+"<br>"
371 htmlStr=htmlStr+padding("",55,"=")+"<br>"
372 for(var simIndex=0;simIndex<simnames.length;simIndex++) {
373 htmlStr=htmlStr+padding(simnames[simIndex]+ " ("+simports[simIndex]+")",35," ");
374 htmlStr=htmlStr+padding(simvar2[simIndex],10," ")
375 htmlStr=htmlStr+padding(simvar1[simIndex],10," ")
376 htmlStr=htmlStr+"<br>";
379 htmlStr=htmlStr+"<br>";
380 htmlStr=htmlStr+padding("Near-RT RIC Simulator name", 35," ")
381 htmlStr=htmlStr+padding("Version", 20," ")
382 htmlStr=htmlStr+padding("Type-IDs", 10," ")+"<br>"
383 htmlStr=htmlStr+padding("",65,"=")+"<br>"
384 for(simIndex=0;simIndex<simnames.length;simIndex++) {
385 htmlStr=htmlStr+padding(simnames[simIndex]+ " ("+simports[simIndex]+")",35," ");
386 htmlStr=htmlStr+padding(simvar4[simIndex],20," ")
387 htmlStr=htmlStr+padding(formatIdRowCompact(simvar3[simIndex]),10," ")
388 htmlStr=htmlStr+"<br>";
391 htmlStr=htmlStr+"<br>";
392 htmlStr=htmlStr+padding("Near-RT RIC Simulator name", 35," ")
393 htmlStr=htmlStr+padding("Remote hosts", 50," ")+"<br>"
394 htmlStr=htmlStr+padding("",90,"=")+"<br>"
395 for(simIndex=0;simIndex<simnames.length;simIndex++) {
396 htmlStr=htmlStr+padding(simnames[simIndex]+ " ("+simports[simIndex]+")",35," ");
397 htmlStr=htmlStr+padding(simvar5[simIndex],50," ")
398 htmlStr=htmlStr+"<br>";
407 var httpServer = http.createServer(app);
409 httpServer.listen(httpPort);
410 console.log("Simulator monitor listening (http) at "+httpPort);
411 console.log("Open the web page on localhost:9999/mon to view the statistics page.")