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.

56 lines
1.6 KiB

4 months ago
5 years ago
4 months ago
4 months ago
  1. const urlMetadata = require('url-metadata')
  2. const csv = require('csvtojson')
  3. const log = console.log
  4. const { spawnSync } = require( 'child_process' ),
  5. uptime = spawnSync( 'uptime', [ '-s' ] )
  6. const csvFilePath = 'server-pages.csv'
  7. // const csvFilePath = 'server-pages-test.csv'
  8. // consider webpage to be up if stored title matches fetched title
  9. const checkIfTitleMatches = obj => {
  10. // console.log(obj['url'])
  11. // console.log(urlMetadata(obj['url']))
  12. return urlMetadata(obj['url']).then(metadata => { // success handler
  13. // log(metadata.title, obj['siteTitle'])
  14. if (metadata.title == obj['siteTitle']) {
  15. obj['status'] = 'up'
  16. } else {
  17. obj['status'] = 'down'
  18. }
  19. // return true
  20. },
  21. error => { // failure handler
  22. console.log(error)
  23. obj['status'] = 'down'
  24. // return false
  25. }).catch(function(err) {
  26. console.log(err);
  27. })
  28. }
  29. // update all sites' status in JSON
  30. const updateSiteStatus = json => {
  31. const promiseArray = []
  32. json.forEach(async function (obj) {
  33. if (obj['siteTitle'] != '') {
  34. promiseArray.push(checkIfTitleMatches(obj))
  35. } else{
  36. obj['status'] = 'up'
  37. }
  38. })
  39. return promiseArray
  40. }
  41. csv().fromFile(csvFilePath).then(async function (json) {
  42. // finish updating all sites' status before outputting JSON
  43. Promise.all(updateSiteStatus(json)).then(results => {
  44. const statusJson = {
  45. "sites": json,
  46. "updateTime": Date.now(),
  47. "upSince": `${uptime.stdout.toString()}`.replace(/\n/g, ''),
  48. }
  49. // console.log(JSON.stringify(statusJson))
  50. console.log(JSON.stringify(statusJson, null, 2))
  51. })
  52. })