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