diff --git a/daemon/manager/manager.go b/daemon/manager/manager.go index 18d9e757c..cee6980ef 100644 --- a/daemon/manager/manager.go +++ b/daemon/manager/manager.go @@ -34,6 +34,7 @@ import ( "kmesh.net/kmesh/pkg/bpf/restart" "kmesh.net/kmesh/pkg/cni" "kmesh.net/kmesh/pkg/controller" + "kmesh.net/kmesh/pkg/kolog" "kmesh.net/kmesh/pkg/logger" "kmesh.net/kmesh/pkg/status" ) @@ -93,6 +94,7 @@ func Execute(configs *options.BootstrapConfigs) error { stopCh := make(chan struct{}) defer close(stopCh) + kolog.KmeshModuleLog(stopCh) c := controller.NewController(configs, bpfLoader.GetBpfKmesh(), bpfLoader.GetBpfWorkload()) if err := c.Start(stopCh); err != nil { return err diff --git a/pkg/kolog/kolog.go b/pkg/kolog/kolog.go new file mode 100644 index 000000000..ac5056fb4 --- /dev/null +++ b/pkg/kolog/kolog.go @@ -0,0 +1,98 @@ +/* + * Copyright The Kmesh Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package kolog + +import ( + "bufio" + "fmt" + "os/exec" + "regexp" + "strings" + "time" + + "kmesh.net/kmesh/pkg/logger" +) + +var ( + log = logger.NewLoggerScope("Kmesh_module") +) + +func parseLogTime(line string) (time.Time, error) { + + re := regexp.MustCompile(`\[(\w{3} \w{3} \d{1,2} \d{2}:\d{2}:\d{2} \d{4})\]`) + match := re.FindStringSubmatch(line) + if len(match) < 2 { + return time.Time{}, fmt.Errorf("no time match found") + } + + logTime, err := time.Parse("Mon Jan 2 15:04:05 2006", match[1]) + if err != nil { + return time.Time{}, err + } + return logTime, nil +} + +func KmeshModuleLog(stopCh <-chan struct{}) { + go func() { + cmd := exec.Command("dmesg", "-wT") + + stdout, err := cmd.StdoutPipe() + if err != nil { + log.Errorf("Error creating stdout pipe: %v", err) + return + } + + startTime := time.Now() + if err := cmd.Start(); err != nil { + log.Errorf("Error starting command: %v", err) + return + } + + scanner := bufio.NewScanner(stdout) + for { + select { + case <-stopCh: + if cmd.Process != nil { + cmd.Process.Kill() + cmd.Process.Wait() + } + return + default: + if !scanner.Scan() { + if err := scanner.Err(); err != nil { + log.Errorf("Error reading from stdout: %v", err) + } + break + } + line := scanner.Text() + + if !strings.Contains(line, "Kmesh_module") { + continue + } + logTime, err := parseLogTime(line) + if err != nil { + log.Errorf("Error parsing log time: %v", err) + log.Info(line) + continue + } + if logTime.After(startTime) { + log.Info(line) + } + } + } + }() +}