chore: add logparser logic for agent and add parsed log to clickhouse
ci-agent / build (push) Failing after 3m30s
ci-agent / build (push) Failing after 3m30s
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
|
||||
"gitea.d3m0k1d.ru/d3m0k1d/HellreigN/agent/internal/logsource"
|
||||
"github.com/hpcloud/tail"
|
||||
)
|
||||
|
||||
var _ logsource.LogSource = new(FileLogSource)
|
||||
|
||||
type FileLogSource struct {
|
||||
*tail.Tail
|
||||
}
|
||||
|
||||
func New(filepath string) (fls *FileLogSource, err error) {
|
||||
if _, err := os.Stat(filepath); os.IsNotExist(err) {
|
||||
if err := os.WriteFile(filepath, []byte{}, 0600); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
t, err := tail.TailFile(filepath, tail.Config{
|
||||
Follow: true,
|
||||
Location: &tail.SeekInfo{
|
||||
Offset: 100,
|
||||
Whence: 2,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return &FileLogSource{t}, nil
|
||||
}
|
||||
func (f *FileLogSource) ReadLine() (string, error) {
|
||||
select {
|
||||
case <-f.Dead():
|
||||
return "", errors.Join(logsource.ErrDead, f.Err())
|
||||
case line := <-f.Lines:
|
||||
return line.Text, line.Err
|
||||
}
|
||||
}
|
||||
func (f *FileLogSource) Close() error {
|
||||
return f.Stop()
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package logsource
|
||||
|
||||
import "errors"
|
||||
|
||||
type LogSource interface {
|
||||
ReadLine() (string, error)
|
||||
Close() error
|
||||
}
|
||||
|
||||
var ErrDead = errors.New("shouldn't continue to read that")
|
||||
@@ -0,0 +1,55 @@
|
||||
package journald
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"os/exec"
|
||||
"syscall"
|
||||
|
||||
"gitea.d3m0k1d.ru/d3m0k1d/HellreigN/agent/internal/config"
|
||||
"gitea.d3m0k1d.ru/d3m0k1d/HellreigN/agent/internal/logsource"
|
||||
)
|
||||
|
||||
var _ logsource.LogSource = new(JournaldLogSource)
|
||||
|
||||
type JournaldLogSource struct {
|
||||
cmd *exec.Cmd
|
||||
stdout io.ReadCloser
|
||||
stdoutscanner *bufio.Scanner
|
||||
}
|
||||
|
||||
// ReadLine implements logsource.LogSource.
|
||||
func (j *JournaldLogSource) ReadLine() (string, error) {
|
||||
if j.stdoutscanner.Scan() {
|
||||
return j.stdoutscanner.Text(), nil
|
||||
} else {
|
||||
if j.stdoutscanner.Err() == nil {
|
||||
return "", fmt.Errorf("%w: %s", logsource.ErrDead, io.EOF)
|
||||
}
|
||||
return "", j.stdoutscanner.Err()
|
||||
}
|
||||
}
|
||||
func (j *JournaldLogSource) Close() error {
|
||||
_ = j.cmd.Process.Signal(syscall.SIGTERM)
|
||||
return j.cmd.Wait()
|
||||
}
|
||||
|
||||
func New(cfg config.ServiceConfig, logdir string) (*JournaldLogSource, error) {
|
||||
args := make([]string, 0)
|
||||
if cfg.Path != nil {
|
||||
args = append(args, "-u", *cfg.Path)
|
||||
}
|
||||
args = append(args, "-f", "-n", "0", "-o", "short", "--no-pager", "--directory", logdir)
|
||||
cmd := exec.Command("journalctl", args...) //nolint:gosec
|
||||
stdout, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = cmd.Start()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
stdoutscanner := bufio.NewScanner(stdout)
|
||||
return &JournaldLogSource{cmd, stdout, stdoutscanner}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user