@@ -47,6 +47,10 @@ func BootTimeWithContext(ctx context.Context) (btime uint64, err error) {
47
47
return timeSince (ut ), nil
48
48
}
49
49
50
+ //11:54AM up 13 mins, 1 user, load average: 2.78, 2.62, 1.79
51
+ //12:41PM up 1 hr, 1 user, load average: 2.47, 2.85, 2.83
52
+ //07:43PM up 5 hrs, 1 user, load average: 3.27, 2.91, 2.72
53
+ //11:18:23 up 83 days, 18:29, 4 users, load average: 0.16, 0.03, 0.01
50
54
func UptimeWithContext (ctx context.Context ) (uint64 , error ) {
51
55
out , err := invoke .CommandWithContext (ctx , "uptime" ).Output ()
52
56
if err != nil {
@@ -56,27 +60,53 @@ func UptimeWithContext(ctx context.Context) (uint64, error) {
56
60
// Convert our uptime to a series of fields we can extract
57
61
ut := strings .Fields (string (out [:]))
58
62
59
- // Convert the second field "Days" value to integer and roll it to minutes
60
- days , err := strconv .Atoi (ut [2 ])
61
- if err != nil {
62
- return 0 , err
63
- }
63
+ // Convert the second field value to integer
64
+ var days uint64 = 0
65
+ var hours uint64 = 0
66
+ var minutes uint64 = 0
67
+ if ut [3 ] == "days," {
68
+ days , err = strconv .ParseUint (ut [2 ], 10 , 64 )
69
+ if err != nil {
70
+ return 0 , err
71
+ }
64
72
65
- // Split field 4 into hours and minutes
66
- hm := strings .Split (ut [4 ], ":" )
67
- hours , err := strconv .Atoi (hm [0 ])
68
- if err != nil {
69
- return 0 , err
70
- }
71
- minutes , err := strconv .Atoi (strings .Replace (hm [1 ], "," , "" , - 1 ))
72
- if err != nil {
73
- return 0 , err
73
+ // Split field 4 into hours and minutes
74
+ hm := strings .Split (ut [4 ], ":" )
75
+ hours , err = strconv .ParseUint (hm [0 ], 10 , 64 )
76
+ if err != nil {
77
+ return 0 , err
78
+ }
79
+ minutes , err = strconv .ParseUint (strings .Replace (hm [1 ], "," , "" , - 1 ), 10 , 64 )
80
+ if err != nil {
81
+ return 0 , err
82
+ }
83
+ } else if ut [3 ] == "hr," || ut [3 ] == "hrs," {
84
+ hours , err = strconv .ParseUint (ut [2 ], 10 , 64 )
85
+ if err != nil {
86
+ return 0 , err
87
+ }
88
+ } else if ut [3 ] == "mins," {
89
+ minutes , err = strconv .ParseUint (ut [2 ], 10 , 64 )
90
+ if err != nil {
91
+ return 0 , err
92
+ }
93
+ } else if _ , err := strconv .ParseInt (ut [3 ], 10 , 64 ); err == nil && strings .Contains (ut [2 ], ":" ) {
94
+ // Split field 2 into hours and minutes
95
+ hm := strings .Split (ut [2 ], ":" )
96
+ hours , err = strconv .ParseUint (hm [0 ], 10 , 64 )
97
+ if err != nil {
98
+ return 0 , err
99
+ }
100
+ minutes , err = strconv .ParseUint (strings .Replace (hm [1 ], "," , "" , - 1 ), 10 , 64 )
101
+ if err != nil {
102
+ return 0 , err
103
+ }
74
104
}
75
105
76
106
// Stack them all together as minutes
77
107
total_time := (days * 24 * 60 ) + (hours * 60 ) + minutes
78
108
79
- return uint64 ( total_time ) , nil
109
+ return total_time , nil
80
110
}
81
111
82
112
// This is a weak implementation due to the limitations on retrieving this data in AIX
0 commit comments