Skip to content
Snippets Groups Projects
Commit 56f5817d authored by Matthias Rampke's avatar Matthias Rampke
Browse files

Log parse errors

parent 5a939ae4
No related branches found
No related tags found
No related merge requests found
...@@ -3,6 +3,7 @@ package main ...@@ -3,6 +3,7 @@ package main
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt"
) )
type action struct { type action struct {
...@@ -14,11 +15,14 @@ type action struct { ...@@ -14,11 +15,14 @@ type action struct {
Resumed int64 `json:"resumed"` Resumed int64 `json:"resumed"`
} }
func newActionFromJSON(b []byte) *action { func newActionFromJSON(b []byte) (*action, error) {
dec := json.NewDecoder(bytes.NewReader(b)) dec := json.NewDecoder(bytes.NewReader(b))
var pstat action var pstat action
dec.Decode(&pstat) err := dec.Decode(&pstat)
return &pstat if err != nil {
return nil, fmt.Errorf("failed to decode action stat %v: %v", b, err)
}
return &pstat, nil
} }
func (a *action) toPoints() []*point { func (a *action) toPoints() []*point {
......
...@@ -12,7 +12,10 @@ func TestNewActionFromJSON(t *testing.T) { ...@@ -12,7 +12,10 @@ func TestNewActionFromJSON(t *testing.T) {
t.Errorf("detected pstat type should be %d but is %d", rsyslogAction, logType) t.Errorf("detected pstat type should be %d but is %d", rsyslogAction, logType)
} }
pstat := newActionFromJSON([]byte(actionLog)) pstat, err := newActionFromJSON([]byte(actionLog))
if err != nil {
t.Fatalf("expected parsing action not to fail, got: %v", err)
}
if want, got := "test_action", pstat.Name; want != got { if want, got := "test_action", pstat.Name; want != got {
t.Errorf("wanted '%s', got '%s'", want, got) t.Errorf("wanted '%s', got '%s'", want, got)
...@@ -40,7 +43,10 @@ func TestNewActionFromJSON(t *testing.T) { ...@@ -40,7 +43,10 @@ func TestNewActionFromJSON(t *testing.T) {
} }
func TestActionToPoints(t *testing.T) { func TestActionToPoints(t *testing.T) {
pstat := newActionFromJSON([]byte(actionLog)) pstat, err := newActionFromJSON([]byte(actionLog))
if err != nil {
t.Fatalf("expected parsing action not to fail, got: %v", err)
}
points := pstat.toPoints() points := pstat.toPoints()
point := points[0] point := points[0]
......
...@@ -2,6 +2,7 @@ package main ...@@ -2,6 +2,7 @@ package main
import ( import (
"bufio" "bufio"
"log"
"os" "os"
"sync" "sync"
...@@ -38,33 +39,51 @@ func newRsyslogExporter() *rsyslogExporter { ...@@ -38,33 +39,51 @@ func newRsyslogExporter() *rsyslogExporter {
func (re *rsyslogExporter) handleStatLine(buf []byte) { func (re *rsyslogExporter) handleStatLine(buf []byte) {
pstatType := getStatType(buf) pstatType := getStatType(buf)
log.Printf("pstatType: %+v", pstatType)
switch pstatType { switch pstatType {
case rsyslogAction: case rsyslogAction:
a := newActionFromJSON(buf) a, err := newActionFromJSON(buf)
if err != nil {
log.Print(err)
return
}
for _, p := range a.toPoints() { for _, p := range a.toPoints() {
re.set(p) re.set(p)
} }
case rsyslogInput: case rsyslogInput:
i := newInputFromJSON(buf) i, err := newInputFromJSON(buf)
if err != nil {
log.Print(err)
return
}
for _, p := range i.toPoints() { for _, p := range i.toPoints() {
re.set(p) re.set(p)
} }
case rsyslogQueue: case rsyslogQueue:
q := newQueueFromJSON(buf) q, err := newQueueFromJSON(buf)
if err != nil {
log.Print(err)
return
}
for _, p := range q.toPoints() { for _, p := range q.toPoints() {
re.set(p) re.set(p)
} }
case rsyslogResource: case rsyslogResource:
r := newResourceFromJSON(buf) r, err := newResourceFromJSON(buf)
if err != nil {
log.Print(err)
return
}
for _, p := range r.toPoints() { for _, p := range r.toPoints() {
re.set(p) re.set(p)
} }
default: default:
log.Printf("unknown pstat type: %v", pstatType)
} }
} }
......
...@@ -3,6 +3,7 @@ package main ...@@ -3,6 +3,7 @@ package main
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt"
) )
type input struct { type input struct {
...@@ -10,11 +11,14 @@ type input struct { ...@@ -10,11 +11,14 @@ type input struct {
Submitted int64 `json:"submitted"` Submitted int64 `json:"submitted"`
} }
func newInputFromJSON(b []byte) *input { func newInputFromJSON(b []byte) (*input, error) {
dec := json.NewDecoder(bytes.NewReader(b)) dec := json.NewDecoder(bytes.NewReader(b))
var pstat input var pstat input
dec.Decode(&pstat) err := dec.Decode(&pstat)
return &pstat if err != nil {
return nil, fmt.Errorf("error decoding input stat %v: %v", b, err)
}
return &pstat, nil
} }
func (i *input) toPoints() []*point { func (i *input) toPoints() []*point {
......
...@@ -12,7 +12,10 @@ func TestgetInput(t *testing.T) { ...@@ -12,7 +12,10 @@ func TestgetInput(t *testing.T) {
t.Errorf("detected pstat type should be %d but is %d", rsyslogInput, logType) t.Errorf("detected pstat type should be %d but is %d", rsyslogInput, logType)
} }
pstat := newInputFromJSON([]byte(inputLog)) pstat, err := newInputFromJSON([]byte(inputLog))
if err != nil {
t.Fatalf("expected parsing input stat not to fail, got: %v", err)
}
if want, got := "test_input", pstat.Name; want != got { if want, got := "test_input", pstat.Name; want != got {
t.Errorf("want '%s', got '%s'", want, got) t.Errorf("want '%s', got '%s'", want, got)
...@@ -24,7 +27,11 @@ func TestgetInput(t *testing.T) { ...@@ -24,7 +27,11 @@ func TestgetInput(t *testing.T) {
} }
func TestInputtoPoints(t *testing.T) { func TestInputtoPoints(t *testing.T) {
pstat := newInputFromJSON([]byte(inputLog)) pstat, err := newInputFromJSON([]byte(inputLog))
if err != nil {
t.Fatalf("expected parsing input stat not to fail, got: %v", err)
}
points := pstat.toPoints() points := pstat.toPoints()
point := points[0] point := points[0]
......
...@@ -3,6 +3,7 @@ package main ...@@ -3,6 +3,7 @@ package main
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt"
) )
type queue struct { type queue struct {
...@@ -15,11 +16,14 @@ type queue struct { ...@@ -15,11 +16,14 @@ type queue struct {
MaxQsize int64 `json:"maxqsize"` MaxQsize int64 `json:"maxqsize"`
} }
func newQueueFromJSON(b []byte) *queue { func newQueueFromJSON(b []byte) (*queue, error) {
dec := json.NewDecoder(bytes.NewReader(b)) dec := json.NewDecoder(bytes.NewReader(b))
var pstat queue var pstat queue
dec.Decode(&pstat) err := dec.Decode(&pstat)
return &pstat if err != nil {
return nil, fmt.Errorf("failed to decode queue stat %v: %v", string(b), err)
}
return &pstat, nil
} }
func (q *queue) toPoints() []*point { func (q *queue) toPoints() []*point {
......
...@@ -12,7 +12,10 @@ func TestNewQueueFromJSON(t *testing.T) { ...@@ -12,7 +12,10 @@ func TestNewQueueFromJSON(t *testing.T) {
t.Errorf("detected pstat type should be %d but is %d", rsyslogQueue, logType) t.Errorf("detected pstat type should be %d but is %d", rsyslogQueue, logType)
} }
pstat := newQueueFromJSON([]byte(queueLog)) pstat, err := newQueueFromJSON([]byte(queueLog))
if err != nil {
t.Fatalf("expected parsing queue stat not to fail, got: %v", err)
}
if want, got := "main Q", pstat.Name; want != got { if want, got := "main Q", pstat.Name; want != got {
t.Errorf("want '%s', got '%s'", want, got) t.Errorf("want '%s', got '%s'", want, got)
...@@ -44,7 +47,10 @@ func TestNewQueueFromJSON(t *testing.T) { ...@@ -44,7 +47,10 @@ func TestNewQueueFromJSON(t *testing.T) {
} }
func TestQueueToPoints(t *testing.T) { func TestQueueToPoints(t *testing.T) {
pstat := newQueueFromJSON([]byte(queueLog)) pstat, err := newQueueFromJSON([]byte(queueLog))
if err != nil {
t.Fatalf("expected parsing queue stat not to fail, got: %v", err)
}
points := pstat.toPoints() points := pstat.toPoints()
point := points[0] point := points[0]
......
...@@ -3,6 +3,7 @@ package main ...@@ -3,6 +3,7 @@ package main
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt"
) )
type resource struct { type resource struct {
...@@ -18,11 +19,14 @@ type resource struct { ...@@ -18,11 +19,14 @@ type resource struct {
Nivcsw int64 `json:"nivcsw"` Nivcsw int64 `json:"nivcsw"`
} }
func newResourceFromJSON(b []byte) *resource { func newResourceFromJSON(b []byte) (*resource, error) {
dec := json.NewDecoder(bytes.NewReader(b)) dec := json.NewDecoder(bytes.NewReader(b))
var pstat resource var pstat resource
dec.Decode(&pstat) err := dec.Decode(&pstat)
return &pstat if err != nil {
return nil, fmt.Errorf("failed to decode resource stat %v: %v", b, err)
}
return &pstat, nil
} }
func (r *resource) toPoints() []*point { func (r *resource) toPoints() []*point {
......
...@@ -12,7 +12,10 @@ func TestNewResourceFromJSON(t *testing.T) { ...@@ -12,7 +12,10 @@ func TestNewResourceFromJSON(t *testing.T) {
t.Errorf("detected pstat type should be %d but is %d", rsyslogResource, logType) t.Errorf("detected pstat type should be %d but is %d", rsyslogResource, logType)
} }
pstat := newResourceFromJSON([]byte(resourceLog)) pstat, err := newResourceFromJSON([]byte(resourceLog))
if err != nil {
t.Fatalf("expected parsing resource stat not to fail, got: %v", err)
}
if want, got := "resource-usage", pstat.Name; want != got { if want, got := "resource-usage", pstat.Name; want != got {
t.Errorf("want '%s', got '%s'", want, got) t.Errorf("want '%s', got '%s'", want, got)
...@@ -56,7 +59,10 @@ func TestNewResourceFromJSON(t *testing.T) { ...@@ -56,7 +59,10 @@ func TestNewResourceFromJSON(t *testing.T) {
} }
func TestResourceToPoints(t *testing.T) { func TestResourceToPoints(t *testing.T) {
pstat := newResourceFromJSON([]byte(resourceLog)) pstat, err := newResourceFromJSON([]byte(resourceLog))
if err != nil {
t.Fatalf("expected parsing resource stat not to fail, got: %v", err)
}
points := pstat.toPoints() points := pstat.toPoints()
point := points[0] point := points[0]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment