Add files extracted from SIMMCTIC project

Signed-off-by: Lucas Fernandes de Oliveira's avatarLucas Fernandes de Oliveira <lfoliveira@inf.ufpr.br>
parent 5c998ac4
lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz
pids
logs
results
node_modules
bower_components
build
tmp
npm-debug.log
config.js
app/config.json
sessions.db
reports/out
/*
* Copyright (C) 2012 Centro de Computacao Cientifica e Software Livre
* Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
*
* This file is part of simmc
*
* simmc is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/
var os = require('os');
var cluster = require('cluster');
var bunyan = require('bunyan');
var cuid = require('cuid');
var fs = require('fs');
// Serialize a request (remove unuseful information from requests,
// log only the necessary).
function requestSerializer(req) {
if ((typeof(req) !== 'object') || (req === null)) {
return req;
}
var headers = req.headers || {};
return {
method: req.method,
url: req.url,
headers: { // selective choice of headers (no cookies)
"host": headers["host"],
"connection": headers["connection"],
"cache-control": headers["cache-control"],
"accept": headers["accept"],
"user-agent": headers["user-agent"],
"referer": headers["referer"],
"accept-encoding": headers["accept-encoding"],
"accept-language": headers["accept-language"]
},
remoteAddress: req.ips[0] || req.ip || null
};
}
// Serialize a reponse (remove unuseful information from responses
// and calculate the reponse time).
function responseSerializer(res) {
if ((typeof(res) !== 'object') || (res === null)) {
return res;
}
// Calculate the response time
var responseTime;
if (res.start) {
responseTime = (new Date() - res.start).toString() + " ms";
}
return {
statusCode: res.statusCode,
responseTime: responseTime
}
}
var defaultLogger;
module.exports = function(mode) {
if(mode === "default") {
// Make sure the logs dir exist
try {
fs.mkdirSync('logs');
}
catch (e) {
if ( e.code != 'EEXIST' )
throw e;
}
// Create the default logger
defaultLogger = bunyan.createLogger({
name: 'default',
streams: [{
level: "info",
type: 'file',
path: 'logs/default.log',
},
{
level: "debug",
type: 'file',
path: 'logs/debug.log',
}],
serializers: {
req: requestSerializer,
res: responseSerializer
}
});
if (!cluster.isMaster) {
defaultLogger = defaultLogger.child({ worker: cluster.worker.id });
}
return defaultLogger;
}
else if (mode === "expressAccess") {
// Export the access logger (log express requests)
return function (req, res, next) {
// insert the current time into the response
// (for later calculation of reponse time).
res.start = new Date();
// Insert a log object into the request.
// The routes can use this object to log messages and all
// of them can be traced to the request (by req_id).
req.log = defaultLogger.child({ req_id: cuid.slug() });
// Log the request
req.log.debug({req: req}, 'request');
// Continue to the next handler
next();
// Log the response
req.log.debug({res: res}, 'response');
};
}
else if (mode === "expressError") {
// Export the express error logger (log errors during request processing)
return function (err, req, res, next) {
if (err) {
defaultLogger.error({err: err}, 'Error while processing request');
}
next();
};
}
else {
defaultLogger = bunyan.createLogger({
name: 'stdout',
stream: process.stdout,
serializers: {
req: requestSerializer,
res: responseSerializer
}
});
if (!cluster.isMaster) {
defaultLogger = defaultLogger.child({ worker: cluster.worker.id });
}
return defaultLogger;
}
};
var pg = require('pg'),
copyFrom = require('pg-copy-streams').from,
fs = require('fs'),
_ = require('lodash');
var tokenPattern = /\$[a-zA-Z]([a-zA-Z0-9_]*)\b/g;
function numericFromNamed(sql, parameters) {
var fillableTokens = Object.keys(parameters);
var matchedTokens = _.uniq(_.map(sql.match(tokenPattern), function (token) {
return token.substring(1); // Remove leading dollar sign
}));
var fillTokens = _.intersection(fillableTokens, matchedTokens).sort();
var fillValues = _.map(fillTokens, function (token) {
return parameters[token];
});
var unmatchedTokens = _.difference(matchedTokens, fillableTokens);
if (unmatchedTokens.length) {
var missing = unmatchedTokens.join(", ");
throw new Error("Missing Parameters: " + missing);
}
var interpolatedSql = _.reduce(fillTokens, function (partiallyInterpolated, token, index) {
var replaceAllPattern = new RegExp('\\$' + fillTokens[index] + '\\b', "g");
return partiallyInterpolated.replace(replaceAllPattern, '$' + (index+1));
}, sql);
var out = {};
out.sql = interpolatedSql;
out.values = fillValues;
return out;
}
function namedPatch(client) {
var originalQuery = client.query;
if (originalQuery.patched) return client;
originalQuery = originalQuery.bind(client);
var patchedQuery = function(config, values, callback) {
if (arguments.length === 1) {
return originalQuery(config);
}
else if (arguments.length === 2 && _.isFunction(values)) {
return originalQuery(config, values);
}
else if (_.isArray(values)) {
return originalQuery(config, values, callback);
} else {
var reparameterized = numericFromNamed(config, values);
return originalQuery(reparameterized.sql, reparameterized.values, callback);
}
};
client.query = patchedQuery;
client.query.patched = true;
return client;
}
exports.pool = function (config) {
return function (req, res, next) {
req.db = {
connect: function (callback) {
pg.on('error', function(err) {
return;
});
pg.connect(config, function (err, client, done) {
if (err) {
setTimeout(req.db.connect, 5000, callback);
return;
}
namedPatch(client);
callback(null, new Connection(client), done);
});
}
};
next();
};
};
function Connection(client) {
this.query = function (query, params, callback) {
return client.query(query, params, callback);
};
this.execute = function (query, params) {
var $this = this;
return function (callback) {
$this.query(query, params, function (err) {
callback(err);
});
};
};
this.queryFromFile = function(file, params, callback) {
var queries_dir = __dirname + "/../queries/";
fs.readFile(queries_dir + file, 'utf8', function (err, data) {
if (err) {
return callback(err);
}
if (typeof params === 'undefined') {
params = [];
}
client.query(data, params, callback);
});
};
this.executeFromFile = function(file, params) {
var $this = this;
return function (callback) {
$this.queryFromFile(file, params, function (err) {
callback(err);
});
};
};
this.copyFrom = function (query, callback) {
var stream = client.query(copyFrom(query));
stream.on('end', callback);
stream.on('error', callback);
return stream;
};
this.transaction = function (callback) {
client.query('BEGIN;', function (err) {
callback(err);
});
};
this.commit = function (callback) {
client.query('COMMIT;', function (err) {
callback(err);
});
};
this.rollback = function (callback) {
client.query('ROLLBACK;', function (err) {
callback(err);
});
};
this.auth = function (user, ip, action, comment) {
return function (callback) {
if (!(user && user.id))
return callback('not_logged_in');
client.query('SELECT auth.user_action($1, $2, $3, $4);',
[user.id, ip, action, comment],
function (err) {
callback(err);
});
};
};
};
This diff is collapsed.
{
"name": "proinfo-ws-novo",
"version": "1.0.0",
"description": "Webservice novo para proinfodata",
"homepage": "http://simmc.c3sl.ufpr.br",
"license": "GPLv3",
"private": true,
"scripts": {
"start": "node server.js"
},
"dependencies": {
"async": "^3.1.0",
"body-parser": "^1.19.0",
"bunyan": "^1.8.12",
"cuid": "^2.1.6",
"express": "^4.17.1",
"lodash": "^4.17.15",
"minimist": "^1.2.0",
"pg": "^6.1.0",
"pg-copy-streams": "^2.2.2"
}
}
INSERT INTO inventory
(
contact_date,
id_point,
machine_type,
macaddr,
agent_version,
os_type,
os_distro,
os_kernel,
processor,
memory,
disk1_model,
disk1_size,
disk1_used,
disk2_model,
disk2_size,
disk2_used,
extra_hds,
mirror_timestamp,
amount_users,
board_name,
board_serial,
board_vendor,
board_version,
date_bios,
product_name,
product_serial,
product_version,
sys_vendor
)
VALUES ($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);
INSERT INTO net_usage
(
contact_date,
id_point,
macaddr,
collect_date,
collect_time,
down_bytes,
down_packages,
up_bytes,
up_packages
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9);
CREATE TABLE inventory (
contact_date DATE
, id_point TEXT -- For some reason they want to use INEP as ID (which is a string)
, machine_type INTEGER
, macaddr TEXT
, os_type TEXT
, os_distro TEXT
, os_kernel TEXT
, processor TEXT
, memory INTEGER
, disk1_model TEXT
, disk1_size INTEGER
, disk1_used INTEGER
, disk2_model TEXT
, disk2_size INTEGER
, disk2_used INTEGER
, extra_hds SMALLINT
, mirror_timestamp TIMESTAMP WITHOUT TIME ZONE
, agent_version TEXT
, amount_users INTEGER
, board_name TEXT
, board_serial TEXT
, board_vendor TEXT
, board_version TEXT
, date_bios DATE
, product_name TEXT
, product_serial TEXT
, product_version TEXT
, sys_vendor TEXT
);
CREATE TABLE net_usage (
contact_date DATE
, id_point INTEGER
, macaddr MACADDR
, collect_date DATE
, collect_time TIME WITH TIME ZONE
, down_bytes BIGINT
, down_packages INTEGER
, up_bytes BIGINT
, up_packages INTEGER
, city_code TEXT
, ip INET
);
/*
* Copyright (C) 2012-2019 Centro de Computacao Cientifica e Software Livre
* Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
*
* This file is part of simmc
*
* simmc is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/
var async = require('async');
var exec = require('child_process').exec;
function isSubSet (array1, array2) {
return array1.every(function(item1) {
return array2.some(function(item2) {
return item1 == item2;
});
});
}
function dateFormat (date) {
if (!date) {
return null;
}
return date.getFullYear() + '-' +
("0" + (date.getMonth() + 1)).slice(-2) + '-' +
("0" + date.getDate()).slice(-2);
}
function timeFormat (date) {
if (!date) {
return null;
}
return ("0" + date.getHours()).slice(-2) + ':' +
("0" + date.getMinutes()).slice(-2) + ':' +
("0" + date.getSeconds()).slice(-2);
}
var routes = {
"net_usage": {
validation: function(body) {
if(!body) {
return false;
}
else {
var keys = [
'id_point',
'mac_address',
'bytes_received',
'bytes_transmitted',
'collect_date',
'collect_time',
'packets_received',
'packets_transmitted',
];
return isSubSet(keys, Object.keys(body));
}
},
parser: function(body) {
var time = body.collect_date + " " + body.collect_time;
return [
dateFormat(new Date()),
body.id_point,
body.mac_address,
(body.collect_date)? dateFormat(new Date(body.collect_date)): null,
(body.collect_time)? timeFormat(new Date(time)): null,
body.bytes_received,
body.packets_received,
body.bytes_transmitted,
body.packets_transmitted,
];
},
query: 'collect/net_usage.sql',
field: 'data_net'
},
"inventory": {
validation: function(body) {
if(!body) {
return false;
}
else {
var keys = [
'id_point',
'agent_version',
'amount_users',
'disk1_model',
'disk1_size',
'disk1_used',
'disk2_model',
'disk2_size',
'extra_hds',
'mac_address',
'machine_type',
'memory',
'os_distro',
'os_kernel',
'os_type',
'processor',
'board_name',
'board_serial',
'board_vendor',
'board_version',
'date_bios',
'product_name',
'product_serial',
'product_version',
'sys_vendor',
];
return isSubSet(keys, Object.keys(body));
}
},
parser: function(body) {
return [
dateFormat(new Date()),
body.id_point,
(body.machine_type === "server") ? 1 : 0,
body.mac_address,
body.agent_version,
body.os_type,
body.os_distro,
body.os_kernel,
body.processor,
body.memory,
body.disk1_model,
body.disk1_size,
body.disk1_used,
body.disk2_model,
body.disk2_size,
null,
body.extra_hds,
null,
body.amount_users,
body.board_name,
body.board_serial,
body.board_vendor,
body.board_version,
(body.date_bios) ? dateFormat(new Date(body.date_bios)): null,
body.product_name,
body.product_serial,
body.product_version,
body.sys_vendor,
];
},
query: 'collect/inventory.sql',
field: 'data_inventory'
}
};
module.exports = function() {
return {
collect: function(req, res) {
var route = routes[req.params.type];
if(!route) {
return res.status(400).json({error: 'invalid_parameter'});
}
if(req.body[route.field] && route.validation(req.body[route.field])) {
var data = route.parser(req.body[route.field]);
req.db.connect(function(err, conn, done) {
if (err) {
req.log.error(err);
res.status(500).json({error: 'db_connection_failed'});
return;
}
async.waterfall([
conn.transaction,
function(callback) {
conn.queryFromFile(route.query, data, function(err, result) {
callback(err);
});
},
conn.commit
], function (err, result) {
if(err) {
req.log.error(err);
res.status(500).json({error: 'db_query_failed'});
conn.rollback(done);
return;
}
res.status(200).json({});
done();
});
});
}
else {
return res.status(400).json({error: 'invalid_body'});
}
}
};
};
#!/usr/bin/env node
/*
* Copyright (C) 2012-2019 Centro de Computacao Cientifica e Software Livre
* Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
*
* This file is part of simmc
*
* simmc is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/
//
// Libraries
//
var os = require('os');
var fs = require('fs');
var cluster = require('cluster');
var bodyParser = require('body-parser');
var express = require('express');
var minimist = require('minimist');
var path = require('path');
var exec = require('child_process').execFile;
var http = require('http');
//
// Parse command line arguments
//
var argv = minimist(process.argv.slice(2));
if (argv.help || argv.h) {
console.log('Usage: server.js [OPTION]...');
console.log('');
console.log('Where OPTION can be:');
console.log(' -c, --config alternative configuration file (default: config.js)');
console.log(' -p, --port port where the server will listen to');
console.log(' in insecure connections');