pkg.re/essentialkaos/ek.10@v12.41.0+incompatible/system/info.go (about) 1 // Package system provides methods for working with system data (metrics/users) 2 package system 3 4 // ////////////////////////////////////////////////////////////////////////////////// // 5 // // 6 // Copyright (c) 2022 ESSENTIAL KAOS // 7 // Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> // 8 // // 9 // ////////////////////////////////////////////////////////////////////////////////// // 10 11 import ( 12 "bufio" 13 "os" 14 "strconv" 15 ) 16 17 // ////////////////////////////////////////////////////////////////////////////////// // 18 19 // OS names 20 const ( 21 LINUX_ARCH = "Arch" 22 LINUX_CENTOS = "CentOS" 23 LINUX_DEBIAN = "Debian" 24 LINUX_FEDORA = "Fedora" 25 LINUX_GENTOO = "Gentoo" 26 LINUX_RHEL = "RHEL" 27 LINUX_SUSE = "SuSe" 28 LINUX_OPEN_SUSE = "openSUSE" 29 LINUX_UBUNTU = "Ubuntu" 30 DARWIN_OSX = "OSX" 31 ) 32 33 // ////////////////////////////////////////////////////////////////////////////////// // 34 35 // LoadAvg contains information about average system load 36 type LoadAvg struct { 37 Min1 float64 `json:"min1"` // LA in last 1 minute 38 Min5 float64 `json:"min5"` // LA in last 5 minutes 39 Min15 float64 `json:"min15"` // LA in last 15 minutes 40 RProc int `json:"rproc"` // Number of currently runnable kernel scheduling entities 41 TProc int `json:"tproc"` // Number of kernel scheduling entities that currently exist on the system 42 } 43 44 // MemUsage contains info about system memory usage 45 type MemUsage struct { 46 MemTotal uint64 `json:"total"` // Total usable ram (i.e. physical ram minus a few reserved bits and the kernel binary code) 47 MemFree uint64 `json:"free"` // The sum of MemFree - (Buffers + Cached) 48 MemUsed uint64 `json:"used"` // MemTotal - MemFree 49 Buffers uint64 `json:"buffers"` // Relatively temporary storage for raw disk blocks shouldn't get tremendously large (20MB or so) 50 Cached uint64 `json:"cached"` // In-memory cache for files read from the disk (the pagecache). Doesn't include SwapCached 51 Active uint64 `json:"active"` // Memory that has been used more recently and usually not reclaimed unless absolutely necessary 52 Inactive uint64 `json:"inactive"` // Memory which has been less recently used. It is more eligible to be reclaimed for other purposes 53 SwapTotal uint64 `json:"swap_total"` // Total amount of swap space available 54 SwapFree uint64 `json:"swap_free"` // Memory which has been evicted from RAM, and is temporarily on the disk still also is in the swapfile 55 SwapUsed uint64 `json:"swap_used"` // SwapTotal - SwapFree 56 SwapCached uint64 `json:"swap_cached"` // Memory that once was swapped out, is swapped back in but 57 Dirty uint64 `json:"dirty"` // Memory which is waiting to get written back to the disk 58 Shmem uint64 `json:"shmem"` // Total used shared memory 59 Slab uint64 `json:"slab"` // In-kernel data structures cache 60 SReclaimable uint64 `json:"sreclaimable"` // The part of the Slab that might be reclaimed (such as caches) 61 } 62 63 // CPUUsage contains info about CPU usage 64 type CPUUsage struct { 65 User float64 `json:"user"` // Normal processes executing in user mode 66 System float64 `json:"system"` // Processes executing in kernel mode 67 Nice float64 `json:"nice"` // Niced processes executing in user mode 68 Idle float64 `json:"idle"` // Twiddling thumbs 69 Wait float64 `json:"wait"` // Waiting for I/O to complete 70 Average float64 `json:"average"` // Average CPU usage 71 Count int `json:"count"` // Number of CPU cores 72 } 73 74 // CPUInfo contains info about CPU 75 type CPUInfo struct { 76 Vendor string `json:"vendor"` // Processor vandor name 77 Model string `json:"model"` // Common name of the processor 78 Cores int `json:"cores"` // Number of cores 79 Siblings int `json:"siblings"` // Total number of sibling CPUs on the same physical CPU 80 CacheSize uint64 `json:"cache_size"` // Amount of level 2 memory cache available to the processor 81 Speed []float64 `json:"speed"` // Speed in megahertz for the processor 82 } 83 84 // CPUStats contains basic CPU stats 85 type CPUStats struct { 86 User uint64 `json:"user"` 87 Nice uint64 `json:"nice"` 88 System uint64 `json:"system"` 89 Idle uint64 `json:"idle"` 90 Wait uint64 `json:"wait"` 91 IRQ uint64 `json:"irq"` 92 SRQ uint64 `json:"srq"` 93 Steal uint64 `json:"steal"` 94 Total uint64 `json:"total"` 95 Count int `json:"count"` 96 } 97 98 // CPUCount contains info about number of CPU 99 type CPUCount struct { 100 Possible uint32 `json:"possible"` 101 Present uint32 `json:"present"` 102 Online uint32 `json:"online"` 103 Offline uint32 `json:"offline"` 104 } 105 106 // FSUsage contains info about FS usage 107 type FSUsage struct { 108 Type string `json:"type"` // FS type (ext4/ntfs/etc...) 109 Device string `json:"device"` // Device spec 110 Used uint64 `json:"used"` // Used space 111 Free uint64 `json:"free"` // Free space 112 Total uint64 `json:"total"` // Total space 113 IOStats *IOStats `json:"iostats"` // IO statistics 114 } 115 116 // IOStats contains information about I/O 117 type IOStats struct { 118 ReadComplete uint64 `json:"read_complete"` // Reads completed successfully 119 ReadMerged uint64 `json:"read_merged"` // Reads merged 120 ReadSectors uint64 `json:"read_sectors"` // Sectors read 121 ReadMs uint64 `json:"read_ms"` // Time spent reading (ms) 122 WriteComplete uint64 `json:"write_complete"` // Writes completed 123 WriteMerged uint64 `json:"write_merged"` // Writes merged 124 WriteSectors uint64 `json:"write_sectors"` // Sectors written 125 WriteMs uint64 `json:"write_ms"` // Time spent writing (ms) 126 IOPending uint64 `json:"io_pending"` // I/Os currently in progress 127 IOMs uint64 `json:"io_ms"` // Time spent doing I/Os (ms) 128 IOQueueMs uint64 `json:"io_queue_ms"` // Weighted time spent doing I/Os (ms) 129 } 130 131 // InterfaceStats contains stats about network interfaces usage 132 type InterfaceStats struct { 133 ReceivedBytes uint64 `json:"received_bytes"` 134 ReceivedPackets uint64 `json:"received_packets"` 135 TransmittedBytes uint64 `json:"transmitted_bytes"` 136 TransmittedPackets uint64 `json:"transmitted_packets"` 137 } 138 139 // SystemInfo contains info about a system (hostname, OS, arch...) 140 type SystemInfo struct { 141 Hostname string `json:"hostname"` // Hostname 142 OS string `json:"os"` // OS name 143 Distribution string `json:"distribution"` // OS distribution 144 Version string `json:"version"` // OS version 145 Kernel string `json:"kernel"` // Kernel version 146 Arch string `json:"arch"` // System architecture (i386/i686/x86_64/etc...) 147 ArchBits int `json:"arch_bits"` // Architecture bits (32/64) 148 } 149 150 // OSInfo contains info about OS 151 type OSInfo struct { 152 Name string `json:"name"` 153 PrettyName string `json:"pretty_name"` 154 Version string `json:"version"` 155 VersionID string `json:"version_id"` 156 VersionCodename string `json:"version_codename"` 157 ID string `json:"id"` 158 IDLike string `json:"id_like"` 159 HomeURL string `json:"home_url"` 160 BugReportURL string `json:"bugreport_url"` 161 SupportURL string `json:"support_url"` 162 } 163 164 // ////////////////////////////////////////////////////////////////////////////////// // 165 166 // getFileScanner opens file and creates scanner for reading text files line by line 167 func getFileScanner(file string) (*bufio.Scanner, func() error, error) { 168 fd, err := os.OpenFile(file, os.O_RDONLY, 0) 169 170 if err != nil { 171 return nil, nil, err 172 } 173 174 r := bufio.NewReader(fd) 175 s := bufio.NewScanner(r) 176 177 return s, fd.Close, nil 178 } 179 180 // parseSize parse size in kB 181 func parseSize(v string) (uint64, error) { 182 size, err := strconv.ParseUint(v, 10, 64) 183 184 if err != nil { 185 return 0, err 186 } 187 188 return size * 1024, nil 189 }