diff --git a/server-pages.service b/server-pages.service new file mode 100644 index 0000000..505987a --- /dev/null +++ b/server-pages.service @@ -0,0 +1,14 @@ +[Unit] +Description=Update server page statuses + +[Service] +Type=oneshot +ExecStart=/usr/bin/sh -c 'node /home/kevin/server-pages/server-status.js > /home/kevin/my-site/public/server-apps.json' +User=kevin +Group=kevin +Environment=PATH=/usr/bin:/usr/local/bin +Environment=NODE_ENV=production +WorkingDirectory=/home/kevin/server-pages + +[Install] +WantedBy=multi-user.target diff --git a/server-pages.timer b/server-pages.timer new file mode 100644 index 0000000..0f2a9db --- /dev/null +++ b/server-pages.timer @@ -0,0 +1,9 @@ +[Unit] +Description=Update server page statuses + +[Timer] +OnCalendar=*:0 +Persistent=true + +[Install] +WantedBy=timers.target diff --git a/server-status.js b/server-status.js index b595983..e862489 100644 --- a/server-status.js +++ b/server-status.js @@ -2,19 +2,17 @@ const urlMetadata = require('url-metadata') const csv = require('csvtojson') const log = console.log const { spawnSync } = require( 'child_process' ), - // uptime = spawnSync( 'uptime', [ '-p' ] ) 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 if (metadata.title == obj['siteTitle']) { - // log(obj['shortUrl']) obj['status'] = 'up' } else { - // console.log(metadata) obj['status'] = 'down' } return true @@ -24,6 +22,7 @@ const checkIfTitleMatches = obj => { }) } +// update all sites' status in JSON const updateSiteStatus = json => { const promiseArray = [] json.forEach(async function (obj) { @@ -37,6 +36,7 @@ const updateSiteStatus = json => { } 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,