@@ -2,7 +2,6 @@ package ovnmonitor
22
33import (
44 "fmt"
5- "os"
65 "os/exec"
76 "strconv"
87 "strings"
@@ -11,6 +10,7 @@ import (
1110 "github.com/kubeovn/ovsdb"
1211 "k8s.io/klog/v2"
1312
13+ "github.com/kubeovn/kube-ovn/pkg/ovs"
1414 "github.com/kubeovn/kube-ovn/pkg/ovsdb/ovnnb"
1515 "github.com/kubeovn/kube-ovn/pkg/ovsdb/ovnsb"
1616)
@@ -26,46 +26,34 @@ func (e *Exporter) getOvnStatus() map[string]int {
2626 result := make (map [string ]int )
2727
2828 // get ovn-northbound status
29- cmd := exec .Command ("ovn-appctl" , "-t" , "/var/run/ovn/ovnnb_db.ctl" , "cluster/status" , ovnnb .DatabaseName ) // #nosec G204
30- output , err := cmd .CombinedOutput ()
29+ output , err := ovs .OvnDatabaseControl (ovnnb .DatabaseName , "cluster/status" , ovnnb .DatabaseName )
3130 if err != nil {
3231 klog .Errorf ("get ovn-northbound status failed, err %v" , err )
3332 result ["ovsdb-server-northbound" ] = 0
3433 }
35- result ["ovsdb-server-northbound" ] = parseDbStatus (string ( output ) )
34+ result ["ovsdb-server-northbound" ] = parseDbStatus (output )
3635
3736 // get ovn-southbound status
38- cmd = exec .Command ("ovn-appctl" , "-t" , "/var/run/ovn/ovnsb_db.ctl" , "cluster/status" , ovnsb .DatabaseName ) // #nosec G204
39- output , err = cmd .CombinedOutput ()
37+ output , err = ovs .OvnDatabaseControl (ovnsb .DatabaseName , "cluster/status" , ovnsb .DatabaseName )
4038 if err != nil {
4139 klog .Errorf ("get ovn-southbound status failed, err %v" , err )
4240 result ["ovsdb-server-southbound" ] = 0
4341 }
44- result ["ovsdb-server-southbound" ] = parseDbStatus (string ( output ) )
42+ result ["ovsdb-server-southbound" ] = parseDbStatus (output )
4543
4644 // get ovn-northd status
47- pid , err := os .ReadFile (e .Client .Service .Northd .File .Pid .Path )
48- if err != nil {
49- klog .Errorf ("read ovn-northd pid failed, err %v" , err )
45+ if output , err = ovs .Appctl ("ovn-northd" , "status" ); err != nil {
46+ klog .Errorf ("get ovn-northd status failed, err %v" , err )
47+ result ["ovn-northd" ] = 0
48+ } else if len (strings .Split (output , ":" )) != 2 {
5049 result ["ovn-northd" ] = 0
5150 } else {
52- // #nosec G204
53- cmd := exec .Command ("ovn-appctl" , "-t" , fmt .Sprintf ("/var/run/ovn/ovn-northd.%s.ctl" , strings .Trim (string (pid ), "\n " )), "status" )
54- output , err := cmd .CombinedOutput ()
55- if err != nil {
56- klog .Errorf ("get ovn-northd status failed, err %v" , err )
57- result ["ovn-northd" ] = 0
58- }
59- if len (strings .Split (string (output ), ":" )) != 2 {
60- result ["ovn-northd" ] = 0
61- } else {
62- status := strings .TrimSpace (strings .Split (string (output ), ":" )[1 ])
63- switch status {
64- case "standby" :
65- result ["ovn-northd" ] = 2
66- case "active" :
67- result ["ovn-northd" ] = 1
68- }
51+ status := strings .TrimSpace (strings .Split (output , ":" )[1 ])
52+ switch status {
53+ case "standby" :
54+ result ["ovn-northd" ] = 2
55+ case "active" :
56+ result ["ovn-northd" ] = 1
6957 }
7058 }
7159
@@ -76,24 +64,22 @@ func (e *Exporter) getOvnStatusContent() map[string]string {
7664 result := map [string ]string {"ovsdb-server-northbound" : "" , "ovsdb-server-southbound" : "" }
7765
7866 // get ovn-northbound status
79- cmd := exec .Command ("ovn-appctl" , "-t" , "/var/run/ovn/ovnnb_db.ctl" , "cluster/status" , ovnnb .DatabaseName ) // #nosec G204
80- output , err := cmd .CombinedOutput ()
67+ output , err := ovs .OvnDatabaseControl (ovnnb .DatabaseName , "cluster/status" , ovnnb .DatabaseName )
8168 if err != nil {
8269 klog .Errorf ("get ovn-northbound status failed, err %v" , err )
8370 }
84- if strings .Contains (string ( output ) , "Servers:" ) {
85- servers := strings .Split (string ( output ) , "Servers:" )[1 ]
71+ if strings .Contains (output , "Servers:" ) {
72+ servers := strings .Split (output , "Servers:" )[1 ]
8673 result ["ovsdb-server-northbound" ] = servers
8774 }
8875
8976 // get ovn-southbound status
90- cmd = exec .Command ("ovn-appctl" , "-t" , "/var/run/ovn/ovnsb_db.ctl" , "cluster/status" , ovnsb .DatabaseName ) // #nosec G204
91- output , err = cmd .CombinedOutput ()
77+ output , err = ovs .OvnDatabaseControl (ovnsb .DatabaseName , "cluster/status" , ovnsb .DatabaseName )
9278 if err != nil {
9379 klog .Errorf ("get ovn-southbound status failed, err %v" , err )
9480 }
95- if strings .Contains (string ( output ) , "Servers:" ) {
96- servers := strings .Split (string ( output ) , "Servers:" )[1 ]
81+ if strings .Contains (output , "Servers:" ) {
82+ servers := strings .Split (output , "Servers:" )[1 ]
9783 result ["ovsdb-server-southbound" ] = servers
9884 }
9985
@@ -174,15 +160,14 @@ func (e *Exporter) setLogicalSwitchPortInfoMetric() {
174160 }
175161}
176162
177- func getClusterInfo (direction , dbName string ) (* OVNDBClusterStatus , error ) {
178- cmd := exec .Command ("ovn-appctl" , "-t" , fmt .Sprintf ("/var/run/ovn/ovn%s_db.ctl" , direction ), "cluster/status" , dbName ) // #nosec G204
179- output , err := cmd .CombinedOutput ()
163+ func getClusterInfo (dbName string ) (* OVNDBClusterStatus , error ) {
164+ output , err := ovs .OvnDatabaseControl (dbName , "cluster/status" , dbName )
180165 if err != nil {
181166 return nil , fmt .Errorf ("failed to retrieve cluster/status info for database %s: %w" , dbName , err )
182167 }
183168
184169 clusterStatus := & OVNDBClusterStatus {}
185- for line := range strings .SplitSeq (string ( output ) , "\n " ) {
170+ for line := range strings .SplitSeq (output , "\n " ) {
186171 idx := strings .Index (line , ":" )
187172 if idx == - 1 {
188173 continue
@@ -305,35 +290,21 @@ func parseDbStatus(output string) int {
305290}
306291
307292func getDBStatus (dbName string ) (bool , error ) {
308- var cmd * exec.Cmd
309- var result bool
310- switch dbName {
311- case ovnnb .DatabaseName :
312- cmd = exec .Command ("ovn-appctl" , "-t" , "/var/run/ovn/ovnnb_db.ctl" , "ovsdb-server/get-db-storage-status" , dbName ) // #nosec G204
313- case ovnsb .DatabaseName :
314- cmd = exec .Command ("ovn-appctl" , "-t" , "/var/run/ovn/ovnsb_db.ctl" , "ovsdb-server/get-db-storage-status" , dbName ) // #nosec G204
315- default :
316- return false , fmt .Errorf ("unknown db name %s" , dbName )
317- }
318-
319- output , err := cmd .CombinedOutput ()
293+ output , err := ovs .OvnDatabaseControl (dbName , "ovsdb-server/get-db-storage-status" , dbName )
320294 if err != nil {
321295 klog .Errorf ("get %s status failed, err %v" , dbName , err )
322296 return false , err
323297 }
324- lines := strings .SplitSeq (string (output ), "\n " )
325- for line := range lines {
298+ for line := range strings .SplitSeq (output , "\n " ) {
326299 if strings .Contains (line , "status: ok" ) {
327- result = true
328- break
300+ return true , nil
329301 }
330302 if strings .Contains (line , "ovsdb error" ) {
331- result = false
332- break
303+ return false , nil
333304 }
334305 }
335306
336- return result , nil
307+ return false , nil
337308}
338309
339310func resetLogicalSwitchMetrics () {
0 commit comments