-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStats.OS.pas
More file actions
171 lines (141 loc) · 4.01 KB
/
Stats.OS.pas
File metadata and controls
171 lines (141 loc) · 4.01 KB
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
unit Stats.OS;
interface
uses
System.Classes, System.SysUtils, Linux.Utils, System.IOUtils, System.DateUtils, System.JSON.Serializers;
type
TOSInfo = class
private
[JsonName('UpTime')]
FUptime: string;
[JsonName('CurrentUsers')]
FCurrentUsers: Integer;
[JsonName('CurrentDatetime')]
FCurrentDateTime: TDateTime;
[JsonName('Kernel')]
FKernel: string;
[JsonName('Hostname')]
FHostname: string;
[JsonName('OS')]
FOS: string;
procedure GetOSName;
procedure GetHostname;
procedure GetKernel;
procedure GetUptime;
procedure GetCurrentUsers;
procedure GetCurrentDateTime;
public
property Hostname : string read FHostname;
property OS : string read FOS;
property Kernel : string read FKernel;
property Uptime : string read FUptime;
property CurrentUsers : Integer read FCurrentUsers;
property CurrentDateTime : TDateTime read FCurrentDateTime;
constructor Create;
end;
implementation
{ TOSInfo }
constructor TOSInfo.Create;
begin
GetOSName;
GetHostname;
GetKernel;
GetUptime;
// GetLastBoot;
GetCurrentUsers;
GetCurrentDateTime;
end;
procedure TOSInfo.GetCurrentDateTime;
var
Cmd, CmdResult : string;
begin
Cmd := 'date ''+%d/%m/%y %T''';
CmdResult := TLinuxUtils.RunCommandLine(Cmd).Text.Trim;
FCurrentDateTime := StrToDateTimeDef(CmdResult,0);
end;
procedure TOSInfo.GetCurrentUsers;
var
Cmd, CmdResult : string;
begin
Cmd := 'who | wc -l';
CmdResult := TLinuxUtils.RunCommandLine(Cmd).Text.Trim;
FCurrentUsers := StrToIntDef(CmdResult,0);
end;
procedure TOSInfo.GetHostname;
var
Cmd, CmdResult : string;
begin
Cmd := 'hostname';
CmdResult := TLinuxUtils.RunCommandLine(Cmd).Text.Trim;
FHostname := CmdResult;
end;
procedure TOSInfo.GetKernel;
var
Cmd : String;
CmdResult : String;
begin
Cmd := 'uname -r';
CmdResult := TLinuxUtils.RunCommandLine(Cmd).Text.Trim;
FKernel := CmdResult;
end;
//procedure TOSInfo.GetLastBoot;
//var
// Cmd : String;
// CmdResult : String;
// Date : TDate;
// Time : TTime;
//begin
// Cmd := 'who -b | awk ''{print $3}''';
// CmdResult := TLinuxUtils.RunCommandLine(Cmd).Text.Trim;
//
// Date := StrToDateDef(cmdResult,0,TFormatSettings.Create('pt-BR'));
//
// Cmd := 'who -b | awk ''{print $4}''';
// CmdResult := TLinuxUtils.RunCommandLine(Cmd).Text.Trim;
//
// Time := StrToTimeDef(CmdResult,0,TFormatSettings.Create('pt-BR'));
//
// FLastBoot := Date + Time;
//end;
procedure TOSInfo.GetOSName;
var
Cmd : String;
CmdResult : String;
begin
Cmd := 'uname -s';
CmdResult := TLinuxUtils.RunCommandLine(Cmd).Text.Trim;
FOS := CmdResult;
if TFile.Exists('/usr/bin/lsb_release') then
Cmd := '/usr/bin/lsb_release -ds'
else if TFile.Exists('/etc/system-release') then
Cmd := 'cat /etc/system-release'
else
Cmd := 'find /etc/*-release -type f -exec cat {} \; | grep NAME | tail -n 1 | cut -d= -f2 | tr -d ''"''';
CmdResult := TLinuxUtils.RunCommandLine(Cmd).Text.Trim;
FOS := FOS + ' ' + CmdResult;
end;
procedure TOSInfo.GetUptime;
var
Cmd, CmdResult : string;
TotalSeconds : Int64;
Days : Integer;
Hours : Integer;
Minutes : Integer;
begin
Cmd := 'cat /proc/uptime | awk ''{print $1}''';
CmdResult := TLinuxUtils.RunCommandLine(Cmd).Text.Trim;
TotalSeconds := Trunc(StrToFloatDef(CmdResult.Replace(FormatSettings.ThousandSeparator,FormatSettings.DecimalSeparator),0));
Minutes := trunc(TotalSeconds/ 60) mod 60;
Hours := trunc(TotalSeconds / 60 / 60) mod 24;
Days := trunc(TotalSeconds / 60 / 60 / 24);
if Days > 0 then
FUptime := Days.ToString + 'dias';
if (Days > 0) and (Hours > 0) then
FUptime := FUptime + ', ';
if Hours > 0 then
FUptime := FUptime + Hours.ToString + ' horas';
if (Days >0) or (Hours > 0) then
FUptime := FUptime + ' e ';
if Minutes > 0 then
FUptime := FUptime + Minutes.ToString + ' minutos';
end;
end.