@@ -10,6 +10,9 @@ import (
1010
1111 "github.com/kubeovn/ovsdb"
1212 "k8s.io/klog/v2"
13+
14+ "github.com/kubeovn/kube-ovn/pkg/ovsdb/ovnnb"
15+ "github.com/kubeovn/kube-ovn/pkg/ovsdb/ovnsb"
1316)
1417
1518// IncrementErrorCounter increases the counter of failed queries to OVN server.
@@ -23,8 +26,7 @@ func (e *Exporter) getOvnStatus() map[string]int {
2326 result := make (map [string ]int )
2427
2528 // get ovn-northbound status
26- cmdstr := "ovn-appctl -t /var/run/ovn/ovnnb_db.ctl cluster/status OVN_Northbound"
27- cmd := exec .Command ("sh" , "-c" , cmdstr )
29+ cmd := exec .Command ("ovn-appctl" , "-t" , "/var/run/ovn/ovnnb_db.ctl" , "cluster/status" , ovnnb .DatabaseName ) // #nosec G204
2830 output , err := cmd .CombinedOutput ()
2931 if err != nil {
3032 klog .Errorf ("get ovn-northbound status failed, err %v" , err )
@@ -33,8 +35,7 @@ func (e *Exporter) getOvnStatus() map[string]int {
3335 result ["ovsdb-server-northbound" ] = parseDbStatus (string (output ))
3436
3537 // get ovn-southbound status
36- cmdstr = "ovn-appctl -t /var/run/ovn/ovnsb_db.ctl cluster/status OVN_Southbound"
37- cmd = exec .Command ("sh" , "-c" , cmdstr )
38+ cmd = exec .Command ("ovn-appctl" , "-t" , "/var/run/ovn/ovnsb_db.ctl" , "cluster/status" , ovnsb .DatabaseName ) // #nosec G204
3839 output , err = cmd .CombinedOutput ()
3940 if err != nil {
4041 klog .Errorf ("get ovn-southbound status failed, err %v" , err )
@@ -48,9 +49,8 @@ func (e *Exporter) getOvnStatus() map[string]int {
4849 klog .Errorf ("read ovn-northd pid failed, err %v" , err )
4950 result ["ovn-northd" ] = 0
5051 } else {
51- cmdstr := fmt .Sprintf ("ovn-appctl -t /var/run/ovn/ovn-northd.%s.ctl status" , strings .Trim (string (pid ), "\n " ))
52- klog .V (3 ).Infof ("cmd is %v" , cmdstr )
53- cmd := exec .Command ("sh" , "-c" , cmdstr ) // #nosec G204
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" )
5454 output , err := cmd .CombinedOutput ()
5555 if err != nil {
5656 klog .Errorf ("get ovn-northd status failed, err %v" , err )
@@ -76,8 +76,7 @@ func (e *Exporter) getOvnStatusContent() map[string]string {
7676 result := map [string ]string {"ovsdb-server-northbound" : "" , "ovsdb-server-southbound" : "" }
7777
7878 // get ovn-northbound status
79- cmdstr := "ovn-appctl -t /var/run/ovn/ovnnb_db.ctl cluster/status OVN_Northbound"
80- cmd := exec .Command ("sh" , "-c" , cmdstr )
79+ cmd := exec .Command ("ovn-appctl" , "-t" , "/var/run/ovn/ovnnb_db.ctl" , "cluster/status" , ovnnb .DatabaseName ) // #nosec G204
8180 output , err := cmd .CombinedOutput ()
8281 if err != nil {
8382 klog .Errorf ("get ovn-northbound status failed, err %v" , err )
@@ -88,8 +87,7 @@ func (e *Exporter) getOvnStatusContent() map[string]string {
8887 }
8988
9089 // get ovn-southbound status
91- cmdstr = "ovn-appctl -t /var/run/ovn/ovnsb_db.ctl cluster/status OVN_Southbound"
92- cmd = exec .Command ("sh" , "-c" , cmdstr )
90+ cmd = exec .Command ("ovn-appctl" , "-t" , "/var/run/ovn/ovnsb_db.ctl" , "cluster/status" , ovnsb .DatabaseName ) // #nosec G204
9391 output , err = cmd .CombinedOutput ()
9492 if err != nil {
9593 klog .Errorf ("get ovn-southbound status failed, err %v" , err )
@@ -103,8 +101,7 @@ func (e *Exporter) getOvnStatusContent() map[string]string {
103101}
104102
105103func getClusterEnableState (dbName string ) (bool , error ) {
106- cmdstr := "ovsdb-tool db-is-clustered " + dbName
107- cmd := exec .Command ("sh" , "-c" , cmdstr ) // #nosec G204
104+ cmd := exec .Command ("ovsdb-tool" , "db-is-clustered" , dbName ) // #nosec G204
108105 _ , err := cmd .CombinedOutput ()
109106 if err != nil {
110107 klog .Error (err )
@@ -116,7 +113,7 @@ func getClusterEnableState(dbName string) (bool, error) {
116113func (e * Exporter ) setLogicalSwitchInfoMetric () {
117114 lsws , err := e .Client .GetLogicalSwitches ()
118115 if err != nil {
119- klog .Errorf ("%s : %v" , e . Client . Database . Southbound . Name , err )
116+ klog .Errorf ("failed to get logical switches : %v" , err )
120117 e .IncrementErrorCounter ()
121118 } else {
122119 for _ , lsw := range lsws {
@@ -178,16 +175,13 @@ func (e *Exporter) setLogicalSwitchPortInfoMetric() {
178175}
179176
180177func getClusterInfo (direction , dbName string ) (* OVNDBClusterStatus , error ) {
181- clusterStatus := & OVNDBClusterStatus {}
182- var err error
183-
184- cmdstr := fmt .Sprintf ("ovn-appctl -t /var/run/ovn/ovn%s_db.ctl cluster/status %s" , direction , dbName )
185- cmd := exec .Command ("sh" , "-c" , cmdstr ) // #nosec G204
178+ cmd := exec .Command ("ovn-appctl" , "-t" , fmt .Sprintf ("/var/run/ovn/ovn%s_db.ctl" , direction ), "cluster/status" , dbName ) // #nosec G204
186179 output , err := cmd .CombinedOutput ()
187180 if err != nil {
188181 return nil , fmt .Errorf ("failed to retrieve cluster/status info for database %s: %w" , dbName , err )
189182 }
190183
184+ clusterStatus := & OVNDBClusterStatus {}
191185 for line := range strings .SplitSeq (string (output ), "\n " ) {
192186 idx := strings .Index (line , ":" )
193187 if idx == - 1 {
@@ -311,16 +305,17 @@ func parseDbStatus(output string) int {
311305}
312306
313307func getDBStatus (dbName string ) (bool , error ) {
314- var cmdstr string
308+ var cmd * exec. Cmd
315309 var result bool
316310 switch dbName {
317- case "OVN_Northbound" :
318- cmdstr = "ovn-appctl -t /var/run/ovn/ovnnb_db.ctl ovsdb-server/get-db-storage-status " + dbName
319- case "OVN_Southbound" :
320- cmdstr = "ovn-appctl -t /var/run/ovn/ovnsb_db.ctl ovsdb-server/get-db-storage-status " + 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 )
321317 }
322318
323- cmd := exec .Command ("sh" , "-c" , cmdstr ) // #nosec G204
324319 output , err := cmd .CombinedOutput ()
325320 if err != nil {
326321 klog .Errorf ("get %s status failed, err %v" , dbName , err )
0 commit comments