-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Lars T Hansen
committed
Mar 7, 2025
1 parent
5662782
commit 197ee93
Showing
4 changed files
with
193 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,6 @@ target/ | |
*.rej | ||
*.orig | ||
util/ingest-kafka/ingest-kafka | ||
util/ingest-files/ingest-files | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module ingest-files | ||
|
||
go 1.23.6 | ||
|
||
require formats v0.0.0-00010101000000-000000000000 // indirect | ||
replace formats => ../formats |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
// Ingest old-format files. The trailing arguments are names of files or directories. If they are | ||
// files they must match '*.csv' for old-style `sonar ps` data or `sysinfo-*.json` for old-style | ||
// `sonar sysinfo` data. Otherwise they are going to be treated as directories. | ||
|
||
package main | ||
|
||
import ( | ||
"encoding/csv" | ||
"encoding/json" | ||
"flag" | ||
"fmt" | ||
"io/fs" | ||
"log" | ||
"os" | ||
"path" | ||
"regexp" | ||
"strings" | ||
|
||
"formats/oldfmt" | ||
) | ||
|
||
var ( | ||
verbose = flag.Bool("v", false, "Verbose") | ||
) | ||
|
||
var ( | ||
sampleFile = regexp.MustCompile(`^(?:.*/)?(.*)\.csv$`) | ||
sysinfoFile = regexp.MustCompile(`^(?:.*/)?sysinfo-(.*)\.json$`) | ||
) | ||
|
||
func main() { | ||
// TODO: Usage message to indicate trailing args | ||
flag.Parse() | ||
for _, candidate := range flag.Args() { | ||
if tryMatch(candidate) { | ||
continue | ||
} | ||
fs.WalkDir(os.DirFS(candidate), ".", func(fpath string, _ fs.DirEntry, err error) error { | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
tryMatch(path.Join(candidate, fpath)) | ||
return nil | ||
}) | ||
} | ||
} | ||
|
||
func tryMatch(candidate string) bool { | ||
if m := sampleFile.FindStringSubmatch(candidate); m != nil { | ||
consumeOldSampleFile(candidate, m[1]) | ||
return true | ||
} | ||
if m := sysinfoFile.FindStringSubmatch(candidate); m != nil { | ||
consumeOldSysinfoFile(candidate, m[1]) | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func consumeOldSampleFile(fn, hostname string) { | ||
// The file contains zero or more records on the old sample format. v0.6.0 and earlier are | ||
// ignored. | ||
f, err := os.Open(fn) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer f.Close() | ||
r := csv.NewReader(f) | ||
r.FieldsPerRecord = -1 // variable | ||
var lineno int | ||
for { | ||
lineno++ | ||
fields, err := r.Read() | ||
if err != nil { | ||
log.Fatalf("Error on line %d: %v", lineno, err) | ||
} | ||
if len(fields) == 0 { | ||
continue | ||
} | ||
// Old-format records start with the digit `2` (initial digit in timestamp) | ||
if strings.HasPrefix(fields[0], "2") { | ||
continue | ||
} | ||
record := new(oldfmt.SampleEnvelope) | ||
var foundVersion bool | ||
for _, fld := range fields { | ||
key, value, found := strings.Cut(fld, "=") | ||
if !found { | ||
continue | ||
} | ||
switch key { | ||
case "cores": | ||
case "cmd": | ||
case "cpu%": | ||
case "cpukib": | ||
case "cputime_sec": | ||
case "gpu%": | ||
case "gpufail": | ||
case "gpuinfo": | ||
case "gpukib": | ||
case "gpumem%": | ||
case "gpus": | ||
case "host": | ||
case "job": | ||
case "load": | ||
case "memtotalkib": | ||
case "pid": | ||
case "ppid": | ||
case "rssanonkib": | ||
case "rolledup": | ||
case "time": | ||
case "user": | ||
case "v": | ||
foundVersion = true | ||
record.Version = value | ||
} | ||
} | ||
if !foundVersion { | ||
// TODO: More conditions | ||
continue | ||
} | ||
consumeOldSample(record) | ||
} | ||
} | ||
|
||
func consumeOldSysinfoFile(fn, hostname string) { | ||
// The file contains zero or more records on the old sysinfo format, they are not | ||
// comma-separated or in an array. So we must decode one at a time | ||
f, err := os.Open(fn) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer f.Close() | ||
dec := json.NewDecoder(f) | ||
for dec.More() { | ||
info := new(oldfmt.Sysinfo) | ||
err := dec.Decode(info) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
consumeOldSysinfo(info) | ||
} | ||
} | ||
|
||
// The following are the same as in the ingest-kafka code | ||
|
||
func consumeOldSysinfo(info *oldfmt.Sysinfo) { | ||
fmt.Printf("Sysinfo for %s %s\n", info.Hostname, info.Timestamp) | ||
fmt.Printf(" Desc %s\n CpuCores %d\n MemGB %d\n", info.Description, info.CpuCores, info.MemGB) | ||
} | ||
|
||
func consumeOldSample(info *oldfmt.SampleEnvelope) { | ||
fmt.Printf("Sample for %s %s\n", info.Hostname, info.Timestamp) | ||
for _, s := range info.Samples { | ||
if s.CpuTimeSec > 100 { | ||
fmt.Printf(" `%s` %d\n", s.Cmd, s.CpuTimeSec) | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters