获取CPU内存磁盘使用情况
获取CPU内存磁盘使用情况
program hardware; {$APPTYPE CONSOLE} {$R *.res} uses {$IFDEF mswindows} Winapi.Windows, {$ENDIF} {$IFDEF posix} posix.SysStatvfs, {$ENDIF} system.Classes, System.Math, System.SysUtils; var Times: TThread.TSystemTimes; function CPUUsage: string; begin result := 'cpu usage: ' + TThread.GetCPUUsage(Times).ToString + '%'; end; {$IFDEF posix} function meminfo: string; var FS: TFileStream; ch: Char; Line: string; mem, res: TStringList; begin result := ''; FS := TFileStream.Create('/proc/meminfo', fmOpenRead); mem := TStringList.create; res := TStringList.create; try Line := ''; ch := #0; while (FS.Read(ch, 1) = 1) and (ch <> #13) do begin Line := Line + ch; end; mem.text := line; mem.NameValueSeparator := ':'; var total: string := mem.Values['MemTotal']; var available: string := mem.Values['MemAvailable']; var ifree: string := mem.Values['MemFree']; res.Values['MemTotal'] := total; res.Values['MemAvailable'] := available; res.Values['MemFree'] := ifree; result := res.text; finally FS.Free; mem.free; res.free; end; end; {$ENDIF} {$IFDEF posix} function diskinfo: string; var disk: _statvfs; res: tstringlist; begin result := ''; res := tstringlist.Create; try statvfs('/', disk); var available: Single := disk.f_bsize * disk.f_bavail; var capacity: Single := disk.f_bsize * disk.f_blocks; var used: Single := (disk.f_blocks - disk.f_bfree) * disk.f_frsize; res.Values['DiskUsed'] := floattostr(roundto(used / 1024 / 1024, 2)) + ' MB'; res.Values['DiskTotal'] := floattostr(roundto(capacity / 1024 / 1024, 2)) + ' MB'; res.Values['DiskAvailable'] := floattostr(roundto(available / 1024 / 1024, 2)) + ' MB'; result := res.text; finally res.free; end; end; {$ENDIF} {$IFDEF mswindows} function memInfo: string; var info: TMemoryStatus; begin result := 'memory: '; GlobalMemoryStatus(info); result := result + inttostr(info.dwMemoryLoad) + '%'; end; function diskInfo(const disk: string = 'c:'): string; var FreeSpace, TotalSpace: Int64; begin if System.SysUtils.GetDiskFreeSpaceEx(PChar(disk), FreeSpace, TotalSpace, nil) then begin var res: tstringlist := tstringlist.Create; try var t: Single := roundto(TotalSpace / 1024 / 1024, 2); res.Values['DiskTotal'] := t.ToString + ' MB'; t := roundto(FreeSpace / 1024 / 1024, 2); res.Values['DiskFree'] := t.ToString + ' MB'; result := res.Text; finally res.free; end; end; end; {$ENDIF} begin try TThread.GetSystemTimes(Times); while True do begin writeln(CPUUsage); writeln(diskInfo); writeln(meminfo); sleep(5000); end; except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end.
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/14485264.html