I’ve been asked to create a CSV from a log file with clustered output like the example below.
Attempting to 'connect' to endpoint: tcp://localhost:[PORT]
Connected to endpoint: tcp://localhost:[PORT]
Subscribed to topic: "geofence_notify"
[1] Recv event with topic "geofence_notify"
seq: [NUMBER]
timestamp: [UNIX-TIMESTAMP]
op: OP_ADD
topic_seq: [NUMBER]
source_id: [NUMBER]
geofence_notify {
geofence_event: [EVENT]
geofence_id: [NUMBER]
geofence_name: [NAME]
dwell_time: [NUMBER]
hashed_sta_mac: [NUMBER]
}
[2] Recv event with topic "geofence_notify"
seq: [NUMBER]
timestamp: [UNIX-TIMESTAMP]
op: OP_ADD
topic_seq: [NUMBER]
source_id: [NUMBER]
geofence_notify {
geofence_event: [EVENT]
geofence_id: [NUMBER]
geofence_name: [NAME]
dwell_time: [NUMBER]
hashed_sta_mac: [NUMBER]
}
The output I’m looking for would ideally be something like this:
timestamp; geofence_event; geofence_name; dwell_time; hashed_sta_mac
[DATETIME]; [EVENT]; [NAME]; [NUMBER]; [NUMBER]
[DATETIME]; [EVENT]; [NAME]; [NUMBER]; [NUMBER]
I’ve managed to find Select-String to grab the desired values from the log file and also the way to convert the Unix TimeStamp to human readable DateTime format.
To take the timestamp as an example:
$tStamps = Select-String -Path [LOG FILE] -Pattern "(?<=timestamp: ).*.$" | select -ExpandProperty Matches
$tStamps.Value
returns all the Unix timestamps from the log file and I can then convert them to human readable like this:
foreach ($ts in $tStamps){
Get-Date (get-date 01.01.1970).AddSeconds($ts.Value) -Format 'yyyy-MM-dd HH:ss'
}
Everything else is much simpler as I can just modify the -Pattern
parameter to grab the right values.
However, I worry I’m not thinking right here as this would give me five arrays with the TimeStamps, Events, Names, Dwell time and hashed MACs, which would then have to be merged somehow to produce the desired CSV.
I don’t know if I should somehow try to grab the values from each separate Recv event with topic “geofence_notify” block, but I’m not sure how I’d go about doing this best.
Any suggestions are welcome. I may be overthinking this to a massive degree… Would not be the first time.