From fb5112e1c23f3c64a7a89be4e0b7e9ad889eb8b3 Mon Sep 17 00:00:00 2001 From: Khushil Khatri Date: Sun, 31 May 2020 21:17:18 +0530 Subject: [PATCH] initial commit --- .gitignore | 1 + index.js | 111 ++++++++++++++++++++++++++++++++++++++++++++++ package-lock.json | 102 ++++++++++++++++++++++++++++++++++++++++++ package.json | 15 +++++++ readme.md | 67 ++++++++++++++++++++++++++++ 5 files changed, 296 insertions(+) create mode 100644 .gitignore create mode 100644 index.js create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 readme.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..0813a14 --- /dev/null +++ b/index.js @@ -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; + } + }); + }); +}; diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..4015298 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,102 @@ +{ + "name": "communityamerica", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "aws-sdk": { + "version": "2.507.0", + "resolved": "https://registry.npmjs.org/aws-sdk/-/aws-sdk-2.507.0.tgz", + "integrity": "sha512-sOtaZONTfUx1jh9HzrWMLwoA2cZK2Xn2RAmGV4Y11NM2qhMePOQ501dhAq/ygKMZRffjw23b8mT1rAaDGTn05g==", + "requires": { + "buffer": "4.9.1", + "events": "1.1.1", + "ieee754": "1.1.8", + "jmespath": "0.15.0", + "querystring": "0.2.0", + "sax": "1.2.1", + "url": "0.10.3", + "uuid": "3.3.2", + "xml2js": "0.4.19" + } + }, + "base64-js": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.3.1.tgz", + "integrity": "sha512-mLQ4i2QO1ytvGWFWmcngKO//JXAQueZvwEKtjgQFM4jIK0kU+ytMfplL8j+n5mspOfjHwoAg+9yhb7BwAHm36g==" + }, + "buffer": { + "version": "4.9.1", + "resolved": "https://registry.npmjs.org/buffer/-/buffer-4.9.1.tgz", + "integrity": "sha1-bRu2AbB6TvztlwlBMgkwJ8lbwpg=", + "requires": { + "base64-js": "^1.0.2", + "ieee754": "^1.1.4", + "isarray": "^1.0.0" + } + }, + "events": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/events/-/events-1.1.1.tgz", + "integrity": "sha1-nr23Y1rQmccNzEwqH1AEKI6L2SQ=" + }, + "ieee754": { + "version": "1.1.8", + "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.1.8.tgz", + "integrity": "sha1-vjPUCsEO8ZJnAfbwii2G+/0a0+Q=" + }, + "isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" + }, + "jmespath": { + "version": "0.15.0", + "resolved": "https://registry.npmjs.org/jmespath/-/jmespath-0.15.0.tgz", + "integrity": "sha1-o/Iiqarp+Wb10nx5ZRDigJF2Qhc=" + }, + "punycode": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/punycode/-/punycode-1.3.2.tgz", + "integrity": "sha1-llOgNvt8HuQjQvIyXM7v6jkmxI0=" + }, + "querystring": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/querystring/-/querystring-0.2.0.tgz", + "integrity": "sha1-sgmEkgO7Jd+CDadW50cAWHhSFiA=" + }, + "sax": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/sax/-/sax-1.2.1.tgz", + "integrity": "sha1-e45lYZCyKOgaZq6nSEgNgozS03o=" + }, + "url": { + "version": "0.10.3", + "resolved": "https://registry.npmjs.org/url/-/url-0.10.3.tgz", + "integrity": "sha1-Ah5NnHcF8hu/N9A861h2dAJ3TGQ=", + "requires": { + "punycode": "1.3.2", + "querystring": "0.2.0" + } + }, + "uuid": { + "version": "3.3.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.3.2.tgz", + "integrity": "sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA==" + }, + "xml2js": { + "version": "0.4.19", + "resolved": "https://registry.npmjs.org/xml2js/-/xml2js-0.4.19.tgz", + "integrity": "sha512-esZnJZJOiJR9wWKMyuvSE1y6Dq5LCuJanqhxslH2bxM6duahNZ+HMpCLhBQGZkbX6xRf8x1Y2eJlgt2q3qo49Q==", + "requires": { + "sax": ">=0.6.0", + "xmlbuilder": "~9.0.1" + } + }, + "xmlbuilder": { + "version": "9.0.7", + "resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-9.0.7.tgz", + "integrity": "sha1-Ey7mPS7FVlxVfiD0wi35rKaGsQ0=" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..c640091 --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..b26adcc --- /dev/null +++ b/readme.md @@ -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/)