Commit 0f3eba15c070bd7457bb433e5edde4b2826d9d04

Authored by Adhidarma Hadiwinoto
1 parent c2cfd84fe4
Exists in master

Log on log

Showing 1 changed file with 2 additions and 0 deletions Inline Diff

lib/apiserver/routers/smstools.js
1 const childProcess = require('child_process'); 1 const childProcess = require('child_process');
2 const express = require('express'); 2 const express = require('express');
3 const escapeQuotes = require('escape-quotes'); 3 const escapeQuotes = require('escape-quotes');
4 4
5 const config = require('komodo-sdk/config'); 5 const config = require('komodo-sdk/config');
6 const logger = require('komodo-sdk/logger');
6 7
7 const smstoolsUtil = require('../../smstools-util'); 8 const smstoolsUtil = require('../../smstools-util');
8 const smstoolsStatus = require('../../smstools-status'); 9 const smstoolsStatus = require('../../smstools-status');
9 const smstoolsStatusParsed = require('../../smstools-status-parsed'); 10 const smstoolsStatusParsed = require('../../smstools-status-parsed');
10 11
11 const router = express.Router(); 12 const router = express.Router();
12 module.exports = router; 13 module.exports = router;
13 14
14 async function pageStatus(req, res) { 15 async function pageStatus(req, res) {
15 const filename = config.smstools_status_file || '//var/log/smsd/smsd_stats/status'; 16 const filename = config.smstools_status_file || '//var/log/smsd/smsd_stats/status';
16 const status = await smstoolsStatusParsed(filename); 17 const status = await smstoolsStatusParsed(filename);
17 res.json(status); 18 res.json(status);
18 } 19 }
19 20
20 async function pageStatusRaw(req, res) { 21 async function pageStatusRaw(req, res) {
21 const filename = config.smstools_status_file || '//var/log/smsd/smsd_stats/status'; 22 const filename = config.smstools_status_file || '//var/log/smsd/smsd_stats/status';
22 const statusContent = await smstoolsStatus(filename); 23 const statusContent = await smstoolsStatus(filename);
23 res.end(statusContent); 24 res.end(statusContent);
24 } 25 }
25 26
26 async function pageRestart(req, res) { 27 async function pageRestart(req, res) {
27 res.json(await smstoolsUtil.restart()); 28 res.json(await smstoolsUtil.restart());
28 } 29 }
29 30
30 function pageLog(req, res) { 31 function pageLog(req, res) {
31 const maxLines = (Number(req.query.max) || 200); 32 const maxLines = (Number(req.query.max) || 200);
32 const keyword = req.query.keyword && req.query.keyword.trim() && escapeQuotes(req.query.keyword.trim(), '\'"&|*<>[];$'); 33 const keyword = req.query.keyword && req.query.keyword.trim() && escapeQuotes(req.query.keyword.trim(), '\'"&|*<>[];$');
33 const cmd = req.query.keyword ? `tail -n ${maxLines * 50} | grep ${keyword} | tail -n ${maxLines} | tac` 34 const cmd = req.query.keyword ? `tail -n ${maxLines * 50} | grep ${keyword} | tail -n ${maxLines} | tac`
34 : `tail -n ${maxLines} /var/log/smsd/smsd.log | tac`; 35 : `tail -n ${maxLines} /var/log/smsd/smsd.log | tac`;
35 36
37 logger.verbose('ROUTER-SMSTOOLS: Getting log', { keyword, maxLines, cmd });
36 childProcess.exec(cmd, (err, stdout, stderr) => { 38 childProcess.exec(cmd, (err, stdout, stderr) => {
37 res.json({ 39 res.json({
38 err, 40 err,
39 stdout, 41 stdout,
40 stderr, 42 stderr,
41 }); 43 });
42 }); 44 });
43 } 45 }
44 46
45 router.get('/status', pageStatus); 47 router.get('/status', pageStatus);
46 router.get('/status/raw', pageStatusRaw); 48 router.get('/status/raw', pageStatusRaw);
47 router.get('/restart', pageRestart); 49 router.get('/restart', pageRestart);
48 router.get('/log', pageLog); 50 router.get('/log', pageLog);
49 51