-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Khushil Khatri
committed
May 31, 2020
0 parents
commit fb5112e
Showing
5 changed files
with
296 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; | ||
} | ||
}); | ||
}); | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "status-checker", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"build": "zip -r ../status-check.zip ./" | ||
}, | ||
"author": "Khushil Khatri", | ||
"license": "ISC", | ||
"dependencies": { | ||
"aws-sdk": "^2.507.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Webpage status check AWS lambda | ||
|
||
Nodejs lambda webpage status checker | ||
|
||
## Usage | ||
|
||
Change The following things to make it work. (index.js) | ||
|
||
```javascript | ||
AWS.config.update({ region: "us-east-1" }); // <-- Cloud Watch region name | ||
|
||
const CLOUD_WATCH = new AWS.CloudWatch({ | ||
apiVersion: "2010-08-01", | ||
accessKeyId: "xxxxxxxxxxxxxxxxx", // <-- access key | ||
secretAccessKey: "xxxxxxxxxxxxxxxxxx" // <-- secret access key | ||
}); | ||
const URL = "https://example.com"; // <-- Add page URL here for status | ||
const SEARCH_STRING = "Your string for check in body part"; | ||
const VERSION = `1.0.0`; | ||
const MetricName = "example_status"; // <-- Metric name | ||
const PageName = "example"; // <-- Page name "not relevant to HTML title" | ||
``` | ||
|
||
## AWS | ||
|
||
Log in with your AWS account and follow the steps. | ||
|
||
- Go to the IAM page of AWS. | ||
- Select Users from the sidebar. | ||
- Click on create a user. | ||
- Add username with programmatic access to that user click on next. | ||
- Now attach policy `CloudWatchFullAccess` and click next. | ||
- Add `accessKeyId`, `secretAccessKey`, into a `index.js` file. | ||
|
||
Or you can use roles instead of creating the user then you can comment `accessKeyId` and `secretAccessKey`. | ||
|
||
## Installation | ||
|
||
`npm i` install packages. | ||
|
||
`npm run build` this will create a zip file that you can upload on lambda. | ||
|
||
### Create a lambda function and configs | ||
|
||
- Go to the lambda page of AWS. | ||
- Create a new function. | ||
- Add your zip file on the lambda function. | ||
- Make the role of cloud watch if you have not added User detail in code. | ||
- Make lambda function execution time-limit 1-2 minutes. | ||
|
||
### Notifiction config | ||
|
||
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. | ||
|
||
Now you can create a cloud watch alarm on metrics and create SNS alert on email. | ||
|
||
[https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-cloudwatch-createalarm.html](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-cloudwatch-createalarm.html) | ||
|
||
## Contributing | ||
|
||
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. | ||
|
||
Please make sure to update the tests as appropriate. | ||
|
||
## License | ||
|
||
[MIT](https://choosealicense.com/licenses/mit/) |