active-surveillance
v1.1.2
Published
Configurable website monitoring.
Downloads
11
Maintainers
Readme
Active Surveillance: Schedule based website monitoring.
Continously monitor a set of URLs to ensure a website or API is functioning properly by repeatably getting the content and validating that it is correct.
Configs
The configuration for the surveillance is simple:
toWatch
: The only required configuration, this is the an array of objects that represent URL configurations that should be watched.onFailure
: An optional configuration, this is a function that will be run every time a URL under surveillance fails to return a 200 or fails a user defined validation check. By default this will print to console.onSuccess
: An optional configuration, this is a function that will be run every time a URL under surveillance successfully returns a 200 and passes any provided validation check. By default this will print to console.
URL Configs
Each URL configuration provided in toWatch
can pass the following properties:
url
: A required property that is a fully qualified URL (e.g.https://www.mulletify.com
).rateInSeconds
: How often the url should be put under surveillance.name
: An optional property that describes the URL. This is used to report statistics, so any configurations with duplicate names will coalesce their results. By default this is the url.fetchParameters
: An optional property that can be set to an object containing any additional parameters that should be provided to fetchvalidatePage
: An optional property that can be set to a function that accepts the page source and performs any validation of that page.validateJson
: An optional property that can be set to a function that accepts the json returned and performs any validation on that result. If bothvalidatePage
andvalidateJson
are provided than onlyvalidatePage
will be run.
Example Usage
import createActiveSurveillance from 'active-surveillance';
const surveillance = createActiveSurveillance({
toWatch: [{
name: 'Webpage',
url: 'https://www.mulletify.com',
validatePage: pageSource => {
//validate the page text with your favorite assertion library
},
rateInSeconds: 1,
}, {
name: 'API endpoint',
url: 'https://www.mulletify.com/v1/mullets/123',
validateJson: json => {
// validate a pre-parsed json blob with your favorite assertion library
},
rateInSeconds: 1000,
}, {
name: 'POST endpoint',
url: 'https://www.mulletify.com/v1/mullets/',
fetchParameters: {
method: 'POST',
body: { /* your post */ }
}
validateJson: json => {
// validate a pre-parsed json blob with your favorite assertion library
},
rateInSeconds: 1000,
}],
onFailure: f => { console.log(f); },
onSuccess: s => { console.log(s); },
});
// begin monitoring watched urls
surveillance.start();
// get metrics about each url under test
console.log(surveillance.status());
// stop watching urls
surveillance.stop();