Skip to content

Commit

Permalink
Tmp file tracking / cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanblenis committed Feb 11, 2020
1 parent 3b8c6d3 commit e7367d6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
## [Known Issues]
- None. Please feel free to submit an issue via [GitHub](https://github.com/ryanblenis/MeshCentral-EventLog) if you find anything.

## [0.0.22] - 2020-02-10
### Fixed
- Add more reliable tmp file tracking and cleanup

## [0.0.21] - 2020-01-08
### Fixed
- Cleanup console messages for non-windows clients
Expand Down
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
@@ -1,7 1,7 @@
{
"name": "Event Log",
"shortName": "eventlog",
"version": "0.0.21",
"version": "0.0.22",
"author": "Ryan Blenis",
"description": "An event log plugin to view, capture, and store event logs for each endpoint",
"hasAdminPanel": true,
Expand Down
34 changes: 33 additions & 1 deletion modules_meshcore/eventlog.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 56,28 @@ var getlogCallback = function (output) {
}
};

var pushTmpFile = function(fn) {
var fns = getTmpFileNames();
fns.push({ name: fn, time: Math.floor(new Date() / 1000) })
db.Put('pluginEventLog_tmpfns', fns);
dbg('Pushed tmp ' fn)
};
var popTmpFile = function(fn) { // remove tmp file and other (possibly orphaned) files older than 10 sec
var now = Math.floor(new Date() / 1000);
var fns = getTmpFileNames();
var newFns = [];
fns.forEach(function(t) {
dbg('t is ' JSON.stringify(t))
if ( t.name == fn || ((now - t.time) > 10)) {
try { require('fs').unlinkSync(t.name); } catch(e) { }
dbg('popped tmp ' fn)
} else {
newFns.push(t); dbg('pushing ' JSON.stringify(t))
}
});
dbg('tmpfns written ' newFns)
db.Put('pluginEventLog_tmpfns', newFns);
};
var runPwshCollector = function(func, passedParams) {
const defaultParams = {
fromLog: 'Application',
Expand Down Expand Up @@ -96,6 118,7 @@ var runPwshCollector = function(func, passedParams) {
convertToJsonText = " | convertTo-JSON -Compress"
}
var ret = {};
pushTmpFile(fileName);
ret.child = require('child_process').execFile("C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",["-command \"" sinceTimePre "Get-WinEvent -FilterHashTable @{" sinceTimeStr "LogName='" params.fromLog.split(',').join("','") "'; Level=" entryTypeCodes.join(',') "} -MaxEvents " params.num " | Select-Object LogName, Level, TimeCreated, ProviderName, Message, Id " convertToJsonText " | Out-File " fileName " -Encoding UTF8\""]);
ret.child.stdout.str = ''; ret.child.stdout.on('data', function (c) { this.str = c.toString(); });
ret.child.stderr.str = ''; ret.child.stderr.on('data', function (c) { this.str = c.toString(); });
Expand All @@ -112,7 135,7 @@ var runPwshCollector = function(func, passedParams) {
o.stdout = o.stdout.replace(/[^\x20-\x7E]/g, '');
func(o);
}
require('fs').unlinkSync(fileName);
popTmpFile(fileName);
dbg('Running powershell: ' sinceTimePre "Get-WinEvent -FilterHashTable @{" sinceTimeStr "LogName='" params.fromLog.split(',').join("','") "'; Level=" entryTypeCodes.join(',') "} -MaxEvents " params.num " | Select-Object LogName, Level, TimeCreated, ProviderName, Message, Id " convertToJsonText " | Out-File " fileName);
} catch (e) {
dbg('Powershell run error: ' e.stack);
Expand Down Expand Up @@ -288,6 311,15 @@ function getEventLogConfig() {
return cfg;
}

function getTmpFileNames() {
var fns = db.Get('pluginEventLog_tmpfns');
if (fns == '' || fns == null) return [];
try {
fns = JSON.parse(fns);
} catch (e) { return []; }
return fns;
}

function getDefaultConfig() {
return {
id: '',
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,8 1,8 @@

# MeshCentral-EventLog

*Current Version: 0.0.21
Released: 2020-01-08*
*Current Version: 0.0.22
Released: 2020-02-10*

Initially conceived as a proof of concept plugin for the [MeshCentral2](https://github.com/Ylianst/MeshCentral) Project to introduce extensibility into the project without requiring the MeshCentral2 project to incorporate everyone's requested changes into the main project, yet allow it to be accomplished by others. In creating this plugin, we're introducing the appropriate hooks into MeshCentral2 to allow extensibility to anyone who can write a plugin, while trying to modify the core project as little as possible.

Expand Down

0 comments on commit e7367d6

Please sign in to comment.