1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
// SPDX-License-Identifier: BSD-3-Clause
//go:build aix
package host
import (
"context"
"errors"
"strconv"
"strings"
"github.com/shirou/gopsutil/v4/internal/common"
)
// from https://www.ibm.com/docs/en/aix/7.2?topic=files-utmph-file
const (
user_PROCESS = 7
)
func HostIDWithContext(ctx context.Context) (string, error) {
out, err := invoke.CommandWithContext(ctx, "uname", "-u")
if err != nil {
return "", err
}
// The command always returns an extra newline, so we make use of Split() to get only the first line
return strings.Split(string(out[:]), "\n")[0], nil
}
func numProcs(ctx context.Context) (uint64, error) {
return 0, common.ErrNotImplementedError
}
func BootTimeWithContext(ctx context.Context) (btime uint64, err error) {
ut, err := UptimeWithContext(ctx)
if err != nil {
return 0, err
}
if ut <= 0 {
return 0, errors.New("Uptime was not set, so cannot calculate boot time from it.")
}
ut = ut * 60
return timeSince(ut), nil
}
// Parses result from uptime into minutes
// Some examples of uptime output that this command handles:
// 11:54AM up 13 mins, 1 user, load average: 2.78, 2.62, 1.79
// 12:41PM up 1 hr, 1 user, load average: 2.47, 2.85, 2.83
// 07:43PM up 5 hrs, 1 user, load average: 3.27, 2.91, 2.72
// 11:18:23 up 83 days, 18:29, 4 users, load average: 0.16, 0.03, 0.01
// 08:47PM up 2 days, 20 hrs, 1 user, load average: 2.47, 2.17, 2.17
// 01:16AM up 4 days, 29 mins, 1 user, load average: 2.29, 2.31, 2.21
func UptimeWithContext(ctx context.Context) (uint64, error) {
out, err := invoke.CommandWithContext(ctx, "uptime")
if err != nil {
return 0, err
}
return parseUptime(string(out[:])), nil
}
func parseUptime(uptime string) uint64 {
ut := strings.Fields(uptime)
var days, hours, mins uint64
var err error
switch {
case ut[3] == "day," || ut[3] == "days,":
days, err = strconv.ParseUint(ut[2], 10, 64)
if err != nil {
return 0
}
// day provided along with a single hour or hours
// ie: up 2 days, 20 hrs,
if ut[5] == "hr," || ut[5] == "hrs," {
hours, err = strconv.ParseUint(ut[4], 10, 64)
if err != nil {
return 0
}
}
// mins provided along with a single min or mins
// ie: up 4 days, 29 mins,
if ut[5] == "min," || ut[5] == "mins," {
mins, err = strconv.ParseUint(ut[4], 10, 64)
if err != nil {
return 0
}
}
// alternatively day provided with hh:mm
// ie: up 83 days, 18:29
if strings.Contains(ut[4], ":") {
hm := strings.Split(ut[4], ":")
hours, err = strconv.ParseUint(hm[0], 10, 64)
if err != nil {
return 0
}
mins, err = strconv.ParseUint(strings.Trim(hm[1], ","), 10, 64)
if err != nil {
return 0
}
}
case ut[3] == "hr," || ut[3] == "hrs,":
hours, err = strconv.ParseUint(ut[2], 10, 64)
if err != nil {
return 0
}
case ut[3] == "mins," || ut[3] == "mins,":
mins, err = strconv.ParseUint(ut[2], 10, 64)
if err != nil {
return 0
}
}
return (days * 24 * 60) + (hours * 60) + mins
}
// This is a weak implementation due to the limitations on retrieving this data in AIX
func UsersWithContext(ctx context.Context) ([]UserStat, error) {
var ret []UserStat
out, err := invoke.CommandWithContext(ctx, "w")
if err != nil {
return nil, err
}
lines := strings.Split(string(out), "\n")
if len(lines) < 3 {
return []UserStat{}, common.ErrNotImplementedError
}
hf := strings.Fields(lines[1]) // headers
for l := 2; l < len(lines); l++ {
v := strings.Fields(lines[l]) // values
us := &UserStat{}
for i, header := range hf {
// We're done in any of these use cases
if i >= len(v) || v[0] == "-" {
break
}
if t, err := strconv.ParseFloat(v[i], 64); err == nil {
switch header {
case `User`:
us.User = strconv.FormatFloat(t, 'f', 1, 64)
case `tty`:
us.Terminal = strconv.FormatFloat(t, 'f', 1, 64)
}
}
}
// Valid User data, so append it
ret = append(ret, *us)
}
return ret, nil
}
// Much of this function could be static. However, to be future proofed, I've made it call the OS for the information in all instances.
func PlatformInformationWithContext(ctx context.Context) (platform string, family string, version string, err error) {
// Set the platform (which should always, and only be, "AIX") from `uname -s`
out, err := invoke.CommandWithContext(ctx, "uname", "-s")
if err != nil {
return "", "", "", err
}
platform = strings.TrimRight(string(out[:]), "\n")
// Set the family
family = strings.TrimRight(string(out[:]), "\n")
// Set the version
out, err = invoke.CommandWithContext(ctx, "oslevel")
if err != nil {
return "", "", "", err
}
version = strings.TrimRight(string(out[:]), "\n")
return platform, family, version, nil
}
func KernelVersionWithContext(ctx context.Context) (version string, err error) {
out, err := invoke.CommandWithContext(ctx, "oslevel", "-s")
if err != nil {
return "", err
}
version = strings.TrimRight(string(out[:]), "\n")
return version, nil
}
func KernelArch() (arch string, err error) {
out, err := invoke.Command("bootinfo", "-y")
if err != nil {
return "", err
}
arch = strings.TrimRight(string(out[:]), "\n")
return arch, nil
}
func VirtualizationWithContext(ctx context.Context) (string, string, error) {
return "", "", common.ErrNotImplementedError
}
|