Skip to content

Commit

Permalink
user/event: decoding ktime
Browse files Browse the repository at this point in the history
  • Loading branch information
h0x0er authored and cfc4n committed Nov 17, 2023
1 parent 402575f commit 0640e03
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 0 deletions.
6 changes: 6 additions & 0 deletions user/event/event_gnutls.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ func (ge *GnutlsDataEvent) Decode(payload []byte) (err error) {
if err = binary.Read(buf, binary.LittleEndian, &ge.Comm); err != nil {
return
}

decodedKtime, err := DecodeKtime(int64(ge.Timestamp), true)
if err == nil {
ge.Timestamp = uint64(decodedKtime.Unix())
}

return nil
}

Expand Down
6 changes: 6 additions & 0 deletions user/event/event_mysqld.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ func (me *MysqldEvent) Decode(payload []byte) (err error) {
if err = binary.Read(buf, binary.LittleEndian, &me.Retval); err != nil {
return
}

decodedKtime, err := DecodeKtime(int64(me.Timestamp), true)
if err == nil {
me.Timestamp = uint64(decodedKtime.Unix())
}

return nil
}

Expand Down
4 changes: 4 additions & 0 deletions user/event/event_nspr.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ func (ne *NsprDataEvent) Decode(payload []byte) (err error) {
if err = binary.Read(buf, binary.LittleEndian, &ne.Comm); err != nil {
return
}
decodedKtime, err := DecodeKtime(int64(ne.Timestamp), true)
if err == nil {
ne.Timestamp = uint64(decodedKtime.Unix())
}
return nil
}

Expand Down
4 changes: 4 additions & 0 deletions user/event/event_openssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ func (se *SSLDataEvent) Decode(payload []byte) (err error) {
return
}

decodedKtime, err := DecodeKtime(int64(se.Timestamp), true)
if err == nil {
se.Timestamp = uint64(decodedKtime.Unix())
}
return nil
}

Expand Down
4 changes: 4 additions & 0 deletions user/event/event_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ func (pe *PostgresEvent) Decode(payload []byte) (err error) {
if err = binary.Read(buf, binary.LittleEndian, &pe.Comm); err != nil {
return
}
decodedKtime, err := DecodeKtime(int64(pe.Timestamp), true)
if err == nil {
pe.Timestamp = uint64(decodedKtime.Unix())
}
return nil
}

Expand Down
19 changes: 19 additions & 0 deletions user/event/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ package event
import (
"bytes"
"fmt"
"time"

"golang.org/x/sys/unix"
)

// 格式化输出相关
Expand Down Expand Up @@ -88,3 +91,19 @@ func CToGoString(c []byte) string {
}
return string(c[:n+1])
}

func DecodeKtime(ktime int64, monotonic bool) (time.Time, error) {
var clk int32
if monotonic {
clk = int32(unix.CLOCK_MONOTONIC)
} else {
clk = int32(unix.CLOCK_BOOTTIME)
}
currentTime := unix.Timespec{}
if err := unix.ClockGettime(clk, &currentTime); err != nil {
return time.Time{}, err
}
diff := ktime - currentTime.Nano()
t := time.Now().Add(time.Duration(diff))
return t, nil
}

0 comments on commit 0640e03

Please sign in to comment.