Commit 7d16c37376fedf5bac80cd42625d26635625cb4c
1 parent
60ccd8c27c
Exists in
master
api smstools/modems
Showing 1 changed file with 16 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 | const smstoolsConfigData = require('../../smstools-config/config-file'); | ||
11 | 12 | ||
12 | const router = express.Router(); | 13 | const router = express.Router(); |
13 | module.exports = router; | 14 | module.exports = router; |
14 | 15 | ||
15 | async function pageStatus(req, res) { | 16 | async function pageStatus(req, res) { |
16 | const filename = config.smstools_status_file || '//var/log/smsd/smsd_stats/status'; | 17 | const filename = config.smstools_status_file || '//var/log/smsd/smsd_stats/status'; |
17 | const status = await smstoolsStatusParsed(filename); | 18 | const status = await smstoolsStatusParsed(filename); |
18 | res.json(status); | 19 | res.json(status); |
19 | } | 20 | } |
20 | 21 | ||
21 | async function pageStatusRaw(req, res) { | 22 | async function pageStatusRaw(req, res) { |
22 | const filename = config.smstools_status_file || '//var/log/smsd/smsd_stats/status'; | 23 | const filename = config.smstools_status_file || '//var/log/smsd/smsd_stats/status'; |
23 | const statusContent = await smstoolsStatus(filename); | 24 | const statusContent = await smstoolsStatus(filename); |
24 | res.end(statusContent); | 25 | res.end(statusContent); |
25 | } | 26 | } |
26 | 27 | ||
27 | async function pageRestart(req, res) { | 28 | async function pageRestart(req, res) { |
28 | res.json(await smstoolsUtil.restart()); | 29 | res.json(await smstoolsUtil.restart()); |
29 | } | 30 | } |
30 | 31 | ||
32 | async function pageModemList(req, res) { | ||
33 | const modems = []; | ||
34 | // eslint-disable-next-line no-restricted-syntax | ||
35 | for (const [key, value] of Object.entries(smstoolsConfigData.modems || {})) { | ||
36 | modems.push({ | ||
37 | label: `${key}: ${value.device}, INCOMING${value.outgoing ? ' and OUTGOING' : ''}`, | ||
38 | value: key, | ||
39 | data: value, | ||
40 | }); | ||
41 | } | ||
42 | |||
43 | res.json(modems); | ||
44 | } | ||
45 | |||
31 | function pageLog(req, res) { | 46 | function pageLog(req, res) { |
32 | const maxLines = (Number(req.query.max) || 200); | 47 | const maxLines = (Number(req.query.max) || 200); |
33 | const keyword = req.query.keyword && escapeQuotes(req.query.keyword, '\'\n"&|*<>[];$ '); | 48 | 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` | 49 | 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`; | 50 | : `tail -n ${maxLines} /var/log/smsd/smsd.log | tac`; |
36 | 51 | ||
37 | logger.verbose('ROUTER-SMSTOOLS: Getting log', { keyword, maxLines, cmd }); | 52 | logger.verbose('ROUTER-SMSTOOLS: Getting log', { keyword, maxLines, cmd }); |
38 | childProcess.exec(cmd, (err, stdout, stderr) => { | 53 | childProcess.exec(cmd, (err, stdout, stderr) => { |
39 | res.json({ | 54 | res.json({ |
40 | err, | 55 | err, |
41 | stdout, | 56 | stdout, |
42 | stderr, | 57 | stderr, |
43 | }); | 58 | }); |
44 | }); | 59 | }); |
45 | } | 60 | } |
46 | 61 | ||
47 | async function pageSpoolCount(req, res) { | 62 | async function pageSpoolCount(req, res) { |
48 | const checkedCount = await smstoolsUtil.fileCountOnDir('/var/spool/sms/checked'); | 63 | const checkedCount = await smstoolsUtil.fileCountOnDir('/var/spool/sms/checked'); |
49 | const outgoingCount = await smstoolsUtil.fileCountOnDir('/var/spool/sms/outgoing'); | 64 | const outgoingCount = await smstoolsUtil.fileCountOnDir('/var/spool/sms/outgoing'); |
50 | const indosatCount = await smstoolsUtil.fileCountOnDir('/var/spool/sms/queue/indosat'); | 65 | const indosatCount = await smstoolsUtil.fileCountOnDir('/var/spool/sms/queue/indosat'); |
51 | const otherCount = await smstoolsUtil.fileCountOnDir('/var/spool/sms/queue/other'); | 66 | const otherCount = await smstoolsUtil.fileCountOnDir('/var/spool/sms/queue/other'); |
52 | 67 | ||
53 | res.json({ | 68 | res.json({ |
54 | checkedCount, | 69 | checkedCount, |
55 | outgoingCount, | 70 | outgoingCount, |
56 | indosatCount, | 71 | indosatCount, |
57 | otherCount, | 72 | otherCount, |
58 | totalQueueCount: | 73 | totalQueueCount: |
59 | Number(checkedCount) | 74 | Number(checkedCount) |
60 | + Number(outgoingCount) | 75 | + Number(outgoingCount) |
61 | + Number(indosatCount) | 76 | + Number(indosatCount) |
62 | + Number(otherCount), | 77 | + Number(otherCount), |
63 | }); | 78 | }); |
64 | } | 79 | } |
65 | 80 | ||
66 | router.get('/status', pageStatus); | 81 | router.get('/status', pageStatus); |
67 | router.get('/status/raw', pageStatusRaw); | 82 | router.get('/status/raw', pageStatusRaw); |
68 | router.get('/restart', pageRestart); | 83 | router.get('/restart', pageRestart); |
69 | router.get('/log', pageLog); | 84 | router.get('/log', pageLog); |
70 | router.get('/spool-count', pageSpoolCount); | 85 | router.get('/spool-count', pageSpoolCount); |
86 | router.get('/modems', pageModemList); | ||
71 | 87 |