Node.js script to check the status of my server pages.
https://kevin-mok.com/server/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.5 KiB
52 lines
1.5 KiB
const urlMetadata = require('url-metadata')
|
|
const csv = require('csvtojson')
|
|
const log = console.log
|
|
const { spawnSync } = require( 'child_process' ),
|
|
uptime = spawnSync( 'uptime', [ '-s' ] )
|
|
|
|
const csvFilePath = 'server-pages.csv'
|
|
// const csvFilePath = 'server-pages-test.csv'
|
|
|
|
// consider webpage to be up if stored title matches fetched title
|
|
const checkIfTitleMatches = obj => {
|
|
return urlMetadata(obj['url']).then(metadata => { // success handler
|
|
// log(metadata.title, obj['siteTitle'])
|
|
if (metadata.title == obj['siteTitle']) {
|
|
obj['status'] = 'up'
|
|
} else {
|
|
obj['status'] = 'down'
|
|
}
|
|
// return true
|
|
},
|
|
error => { // failure handler
|
|
// console.log(error)
|
|
obj['status'] = 'down'
|
|
// return false
|
|
})
|
|
}
|
|
|
|
// update all sites' status in JSON
|
|
const updateSiteStatus = json => {
|
|
const promiseArray = []
|
|
json.forEach(async function (obj) {
|
|
if (obj['siteTitle'] != '') {
|
|
promiseArray.push(checkIfTitleMatches(obj))
|
|
} else{
|
|
obj['status'] = 'up'
|
|
}
|
|
})
|
|
return promiseArray
|
|
}
|
|
|
|
csv().fromFile(csvFilePath).then(async function (json) {
|
|
// finish updating all sites' status before outputting JSON
|
|
Promise.all(updateSiteStatus(json)).then(results => {
|
|
const statusJson = {
|
|
"sites": json,
|
|
"updateTime": Date.now(),
|
|
"upSince": `${uptime.stdout.toString()}`.replace(/\n/g, ''),
|
|
}
|
|
// console.log(JSON.stringify(statusJson))
|
|
console.log(JSON.stringify(statusJson, null, 2))
|
|
})
|
|
})
|