github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/systemConfig/systemconfig.go (about)

     1  package systemconfig
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	"github.com/shirou/gopsutil/v3/cpu"
     8  	"github.com/shirou/gopsutil/v3/disk"
     9  	"github.com/shirou/gopsutil/v3/host"
    10  	"github.com/shirou/gopsutil/v3/mem"
    11  	"github.com/valyala/fasthttp"
    12  
    13  	"github.com/siglens/siglens/pkg/utils"
    14  	log "github.com/sirupsen/logrus"
    15  
    16  	"math"
    17  	"time"
    18  )
    19  
    20  type SystemInfo struct {
    21  	OS     string     `json:"os"`
    22  	VCPU   int        `json:"v_cpu"`
    23  	Memory MemoryInfo `json:"memory"`
    24  	Disk   DiskInfo   `json:"disk"`
    25  	Uptime int        `json:"uptime"`
    26  }
    27  
    28  type MemoryInfo struct {
    29  	Total       uint64  `json:"total"`
    30  	Free        uint64  `json:"free"`
    31  	UsedPercent float64 `json:"used_percent"`
    32  }
    33  
    34  type DiskInfo struct {
    35  	Total       uint64  `json:"total"`
    36  	Free        uint64  `json:"free"`
    37  	UsedPercent float64 `json:"used_percent"`
    38  }
    39  
    40  func GetSystemInfo(ctx *fasthttp.RequestCtx) {
    41  	cpuInfo, err := cpu.Info()
    42  	if err != nil {
    43  		ctx.SetStatusCode(fasthttp.StatusInternalServerError)
    44  		log.Errorf("GetSystemInfo: Failed to retrieve CPU info: %v", err)
    45  		return
    46  	}
    47  
    48  	var totalCores int
    49  	for _, info := range cpuInfo {
    50  		totalCores += int(info.Cores)
    51  	}
    52  
    53  	memInfo, err := mem.VirtualMemory()
    54  	if err != nil {
    55  		ctx.SetStatusCode(fasthttp.StatusInternalServerError)
    56  		log.Errorf("GetSystemInfo: Failed to retrieve memory info: %v", err)
    57  		return
    58  	}
    59  
    60  	diskInfo, err := disk.Usage("/")
    61  	if err != nil {
    62  		ctx.SetStatusCode(fasthttp.StatusInternalServerError)
    63  		log.Errorf("GetSystemInfo: Failed to retrieve disk info: %v", err)
    64  		return
    65  	}
    66  
    67  	hostInfo, err := host.Info()
    68  	if err != nil {
    69  		ctx.SetStatusCode(fasthttp.StatusInternalServerError)
    70  		log.Errorf("GetSystemInfo: Failed to retrieve host info: %v", err)
    71  		return
    72  	}
    73  
    74  	uptime := math.Round(time.Since(utils.GetServerStartTime()).Minutes())
    75  
    76  	systemInfo := SystemInfo{
    77  		OS:   hostInfo.OS,
    78  		VCPU: totalCores,
    79  		Memory: MemoryInfo{
    80  			Total:       memInfo.Total,
    81  			Free:        memInfo.Free,
    82  			UsedPercent: memInfo.UsedPercent,
    83  		},
    84  		Disk: DiskInfo{
    85  			Total:       diskInfo.Total,
    86  			Free:        diskInfo.Free,
    87  			UsedPercent: diskInfo.UsedPercent,
    88  		},
    89  		Uptime: int(uptime),
    90  	}
    91  
    92  	response, err := json.Marshal(systemInfo)
    93  	if err != nil {
    94  		ctx.SetStatusCode(fasthttp.StatusInternalServerError)
    95  		fmt.Fprintf(ctx, "Failed to marshal system info: %v", err)
    96  		return
    97  	}
    98  
    99  	ctx.SetContentType("application/json")
   100  	ctx.SetBody(response)
   101  }