Commit 770e57599f94ec7c5622c534f8fae52a7fc3bac3
1 parent
1a0da16079
Exists in
master
spoolCount
Showing 2 changed files with 22 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 | const logger = require('komodo-sdk/logger'); |
7 | 7 | ||
8 | const smstoolsUtil = require('../../smstools-util'); | 8 | const smstoolsUtil = require('../../smstools-util'); |
9 | const smstoolsStatus = require('../../smstools-status'); | 9 | const smstoolsStatus = require('../../smstools-status'); |
10 | const smstoolsStatusParsed = require('../../smstools-status-parsed'); | 10 | const smstoolsStatusParsed = require('../../smstools-status-parsed'); |
11 | 11 | ||
12 | const router = express.Router(); | 12 | const router = express.Router(); |
13 | module.exports = router; | 13 | module.exports = router; |
14 | 14 | ||
15 | async function pageStatus(req, res) { | 15 | async function pageStatus(req, res) { |
16 | 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'; |
17 | const status = await smstoolsStatusParsed(filename); | 17 | const status = await smstoolsStatusParsed(filename); |
18 | res.json(status); | 18 | res.json(status); |
19 | } | 19 | } |
20 | 20 | ||
21 | async function pageStatusRaw(req, res) { | 21 | async function pageStatusRaw(req, res) { |
22 | 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'; |
23 | const statusContent = await smstoolsStatus(filename); | 23 | const statusContent = await smstoolsStatus(filename); |
24 | res.end(statusContent); | 24 | res.end(statusContent); |
25 | } | 25 | } |
26 | 26 | ||
27 | async function pageRestart(req, res) { | 27 | async function pageRestart(req, res) { |
28 | res.json(await smstoolsUtil.restart()); | 28 | res.json(await smstoolsUtil.restart()); |
29 | } | 29 | } |
30 | 30 | ||
31 | function pageLog(req, res) { | 31 | function pageLog(req, res) { |
32 | const maxLines = (Number(req.query.max) || 200); | 32 | const maxLines = (Number(req.query.max) || 200); |
33 | const keyword = req.query.keyword && escapeQuotes(req.query.keyword, '\'\n"&|*<>[];$ '); | 33 | const keyword = req.query.keyword && escapeQuotes(req.query.keyword, '\'\n"&|*<>[];$ '); |
34 | const cmd = req.query.keyword ? `tail -n ${maxLines * 50} /var/log/smsd/smsd.log | grep --ignore-case ${keyword} | tail -n ${maxLines} | tac` | 34 | const cmd = req.query.keyword ? `tail -n ${maxLines * 50} /var/log/smsd/smsd.log | grep --ignore-case ${keyword} | tail -n ${maxLines} | tac` |
35 | : `tail -n ${maxLines} /var/log/smsd/smsd.log | tac`; | 35 | : `tail -n ${maxLines} /var/log/smsd/smsd.log | tac`; |
36 | 36 | ||
37 | logger.verbose('ROUTER-SMSTOOLS: Getting log', { keyword, maxLines, cmd }); | 37 | logger.verbose('ROUTER-SMSTOOLS: Getting log', { keyword, maxLines, cmd }); |
38 | childProcess.exec(cmd, (err, stdout, stderr) => { | 38 | childProcess.exec(cmd, (err, stdout, stderr) => { |
39 | res.json({ | 39 | res.json({ |
40 | err, | 40 | err, |
41 | stdout, | 41 | stdout, |
42 | stderr, | 42 | stderr, |
43 | }); | 43 | }); |
44 | }); | 44 | }); |
45 | } | 45 | } |
46 | 46 | ||
47 | async function pageSpoolCount(req, res) { | ||
48 | const outgoingCount = await smstoolsUtil.fileCountOnDir('/var/spool/sms/outgoing'); | ||
49 | const checkedCount = await smstoolsUtil.fileCountOnDir('/var/spool/sms/checked'); | ||
50 | |||
51 | res.json({ | ||
52 | outgoingCount, | ||
53 | checkedCount, | ||
54 | totalQueueCount: Number(outgoingCount) + Number(checkedCount), | ||
55 | }); | ||
56 | } | ||
57 | |||
47 | router.get('/status', pageStatus); | 58 | router.get('/status', pageStatus); |
48 | router.get('/status/raw', pageStatusRaw); | 59 | router.get('/status/raw', pageStatusRaw); |
49 | router.get('/restart', pageRestart); | 60 | router.get('/restart', pageRestart); |
50 | router.get('/log', pageLog); | 61 | router.get('/log', pageLog); |
62 | router.get('/spool-count', pageSpoolCount); | ||
51 | 63 |
lib/smstools-util.js
1 | const fs = require('fs'); | ||
1 | const childProcess = require('child_process'); | 2 | const childProcess = require('child_process'); |
2 | const config = require('komodo-sdk/config'); | 3 | const config = require('komodo-sdk/config'); |
3 | 4 | ||
4 | exports.restart = () => new Promise((resolve) => { | 5 | exports.restart = () => new Promise((resolve) => { |
5 | const fileToExec = config.restart_smstools_wrapper || '/var/lib/smstools/centers/smstools/bin/restart-smstools'; | 6 | const fileToExec = config.restart_smstools_wrapper || '/var/lib/smstools/centers/smstools/bin/restart-smstools'; |
6 | childProcess.execFile(fileToExec, (err, stdout, stderr) => { | 7 | childProcess.execFile(fileToExec, (err, stdout, stderr) => { |
7 | resolve({ | 8 | resolve({ |
8 | err, | 9 | err, |
9 | stdout, | 10 | stdout, |
10 | stderr, | 11 | stderr, |
11 | }); | 12 | }); |
12 | }); | 13 | }); |
13 | }); | 14 | }); |
15 | |||
16 | exports.fileCountOnDir = async (dirname) => { | ||
17 | try { | ||
18 | const files = fs.promises.readdir(dirname); | ||
19 | return files.filter((item) => item.search(/LOCK$/) >= 0).length; | ||
20 | } catch (e) { | ||
21 | return null; | ||
22 | } | ||
23 | }; | ||
14 | 24 |