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.

49 lines
1.4 KiB

  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. return urlMetadata(obj['url']).then(metadata => { // success handler
  11. if (metadata.title == obj['siteTitle']) {
  12. obj['status'] = 'up'
  13. } else {
  14. obj['status'] = 'down'
  15. }
  16. return true
  17. },
  18. error => { // failure handler
  19. console.log(error)
  20. })
  21. }
  22. // update all sites' status in JSON
  23. const updateSiteStatus = json => {
  24. const promiseArray = []
  25. json.forEach(async function (obj) {
  26. if (obj['siteTitle'] != '') {
  27. promiseArray.push(checkIfTitleMatches(obj))
  28. } else{
  29. obj['status'] = 'up'
  30. }
  31. })
  32. return promiseArray
  33. }
  34. csv().fromFile(csvFilePath).then(async function (json) {
  35. // finish updating all sites' status before outputting JSON
  36. Promise.all(updateSiteStatus(json)).then(results => {
  37. const statusJson = {
  38. "sites": json,
  39. "updateTime": Date.now(),
  40. "upSince": `${uptime.stdout.toString()}`.replace(/\n/g, ''),
  41. }
  42. // console.log(JSON.stringify(statusJson))
  43. console.log(JSON.stringify(statusJson, null, 2))
  44. })
  45. })