Files
HY2XS_flamy/apps/util/linux.go
T

116 lines
2.5 KiB
Go

package util
import (
"errors"
"fmt"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/mem"
"github.com/sirupsen/logrus"
"net"
"os"
"os/exec"
"strconv"
"time"
)
func Exec(cmd string) (string, error) {
command := exec.Command("bash", "-c", cmd)
command.Env = os.Environ()
output, err := command.CombinedOutput()
if err != nil {
logrus.Errorf("execute command failed cmd: %s err: %v", cmd, err)
return "", fmt.Errorf("execute command failed cmd: %s", cmd)
}
return string(output), nil
}
func Systemctl(action string, unit string) error {
_, err := Exec(fmt.Sprintf("systemctl %s %s", action, unit))
return err
}
func IsPortAvailable(port uint, network string) bool {
if network == "tcp" {
listener, err := net.ListenTCP(network, &net.TCPAddr{
IP: net.IPv4(0, 0, 0, 0),
Port: int(port),
})
defer func() {
if listener != nil {
listener.Close()
}
}()
if err != nil {
logrus.Errorf("port %d is taken err: %s", port, err)
return false
}
}
if network == "udp" {
listener, err := net.ListenUDP("udp", &net.UDPAddr{
IP: net.IPv4(0, 0, 0, 0),
Port: int(port),
})
defer func() {
if listener != nil {
listener.Close()
}
}()
if err != nil {
logrus.Errorf("port %d is taken err: %s", port, err)
return false
}
}
return true
}
func GetCpuPercent() (float64, error) {
var err error
percent, err := cpu.Percent(time.Second, false)
value, err := strconv.ParseFloat(fmt.Sprintf("%.1f", percent[0]), 64)
return value, err
}
func GetMemPercent() (float64, error) {
var err error
memInfo, err := mem.VirtualMemory()
value, err := strconv.ParseFloat(fmt.Sprintf("%.1f", memInfo.UsedPercent), 64)
return value, err
}
func GetMemInfo() (*mem.VirtualMemoryStat, error) {
return mem.VirtualMemory()
}
func GetDiskPercent() (float64, error) {
var err error
parts, err := disk.Partitions(true)
diskInfo, err := disk.Usage(parts[0].Mountpoint)
value, err := strconv.ParseFloat(fmt.Sprintf("%.1f", diskInfo.UsedPercent), 64)
return value, err
}
func GetDiskInfo() (*disk.UsageStat, error) {
parts, err := disk.Partitions(true)
if err != nil {
return nil, err
}
if len(parts) == 0 {
return nil, errors.New("disk partition not found")
}
return disk.Usage(parts[0].Mountpoint)
}
func VerifyPort(port string) error {
if port != "" {
value, err := strconv.ParseInt(port, 10, 64)
if err != nil {
return errors.New("invalid port value")
}
if value <= 0 || value > 65535 {
return errors.New("the port range is between 0-65535")
}
}
return nil
}