diff --git a/route/api/index.js b/route/api/index.js index 796f9d9..e621ed0 100644 --- a/route/api/index.js +++ b/route/api/index.js @@ -8,19 +8,19 @@ router .get('/', (req, res) => { res.send({ name: 'WebIO API System' }); }) - .all('*', (req,res,n)=>{ - if('x-auth-token' in req.headers) { + .all('*', (req, res, n) => { + if ('x-auth-token' in req.headers) { let token = req.headers['x-auth-token']; - if(so.chkKey(token)){ + if (so.chkKey(token)) { let obj = so.get(token); - if(obj != null) { + if (obj != null) { so.set(token, obj); } } } n(); }) - .get('/showallso', (req,res)=>{ + .get('/showallso', (req, res) => { res.send(so.show()); }) .use('/system', require('./system.js')) @@ -33,11 +33,12 @@ router .use('/link', require('./link.js')) .use('/modbus', require('./modbus.js')) .use('/ipcam', require('./ipcam.js')) + .use('/server', require('./server.js')) .use('/wristband', require('./wristband.js')); // api error handler router.use((err, req, res, n) => { - if ('db' in res && typeof res.db == 'object' && 'close' in res.db && typeof res.db.close == 'function') res.db.close(); + if ('db' in res && typeof res.db == 'object' && 'close' in res.db && typeof res.db.close == 'function') res.db.close(); if ('db' in res && typeof res.db == 'object' && 'release' in res.db && typeof res.db.release == 'function') res.db.release(); let lngs = req.headers['accept-language'] || ''; @@ -52,7 +53,7 @@ router.use((err, req, res, n) => { status: 0 }; - if('sys_err' in res && process.env.NODE_ENV != 'production') json.sys_err = res.sys_err; + if ('sys_err' in res && process.env.NODE_ENV != 'production') json.sys_err = res.sys_err; res.send(json); }) diff --git a/route/api/server.js b/route/api/server.js index 1cb1d51..a1da4e2 100644 --- a/route/api/server.js +++ b/route/api/server.js @@ -22,11 +22,11 @@ router } n(); }) - .post('/getserverlist', (req, res, n) => { + .post('/getdevicelist', (req, res, n) => { if (!config.permission.server) return n('ERR9000'); let query = "select * from ??.??"; - let param = [config.db.db11, 'jciocservert']; + let param = [config.db.db11, 'dev']; res.db.query(query, param, (err, row) => { if (err) return rt.err(res, err, n, 'ERR8000'); @@ -36,28 +36,326 @@ router n(); }) }) - .post('/addserver', (req, res, n) => { + .post('/getdevice', async(req, res, n) => { + if (!config.permission.server) return n('ERR9000'); + let arr = req.body; + if (!arr.data) return n('ERR0000'); + if (!arr.data.id) return n('ERR0028'); + + let query = "select * from ??.?? where `devuid` = ?"; + let param = [config.db.db11, 'dev', arr.data.id]; + try { + let dev = await tool.promiseQuery(res, query, param); + if (dev.data.length == 0) return n('ERR8000'); + + let ipmi_param = [config.db.db11, 'ipmi', arr.data.id]; + let ipmi = await tool.promiseQuery(res, query, ipmi_param); + + let snmp_param = [config.db.db11, 'snmp', arr.data.id]; + let snmp = await tool.promiseQuery(res, query, snmp_param); + + res.api_res = { + record: tool.checkArray(dev.data), + rt: { + ipmi: tool.checkArray(ipmi.data), + snmp: tool.checkArray(snmp.data) + } + } + + } catch (e) { + return rt.err(res, e, n, 'ERR8000'); + } + + n(); + }) + .post('/adddevice', async(req, res, n) => { if (!config.permission.server) return n('ERR9000'); if (!tool.checkPermission(req)) return n('ERR9000'); let arr = req.body; if (!arr.data) return n('ERR0000'); if (!arr.data.name) return n('ERR0026'); - if (arr.data.iip) { - if (!arr.data.iaccount) return n('ERR0016'); - if (!arr.data.ipassword) return n('ERR0017'); - if (!arr.data.imac) return n('ERR0064'); - if (!arr.data.itype) return n('ERR0009'); - } - if (arr.data.sip) { - if (!arr.data.smac) return n('ERR0064'); - if (!arr.data.stype) return n('ERR0009'); - if (!arr.data.sname) return n('ERR0026'); - if (!arr.data.sver) return n('ERR0065'); - if (arr.data.sver == 'v3' && !arr.data.v3level) return n('ERR0066'); - if (arr.data.v3level) { - } + let u = ''; + let obj = so.get(req.headers['x-auth-token'] || ''); + if (obj != null && 'user' in obj && 'account' in obj.user) u = obj.user.account; + + try { + let query = "insert into ??.?? (`name`, `ctime`, `cuser`, `mtime`, `muser`) values \ + (?, unix_timestamp(), ?, unix_timestamp(), ?)"; + let param = [config.db.db11, 'dev', arr.data.name, u, u]; + + await tool.promiseQuery(res, query, param); + } catch (e) { + return rt.err(res, e, n, 'ERR8001'); } + + res.api_res = { + record: [] + }; + n(); + }) + .post('/editdevice', async(req, res, n) => { + if (!config.permission.server) return n('ERR9000'); + if (!tool.checkPermission(req)) return n('ERR9000'); + let arr = req.body; + if (!arr.data) return n('ERR0000'); + if (!arr.data.id) return n('ERR0028'); + if (!arr.data.name) return n('ERR0026'); + + let u = ''; + let obj = so.get(req.headers['x-auth-token'] || ''); + if (obj != null && 'user' in obj && 'account' in obj.user) u = obj.user.account; + + try { + let query = "update ??.?? set \ + `name` = ?, \ + `muser` = ?, \ + `mtime` = unix_timestamp() \ + where \ + `devuid` = ?"; + let param = [config.db.db11, 'dev', arr.data.name, u, arr.data.id]; + await tool.promiseQuery(res, query, param); + } catch (e) { + return rt.err(res, e, n, 'ERR8002'); + } + + res.api_res = { + record: [] + }; + n(); + }) + .post('/deldevice', async(req, res, n) => { + if (!config.permission.server) return n('ERR9000'); + if (!tool.checkPermission(req)) return n('ERR9000'); + let arr = req.body; + if (!arr.data) return n('ERR0000'); + if (!arr.data.id) return n('ERR0028'); + + try { + let query = "delete from ??.?? where `devuid` = ?"; + let param = [config.db.db11, 'dev', arr.data.id]; + await tool.promiseQuery(res, query, param); + } catch (err) { + return rt.err(res, err, n, 'ERR8003'); + } + + res.api_res = { + record: [] + } + return n(); + }) + .post('/addipmi', async(req, res, n) => { + if (!config.permission.server) return n('ERR9000'); + if (!tool.checkPermission(req)) return n('ERR9000'); + let arr = req.body; + if (!arr.data) return n('ERR0000'); + if (!arr.data.devuid) return n('ERR0028'); + if (!arr.data.account) return n('ERR0016'); + if (!arr.data.password) return n('ERR0017'); + if (!arr.data.ip) return n('ERR0010'); + + try { + let query = "select count(*) as c from ??.?? where `ip` = ?"; + let param = [config.db.db11, 'ipmi', arr.data.ip]; + let c = await tool.promiseQuery(res, query, param); + if (c.data.length > 0 && c.data.c > 0) return n('ERR0027'); + } catch (err) { + return rt.err(res, err, n, 'ERR8000'); + } + + let u = ''; + let obj = so.get(req.headers['x-auth-token'] || ''); + if (obj != null && 'user' in obj && 'account' in obj.user) u = obj.user.account; + + try { + let query = "insert into ??.?? (`devuid`, `ip`, `account`, `password`, `cuser`, `ctime`, `muser`, `mtime`) values \ + (?, ?, ?, ?, ?, unix_timestamp(), ?, unix_timestamp())"; + let param = [config.db.db11, 'ipmi', arr.data.devuid, arr.data.ip, arr.data.account, arr.data.password, u, u]; + await tool.promiseQuery(res, query, param); + } catch (err) { + return rt.err(res, err, n, 'ERR8001'); + } + + res.api_res = { + record: [] + }; + return n(); + }) + .post('/editipmi', async(req, res, n) => { + if (!config.permission.server) return n('ERR9000'); + if (!tool.checkPermission(req)) return n('ERR9000'); + let arr = req.body; + if (!arr.data) return n('ERR0000'); + if (!arr.data.id) return n('ERR0028'); + if (!arr.data.account) return n('ERR0016'); + if (!arr.data.password) return n('ERR0017'); + if (!arr.data.ip) return n('ERR0010'); + + try { + let query = "select count(*) as c from ??.?? where `ipmuid` != ? and `ip` = ?"; + let param = [config.db.db11, 'ipmi', arr.data.id, arr.data.ip]; + let c = await tool.promiseQuery(res, query, param); + if (c.data.length > 0 && c.data.c > 0) return n('ERR0027'); + } catch (err) { + return rt.err(res, err, n, 'ERR8000'); + } + + let u = ''; + let obj = so.get(req.headers['x-auth-token'] || ''); + if (obj != null && 'user' in obj && 'account' in obj.user) u = obj.user.account; + + try { + let query = "update ??.?? set \ + `account` = ?, \ + `password` = ?, \ + `ip` = ?, \ + `muser` = ?, \ + `mtime` = unix_timestamp() \ + where \ + `ipmuid` = ?"; + let param = [config.db.db11, 'ipmi', arr.data.account, arr.data.password, arr.data.ip, u, arr.data.id]; + await tool.promiseQuery(res, query, param); + } catch (err) { + return rt.err(res, err, n, 'ERR8002'); + } + + res.api_res = { + record: [] + }; + return n(); + }) + .post('/delipmi', async(req, res, n) => { + if (!config.permission.server) return n('ERR9000'); + if (!tool.checkPermission(req)) return n('ERR9000'); + let arr = req.body; + if (!arr.data) return n('ERR0000'); + if (!arr.data.id) return n('ERR0028'); + + try { + let query = "delete from ??.?? where `ipmuid` = ?"; + let param = [config.db.db11, 'ipmi', arr.data.id]; + + await tool.promiseQuery(res, query, param); + } catch (err) { + return rt.err(res, err, n, 'ERR8003'); + } + + res.api_res = { + record: [] + }; + return n(); + }) + .post('/getipmiinfo', async(req, res, n) => { + if (!config.permission.server) return n('ERR9000'); + let arr = req.body; + if (!arr.data) return n('ERR0000'); + if (!arr.data.id) return n('ERR0028'); + + res.api_res = { + record: [], + rt: { + log: [], + sensor: [], + bmcgroup: [], + bmcdata: [] + } + }; + + try { + let query = "select * from ??.?? where `ipmuid` = ?"; + let param = [config.db.db11, 'ipmi', arr.data.id]; + let ipmi = await tool.promiseQuery(res, query, param); + if (ipmi.data.length == 0) return n('ERR8000'); + delete ipmi.data[0].password; + res.api_res.record = tool.checkArray(ipmi.data); + + query = "select * from ??.?? order by `guid`"; + param = [config.db.db11, 'bmg']; + let bmg = await tool.promiseQuery(res, query, param); + res.api_res.rt.bmcgroup = tool.checkArray(bmg.data); + + query = "select * from ??.?? where `ipmuid` = ?"; + param = [config.db.db11, 'bmrt', arr.data.id]; + let bmrt = await tool.promiseQuery(res, query, param); + res.api_res.rt.bmcdata = tool.checkArray(bmrt.data); + + query = "select * from ??.?? where `ipmuid` = ?"; + param = [config.db.db11, 'ipmilogrt', arr.data.id]; + let log = await tool.promiseQuery(res, query, param); + res.api_res.rt.log = tool.checkArray(log.data); + + query = "select * from ??.?? where `ipmuid` = ?"; + param = [config.db.db11, 'ipmisenrt', arr.data.id]; + let sensor = await tool.promiseQuery(res, query, param); + res.api_res.rt.sensor = tool.checkArray(sensor.data); + + } catch (err) { + return rt.err(res, err, n, 'ERR8000'); + } + + return n(); + }) + .post('/getbmctl', async(req, res, n) => { + if (!config.permission.server) return n('ERR9000'); + + try { + let query = "select `uid`,`name` from ??.??"; + let param = [config.db.db11, 'bmctl']; + + let ctls = await tool.promiseQuery(res, query, param); + + res.api_res = { + record: tool.checkArray(ctls.data) + } + } catch (err) { + return rt.err(res, err, n, 'ERR8000'); + } + + return n(); + }) + .post('/runbmctl', async(req, res, n) => { + if (!config.permission.server) return n('ERR9000'); + if (!tool.checkPermission(req)) return n('ERR9000'); + let arr = req.body; + if (!arr.data) return n('ERR0000'); + if (!arr.data.ctlid || !arr.data.ipmuid) return n('ERR0028'); + + res.api_res = { + record: [] + }; + + try { + let query = "select * from ??.?? where `uid` = ?"; + let param = [config.db.db11, 'bmctl', arr.data.ctlid]; + let ctl = await tool.promiseQuery(res, query, param); + if (ctl.data.length == 0) return n('ERR8000'); + + query = "select * from ??.?? where `ipmuid` = ?"; + param = [config.db.db11, 'ipmi', arr.data.ipmuid]; + let ipmi = await tool.promiseQuery(res, query, param); + if (ipmi.data.length == 0) return n('ERR8000'); + + let cmd = ctl.data[0].cmd; + cmd = cmd.trim(); + if (!cmd) return n('ERR7000'); + + cmd = cmd.replace(/\@ipmuid/i, ipmi.data[0].ipmuid); + let result = await new Promise((resolve, reject) => { + exec(cmd, (err, stderr, stdout) => { + if (err) return reject(err); + return resolve(stdout || stderr); + }); + }) + + result = result.split(/\n/).filter(e => e).join(''); + + if (result == 0) return n('ERR7000'); + + } catch (err) { + return rt.err(res, err, n, 'ERR8000'); + } + + return n(); }) .all('*', rt.send); diff --git a/src/components/AdminPage/Server/DevList.js b/src/components/AdminPage/Server/DevList.js new file mode 100644 index 0000000..6605993 --- /dev/null +++ b/src/components/AdminPage/Server/DevList.js @@ -0,0 +1,40 @@ +import React from 'react'; +import { Menu, List, Icon } from 'semantic-ui-react'; + + +class DevList extends React.Component { + + + render() { + let {list, sel,selectDevice, openModal, delData} = this.props; + return ( +
+ ) + } +} + +export default DevList; \ No newline at end of file diff --git a/src/components/AdminPage/Server/IPMIPage/SetListItem.js b/src/components/AdminPage/Server/IPMIPage/SetListItem.js new file mode 100644 index 0000000..3057784 --- /dev/null +++ b/src/components/AdminPage/Server/IPMIPage/SetListItem.js @@ -0,0 +1,27 @@ +import React from 'react'; +import {Table, Button} from 'semantic-ui-react'; + +const SetListItem = ({ i18n, data, openModal, delData, bmctl, runbmctl }) => { + + return ( +