-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stats.Disk.pas
105 lines (92 loc) · 2.63 KB
/
Stats.Disk.pas
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
unit Stats.Disk;
interface
uses
System.Classes, System.SysUtils, Linux.Utils, System.JSON.Serializers,
Stats.Config, Stats.Types;
type
TDiskInfo = class
private
[JSONName('Name')]
FName: string;
[JSONName('MountPoint')]
FMount: string;
[JSONName('%Used')]
FPerUsed: Double;
[JSONName('TotalSize')]
FSize: Double;
[JSONName('Free')]
FFree: Double;
procedure GetMount(APath : String);
public
property Name : string read FName;
property Mount: string read FMount;
property Size : Double read FSize;
property FreeSpace : Double read FFree;
property PercUsed : Double read FPerUsed;
constructor Create;
end;
implementation
{ TDiskInfo }
constructor TDiskInfo.Create;
var
Config : TStatsConfig;
begin
Config := TStatsConfig.Create;
try
Config := TStatsConfig.LoadFromJSON(TLinuxUtils.GetApplicationPath + TStatsConfig.ConfigFileName,seUTF8);
GetMount(Config.DatabaseMountPoint);
finally
Config.Free;
end;
end;
procedure TDiskInfo.GetMount(APath : String);
var
Cmd: String;
Usado : Double;
CmdResult : TStringList;
teste : String;
begin
try
Cmd := 'df -m ' + APath;
CmdResult := TLinuxUtils.RunCommandLine(Cmd);
try
if CmdResult.Count > 2 then
begin
if Assigned(CmdResult) then
CmdResult.Free;
Cmd := 'df -m ' + APath + ' | sed "1 d" | grep -iv "^Filesystem|Sys."';
CmdResult := TLinuxUtils.RunCommandLine(Cmd);
try
FName := CmdResult[0].Trim;
finally
CmdResult.Free;
end;
Cmd :='df -m / | sed "1 d" | grep -iv "^Filesystem|Sys." | sort | head -1 | awk ''{print ". "$5" "$1" "$3}''';
CmdResult := TLinuxUtils.RunCommandLine(Cmd);
end else begin
if Assigned(CmdResult) then
CmdResult.Free;
Cmd := 'df -m ' + APath + ' | awk END{print} | awk ''{print $1" "$6" "$2" "$4}''';
CmdResult := TLinuxUtils.RunCommandLine(Cmd);
FName := CmdResult.Text.Split([' '])[0];
end;
teste := CmdResult.Text;
FMount := CmdResult.Text.Split([' '])[1];
FSize := StrToFloatDef(CmdResult.Text.Split([' '])[2],0);
FFree := TLinuxUtils.OnlyNumber(CmdResult.Text.Split([' '])[3]);
Usado := FSize - FFree;
FPerUsed := Round((Usado / FSize)*100);
finally
if Assigned(CmdResult) then
CmdResult.Free;
end;
except
on E:Exception do
begin
{$IFDEF DEBUG}
TLinuxUtils.LogError('TDiskInfo.GetMount - ' + E.Message);
{$ENDIF}
end;
end;
end;
end.