I need to convert csv file to syslog.
CSV:(From)
"Type","RowID","Name"
"SecurityIncident","ace5c4550c76","MalwareInfection"
"SecurityIncident","ace5c4550c77","MalwareInfection"
Syslog: To
<13>1 2023-09-09T10:19:28.428851-08:00 DESKTOP-TVVB676 - - - [LOG@14506 EventReceivedTime="2020-09-09 10:19:28" SourceModuleName="csv" SourceModuleType="file" Type="SecurityIncident" RowID="ace5c4550c76" Name="MalwareInfection"
<13>1 2023-09-09T10:19:28.428851-08:00 DESKTOP-TVVB676 - - - [LOG@14506 EventReceivedTime="2020-09-09 10:19:28" SourceModuleName="csv" SourceModuleType="file" Type="SecurityIncident" RowID="ace5c4550c77" Name="MalwareInfection"
How to do it in Java? Is there any java library available?
java code for converting CSV to Syslog
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.time.LocalDateTime;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
public class Test {
public static void main(String[] args) {
String csvFile = "file.csv"; // Replace with your CSV file path
try {
BufferedReader reader = new BufferedReader(new FileReader(csvFile));
CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT.withHeader());
for (CSVRecord csvRecord : csvParser) {
String type = csvRecord.get("Type");
String rowID = csvRecord.get("RowID");
String name = csvRecord.get("Name");
String timestamp = LocalDateTime.now().toString();
String hostname = "DESKTOP-TVVB676";
String syslogEntry = String.format("<13>1 %s %s - - - [LOG@14506 EventReceivedTime=\"%s\" " +
"SourceModuleName=\"csv\" SourceModuleType=\"file\" Type=\"%s\" RowID=\"%s\" Name=\"%s\"",
timestamp, hostname, timestamp, type, rowID, name);
System.out.println(syslogEntry);
}
csvParser.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:
<13>1 2023-09-14T19:11:52.057131900 DESKTOP-TVVB676 - - - [LOG@14506 EventReceivedTime="2023-09-14T19:11:52.057131900" SourceModuleName="csv" SourceModuleType="file" Type="SecurityIncident" RowID="ace5c4550c76" Name="MalwareInfection"
<13>1 2023-09-14T19:11:52.068649300 DESKTOP-TVVB676 - - - [LOG@14506 EventReceivedTime="2023-09-14T19:11:52.068649300" SourceModuleName="csv" SourceModuleType="file" Type="SecurityIncident" RowID="ace5c4550c77" Name="MalwareInfection"