mongo-replica-initializer/systemcheck.go

53 lines
1.1 KiB
Go

package main
import (
"fmt"
"os"
"os/user"
"github.com/go-ini/ini"
"github.com/shirou/gopsutil/v3/mem"
)
func check_root() {
currentUser, err := user.Current()
if err != nil {
fmt.Println("Error getting current user:", err)
os.Exit(1)
}
if currentUser.Uid != "1000" {
fmt.Println("You must be root to run this script")
os.Exit(1)
}
}
func check_settings() {
if numberOfContainers < 1 && numberOfContainers > 7 {
fmt.Println("Number of containers must be between 1 and 7")
os.Exit(2)
}
}
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 {
OSInfo := ReadOSRelease("/etc/os-release")
OSRelease := OSInfo["ID"]
return OSRelease
}
func get_memory() string {
system_mem, _ := mem.VirtualMemory()
mem_gb := fmt.Sprintf("%.2f", (float64(system_mem.Total)/1073741824)*float64(maxMemAllocPercent/(numberOfContainers*100)))
return mem_gb
}