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.

50 lines
1.4 KiB

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