Skip to content
Snippets Groups Projects
system_linux.go 2.61 KiB
Newer Older
  • Learn to ignore specific revisions
  • Shrey Garg's avatar
    Shrey Garg committed
    package additions
    
    import (
    
    Shrey Garg's avatar
    Shrey Garg committed
    	"os"
    
    	"os/exec"
    
    Shrey Garg's avatar
    Shrey Garg committed
    	"strings"
    	"syscall"
    
    
    	"github.com/prometheus/procfs"
    	log "github.com/sirupsen/logrus"
    
    Shrey Garg's avatar
    Shrey Garg committed
    )
    
    
    type system struct {
    	// procfs handle
    	pfs procfs.FS
    
    Shrey Garg's avatar
    Shrey Garg committed
    }
    
    
    // NewSystem() Initalizes OS specific interfaces
    // Linux:
    //   - Access to procfs
    func NewSystem() (System, error) {
    	newProcFS, err := procfs.NewFS("/proc")
    
    Shrey Garg's avatar
    Shrey Garg committed
    
    
    	if err != nil {
    		return nil, err
    	}
    
    Shrey Garg's avatar
    Shrey Garg committed
    
    
    	return &system{
    		pfs: newProcFS,
    	}, nil
    
    Shrey Garg's avatar
    Shrey Garg committed
    }
    
    
    func (sys *system) SetHostname(hostname *string) error {
    	// TODO: potentially some safety checks?
    	return syscall.Sethostname([]byte(*hostname))
    
    Shrey Garg's avatar
    Shrey Garg committed
    }
    
    
    func (sys *system) GetFreeMemory() uint64 {
    	memInfo, err := sys.pfs.Meminfo()
    	if err != nil {
    		// TODO: better error handling is required
    		log.Error("GetTotalMemory ", err)
    		return 0
    	}
    	return *memInfo.MemFree
    
    Shrey Garg's avatar
    Shrey Garg committed
    }
    
    
    func (sys *system) GetTotalMemory() uint64 {
    
    	memInfo, err := sys.pfs.Meminfo()
    	if err != nil {
    		// TODO: better error handling is required
    		log.Error("GetTotalMemory ", err)
    		return 0
    	}
    
    	return *memInfo.MemTotal
    
    Shrey Garg's avatar
    Shrey Garg committed
    }
    
    
    func (sys *system) GetUsedMemory() uint64 {
    	memInfo, err := sys.pfs.Meminfo()
    	if err != nil {
    		// TODO: better error handling is required
    		log.Error("GetTotalMemory ", err)
    		return 0
    	}
    
    	return *memInfo.MemTotal - *memInfo.MemFree
    
    Shrey Garg's avatar
    Shrey Garg committed
    }
    
    
    func (sys *system) GetSoftwareVersion() (string, error) {
    
    	var returnString string
    	var osID string
    	var osVersion string
    
    	// open corresponding file with OS version in it
    	// seems to be /etc/os-release on Linux
    	osVersionFile, err := os.Open("/etc/os-release")
    	if err != nil {
    		return "", err
    	}
    
    	fileRdr := bufio.NewScanner((osVersionFile))
    	fileRdr.Split(bufio.ScanLines)
    
    	// go line by line and look for
    	// ID and VERSION_ID entries
    	// this version may result in empty or incomplete information
    	// TODO: better error handling while parsing the file.
    	for fileRdr.Scan() {
    		strElement := strings.FieldsFunc(fileRdr.Text(), func(r rune) bool {
    			if r == '=' {
    				return true
    			} else if r == '"' {
    				return true
    			}
    			return false
    		})
    		if strElement[0] == "ID" {
    			osID = strElement[1]
    		}
    
    		if strElement[0] == "VERSION_ID" {
    			osVersion = strElement[1]
    		}
    	}
    
    	returnString = osID + ":" + osVersion
    
    	return returnString, nil
    
    Shrey Garg's avatar
    Shrey Garg committed
    }
    
    
    	motdMessage, err := os.ReadFile("/etc/motd")
    	if err == nil {
    		return string(motdMessage), nil
    	}
    
    
    	cmd := exec.Command("run-parts", "/etc/update-motd.d/")
    
    	if err != nil {
    		return "", err
    	}
    
    	return string(motdMessage), nil
    }
    
    func (sys *system) SetMotd(message string) error {
    	return os.WriteFile("/etc/motd", []byte(message), 0644)
    }