Skip to content

Commit f0fb0e7

Browse files
committed
BF: qhost should parse entries of hosts in unknown state
1 parent 6b0a391 commit f0fb0e7

File tree

4 files changed

+74
-16
lines changed

4 files changed

+74
-16
lines changed

pkg/helper/helper_suite_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*
1717
************************************************************************/
1818
/*___INFO__MARK_END__*/
19+
1920
package helper_test
2021

2122
import (

pkg/helper/memory.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ func ParseMemoryFromString(m string) (int64, error) {
4242
return 0, errors.New("empty string")
4343
}
4444

45+
// special value for unknown memory
46+
if m == "-" {
47+
return 0, nil
48+
}
49+
4550
if m == "0" || m == "0.0" || m == "0.00" || m == "0.000" {
4651
return 0, nil
4752
}

pkg/qhost/v9.0/parsers.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,20 @@ func ParseHosts(out string) ([]Host, error) {
6868
return nil, fmt.Errorf("invalid NCPU: %s", fields[2])
6969
}
7070

71-
host.NSOC, err = strconv.Atoi(fields[3])
71+
host.NSOC, err = ParseIntOrUnknown(fields[3])
7272
if err != nil {
7373
return nil, fmt.Errorf("invalid NSOC: %s", fields[3])
7474
}
75-
host.NCOR, err = strconv.Atoi(fields[4])
75+
76+
host.NCOR, err = ParseIntOrUnknown(fields[4])
7677
if err != nil {
7778
return nil, fmt.Errorf("invalid NCOR: %s", fields[4])
7879
}
79-
host.NTHR, err = strconv.Atoi(fields[5])
80+
host.NTHR, err = ParseIntOrUnknown(fields[5])
8081
if err != nil {
8182
return nil, fmt.Errorf("invalid NTHR: %s", fields[5])
8283
}
83-
host.LOAD, err = strconv.ParseFloat(fields[6], 64)
84+
host.LOAD, err = ParseFloatOrUnknown(fields[6])
8485
if err != nil {
8586
return nil, fmt.Errorf("invalid LOAD: %s", fields[6])
8687
}
@@ -105,6 +106,24 @@ func ParseHosts(out string) ([]Host, error) {
105106
return hosts, nil
106107
}
107108

109+
func ParseIntOrUnknown(v string) (int, error) {
110+
if v == "-" {
111+
return 0, nil
112+
}
113+
ret, err := strconv.Atoi(v)
114+
if err != nil {
115+
return 0, fmt.Errorf("no int or -: %s", v)
116+
}
117+
return ret, nil
118+
}
119+
120+
func ParseFloatOrUnknown(v string) (float64, error) {
121+
if v == "-" {
122+
return 0, nil
123+
}
124+
return strconv.ParseFloat(v, 64)
125+
}
126+
108127
/*
109128
HOSTNAME ARCH NCPU NSOC NCOR NTHR LOAD MEMTOT MEMUSE SWAPTO SWAPUS
110129
----------------------------------------------------------------------------------------------

pkg/qhost/v9.0/parsers_test.go

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ global - - - - - - - -
1616
master lx-amd64 4 1 4 4 0.31 15.6G 422.9M 1.5G 0.0
1717
exec lx-amd64 4 1 4 4 0.31 15.6G 422.9M 1.5G 0.0
1818
`
19+
20+
sampleWithUnknownHost := `
21+
HOSTNAME ARCH NCPU NSOC NCOR NTHR LOAD MEMTOT MEMUSE SWAPTO SWAPUS
22+
----------------------------------------------------------------------------------------------
23+
global - - - - - - - - - -
24+
valid lx-amd64 4 1 4 4 0.31 15.6G 422.9M 1.5G 0.0
25+
unknown lx-amd64 14 - - - - 7.7G - 1.5G -
26+
`
27+
1928
Context("ParseQhostOutput", func() {
2029

2130
It("should return error if output is invalid", func() {
@@ -46,6 +55,35 @@ exec lx-amd64 4 1 4 4 0.31 15.6G 422.9M
4655
Expect(hosts[1].SWAPUS).To(Equal(int64(0.0)))
4756
})
4857

58+
It("should parse without issues when host is unknown", func() {
59+
hosts, err := qhost.ParseHosts(sampleWithUnknownHost)
60+
Expect(err).To(BeNil())
61+
Expect(hosts).To(HaveLen(2))
62+
Expect(hosts[0].Name).To(Equal("valid"))
63+
Expect(hosts[0].Arch).To(Equal("lx-amd64"))
64+
Expect(hosts[0].NCPU).To(Equal(4))
65+
Expect(hosts[0].NSOC).To(Equal(1))
66+
Expect(hosts[0].NCOR).To(Equal(4))
67+
Expect(hosts[0].NTHR).To(Equal(4))
68+
Expect(hosts[0].LOAD).To(Equal(0.31))
69+
Expect(hosts[0].MEMTOT).To(Equal(int64(156 * 1024 * 1024 * 1024 / 10)))
70+
Expect(hosts[0].MEMUSE).To(Equal(int64(4229 * 1024 * 1024 / 10)))
71+
Expect(hosts[0].SWAPTO).To(Equal(int64(1.5 * 1024 * 1024 * 1024)))
72+
Expect(hosts[0].SWAPUS).To(Equal(int64(0.0)))
73+
Expect(hosts[1].Name).To(Equal("unknown"))
74+
Expect(hosts[1].Arch).To(Equal("lx-amd64"))
75+
Expect(hosts[1].NCPU).To(Equal(14))
76+
Expect(hosts[1].NSOC).To(Equal(0))
77+
Expect(hosts[1].NCOR).To(Equal(0))
78+
Expect(hosts[1].NTHR).To(Equal(0))
79+
Expect(hosts[1].LOAD).To(Equal(0.0))
80+
Expect(hosts[1].MEMTOT).To(Equal(int64(77 * 1024 * 1024 * 1024 / 10)))
81+
Expect(hosts[1].MEMUSE).To(Equal(int64(0)))
82+
Expect(hosts[1].SWAPTO).To(Equal(int64(1.5 * 1024 * 1024 * 1024)))
83+
Expect(hosts[1].SWAPUS).To(Equal(int64(0.0)))
84+
85+
})
86+
4987
})
5088

5189
Context("ParseHostFullMetrics", func() {
@@ -435,6 +473,9 @@ master lx-amd64 14 1 14 14 1.50 7.7G 2.0G 1
435473
hl:np_load_short=0.119286
436474
hl:np_load_medium=0.107143
437475
hl:np_load_long=0.078571
476+
unknown lx-amd64 14 - - - - 7.7G - 1024.0M -
477+
hl:arch=lx-amd64
478+
hl:num_proc=14.000000
438479
`
439480

440481
It("should return error if output is invalid", func() {
@@ -455,20 +496,12 @@ master lx-amd64 14 1 14 14 1.50 7.7G 2.0G 1
455496
It("should parse host full metrics with global host values", func() {
456497
hosts, err := qhost.ParseHostFullMetrics(qhostFOutput2)
457498
Expect(err).To(BeNil())
458-
Expect(hosts).To(HaveLen(2))
499+
Expect(hosts).To(HaveLen(3))
459500
Expect(hosts[0].Name).To(Equal("global"))
460501
Expect(hosts[1].Name).To(Equal("master"))
461-
Expect(len(hosts[0].Resources)).To(Equal(1))
462-
Expect(hosts[0].Resources["testc"]).To(Equal(
463-
qhost.ResourceAvailability{
464-
Name: "testc",
465-
StringValue: "100000.000000",
466-
FloatValue: 100000.000000,
467-
ResourceAvailabilityLimitedBy: "g",
468-
Source: "c",
469-
FullString: "gc:testc=100000.000000",
470-
},
471-
))
502+
Expect(hosts[2].Name).To(Equal("unknown"))
503+
Expect(len(hosts[2].Resources)).To(Equal(0))
504+
Expect(hosts[2].MemUsed).To(Equal(int64(0)))
472505
})
473506

474507
})

0 commit comments

Comments
 (0)