var express = require('express');//引入组件并且实例化
var app = express();
//引入axios请求
const axios = require('axios')
//定义全局变量用来存放请求来的数据
global.a='';
global.blist={};
global.ctotal={};
global.dcpu={};
global.fsystem={}
global.men={}
//配置的主机地址
global.ip=['192.168.43.9','192.168.43.127']
//获取监视服务器插件信息的函数,通过访问对应的路径获取
function getplug(ip){axios.get('http://'+ip+':61208/api/3/pluginslist')
.then(response => {
global.a=JSON.stringify(response.data);
})
.catch(error => {
console.log(error);
});
}
function getprocesscount(ip){
console.log('地址是'+ip)
axios.get('http://'+ ip +':61208/api/3/processcount')
.then(response => {
global.ctotal=JSON.stringify(response.data);
})
.catch(error => {
console.log(error);
});
}
function getprocesslist(ip){
console.log('地址是'+ip)
axios.get(`http://${ip}:61208/api/3/processlist`)
.then(response => {
global.blist=JSON.stringify(response.data);
})
.catch(error => {
console.log(error);
});
}
function getcpu(ip){
console.log('地址是'+ip)
axios.get('http://'+ ip +':61208/api/3/cpu')
.then(response => {
global.dcpu=JSON.stringify(response.data);
})
.catch(error => {
console.log(error);
});
}
function getsystem(ip){
console.log('地址是'+ip)
axios.get('http://'+ ip +':61208/api/3/system')
.then(response => {
global.fsystem=JSON.stringify(response.data);
})
.catch(error => {
console.log(error);
});
}
function getmen(ip){
console.log('地址是'+ip)
axios.get('http://'+ ip +':61208/api/3/memswap')
.then(response => {
global.men=JSON.stringify(response.data);
})
.catch(error => {
console.log(error);
});
}
//数据库密码登录
app.get('/idp', function (req, res) {
console.log(req.query.itid)
let check =req.query.itid
let pad = req.query.itpad
var mysql = require('mysql');
var connection = mysql.createConnection({
port: 3306,
host : 'localhost',
user : 'root',
password : '123456',
database: 'test'
});
global.id = ''
connection.connect();
var sql = 'select url from websites where name = ? ';
//查
connection.query(sql,[check],function (err, result) {
if(err){
console.log('[SELECT ERROR] - ',err.message);
return;
}
global.id= JSON.parse( JSON.stringify(result));
console.log(global.id[0].url)
if(pad==global.id[0].url) res.send('true');
else res.send('false');
});
connection.end();
})
//当你访问的地址为这台服务器时候获得的数据,比如说服务器IP是192.1.1.3 当你的客户端访问192.1.1.3时候就会返回'Hello GET'
app.get('/', function (req, res) {
console.log("主页 GET 请求");
res.send('Hello GET');
})
//当你访问的地址为这台服务器时候获得的数据,比如说服务器IP是192.1.1.3 当你的客户端访问192.1.1.3/plug时候就会返回'Hello GET'
app.get('/plug', function (req, res) {
console.log("/del_user 响应 DELETE 请求");
// async function getPageUrl() {
// let httpUrl = "http://localhost:61208/api/3/quicklook/cpu"
// global.a = await axios.get(httpUrl)
// }
// getPageUrl()
res.send(global.a);
})
//比如说服务器IP是192.1.1.3 当你的客户端访问192.1.1.3/ipads/时候就会返回'Hello GET'
app.get('/ipads/'+global.ip[0]+'/processcount', function (req, res) {
console.log("/pro 响应 DELETE 请求");
getprocesscount(global.ip[0])
res.send(global.ctotal);
})
app.get('/ipads/'+global.ip[0]+'/processlist', function (req, res) {
console.log("/prolist 响应 DELETE 请求");
getprocesslist(global.ip[0])
res.send(global.blist);
})
app.get('/ipads/'+global.ip[0]+'/cpu', function (req, res) {
console.log("/cpu 响应 DELETE 请求");
getcpu(global.ip[0])
res.send(global.dcpu);
})
app.get('/ipads/'+global.ip[0]+'/system', function (req, res) {
console.log("/system 响应 DELETE 请求");
getsystem(global.ip[0])
res.send(global.fsystem);
})
app.get('/ipads/'+global.ip[0]+'/memswap', function (req, res) {
console.log("/system 响应 DELETE 请求");
getmen(global.ip[0])
res.send(global.men);
})
app.get('/ipads/'+global.ip[1]+'/processcount', function (req, res) {
console.log("/pro 响应 DELETE 请求");
getprocesscount(global.ip[1])
res.send(global.ctotal);
})
app.get('/ipads/'+global.ip[1]+'/processlist', function (req, res) {
console.log("/prolist 响应 DELETE 请求");
getprocesslist(global.ip[1])
res.send(global.blist);
})
app.get('/ipads/'+global.ip[1]+'/cpu', function (req, res) {
console.log("/cpu 响应 DELETE 请求");
getcpu(global.ip[1])
res.send(global.dcpu);
})
app.get('/ipads/'+global.ip[1]+'/system', function (req, res) {
console.log("/system 响应 DELETE 请求");
getsystem(global.ip[1])
res.send(global.fsystem);
})
app.get('/ipads/'+global.ip[1]+'/memswap', function (req, res) {
console.log("/system 响应 DELETE 请求");
getmen(global.ip[1])
res.send(global.men);
})
// /del_user 页面响应
app.get('/ipads', function (req, res) {
console.log("/del_user 响应 DELETE 请求");
// async function getPageUrl() {
// let httpUrl = "http://localhost:61208/api/3/quicklook/cpu"
// global.a = await axios.get(httpUrl)
// }
// getPageUrl()
res.send(global.ip);
})
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log("应用实例,访问地址为 http://%s:%s", host, port)
})