diff --git a/installer/sample/.gitignore b/installer/sample/.gitignore new file mode 100644 index 000000000..afed0735d --- /dev/null +++ b/installer/sample/.gitignore @@ -0,0 +1 @@ +*.csv diff --git a/installer/sample/sample.go b/installer/sample/sample.go new file mode 100644 index 000000000..de187f263 --- /dev/null +++ b/installer/sample/sample.go @@ -0,0 +1,85 @@ +package main + +import ( + "encoding/csv" + "fmt" + "log" + "os" + "sort" + "strconv" + + "github.com/utmstack/UTMStack/installer/types" + "github.com/utmstack/UTMStack/installer/utils" +) + +func main() { + // Total memory values in MB. + totals := []int{16000, 20000, 32000, 48000, 64000, 120000, 200000} + + // Create a sorted copy of the services (sorted by Priority then Name). + sortedServices := make([]utils.ServiceConfig, len(types.Services)) + copy(sortedServices, types.Services) + sort.Slice(sortedServices, func(i, j int) bool { + if sortedServices[i].Priority == sortedServices[j].Priority { + return sortedServices[i].Name < sortedServices[j].Name + } + return sortedServices[i].Priority < sortedServices[j].Priority + }) + + // Create the output CSV file. + file, err := os.Create("output.csv") + if err != nil { + log.Fatalf("Error creating CSV file: %v", err) + } + defer file.Close() + + // Create a CSV writer. + writer := csv.NewWriter(file) + defer writer.Flush() + + // Iterate over each total memory value. + for _, total := range totals { + // Write a comment line indicating the total memory. + comment := fmt.Sprintf("# Total Memory: %d MB", total) + // Since csv.Writer does not support comment lines, + // write the comment directly to the file. + if _, err := file.WriteString(comment + "\n"); err != nil { + log.Fatalf("Error writing comment: %v", err) + } + + // Write the header row. + if err := writer.Write([]string{"Service", "AssignedMemory"}); err != nil { + log.Fatalf("Error writing header: %v", err) + } + + // Balance memory using the utils package. + rsrcs, err := utils.BalanceMemory(types.Services, total) + if err != nil { + log.Fatalf("Error balancing memory for total %d MB: %v", total, err) + } + + // Write each service's allocated memory. + for _, svc := range sortedServices { + // Assume that rsrcs[svc.Name] returns a pointer to a ServiceConfig, + // and that AssignedMemory is an integer field. + assigned := rsrcs[svc.Name].AssignedMemory + row := []string{svc.Name, strconv.Itoa(assigned)} + if err := writer.Write(row); err != nil { + log.Fatalf("Error writing row: %v", err) + } + } + + // Flush the CSV writer so far. + writer.Flush() + if err := writer.Error(); err != nil { + log.Fatalf("Error flushing CSV writer: %v", err) + } + + // Write an empty line to separate tables. + if _, err := file.WriteString("\n"); err != nil { + log.Fatalf("Error writing empty line: %v", err) + } + } + + log.Println("CSV file 'output.csv' created successfully") +} diff --git a/installer/types/stack.go b/installer/types/stack.go index a3393e8ed..490f3cfc2 100644 --- a/installer/types/stack.go +++ b/installer/types/stack.go @@ -21,6 +21,26 @@ type StackConfig struct { ShmFolder string } +var Services = []utils.ServiceConfig{ + {Name: "correlation", Priority: 1, MinMemory: 3 * 1024, MaxMemory: 60 * 1024}, + {Name: "logstash", Priority: 1, MinMemory: 2700, MaxMemory: 60 * 1024}, + {Name: "opensearch", Priority: 1, MinMemory: 4500, MaxMemory: 60 * 1024}, + {Name: "log-auth-proxy", Priority: 1, MinMemory: 1 * 1024, MaxMemory: 4 * 1024}, + {Name: "backend", Priority: 2, MinMemory: 700, MaxMemory: 8 * 1024}, + {Name: "web-pdf", Priority: 2, MinMemory: 1024, MaxMemory: 2 * 1024}, + {Name: "postgres", Priority: 2, MinMemory: 500, MaxMemory: 1 * 1024}, + {Name: "user-auditor", Priority: 3, MinMemory: 200, MaxMemory: 1024}, + {Name: "agentmanager", Priority: 3, MinMemory: 200, MaxMemory: 1024}, + {Name: "mutate", Priority: 3, MinMemory: 50, MaxMemory: 1024}, + {Name: "aws", Priority: 3, MinMemory: 50, MaxMemory: 1024}, + {Name: "filebrowser", Priority: 3, MinMemory: 50, MaxMemory: 512}, + {Name: "sophos", Priority: 3, MinMemory: 50, MaxMemory: 1024}, + {Name: "frontend", Priority: 3, MinMemory: 80, MaxMemory: 1024}, + {Name: "socai", Priority: 3, MinMemory: 30, MaxMemory: 512}, + {Name: "bitdefender", Priority: 3, MinMemory: 30, MaxMemory: 100}, + {Name: "office365", Priority: 3, MinMemory: 30, MaxMemory: 100}, +} + func (s *StackConfig) Populate(c *Config) error { cores, err := cpu.Counts(false) if err != nil { @@ -45,29 +65,9 @@ func (s *StackConfig) Populate(c *Config) error { s.LocksDir = utils.MakeDir(0777, c.DataDir, "locks") s.ShmFolder = utils.MakeDir(0777, c.DataDir, "tmpfs") - services := []utils.ServiceConfig{ - {Name: "correlation", Priority: 1, MinMemory: 4 * 1024, MaxMemory: 60 * 1024}, - {Name: "logstash", Priority: 1, MinMemory: 2700, MaxMemory: 60 * 1024}, - {Name: "opensearch", Priority: 1, MinMemory: 4500, MaxMemory: 60 * 1024}, - {Name: "log-auth-proxy", Priority: 1, MinMemory: 128, MaxMemory: 512}, - {Name: "backend", Priority: 2, MinMemory: 700, MaxMemory: 2 * 1024}, - {Name: "web-pdf", Priority: 2, MinMemory: 1024, MaxMemory: 2 * 1024}, - {Name: "postgres", Priority: 2, MinMemory: 500, MaxMemory: 1 * 1024}, - {Name: "user-auditor", Priority: 3, MinMemory: 200, MaxMemory: 1024}, - {Name: "agentmanager", Priority: 3, MinMemory: 200, MaxMemory: 1024}, - {Name: "mutate", Priority: 3, MinMemory: 50, MaxMemory: 1024}, - {Name: "aws", Priority: 3, MinMemory: 50, MaxMemory: 1024}, - {Name: "filebrowser", Priority: 3, MinMemory: 50, MaxMemory: 512}, - {Name: "sophos", Priority: 3, MinMemory: 50, MaxMemory: 1024}, - {Name: "frontend", Priority: 3, MinMemory: 80, MaxMemory: 1024}, - {Name: "socai", Priority: 3, MinMemory: 30, MaxMemory: 1024}, - {Name: "bitdefender", Priority: 3, MinMemory: 30, MaxMemory: 512}, - {Name: "office365", Priority: 3, MinMemory: 30, MaxMemory: 512}, - } - - total := int(mem.Total/1024/1024) - utils.SYSTEM_RESERVED_MEMORY + total := int(mem.Total / 1024 / 1024) - rsrcs, err := utils.BalanceMemory(services, total) + rsrcs, err := utils.BalanceMemory(Services, total) if err != nil { return err }