forked from CodeSmith-Team-ARKE/ARKE-Hub-Visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
207 lines (186 loc) · 6.61 KB
/
server.js
File metadata and controls
207 lines (186 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const app = express();
function useAccessObject (accessObject){
// https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Config.html
const AWS = require('aws-sdk');
AWS.config.update(accessObject);
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'build')));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept'
);
next();
});
app.use(function(req, res, next) {
req.header('Access-Control-Allow-Origin', '*');
req.header(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept'
);
next();
});
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
// console.log(AWS.config);
const ec2 = new AWS.EC2();
const cloudwatch = new AWS.CloudWatch();
// logs the current S3 buckets
var s3 = new AWS.S3();
// s3.listBuckets({}, function(err, data) {
// if (err) console.log(err, err.stack);
// else {
// var params = {
// StartTime: new Date('Aug 20 2018 00:00 GMT-0400 (EST)'),
// EndTime: new Date(),
// MetricDataQueries: [
// {
// Id: 's3m1',
// MetricStat: {
// Metric: {
// Namespace: 'AWS/S3', // AWS/ EC2 || S3 !--Important--!
// MetricName: 'BytesUploaded', // NetworkIn || NetworkOut || CPUUtilization !--Important--!
// Dimensions: [
// {
// Name: 'BucketName', // Dimension Name !--Important--!
// Value: 'test-bucket-for-node-manipulation' // Dimension Value !--Important--!
// }
// ]
// },
// Period: 3600, // 60 min intervals - period works in 1 second increments
// Stat: 'Average', // Statistic Type
// Unit: 'Bytes' // Unit Type
// }
// }
// ],
// MaxDatapoints: 24
// };
// cloudwatch.getMetricData(params, function(err, data) {
// if (err) console.log(err, err.stack);
// // an error occurred
// else {
// console.log(data);
// // console.log(data.MetricDataResults[0]); // successful response
// // console.log(data.MetricDataResults[0].Values);
// // console.log(data.MetricDataResults[0].Timestamps);
// // return res.json(data.MetricDataResults[0]);
// }
// });
// }
// });
// <==================== This information is requested when the page if first loaded ===================> //
app.get('/EC2info', function(req, res) {
var params = {
Filters: [
{
Name: 'instance-state-name',
Values: ['running']
}
]
};
ec2.describeInstances(params, function(err, data) {
let allEC2Inst = [];
let ec2Instance = {};
if (err) console.log(err, err.stack);
// an error occurred
else {
for (let i = 0; i < data.Reservations.length; i++) {
let tagsArray = data.Reservations[i].Instances[0].Tags;
let dataProps = data.Reservations[i].Instances[0];
var name;
for (let j = 0; j < tagsArray.length; j++) {
if (tagsArray[j].Key === 'Name') {
var name = tagsArray[j].Value;
}
}
ec2Instance = {
Name: name,
InstanceId: dataProps.InstanceId,
InstanceType: dataProps.InstanceType,
LaunchTime: new Date(dataProps.LaunchTime)
};
allEC2Inst.push(ec2Instance);
}
}
// console.log(allEC2Inst);
console.log('EC2 instances queried');
return res.json(allEC2Inst);
});
});
app.post('/metric-data', (req, res, next) => {
/// console.log(req.body);
// Pulls EC2 CPU Utilization Metrics
const { serviceName, metricName, instanceId } = req.body.selectedOptions;
//this is kinda've a hard coded hacky solution
var currUnits;
if (metricName === ('NetworkIn' || 'NetworkOut')) {
currUnits = 'Bytes';
} else if (metricName === 'CPUUtilization') {
currUnits = 'Percent';
}
var params = {
StartTime: new Date('Aug 25 2018 00:00 GMT-0400 (EST)'),
EndTime: new Date(),
MetricDataQueries: [
{
Id: 'm1',
MetricStat: {
Metric: {
Namespace: serviceName, // Service name !--Important--!
MetricName: metricName, // NetworkIn || NetworkOut ||CPUUtilization !--Important--!
Dimensions: [
{
Name: 'InstanceId', // Dimension Name !--Important--!
Value: instanceId // Dimension Value !--Important--!
}
]
},
Period: 3600, // 60 min intervals - period works in 1 second increments
Stat: 'Average', // Statistic Type
Unit: currUnits // Unit Type
}
}
],
MaxDatapoints: 24
};
cloudwatch.getMetricData(params, function(err, data) {
if (err) console.log(err, err.stack);
// an error occurred
else {
// console.log(data.MetricDataResults[0]); // successful response
// console.log(data.MetricDataResults[0].Values);
// console.log(data.MetricDataResults[0].Timestamps);
return res.json(data.MetricDataResults[0]);
}
});
// var parsedbody = JSON.parse(req.body.selectedOptions);
// console.log(req);
// console.log('This is the service Name', req.body.selectedOptions.serviceName);
// return res.json(req.body.selectedOptions);
});
app.listen(process.env.PORT || 8080, (err) => {
if(err) throw err;
console.log('your arke dashboard is now hosted on localhost 8000')
console.log('please open a new terminal and run npm start to render your dash')
console.log(' * * ** ** * * * * * ** * ** *')
console.log(' * * ** * * ** * ** * ** ** * * ** **')
console.log('* * * * * * ** *** *')
console.log(' * * * * * ')
console.log(' * * * * * ')
console.log(' * * * * *')
console.log(' * * * * **')
console.log(' * * * * * *')
console.log(' * * * * * *')
console.log(' *')
console.log('* * * * *')
console.log(' * * * * * *')
});
}
module.exports = useAccessObject;
//process.env.PORT ||