Skip to content

Commit fb5112e

Browse files
author
Khushil Khatri
committed
initial commit
0 parents  commit fb5112e

File tree

5 files changed

+296
-0
lines changed

5 files changed

+296
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

index.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
const HTTPS = require("https");
2+
const AWS = require("aws-sdk");
3+
4+
AWS.config.update({ region: "us-east-1" }); // <-- Cloud Watch region name
5+
6+
const CLOUD_WATCH = new AWS.CloudWatch({
7+
apiVersion: "2010-08-01",
8+
accessKeyId: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", // <-- access key
9+
secretAccessKey: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" // <-- secret access key
10+
});
11+
12+
const URL = "https://example.com";
13+
const SEARCH_STRING = "UA-2181092-2";
14+
const VERSION = `1.0.0`;
15+
const MetricName = "example_status";
16+
const PageName = "example";
17+
18+
exports.handler = () => {
19+
// const main = () => {
20+
console.log(VERSION);
21+
HTTPS.get(URL, res => {
22+
let str = "";
23+
res.on("data", d => {
24+
str += d.toString();
25+
});
26+
res.on("end", () => {
27+
if (str.toString().indexOf(SEARCH_STRING) > -1) {
28+
console.log("Passed");
29+
let params = {
30+
MetricData: [
31+
{
32+
MetricName: MetricName,
33+
Dimensions: [
34+
{
35+
Name: "PAGE_NAME",
36+
Value: PageName
37+
}
38+
],
39+
Unit: "Count",
40+
Value: 5
41+
}
42+
],
43+
Namespace: "STATUS"
44+
};
45+
CLOUD_WATCH.putMetricData(params, function(err, data) {
46+
if (err) {
47+
console.log("Error", err);
48+
return;
49+
} else {
50+
console.log("Success", JSON.stringify(data));
51+
return;
52+
}
53+
});
54+
} else {
55+
console.log("Failed");
56+
let params = {
57+
MetricData: [
58+
{
59+
MetricName: MetricName,
60+
Dimensions: [
61+
{
62+
Name: "PAGE_NAME",
63+
Value: PageName
64+
}
65+
],
66+
Unit: "Count",
67+
Value: 0
68+
}
69+
],
70+
Namespace: "STATUS"
71+
};
72+
CLOUD_WATCH.putMetricData(params, function(err, data) {
73+
if (err) {
74+
console.log("Error", err);
75+
return;
76+
} else {
77+
console.log("Success", JSON.stringify(data));
78+
return;
79+
}
80+
});
81+
}
82+
});
83+
}).on("error", error => {
84+
console.log("Something wrong");
85+
let params = {
86+
MetricData: [
87+
{
88+
MetricName: MetricName,
89+
Dimensions: [
90+
{
91+
Name: "PAGE_NAME",
92+
Value: PageName
93+
}
94+
],
95+
Unit: "Count",
96+
Value: 0
97+
}
98+
],
99+
Namespace: "STATUS"
100+
};
101+
CLOUD_WATCH.putMetricData(params, function(err, data) {
102+
if (err) {
103+
console.log("Error", err);
104+
return;
105+
} else {
106+
console.log("Success", JSON.stringify(data));
107+
return;
108+
}
109+
});
110+
});
111+
};

package-lock.json

Lines changed: 102 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "status-checker",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"build": "zip -r ../status-check.zip ./"
9+
},
10+
"author": "Khushil Khatri",
11+
"license": "ISC",
12+
"dependencies": {
13+
"aws-sdk": "^2.507.0"
14+
}
15+
}

readme.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Webpage status check AWS lambda
2+
3+
Nodejs lambda webpage status checker
4+
5+
## Usage
6+
7+
Change The following things to make it work. (index.js)
8+
9+
```javascript
10+
AWS.config.update({ region: "us-east-1" }); // <-- Cloud Watch region name
11+
12+
const CLOUD_WATCH = new AWS.CloudWatch({
13+
apiVersion: "2010-08-01",
14+
accessKeyId: "xxxxxxxxxxxxxxxxx", // <-- access key
15+
secretAccessKey: "xxxxxxxxxxxxxxxxxx" // <-- secret access key
16+
});
17+
const URL = "https://example.com"; // <-- Add page URL here for status
18+
const SEARCH_STRING = "Your string for check in body part";
19+
const VERSION = `1.0.0`;
20+
const MetricName = "example_status"; // <-- Metric name
21+
const PageName = "example"; // <-- Page name "not relevant to HTML title"
22+
```
23+
24+
## AWS
25+
26+
Log in with your AWS account and follow the steps.
27+
28+
- Go to the IAM page of AWS.
29+
- Select Users from the sidebar.
30+
- Click on create a user.
31+
- Add username with programmatic access to that user click on next.
32+
- Now attach policy `CloudWatchFullAccess` and click next.
33+
- Add `accessKeyId`, `secretAccessKey`, into a `index.js` file.
34+
35+
Or you can use roles instead of creating the user then you can comment `accessKeyId` and `secretAccessKey`.
36+
37+
## Installation
38+
39+
`npm i` install packages.
40+
41+
`npm run build` this will create a zip file that you can upload on lambda.
42+
43+
### Create a lambda function and configs
44+
45+
- Go to the lambda page of AWS.
46+
- Create a new function.
47+
- Add your zip file on the lambda function.
48+
- Make the role of cloud watch if you have not added User detail in code.
49+
- Make lambda function execution time-limit 1-2 minutes.
50+
51+
### Notifiction config
52+
53+
This function will add a metric in the cloud watch if your webpage is up then you will send count `5` into the cloud watch metric or it will send `0` in metric.
54+
55+
Now you can create a cloud watch alarm on metrics and create SNS alert on email.
56+
57+
[https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-cloudwatch-createalarm.html](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-cloudwatch-createalarm.html)
58+
59+
## Contributing
60+
61+
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
62+
63+
Please make sure to update the tests as appropriate.
64+
65+
## License
66+
67+
[MIT](https://choosealicense.com/licenses/mit/)

0 commit comments

Comments
 (0)