const request = require("request-promise"); const fs = require("fs"); const winston = require("winston"); const disk = require('diskusage'); const logger = winston.createLogger({ level: 'info', format: winston.format.combine( winston.format.timestamp(), winston.format.printf(info => { return `${info.timestamp} ${info.level}: ${info.message}`; }) ), transports: [ new winston.transports.Console(), new winston.transports.File({ filename: './log/diskUsageMonitor.log', level: 'error' }), new winston.transports.File({ filename: './log/Combined.log' }) ] }); const processingType = "diskusage"; logger.log("info", "Starting diskUsageMonitor v0.1 by j.tretter@gmail.com"); const config = JSON.parse(fs.readFileSync("diskUsageMonitor.config.json", "utf8")); logger.log("debug", "Config" + JSON.stringify(config)); function cb(){ config.diskLimits.forEach(theDiskLimit=>{ theMessage={}; theMessage.processingType=processingType; theMessage.sourceTag=config.sourceTag; theMessage.timestamp=new Date(); theDiskLimit.isWarn=false; disk.check(theDiskLimit.path, function(err, info) { if (err) { logger.log("error","Error reading disk Information " + err); theDiskLimit.error = err; theDiskLimit.isWarn = true; } else { let availGB=info.available/1024/1024; let totalGB=info.total/1024/1024; theDiskLimit.availGB=availGB; theDiskLimit.totalGB=totalGB; if (availGB<=theDiskLimit.minFree) { theDiskLimit.isWarn=true; } } theMessage.data=theDiskLimit; logger.log("debug","Sending Message: " + JSON.stringify(theMessage)); request({ method: 'POST', uri: config.serverURI, body: JSON.stringify([theMessage]), json: true }).then(_ => { logger.log("debug","Sent Request to Service"); }).catch(err=>{ logger.log("error","Sending Request to Service: " + err); }); }); }) } cb(); setInterval(cb,config.pollInterval);