Skip to content

Instantly share code, notes, and snippets.

@mrbarletta
Created June 20, 2024 21:58
Show Gist options
  • Save mrbarletta/fd47942dd3eb9574758bebac55e25927 to your computer and use it in GitHub Desktop.
Save mrbarletta/fd47942dd3eb9574758bebac55e25927 to your computer and use it in GitHub Desktop.
An easy way to get From/To list from the postfix.log mainlog. I wanted a quick way to find in the log if postfix received an email.
import fs from "fs";
import util from "util";
util.inspect.defaultOptions={depth :null, maxArrayLength: null};
function parseLogSection(section) {
const lines = section
.split("\n")
.map((line) => line.trim())
.filter((line) => line);
const result = {
date: null,
from: null,
to: [],
connect: null,
};
for (const line of lines) {
// Extract date from the first line
if (!result.date) {
const dateMatch = line.match(/^\d{8}-(.+?)\s/);
if (dateMatch) {
result.date = dateMatch[1].trim();
}
}
// Extract connect
const connectMatch = line.match(/connect from unknown\[(.+?)\]/);
if (connectMatch) {
result.connect = connectMatch[1].trim();
}
// Extract from
const fromMatch = line.match(/from=<([^>]+)>/);
if (fromMatch) {
result.from = fromMatch[1].trim();
}
// Extract all to fields
const toMatch = line.match(/to=<([^>]+)>/g);
if (toMatch) {
toMatch.forEach((toField) => {
const email = toField.match(/to=<([^>]+)>/)[1].trim();
if (!result.to.includes(email)) {
result.to.push(email);
}
});
}
}
return result;
}
function parseToLine(line) {
const regex = /to=<([^>]+)>/;
const match = line.match(regex);
if (match) {
const to = match[1];
return to;
} else {
return null;
}
}
function parseFromLine(line) {
const regex = /from=<([^>]+)>/;
const match = line.match(regex);
if (match) {
const from = match[1];
return from;
} else {
return null;
}
}
function parseAndConvertDate(logLine) {
const dateRegex = /^(\w{3} \d{2} \d{2}:\d{2}:\d{2})/;
const match = logLine.match(dateRegex);
if (match) {
const dateString = match[1];
const currentYear = new Date().getFullYear();
const date = new Date(`${currentYear} ${dateString} GMT`);
// Check if the date parsed correctly
if (!isNaN(date.getTime())) {
return date.toISOString();
} else {
console.error("Invalid date string:", dateString);
return null;
}
} else {
console.error("No date found in the log line.");
return null;
}
}
function parseLogFile(filePath) {
try {
const data = fs.readFileSync(filePath, "utf-8");
const sectionsArray = [];
let sectionItem = { from: "" };
const fileLines = data.split("\n");
for (const line of fileLines) {
if (line.includes("from=<")) {
if (sectionItem.to) {
// console.log(line);
sectionsArray.push(sectionItem);
sectionItem = { from: parseFromLine(line) };
const parsedDate = parseAndConvertDate(line);
sectionItem = { date: parsedDate, from: parseFromLine(line) };
continue;
}
const parsedDate = parseAndConvertDate(line);
sectionItem = { date: parsedDate, from: parseFromLine(line) };
}
if (line.includes("to=<") && !line.includes("postfix/smtp")) {
if (!sectionItem.to) {
sectionItem.to = [];
}
sectionItem.to.push(parseToLine(line));
// console.log(line);
}
}
console.log(sectionsArray);
} catch (error) {
console.error(`Error reading file: ${error.message}`);
}
}
// Example usage
const logFilePath = "postfix.log";
parseLogFile(logFilePath);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment