Skip to content

Commit

Permalink
Merge pull request #38 from cafebazaar/data-parser
Browse files Browse the repository at this point in the history
fix: support older array prop for keepalived data
  • Loading branch information
mehdy authored Jul 30, 2020
2 parents 93adbc3 + 20bf5ed commit bbd65a2
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 12 deletions.
29 changes: 23 additions & 6 deletions internal/collector/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,18 @@ func (k *KeepalivedCollector) parseJSON(i io.Reader) ([]VRRP, error) {
return stats, nil
}

// isKeyArray checks if key is array in keepalived.data file
func isKeyArray(key string) bool {
supportedKeys := []string{"Virtual IP"}
for _, supportedKey := range supportedKeys {
if supportedKey == key {
return true
}
}
logrus.WithField("Key", key).Debug("Unsupported array key")
return false
}

func (k *KeepalivedCollector) parseVRRPData(i io.Reader) ([]VRRPData, error) {
data := make([]VRRPData, 0)

Expand All @@ -211,19 +223,24 @@ func (k *KeepalivedCollector) parseVRRPData(i io.Reader) ([]VRRPData, error) {

s := strings.Split(strings.TrimSpace(l), prop)
d.IName = strings.TrimSpace(s[1])
} else if (strings.HasPrefix(l, " ") || strings.HasPrefix(l, " ")) && d.IName != "" {
} else if strings.HasPrefix(l, " ") && d.IName != "" {
if strings.HasPrefix(l, " ") {
val = strings.TrimSpace(l)
} else {
var args []string
if strings.Contains(l, prop) {
s := strings.Split(strings.TrimSpace(l), prop)
key = strings.TrimSpace(s[0])
val = strings.TrimSpace(s[1])
args = strings.Split(strings.TrimSpace(l), prop)
} else if strings.Contains(l, arrayProp) {
s := strings.Split(strings.TrimSpace(l), arrayProp)
key = strings.TrimSpace(s[0])
args = strings.Split(strings.TrimSpace(l), arrayProp)
} else {
continue
}

key = strings.TrimSpace(args[0])
if isKeyArray(key) {
continue
}
val = strings.TrimSpace(args[1])
}
switch key {
case "State":
Expand Down
95 changes: 89 additions & 6 deletions internal/collector/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ func TestVRRPDataStringToIntState(t *testing.T) {
}
}

func TestParseVRRPData(t *testing.T) {
f, err := os.Open("../../test_files/keepalived.data")
func TestV215ParseVRRPData(t *testing.T) {
f, err := os.Open("../../test_files/v2.1.5/keepalived.data")
if err != nil {
t.Log(err)
t.Fail()
Expand Down Expand Up @@ -149,8 +149,76 @@ func TestParseVRRPData(t *testing.T) {
}
}

func TestParseVRRPScript(t *testing.T) {
f, err := os.Open("../../test_files/keepalived.data")
func TestV2010ParseVRRPData(t *testing.T) {
f, err := os.Open("../../test_files/v2.0.10/keepalived.data")
if err != nil {
t.Log(err)
t.Fail()
}
defer f.Close()

k := &KeepalivedCollector{}
vrrpData, err := k.parseVRRPData(f)
if err != nil {
t.Log(err)
t.Fail()
}

if len(vrrpData) != 1 {
t.Fail()
}

vi1 := VRRPData{
IName: "VI_1",
State: 2,
WantState: 2,
Intf: "ens192",
GArpDelay: 5,
VRID: 52,
VIPs: []string{"2.2.2.2/32 dev ens192 scope global"},
}

for _, data := range vrrpData {
if data.IName == "VI_1" {
if !reflect.DeepEqual(data, vi1) {
t.Fail()
}
} else {
t.Fail()
}
}
}

func TestV215ParseVRRPScript(t *testing.T) {
f, err := os.Open("../../test_files/v2.0.10/keepalived.data")
if err != nil {
t.Log(err)
t.Fail()
}
defer f.Close()

k := &KeepalivedCollector{}
vrrpScripts := k.parseVRRPScript(f)

if len(vrrpScripts) != 1 {
t.Fail()
}

for _, script := range vrrpScripts {
if script.Name != "chk_service" {
t.Fail()
}
if script.Status != "GOOD" {
t.Fail()
}
if script.State != "idle" {
t.Fail()
}
}
}

func TestV2010ParseVRRPScript(t *testing.T) {
f, err := os.Open("../../test_files/v2.1.5/keepalived.data")
if err != nil {
t.Log(err)
t.Fail()
Expand All @@ -177,8 +245,8 @@ func TestParseVRRPScript(t *testing.T) {
}
}

func TestParseStats(t *testing.T) {
f, err := os.Open("../../test_files/keepalived.stats")
func TestV215ParseStats(t *testing.T) {
f, err := os.Open("../../test_files/v2.1.5/keepalived.stats")
if err != nil {
t.Log(err)
t.Fail()
Expand Down Expand Up @@ -274,3 +342,18 @@ func TestParseVIP(t *testing.T) {
t.Fail()
}
}

func TestIsKeyArray(t *testing.T) {
supportedKeys := []string{"Virtual IP"}

for _, key := range supportedKeys {
if !isKeyArray(key) {
t.Fail()
}
}

notArrayKey := "NoArray"
if isKeyArray(notArrayKey) {
t.Fail()
}
}
49 changes: 49 additions & 0 deletions test_files/v2.0.10/keepalived.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
------< VRRP Topology >------
VRRP Instance = VI_1
VRRP Version = 2
State = MASTER
Wantstate = MASTER
Number of interface and track script faults = 0
Number of track scripts init = 0
Last transition = 1595875667 (Mon Jul 27 18:47:47 2020)
Read timeout = 1596022954.764907 (Wed Jul 29 11:42:34.764907)
Master down timer = 804687 usecs
Interface = ens192
Using src_ip = 1.1.1.1
Gratuitous ARP delay = 5
Gratuitous ARP repeat = 5
Gratuitous ARP refresh = 0
Gratuitous ARP refresh repeat = 1
Gratuitous ARP lower priority delay = 5
Gratuitous ARP lower priority repeat = 5
Send advert after receive lower priority advert = true
Send advert after receive higher priority advert = false
Virtual Router ID = 52
Priority = 50
Effective priority = 50
Total priority = 50
Advert interval = 1 sec
Accept = enabled
Preempt = enabled
Promote_secondaries = disabled
Authentication type = none
Virtual IP = 1
2.2.2.2/32 dev ens192 scope global
fd_in 12, fd_out 13
Tracked scripts = 1
chk_service weight 0
Using smtp notification = no
------< VRRP Scripts >------
VRRP Script = chk_service
Command = '/usr/bin/killall' '-0' 'haproxy'
Interval = 2 sec
Timeout = 0 sec
Weight = 0
Rise = 1
Fall = 1
Insecure = no
Status = GOOD
Script uid:gid = 0:0
VRRP instances = 1
VI_1, weight 0
State = idle
File renamed without changes.
File renamed without changes.

0 comments on commit bbd65a2

Please sign in to comment.