Skip to content
This repository has been archived by the owner on May 30, 2022. It is now read-only.

Commit

Permalink
Merge pull request #854 from arbulu89/hotfix/associate-attached-datab…
Browse files Browse the repository at this point in the history
…ase-ip-resolution

Associate attached database properly when the database name is resolved
  • Loading branch information
arbulu89 authored Mar 8, 2022
2 parents 47bca06 + 969c93d commit 3375ccb
Show file tree
Hide file tree
Showing 33 changed files with 116 additions and 34 deletions.
36 changes: 35 additions & 1 deletion internal/sapsystem/sapsystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"fmt"
"io/ioutil"
"net"
"os"
"os/exec"
"path"
Expand Down Expand Up @@ -63,6 +64,8 @@ type SAPSystem struct {
Instances map[string]*SAPInstance `mapstructure:"instances,omitempty"`
// Only for Database type
Databases []*DatabaseData `mapstructure:"databases,omitempty"`
// Only for Application type
DBAddress string `mapstructure:"db_address,omitempty"`
}

// The value is interface{} as some of the entries in the SAP profiles files and commands
Expand Down Expand Up @@ -202,13 +205,21 @@ func NewSAPSystem(fs afero.Fs, sysPath string) (*SAPSystem, error) {
system.Instances[instance.Name] = instance
}

