Skip to content

Commit

Permalink
update for review comment
Browse files Browse the repository at this point in the history
  • Loading branch information
takara9 committed Oct 15, 2024
1 parent 954a213 commit 12adc01
Showing 1 changed file with 18 additions and 29 deletions.
47 changes: 18 additions & 29 deletions bmc-log-collector/log-collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ type selCollector struct {
}

func (c *selCollector) collectSystemEventLog(ctx context.Context, m Machine, logWriter bmcLogWriter) {
const layout = "2006-01-02T15:04:05Z07:00"
var currentLastId int
filePath := path.Join(c.ptrDir, m.Serial)

err := checkAndCreatePointerFile(filePath)
Expand Down Expand Up @@ -89,7 +87,7 @@ func (c *selCollector) collectSystemEventLog(ctx context.Context, m Machine, log

err = updateLastPointer(lastPtr, filePath)
if err != nil {
slog.Error("failed to write a pointer file.", "err", err, "serial", m.Serial, "currentLastReadId", currentLastId, "ptrDir", c.ptrDir)
slog.Error("failed to write a pointer file.", "err", err, "serial", m.Serial, "ptrDir", c.ptrDir)
}
return
}
Expand All @@ -103,70 +101,61 @@ func (c *selCollector) collectSystemEventLog(ctx context.Context, m Machine, log
return
}

createTime, err := time.Parse(layout, response.Sel[len(response.Sel)-1].Create)
createTime, err := time.Parse(time.RFC3339, response.Sel[len(response.Sel)-1].Create)
if err != nil {
slog.Error("failed to parse for time", "err", err, "serial", m.Serial, "currentLastReadId", currentLastId, "ptrDir", c.ptrDir)
slog.Error("failed to parse for time", "err", err, "serial", m.Serial)
return
}
FirstCreateTime := createTime.Unix()
firstCreateTime := createTime.Unix()

for i := len(response.Sel) - 1; i >= 0; i-- {
t, err := time.Parse(layout, response.Sel[i].Create)
currentId, err := strconv.Atoi(response.Sel[i].Id)
if err != nil {
slog.Error("failed to time parse", "err", err, "serial", m.Serial, "LastReadId", currentLastId, "ptrDir", c.ptrDir)
continue
}
currentCreateUnixtime := t.Unix()
currentLastId, err = strconv.Atoi(response.Sel[i].Id)
if err != nil {
slog.Error("failed to strconv", "err", err, "serial", m.Serial, "LastReadId", currentLastId, "ptrDir", c.ptrDir)
slog.Error("failed to strconv", "err", err, "serial", m.Serial, "LastReadId", currentId, "ptrDir", c.ptrDir)
continue
}
// Add the information to identify of the node
response.Sel[i].Serial = m.Serial
response.Sel[i].BmcIP = m.BmcIP
response.Sel[i].NodeIP = m.NodeIP

if lastPtr.LastReadId < currentLastId {
if lastPtr.LastReadId < currentId {
bmcByteJsonLog, err := json.Marshal(response.Sel[i])
if err != nil {
slog.Error("failed to marshal the system event log", "err", err, "serial", m.Serial, "lastPtr.LastReadId", lastPtr.LastReadId, "currentLastReadId", currentLastId, "ptrDir", c.ptrDir)
slog.Error("failed to marshal the system event log", "err", err, "serial", m.Serial, "lastPtr.LastReadId", lastPtr.LastReadId, "currentLastReadId", currentId, "ptrDir", c.ptrDir)
}

err = logWriter.write(string(bmcByteJsonLog), m.Serial)
if err != nil {
slog.Error("failed to output log", "err", err, "serial", m.Serial, "bmcByteJsonLog", string(bmcByteJsonLog), "currentLastReadId", currentLastId, "ptrDir", c.ptrDir)
slog.Error("failed to output log", "err", err, "serial", m.Serial, "bmcByteJsonLog", string(bmcByteJsonLog), "currentLastReadId", currentId, "ptrDir", c.ptrDir)
}

lastPtr.LastReadId = currentLastId
lastPtr.LastReadTime = currentCreateUnixtime
lastPtr.LastReadId = currentId
lastPtr.LastReadTime = firstCreateTime
lastPtr.LastError = nil
if currentLastId == 1 {
lastPtr.FirstCreateTime = currentCreateUnixtime
}
} else {
// If the log is reset in iDRAC, the ID starts from 1.
// In that case, determine if generated time been changed to identify log reseted.
if lastPtr.FirstCreateTime != FirstCreateTime {
if lastPtr.FirstCreateTime != firstCreateTime {
bmcByteJsonLog, err := json.Marshal(response.Sel[i])
if err != nil {
slog.Error("failed to convert JSON", "err", err, "serial", m.Serial, "i", i, "Event", response.Sel[i], "currentLastReadId", currentLastId)
slog.Error("failed to convert JSON", "err", err, "serial", m.Serial, "i", i, "Event", response.Sel[i], "currentLastReadId", currentId)
}

err = logWriter.write(string(bmcByteJsonLog), m.Serial)
if err != nil {
slog.Error("failed to output log", "err", err, "serial", m.Serial, "bmcByteJsonLog", string(bmcByteJsonLog), "currentLastReadId", currentLastId)
slog.Error("failed to output log", "err", err, "serial", m.Serial, "bmcByteJsonLog", string(bmcByteJsonLog), "currentLastReadId", currentId)
}
lastPtr.LastReadId = currentLastId
lastPtr.LastReadTime = currentCreateUnixtime
lastPtr.LastReadId = currentId
lastPtr.LastReadTime = firstCreateTime
lastPtr.LastError = nil
}
}
}
lastPtr.FirstCreateTime = FirstCreateTime
lastPtr.FirstCreateTime = firstCreateTime
err = updateLastPointer(lastPtr, filePath)
if err != nil {
slog.Error("failed to write a pointer file.", "err", err, "serial", m.Serial, "FirstCreateTime", FirstCreateTime, "filePath", filePath)
slog.Error("failed to write a pointer file.", "err", err, "serial", m.Serial, "firstCreateTime", firstCreateTime, "filePath", filePath)
return
}
}

0 comments on commit 12adc01

Please sign in to comment.