Skip to content

Commit

Permalink
kolog
Browse files Browse the repository at this point in the history
Signed-off-by: lec-bit <[email protected]>
  • Loading branch information
lec-bit committed Jan 23, 2025
1 parent 9d735e4 commit e2b3001
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
2 changes: 2 additions & 0 deletions daemon/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down
98 changes: 98 additions & 0 deletions pkg/kolog/kolog.go
Original file line number Diff line number Diff line change
@@ -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) {

Check failure on line 34 in pkg/kolog/kolog.go

View workflow job for this annotation

GitHub Actions / build (1.23)

unnecessary leading newline (whitespace)

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()

Check failure on line 70 in pkg/kolog/kolog.go

View workflow job for this annotation

GitHub Actions / build (1.23)

Error return value of `cmd.Process.Kill` is not checked (errcheck)
cmd.Process.Wait()

Check failure on line 71 in pkg/kolog/kolog.go

View workflow job for this annotation

GitHub Actions / build (1.23)

Error return value of `cmd.Process.Wait` is not checked (errcheck)
}
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)
}
}
}
}()
}

0 comments on commit e2b3001

Please sign in to comment.