if system.Type == Database {
switch system.Type {
case Database:
databaseList, err := getDatabases(fs, system.SID)
if err != nil {
log.Printf("Error getting the database list: %s", err)
} else {
system.Databases = databaseList
}
case Application:
addr, err := getDBAddress(system)
if err != nil {
log.Printf("Error getting the database address: %s", err)
} else {
system.DBAddress = addr
}
}

system, err = setSystemId(fs, system)
Expand Down Expand Up @@ -291,6 +302,29 @@ func getProfileData(fs afero.Fs, profilePath string) (map[string]interface{}, er
return configMap, nil
}

func getDBAddress(system *SAPSystem) (string, error) {
sapdbhost, found := system.Profile["SAPDBHOST"]
if !found {
return "", fmt.Errorf("SAPDBHOST field not found in the SAP profile")
}

addrList, err := net.LookupIP(sapdbhost.(string))
if err != nil {
return "", fmt.Errorf("could not resolve \"%s\" hostname", sapdbhost)
}

// Get 1st IPv4 address
for _, addr := range addrList {
addrStr := addr.String()
ip := net.ParseIP(addrStr)
if ip.To4() != nil {
return addrStr, nil
}
}

return "", fmt.Errorf("could not get any IPv4 address")
}

func setSystemId(fs afero.Fs, system *SAPSystem) (*SAPSystem, error) {
// Set system ID
var err error
Expand Down
13 changes: 13 additions & 0 deletions internal/sapsystem/sapsystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,19 @@ ERR:::
assert.ElementsMatch(t, expectedDbs, dbs)
}

func TestGetDBAddress(t *testing.T) {
s := &SAPSystem{Profile: SAPProfile{"SAPDBHOST": "localhost"}}
addr, err := getDBAddress(s)
assert.NoError(t, err)
assert.Equal(t, "127.0.0.1", addr)
}

func TestGetDBAddress_ResolveError(t *testing.T) {
s := &SAPSystem{Profile: SAPProfile{"SAPDBHOST": "other"}}
_, err := getDBAddress(s)
assert.EqualError(t, err, "could not resolve \"other\" hostname")
}

func TestNewSAPInstanceDatabase(t *testing.T) {
mockWebService := new(sapControlMocks.WebService)
mockCommand := new(sapSystemMocks.CustomCommand)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
"Id": "d01fdc69aeba7bd5133b210eb2884853",
"SID": "NWQ",
"Type": 2,
"DBAddress": "10.90.1.13",
"Profile": {
"SAPDBHOST": "10.90.1.13",
"SAPDBHOST": "ha-cluster.suse.de",
"dbms/name": "HDQ",
"dbms/type": "hdb",
"gw/acl_mode": "1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"Id": "97a1e70aeff3c0685d65c4c3d32d533b",
"SID": "NWP",
"Type": 2,
"DBAddress": "10.80.1.13",
"Profile": {
"SAPDBHOST": "10.80.1.13",
"dbms/name": "HDP",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"Id": "97a1e70aeff3c0685d65c4c3d32d533b",
"SID": "NWP",
"Type": 2,
"DBAddress": "10.80.1.13",
"Profile": {
"SAPDBHOST": "10.80.1.13",
"dbms/name": "HDP",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"Id": "a1e80e3e152a903662f7882fb3f8a851",
"SID": "NWD",
"Type": 2,
"DBAddress": "10.100.1.13",
"Profile": {
"SAPDBHOST": "10.100.1.13",
"dbms/name": "HDD",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
"Id": "d01fdc69aeba7bd5133b210eb2884853",
"SID": "NWQ",
"Type": 2,
"DBAddress": "10.90.1.13",
"Profile": {
"SAPDBHOST": "10.90.1.13",
"SAPDBHOST": "ha-cluster.suse.de",
"dbms/name": "HDQ",
"dbms/type": "hdb",
"gw/acl_mode": "1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
"Id": "d01fdc69aeba7bd5133b210eb2884853",
"SID": "NWQ",
"Type": 2,
"DBAddress": "10.90.1.13",
"Profile": {
"SAPDBHOST": "10.90.1.13",
"SAPDBHOST": "ha-cluster.suse.de",
"dbms/name": "HDQ",
"dbms/type": "hdb",
"gw/acl_mode": "1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"Id": "97a1e70aeff3c0685d65c4c3d32d533b",
"SID": "NWP",
"Type": 2,
"DBAddress": "10.80.1.13",
"Profile": {
"SAPDBHOST": "10.80.1.13",
"dbms/name": "HDP",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"Id": "a1e80e3e152a903662f7882fb3f8a851",
"SID": "NWD",
"Type": 2,
"DBAddress": "10.100.1.13",
"Profile": {
"SAPDBHOST": "10.100.1.13",
"dbms/name": "HDD",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
"Id": "d01fdc69aeba7bd5133b210eb2884853",
"SID": "NWQ",
"Type": 2,
"DBAddress": "10.90.1.13",
"Profile": {
"SAPDBHOST": "10.90.1.13",
"SAPDBHOST": "ha-cluster.suse.de",
"dbms/name": "HDQ",
"dbms/type": "hdb",
"gw/acl_mode": "1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"Id": "a1e80e3e152a903662f7882fb3f8a851",
"SID": "NWD",
"Type": 2,
"DBAddress": "10.100.1.13",
"Profile": {
"SAPDBHOST": "10.100.1.13",
"dbms/name": "HDD",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"Id": "97a1e70aeff3c0685d65c4c3d32d533b",
"SID": "NWP",
"Type": 2,
"DBAddress": "10.80.1.13",
"Profile": {
"SAPDBHOST": "10.80.1.13",
"dbms/name": "HDP",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"Id": "a1e80e3e152a903662f7882fb3f8a851",
"SID": "NWD",
"Type": 2,
"DBAddress": "10.100.1.13",
"Profile": {
"SAPDBHOST": "10.100.1.13",
"dbms/name": "HDD",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"Id": "a1e80e3e152a903662f7882fb3f8a851",
"SID": "NWD",
"Type": 2,
"DBAddress": "10.100.1.13",
"Profile": {
"SAPDBHOST": "10.100.1.13",
"dbms/name": "HDD",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"Id": "a1e80e3e152a903662f7882fb3f8a851",
"SID": "NWD",
"Type": 2,
"DBAddress": "10.100.1.13",
"Profile": {
"SAPDBHOST": "10.100.1.13",
"dbms/name": "HDD",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"Id": "a1e80e3e152a903662f7882fb3f8a851",
"SID": "NWD",
"Type": 2,
"DBAddress": "10.100.1.13",
"Profile": {
"SAPDBHOST": "10.100.1.13",
"dbms/name": "HDD",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"Id": "a1e80e3e152a903662f7882fb3f8a851",
"SID": "NWD",
"Type": 2,
"DBAddress": "10.100.1.13",
"Profile": {
"SAPDBHOST": "10.100.1.13",
"dbms/name": "HDD",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"Id": "a1e80e3e152a903662f7882fb3f8a851",
"SID": "NWD",
"Type": 2,
"DBAddress": "10.100.1.13",
"Profile": {
"SAPDBHOST": "10.100.1.13",
"dbms/name": "HDD",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"Id": "a1e80e3e152a903662f7882fb3f8a851",
"SID": "NWD",
"Type": 2,
"DBAddress": "10.100.1.13",
"Profile": {
"SAPDBHOST": "10.100.1.13",
"dbms/name": "HDD",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"Id": "a1e80e3e152a903662f7882fb3f8a851",
"SID": "NWD",
"Type": 2,
"DBAddress": "10.100.1.13",
"Profile": {
"SAPDBHOST": "10.100.1.13",
"dbms/name": "HDD",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"Id": "a1e80e3e152a903662f7882fb3f8a851",
"SID": "NWD",
"Type": 2,
"DBAddress": "10.100.1.13",
"Profile": {
"SAPDBHOST": "10.100.1.13",
"dbms/name": "HDD",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export const availableSAPSystems = [
sid: 'HDQ',
id: '9953878f07bb54cac20d5d5d7ff08af2',
tenant: 'HDQ',
dbAddress: '10.90.1.13',
dbAddress: 'ha-cluster.suse.de',
},
instances: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"Id": "7b65dc281f9fae2c8e68e6cab669993e",
"SID": "HA1",
"Type": 2,
"DBAddress": "10.74.1.12",
"Profile": {
"SAPDBHOST": "10.74.1.12",
"gw/acl_mode": "1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"Id": "e06e328f8d6b0f46c1e66ffcd44d0dd7",
"SID": "PRD",
"Type": 1,
"DBAddress": "",
"Profile": {
"SAPGLOBALHOST": "vmhana01",
"SAPSYSTEMNAME": "PRD",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"Id": "7b65dc281f9fae2c8e68e6cab669993e",
"SID": "HA1",
"Type": 2,
"DBAddress": "10.74.1.12",
"Profile": {
"SAPDBHOST": "10.74.1.12",
"gw/acl_mode": "1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"Id": "e06e328f8d6b0f46c1e66ffcd44d0dd7",
"SID": "PRD",
"Type": 1,
"DBAddress": "",
"Profile": {
"SAPGLOBALHOST": "vmhana01",
"SAPSYSTEMNAME": "PRD",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"Type": 3,
"Profile": {},
"Databases": null,
"DBAddress": "",
"Instances": {
"SMDA98": {
"Host": "vmhana01",
Expand Down
20 changes: 11 additions & 9 deletions web/datapipeline/sap_systems_projector.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func SAPSystemsProjector_SAPSystemsDiscoveryHandler(dataCollectedEvent *DataColl
}

for _, s := range discoveredSAPSystems {
var sapSystemType, dbHost, dbName string
var sapSystemType, dbHost, dbName, dbAddress string
var tenants []string

switch s.Type {
Expand All @@ -52,6 +52,7 @@ func SAPSystemsProjector_SAPSystemsDiscoveryHandler(dataCollectedEvent *DataColl
if hdb, ok := s.Profile["dbs/hdb/dbname"]; ok {
dbName = hdb.(string)
}
dbAddress = s.DBAddress
case 3:
log.Infof("SAP diagnostics agent with %s identifier found. Skipping projection", s.SID)
continue
Expand All @@ -60,13 +61,14 @@ func SAPSystemsProjector_SAPSystemsDiscoveryHandler(dataCollectedEvent *DataColl
var instances []entities.SAPSystemInstance
for _, i := range s.Instances {
instance := entities.SAPSystemInstance{
AgentID: dataCollectedEvent.AgentID,
ID: s.Id,
SID: s.SID,
Type: sapSystemType,
Tenants: tenants,
DBHost: dbHost,
DBName: dbName,
AgentID: dataCollectedEvent.AgentID,
ID: s.Id,
SID: s.SID,
Type: sapSystemType,
Tenants: tenants,
DBHost: dbHost,
DBName: dbName,
DBAddress: dbAddress,
}

var features string
Expand Down Expand Up @@ -100,7 +102,7 @@ func SAPSystemsProjector_SAPSystemsDiscoveryHandler(dataCollectedEvent *DataColl
"id", "sid", "type", "features", "instance_number",
"system_replication", "system_replication_status",
"sap_hostname", "start_priority", "http_port", "https_port", "status",
"tenants", "db_host", "db_name")
"tenants", "db_host", "db_name", "db_address")
if err != nil {
return err
}
Expand Down
14 changes: 8 additions & 6 deletions web/entities/sap_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type SAPSystemInstance struct {
SystemReplicationStatus string
DBHost string
DBName string
DBAddress string
Tenants pq.StringArray `gorm:"type:text[]"`
Host *Host `gorm:"foreignKey:AgentID"`
UpdatedAt time.Time
Expand Down Expand Up @@ -52,12 +53,13 @@ func (s SAPSystemInstances) ToModel() []*models.SAPSystem {
}

sapSystem = &models.SAPSystem{
ID: i.ID,
Type: i.Type,
SID: i.SID,
DBName: i.DBName,
DBHost: i.DBHost,
Tags: tags,
ID: i.ID,
Type: i.Type,
SID: i.SID,
DBName: i.DBName,
DBHost: i.DBHost,
DBAddress: i.DBAddress,
Tags: tags,
}
set[i.ID] = sapSystem
}
Expand Down
1 change: 1 addition & 0 deletions web/models/sap_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type SAPSystem struct {
AttachedDatabase *SAPSystem
DBName string
DBHost string
DBAddress string
Health string
Tags []string
// TODO: this is frontend specific, should be removed
Expand Down
Loading

0 comments on commit 3375ccb

Please sign in to comment.