mongo-replica-initializer/systemcheck.go

53 lines
1.1 KiB
Go
Raw Normal View History

2024-05-13 07:55:22 +00:00
package main
import (
"fmt"
"os"
"os/user"
2024-05-13 10:22:53 +00:00
"github.com/go-ini/ini"
"github.com/shirou/gopsutil/v3/mem"
2024-05-13 07:55:22 +00:00
)
func checkRoot() {
2024-05-13 07:55:22 +00:00
currentUser, err := user.Current()
if err != nil {
fmt.Println("Error getting current user:", err)
os.Exit(1)
}
if currentUser.Uid != "0" {
2024-05-13 07:55:22 +00:00
fmt.Println("You must be root to run this script")
2024-05-13 10:22:53 +00:00
os.Exit(1)
}
}
func checkSettings() {
if numberOfContainers < 1 && numberOfContainers > 7 {
2024-05-13 10:22:53 +00:00
fmt.Println("Number of containers must be between 1 and 7")
2024-05-13 07:55:22 +00:00
os.Exit(2)
}
2024-05-13 10:22:53 +00:00
2024-05-13 07:55:22 +00:00
}
2024-05-13 10:22:53 +00:00
func ReadOSRelease(configfile string) map[string]string {
cfg, err := ini.Load(configfile)
if err != nil {
fmt.Println("Fail to read file: ", err)
}
ConfigParams := make(map[string]string)
ConfigParams["ID"] = cfg.Section("").Key("ID").String()
return ConfigParams
}
func getDistro() string {
2024-05-13 10:22:53 +00:00
OSInfo := ReadOSRelease("/etc/os-release")
OSRelease := OSInfo["ID"]
return OSRelease
}
func getMemory() string {
system_mem, _ := mem.VirtualMemory()
mem_gb := fmt.Sprintf("%.2f", (float64(system_mem.Total)/1073741824)*(float64(maxMemAllocPercent)/(float64(numberOfContainers)*100)))
return mem_gb
2024-05-13 07:55:22 +00:00
}