pkg.re/essentialkaos/ek.10@v12.41.0+incompatible/system/examples_test.go (about)

     1  //go:build linux
     2  // +build linux
     3  
     4  package system
     5  
     6  // ////////////////////////////////////////////////////////////////////////////////// //
     7  //                                                                                    //
     8  //                         Copyright (c) 2022 ESSENTIAL KAOS                          //
     9  //      Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0>     //
    10  //                                                                                    //
    11  // ////////////////////////////////////////////////////////////////////////////////// //
    12  
    13  import (
    14  	"fmt"
    15  	"time"
    16  )
    17  
    18  // ////////////////////////////////////////////////////////////////////////////////// //
    19  
    20  func ExampleGetFSUsage() {
    21  	fsInfo, err := GetFSUsage()
    22  
    23  	if err != nil {
    24  		return
    25  	}
    26  
    27  	// info is slice path -> info
    28  	for path, info := range fsInfo {
    29  		fmt.Printf(
    30  			"Path: %s Type: %s Device: %s Used: %d Free: %d Total: %d\n",
    31  			path, info.Type, info.Device, info.Used, info.Free, info.Total,
    32  		)
    33  	}
    34  }
    35  
    36  func ExampleGetIOStats() {
    37  	ioStats, err := GetIOStats()
    38  
    39  	if err != nil {
    40  		return
    41  	}
    42  
    43  	// print info for each device
    44  	for device, info := range ioStats {
    45  		fmt.Printf("Device: %s", device)
    46  		fmt.Printf(
    47  			"  ReadComplete: %d ReadMerged: %d ReadSectors: %d ReadMs: %d\n",
    48  			info.ReadComplete, info.ReadMerged, info.ReadSectors, info.ReadMs,
    49  		)
    50  
    51  		fmt.Printf(
    52  			"  WriteComplete: %d WriteMerged: %d WriteSectors: %d WriteMs: %d\n",
    53  			info.WriteComplete, info.WriteMerged, info.WriteSectors, info.WriteMs,
    54  		)
    55  
    56  		fmt.Printf(
    57  			"  IOPending: %d IOMs: %d IOQueueMs: %d\n\n",
    58  			info.IOPending, info.IOMs, info.IOQueueMs,
    59  		)
    60  	}
    61  }
    62  
    63  func ExampleGetIOUtil() {
    64  	// get 5 sec IO utilization
    65  	ioUtil, err := GetIOUtil(5 * time.Second)
    66  
    67  	if err != nil {
    68  		return
    69  	}
    70  
    71  	// print utilization for each device
    72  	for device, utilization := range ioUtil {
    73  		fmt.Printf("Device: %s Utilization: %g\n", device, utilization)
    74  	}
    75  }
    76  
    77  func ExampleGetNetworkSpeed() {
    78  	input, output, err := GetNetworkSpeed(5 * time.Second)
    79  
    80  	if err != nil {
    81  		return
    82  	}
    83  
    84  	// print input and output speed for all interfaces
    85  	fmt.Printf("Input: %d kb/s\n Output: %d kb/s\n", input/1024, output/1024)
    86  }
    87  
    88  func ExampleGetUptime() {
    89  	uptime, err := GetUptime()
    90  
    91  	if err != nil {
    92  		return
    93  	}
    94  
    95  	// print uptime
    96  	fmt.Printf("Uptime: %d seconds\n", uptime)
    97  }
    98  
    99  func ExampleGetLA() {
   100  	la, err := GetLA()
   101  
   102  	if err != nil {
   103  		return
   104  	}
   105  
   106  	// print 1, 5 and 15 min load average
   107  	fmt.Printf("Min1: %g Min5: %g Min15: %g\n", la.Min1, la.Min5, la.Min15)
   108  }
   109  
   110  func ExampleGetMemUsage() {
   111  	usage, err := GetMemUsage()
   112  
   113  	if err != nil {
   114  		return
   115  	}
   116  
   117  	// print all available memory info
   118  	fmt.Printf("MemTotal: %d\n", usage.MemTotal)
   119  	fmt.Printf("MemFree: %d\n", usage.MemFree)
   120  	fmt.Printf("MemUsed: %d\n", usage.MemUsed)
   121  	fmt.Printf("Buffers: %d\n", usage.Buffers)
   122  	fmt.Printf("Cached: %d\n", usage.Cached)
   123  	fmt.Printf("Active: %d\n", usage.Active)
   124  	fmt.Printf("Inactive: %d\n", usage.Inactive)
   125  	fmt.Printf("SwapTotal: %d\n", usage.SwapTotal)
   126  	fmt.Printf("SwapFree: %d\n", usage.SwapFree)
   127  	fmt.Printf("SwapUsed: %d\n", usage.SwapUsed)
   128  	fmt.Printf("SwapCached: %d\n", usage.SwapCached)
   129  	fmt.Printf("Dirty: %d\n", usage.Dirty)
   130  	fmt.Printf("Slab: %d\n", usage.Slab)
   131  }
   132  
   133  func ExampleGetCPUUsage() {
   134  	usage, err := GetCPUUsage(time.Minute)
   135  
   136  	if err != nil {
   137  		return
   138  	}
   139  
   140  	// print all available CPU info
   141  	fmt.Printf("User: %f\n", usage.User)
   142  	fmt.Printf("System: %f\n", usage.System)
   143  	fmt.Printf("Nice: %f\n", usage.Nice)
   144  	fmt.Printf("Idle: %f\n", usage.Idle)
   145  	fmt.Printf("Wait: %f\n", usage.Wait)
   146  	fmt.Printf("Average: %f\n", usage.Average)
   147  	fmt.Printf("CPU Count: %d\n", usage.Count)
   148  }
   149  
   150  func ExampleGetSystemInfo() {
   151  	sysInfo, err := GetSystemInfo()
   152  
   153  	if err != nil {
   154  		return
   155  	}
   156  
   157  	// print all available system info
   158  	fmt.Printf("Hostname: %s\n", sysInfo.Hostname)
   159  	fmt.Printf("OS: %s\n", sysInfo.OS)
   160  	fmt.Printf("Kernel: %s\n", sysInfo.Kernel)
   161  	fmt.Printf("Arch: %s\n", sysInfo.Arch)
   162  }