-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
111 lines (107 loc) · 2.71 KB
/
index.js
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
const HTTPS = require("https");
const AWS = require("aws-sdk");
AWS.config.update({ region: "us-east-1" }); // <-- Cloud Watch region name
const CLOUD_WATCH = new AWS.CloudWatch({
apiVersion: "2010-08-01",
accessKeyId: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", // <-- access key
secretAccessKey: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" // <-- secret access key
});
const URL = "https://example.com";
const SEARCH_STRING = "UA-2181092-2";
const VERSION = `1.0.0`;
const MetricName = "example_status";
const PageName = "example";
exports.handler = () => {
// const main = () => {
console.log(VERSION);
HTTPS.get(URL, res => {
let str = "";
res.on("data", d => {
str += d.toString();
});
res.on("end", () => {
if (str.toString().indexOf(SEARCH_STRING) > -1) {
console.log("Passed");
let params = {
MetricData: [
{
MetricName: MetricName,
Dimensions: [
{
Name: "PAGE_NAME",
Value: PageName
}
],
Unit: "Count",
Value: 5
}
],
Namespace: "STATUS"
};
CLOUD_WATCH.putMetricData(params, function(err, data) {
if (err) {
console.log("Error", err);
return;
} else {
console.log("Success", JSON.stringify(data));
return;
}
});
} else {
console.log("Failed");
let params = {
MetricData: [
{
MetricName: MetricName,
Dimensions: [
{
Name: "PAGE_NAME",
Value: PageName
}
],
Unit: "Count",
Value: 0
}
],
Namespace: "STATUS"
};
CLOUD_WATCH.putMetricData(params, function(err, data) {
if (err) {
console.log("Error", err);
return;
} else {
console.log("Success", JSON.stringify(data));
return;
}
});
}
});
}).on("error", error => {
console.log("Something wrong");
let params = {
MetricData: [
{
MetricName: MetricName,
Dimensions: [
{
Name: "PAGE_NAME",
Value: PageName
}
],
Unit: "Count",
Value: 0
}
],
Namespace: "STATUS"
};
CLOUD_WATCH.putMetricData(params, function(err, data) {
if (err) {
console.log("Error", err);
return;
} else {
console.log("Success", JSON.stringify(data));
return;
}
});
});
};