73 lines
2.7 KiB
JavaScript
73 lines
2.7 KiB
JavaScript
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";
|
|
|
|
console.log(`diskUsageMonitor Copyright (C) 2019 j.tretter@gmail.com
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.`);
|
|
|
|
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();
|
|
disk.check(theDiskLimit.path, function(err, info) {
|
|
if (err) {
|
|
logger.log("error","Error reading disk Information " + err);
|
|
theDiskLimit.error = err;
|
|
} else {
|
|
let availGB=info.available/1024/1024;
|
|
let totalGB=info.total/1024/1024;
|
|
theDiskLimit.availGB=availGB;
|
|
theDiskLimit.totalGB=totalGB;
|
|
}
|
|
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);
|