Skip to content

Commit

Permalink
Backup
Browse files Browse the repository at this point in the history
  • Loading branch information
g41797 committed Oct 21, 2023
1 parent f9c4785 commit e331608
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 137 deletions.
8 changes: 8 additions & 0 deletions parts.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ func newparts(initialCapacity int) *parts {
return &parts{data: make([]rune, initialCapacity)}
}

// Initiates parts
func (p *parts) set() {
if len(p.data) == 0 {
p.data = make([]rune, 128)
}
p.rewind()
}

// Appends a text to the parts instance
func (p *parts) appendText(text string) int {
if len(text) == 0 {
Expand Down
266 changes: 129 additions & 137 deletions syslogmsgparts.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,71 +9,74 @@ import (
"github.com/g41797/sputnik"
)

type syslogmsgparts struct {
parts
}

func (mp *syslogmsgparts) Extract(ef func(name, value string) error) error {

count, _ := mp.runeAt(0)

switch int(count) {
case BadMessageParts:
return mp.extractBadMessage(ef)
case RFC5424Parts:
return mp.extractRFCMessage(rfc5424names[:], ef)
case RFC3164Parts:
return mp.extractRFCMessage(rfc3164names[:], ef)
}
const (
RFC3164OnlyKey = "tag"
RFC5424OnlyKey = "structured_data"
RFCFormatKey = "rfc"
RFC3164 = "RFC3164"
RFC5424 = "RFC5424"
SeverityKey = "severity"
ParserError = "parsererror"
FormerMessage = "data"
BrokenParts = 2
BadMessageParts = 1
RFC5424Parts = len(rfc5424parts) // RFC + 11
RFC3164Parts = len(rfc3164parts) // RFC + 7
)

return fmt.Errorf("Wrong packed syslog message")
type partType struct {
name string
kind string
}

func (mp *syslogmsgparts) extractBadMessage(extr func(name, value string) error) error {
mlen, _ := mp.runeAt(1)
mp.skip(2)

value, err := mp.part(int(mlen))
//
// https://blog.datalust.co/seq-input-syslog/
//
// ------------------------------------
// priority = (facility * 8) + severity
// ------------------------------------

if err != nil {
return err
}
// RFC3164 parameters with type
// https://documentation.solarwinds.com/en/success_center/kss/content/kss_adminguide_syslog_protocol.htm

return extr(FormerMessage, value)
var rfc3164parts = [...]partType{
{RFCFormatKey, "string"}, // Non-RFC: Added by syslogcar
{"priority", "int"},
{"facility", "int"},
{SeverityKey, "int"},
{"timestamp", "time.Time"},
{"hostname", "string"},
{RFC3164OnlyKey, "string"},
{"content", "string"},
}

func (mp *syslogmsgparts) extractRFCMessage(names []string, extr func(name, value string) error) error {
mp.rewind()

count, _ := mp.runeAt(1)
mp.skip(int(count + 1))

vlen, _ := mp.runeAt(2)
value, err := mp.part(int(vlen))
if err != nil {
return err
}
err = extr(RFCFormatKey, value)
if err != nil {
return err
}
// RFC5424 parameters with type
// https://hackmd.io/@njjack/syslogformat
var rfc5424parts = [...]partType{
{RFCFormatKey, "string"}, // Non-RFC: Added by syslogcar
{"priority", "int"},
{"facility", "int"},
{SeverityKey, "int"},
{"version", "int"},
{"timestamp", "time.Time"},
{"hostname", "string"},
{"app_name", "string"},
{"proc_id", "string"},
{"msg_id", "string"},
{RFC5424OnlyKey, "string"},
{"message", "string"},
}

for i, name := range names {
vlen, _ := mp.runeAt(2 + i)
value, err := mp.part(int(vlen))
if err != nil {
return err
}
err = extr(name, value)
if err != nil {
return err
}
}
// Former message - for badly formatted syslog message
var formerMessage = [...]partType{
{FormerMessage, "string"},
}

return nil
type syslogmsgparts struct {
parts
}

func (mp *syslogmsgparts) fillMsg(logParts format.LogParts, msgLen int64, err error) bool {
func (mp *syslogmsgparts) pack(logParts format.LogParts, msgLen int64, err error) bool {

if logParts == nil {
return false
Expand All @@ -84,104 +87,106 @@ func (mp *syslogmsgparts) fillMsg(logParts format.LogParts, msgLen int64, err er
}

if err != nil {
mp.fillFormerMessage(logParts)
mp.packParts(formerMessage[:], logParts)
return true
}

if _, exists := logParts[RFC5424OnlyKey]; exists {
mp.fillRFC5424(logParts)
logParts[RFCFormatKey] = RFC5424
mp.packParts(rfc5424parts[:], logParts)
return true
}

if _, exists := logParts[RFC3164OnlyKey]; exists {
mp.fillRFC3164(logParts)
logParts[RFCFormatKey] = RFC3164
mp.packParts(rfc3164parts[:], logParts)
return true
}
mp.fillFormerMessage(logParts)
mp.packParts(formerMessage[:], logParts)
return true
}

func (mp *syslogmsgparts) set() {
if len(mp.data) == 0 {
mp.data = make([]rune, 128)
}
}

func (mp *syslogmsgparts) fillFormerMessage(logParts format.LogParts) {
mp.set()
mp.setRuneAt(0, 1)
mp.skip(2)
mp.setRuneAt(1, rune(mp.appendText(logParts[FormerMessage].(string))))
}

func (mp *syslogmsgparts) fillRFC5424(logParts format.LogParts) {
func (mp *syslogmsgparts) packParts(parts []partType, logParts format.LogParts) {
mp.set()

count := 1 + len(rfc5424names)
count := len(parts)
mp.setRuneAt(0, rune(count))
mp.skip(count + 1)

mp.setRuneAt(1, rune(mp.appendText(RFC5424)))

for i, name := range rfc5424names {
v, exists := logParts[name]
for i, part := range parts {
v, exists := logParts[part.name]

if !exists {
mp.setRuneAt(i+2, 0)
mp.setRuneAt(i+1, 0)
continue
}

mp.setRuneAt(i+2, rune(mp.appendText(toString(v, rfc5424props[name]))))
mp.setRuneAt(i+1, rune(mp.appendText(toString(v, part.kind))))
}
}

func (mp *syslogmsgparts) fillRFC3164(logParts format.LogParts) {
mp.set()
func (mp *syslogmsgparts) Unpack(f func(name, value string) error) error {

count := 1 + len(rfc3164names)
mp.setRuneAt(0, rune(count))
mp.skip(count + 1)
count, _ := mp.runeAt(0)

mp.setRuneAt(1, rune(mp.appendText(RFC3164)))
switch int(count) {
case BadMessageParts:
return mp.unpackParts(formerMessage[:], f)
case RFC5424Parts:
return mp.unpackParts(rfc5424parts[:], f)
case RFC3164Parts:
return mp.unpackParts(rfc3164parts[:], f)
}

for i, name := range rfc3164names {
v, exists := logParts[name]
return fmt.Errorf("Wrong packed syslog message")
}

if !exists {
mp.setRuneAt(i+2, 0)
continue
}
func (mp *syslogmsgparts) unpackParts(parts []partType, f func(name, value string) error) error {
mp.rewind()
count, _ := mp.runeAt(0)
mp.skip(int(count + 1))

mp.setRuneAt(i+2, rune(mp.appendText(toString(v, rfc3164props[name]))))
for i, part := range parts {
vlen, _ := mp.runeAt(1 + i)
value, err := mp.part(int(vlen))
if err != nil {
return err
}
err = f(part.name, value)
if err != nil {
return err
}
}

return nil
}

const (
RFC3164OnlyKey = "tag"
RFC5424OnlyKey = "structured_data"
RFCFormatKey = "rfc"
RFC3164 = "RFC3164"
RFC5424 = "RFC5424"
SeverityKey = "severity"
ParserError = "parsererror"
FormerMessage = "data"
BrokenParts = 2
BadMessageParts = 1
RFC5424Parts = 12 // RFC + 11
RFC3164Parts = 8 // RFC + 7
)
func toString(val any, typ string) string {
result := ""

//
// https://blog.datalust.co/seq-input-syslog/
//
if val == nil {
return result
}

// ------------------------------------
// priority = (facility * 8) + severity
// ------------------------------------
switch typ {
case "string":
result, _ = val.(string)
return result
case "int":
intval, _ := val.(int)
result = strconv.Itoa(intval)
return result
case "time.Time":
tval, _ := val.(time.Time)
result = tval.UTC().String()
return result
}

return result
}

//////////////////////////////////////////////////////////////////////

// RFC3164 parameters with type
// https://documentation.solarwinds.com/en/success_center/kss/content/kss_adminguide_syslog_protocol.htm
func RFC3164Props() map[string]string {
return map[string]string{
"priority": "int",
Expand All @@ -199,8 +204,6 @@ var rfc3164props = RFC3164Props()
var rfc3164names = [7]string{
"priority", "facility", SeverityKey, "timestamp", "hostname", RFC3164OnlyKey, "content"}

// RFC5424 parameters with type
// https://hackmd.io/@njjack/syslogformat
func RFC5424Props() map[string]string {
return map[string]string{
"priority": "int",
Expand Down Expand Up @@ -255,7 +258,6 @@ func msgFromFormerMsg(logParts format.LogParts) sputnik.Msg {
return msg
}

// Convert syslog RFC5424 values to strings
func toRFC5424(logParts format.LogParts) sputnik.Msg {
msg := make(sputnik.Msg)
msg[RFCFormatKey] = RFC5424
Expand All @@ -272,7 +274,6 @@ func toRFC5424(logParts format.LogParts) sputnik.Msg {
return msg
}

// Convert syslog RFC3164 values to strings
func toRFC3164(logParts format.LogParts) sputnik.Msg {
msg := make(sputnik.Msg)
msg[RFCFormatKey] = RFC3164
Expand All @@ -289,26 +290,17 @@ func toRFC3164(logParts format.LogParts) sputnik.Msg {
return msg
}

func toString(val any, typ string) string {
result := ""
//////////////////////////////////////////////////////////////////////

if val == nil {
return result
}
type Pack interface {
Pack(f func(name string) (value string, err error)) error
}

switch typ {
case "string":
result, _ = val.(string)
return result
case "int":
intval, _ := val.(int)
result = strconv.Itoa(intval)
return result
case "time.Time":
tval, _ := val.(time.Time)
result = tval.UTC().String()
return result
}
type Unpack interface {
Unpack(f func(name, value string) error) error
}

return result
type PackUnpack interface {
Pack
Unpack
}

0 comments on commit e331608

Please sign in to comment.