smstools.js
1.69 KB
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
const childProcess = require('child_process');
const express = require('express');
const escapeQuotes = require('escape-quotes');
const config = require('komodo-sdk/config');
const logger = require('komodo-sdk/logger');
const smstoolsUtil = require('../../smstools-util');
const smstoolsStatus = require('../../smstools-status');
const smstoolsStatusParsed = require('../../smstools-status-parsed');
const router = express.Router();
module.exports = router;
async function pageStatus(req, res) {
const filename = config.smstools_status_file || '//var/log/smsd/smsd_stats/status';
const status = await smstoolsStatusParsed(filename);
res.json(status);
}
async function pageStatusRaw(req, res) {
const filename = config.smstools_status_file || '//var/log/smsd/smsd_stats/status';
const statusContent = await smstoolsStatus(filename);
res.end(statusContent);
}
async function pageRestart(req, res) {
res.json(await smstoolsUtil.restart());
}
function pageLog(req, res) {
const maxLines = (Number(req.query.max) || 200);
const keyword = req.query.keyword && req.query.keyword.trim() && escapeQuotes(req.query.keyword.trim(), '\'"&|*<>[];$');
const cmd = req.query.keyword ? `tail -n ${maxLines * 50} | grep ${keyword} | tail -n ${maxLines} | tac`
: `tail -n ${maxLines} /var/log/smsd/smsd.log | tac`;
logger.verbose('ROUTER-SMSTOOLS: Getting log', { keyword, maxLines, cmd });
childProcess.exec(cmd, (err, stdout, stderr) => {
res.json({
err,
stdout,
stderr,
});
});
}
router.get('/status', pageStatus);
router.get('/status/raw', pageStatusRaw);
router.get('/restart', pageRestart);
router.get('/log', pageLog);