UtilTools.pas
资源名称:计算机远程监控.rar [点击查看]
上传用户:rickyhu
上传日期:2007-05-27
资源大小:842k
文件大小:11k
源码类别:
控制台编程
开发平台:
Delphi
- ////////////////////////////////////////////////////////////////////////////////
- //
- // 2004 (C) Copyrights Reserved
- // Author:Aureala
- //
- ////////////////////////////////////////////////////////////////////////////////
- unit UtilTools;
- interface
- uses
- Windows, Classes, IniFiles, SysUtils, ComCtrls, StdCtrls,
- ValEdit, Dialogs;
- const
- ConfigFileName='.monsys.sys';
- function IsValidAddr(TmpAddr:String):Boolean;
- function ValueBetween(ValueStr:String;MinValue,MaxValue:Cardinal):Boolean;
- procedure AddClient(Param:String);
- procedure DelClient(Param:String);
- function ExistClient(Param:String):Boolean;
- procedure ShowClients(ListView:TListView);
- procedure UpdateClient(CBClient:TComboBox);
- procedure LoadClientView(VLEClient:TValueListEditor);
- procedure UpdateClientStatus(VLEClient:TValueListEditor;ClientAddr,Status:String);
- procedure SaveConfig(Values:TStrings);
- function LoadConfig():TStrings;
- procedure SaveLog(StrInfo:String);
- procedure LoadLog(FileName,Addr:String;ViewItem:Integer;LVValues:TListView);
- function ExtractStr(const StrOrg,SepChar:String):TStrings;
- var
- ConfigFile:TIniFile;
- implementation
- function IsValidAddr(TmpAddr:String):Boolean;
- var TmpStr:TStringList;
- TmpNum,n:Integer;
- begin
- TmpStr:=TStringList.Create;
- TmpStr.Delimiter:='.';
- TmpStr.DelimitedText:=TmpAddr;
- if(TmpStr.Count<>4) then
- begin
- result:=false;
- exit;
- end;
- try
- n:=0;
- while(n<4) do
- begin
- TmpNum:=StrToInt(TmpStr[n]);
- if(TmpNum>255) or (TmpNum<0) then
- begin
- result:=false;
- exit;
- end;
- n:=n+1;
- end;
- except
- on EConvertError do
- begin
- result:=false;
- exit;
- end;
- end;
- result:=true;
- end;
- function ValueBetween(ValueStr:String;MinValue,MaxValue:Cardinal):Boolean;
- var
- TmpValue:Cardinal;
- begin
- try
- TmpValue:=StrToInt64(ValueStr);
- if((TmpValue<MinValue) or (TmpValue>MaxValue)) then
- result:=false
- else
- result:=true;
- except
- result:=false;
- end;
- end;
- procedure AddClient(Param:String);
- begin
- ConfigFile:=TIniFile.Create(ConfigFileName);
- ConfigFile.WriteString('CLIENT',Param,'');
- ConfigFile.Destroy;
- end;
- procedure DelClient(Param:String);
- begin
- ConfigFile:=TIniFile.Create(ConfigFileName);
- ConfigFile.DeleteKey('CLIENT',Param);
- ConfigFile.Destroy;
- end;
- function ExistClient(Param:String):Boolean;
- var
- StrParam:TStrings;
- Count:Integer;
- begin
- StrParam:=TStringList.Create;
- ConfigFile:=TIniFile.Create(ConfigFileName);
- ConfigFile.ReadSection('CLIENT',StrParam);
- ConfigFile.Destroy;
- Count:=0;
- while(Count<StrParam.Count) do
- begin
- if(StrParam.Strings[Count]=Param) then
- begin
- result:=true;
- exit;
- end;
- Count:=Count+1;
- end;
- result:=false;
- end;
- procedure ShowClients(ListView:TListView);
- var
- StrParam:TStrings;
- Count:Integer;
- begin
- StrParam:=TStringList.Create;
- ConfigFile:=TIniFile.Create(ConfigFileName);
- ConfigFile.ReadSection('CLIENT',StrParam);
- ConfigFile.Destroy;
- ListView.Items.Clear;
- Count:=StrParam.Count-1;
- while(Count>=0) do
- begin
- ListView.Items.Add.SubItems.Add(StrParam.Strings[Count]);
- Count:=Count-1;
- end;
- end;
- procedure UpdateClient(CBClient:TComboBox);
- var
- StrParam:TStrings;
- Count:Integer;
- begin
- StrParam:=TStringList.Create;
- ConfigFile:=TIniFile.Create(ConfigFileName);
- ConfigFile.ReadSection('CLIENT',StrParam);
- ConfigFile.Destroy;
- CBClient.Clear;
- CBClient.Items.Add('请选择计算机');
- Count:=StrParam.Count-1;
- while(Count>=0) do
- begin
- CBClient.Items.Add(StrParam.Strings[Count]);
- Count:=Count-1;
- end;
- CBClient.ItemIndex:=0;
- end;
- procedure LoadClientView(VLEClient:TValueListEditor);
- var
- StrParam:TStrings;
- Count:Integer;
- begin
- StrParam:=TStringList.Create;
- ConfigFile:=TIniFile.Create(ConfigFileName);
- ConfigFile.ReadSection('CLIENT',StrParam);
- ConfigFile.Destroy;
- VLEClient.Strings.Clear;
- VLEClient.Strings.Add('计算机地址=连接');
- Count:=StrParam.Count-1;
- while(Count>=0) do
- begin
- VLEClient.Strings.Add(StrParam.Strings[Count]+'=否');
- Count:=Count-1;
- end;
- end;
- procedure UpdateClientStatus(VLEClient:TValueListEditor;ClientAddr,Status:String);
- var
- Id:Integer;
- begin
- try
- Id:=VLEClient.Strings.IndexOfName(ClientAddr);
- if(Id<0) then
- begin
- VLEClient.Strings.Add(ClientAddr+'='+Status);
- end
- else
- VLEClient.Cells[1,Id]:=Status;
- except
- ;
- end;
- end;
- procedure SaveConfig(Values:TStrings);
- begin
- ConfigFile:=TIniFile.Create(ConfigFileName);
- try
- ConfigFile.WriteString('MONSYSTEM','moncpu',Values.Strings[0]);
- ConfigFile.WriteString('MONSYSTEM','monmem',Values.Strings[1]);
- ConfigFile.WriteString('MONSYSTEM','monproc',Values.Strings[2]);
- ConfigFile.WriteString('MONSYSTEM','monfile',Values.Strings[3]);
- ConfigFile.WriteString('MONSYSTEM','monnet',Values.Strings[4]);
- ConfigFile.WriteString('MONSYSTEM','scaninterval',Values.Strings[5]);
- ConfigFile.WriteString('MONSYSTEM','interval',Values.Strings[6]);
- ConfigFile.WriteString('MONSYSTEM','consolemaxline',Values.Strings[7]);
- ConfigFile.WriteString('MONSYSTEM','savelog',Values.Strings[8]);
- except
- ;
- end;
- ConfigFile.Destroy;
- end;
- function LoadConfig():TStrings;
- var
- StrConfig:TStrings;
- begin
- StrConfig:=TStringList.Create;
- ConfigFile:=TIniFile.Create(ConfigFileName);
- if(ConfigFile.ReadString('MONSYSTEM','moncpu','false')='false') then
- StrConfig.Add('false')
- else
- StrConfig.Add('true');
- if(ConfigFile.ReadString('MONSYSTEM','monmem','false')='false') then
- StrConfig.Add('false')
- else
- StrConfig.Add('true');
- if(ConfigFile.ReadString('MONSYSTEM','monproc','false')='false') then
- StrConfig.Add('false')
- else
- StrConfig.Add('true');
- if(ConfigFile.ReadString('MONSYSTEM','monfile','false')='false') then
- StrConfig.Add('false')
- else
- StrConfig.Add('true');
- if(ConfigFile.ReadString('MONSYSTEM','monnet','false')='false') then
- StrConfig.Add('false')
- else
- StrConfig.Add('true');
- StrConfig.Add(IntToStr(ConfigFile.ReadInteger('MONSYSTEM','scaninterval',1)));
- StrConfig.Add(IntToStr(ConfigFile.ReadInteger('MONSYSTEM','interval',10)));
- StrConfig.Add(IntToStr(ConfigFile.ReadInteger('MONSYSTEM','consolemaxline',100)));
- if(ConfigFile.ReadString('MONSYSTEM','savelog','false')='false') then
- StrConfig.Add('false')
- else
- StrConfig.Add('true');
- result:=StrConfig;
- ConfigFile.Destroy;
- end;
- procedure SaveLog(StrInfo:String);
- var
- LogFileName:string;
- LogFile:TextFile;
- hFile:Cardinal;
- begin
- LogFileName:=FormatDateTime('YYYYMMDDHH',NOW)+'.rec';
- if((not DirectoryExists('.log')) and
- (not CreateDirectory('.log',nil))) then
- exit;
- LogFileName:='.log'+LogFileName;
- if(not FileExists(LogFileName)) then
- begin
- hFile:=CreateFile(PAnsiChar(LogFileName),GENERIC_WRITE+GENERIC_READ,
- FILE_SHARE_READ,nil,CREATE_ALWAYS,FILE_ATTRIBUTE_SYSTEM,0);
- CloseHandle(hFile);
- end;
- AssignFile(LogFile,LogFileName);
- Reset(LogFile);
- try
- Append(LogFile);
- WriteLn(LogFile,StrInfo);
- except
- ;
- end;
- CloseFile(LogFile);
- end;
- procedure LoadLog(FileName,Addr:String;ViewItem:Integer;LVValues:TListView);
- var
- StrData:string;
- LogFile:TextFile;
- StrMsg:TStrings;
- RetId:Integer;
- begin
- if(not FileExists(FileName)) then
- begin
- ShowMessage('没有找到文件');
- exit;
- end;
- LVValues.Items.Clear;
- StrMsg:=TStringList.Create;
- AssignFile(LogFile,FileName);
- Reset(LogFile);
- while(not Eof(LogFile)) do
- begin
- ReadLn(LogFile,StrData);
- StrMsg.Clear;
- StrMsg.AddStrings(ExtractStr(StrData,'|'));
- if(StrMsg.Count<4) then
- exit;
- RetId:=StrToInt('$'+StrMsg[2]);
- if(Addr=StrMsg[0]) or (Addr='全部') then
- begin
- if(RetId in [$20,$21]) and
- ((ViewItem=3) or (ViewItem=0)) then
- begin
- with LVValues.Items.Add do
- begin
- Subitems.Add(StrMsg[0]);
- Subitems.Add(StrMSg[1]);
- Subitems.Add('进程创建、终止');
- if(RetId=$20) then
- Subitems.Add('创建')
- else
- Subitems.Add('终止');
- Subitems.Add(StrMsg[3]);
- Subitems.Add(StrMsg[4]);
- end;
- end
- else if(RetId in [$30..$33]) and
- ((ViewItem=4) or (ViewItem=0)) then
- begin
- with LVValues.Items.Add do
- begin
- Subitems.Add(StrMsg[0]);
- Subitems.Add(StrMSg[1]);
- Subitems.Add('文件操作');
- case RetId of
- $30:Subitems.Add('新建文件');
- $31:Subitems.Add('删除文件');
- $32:Subitems.Add('文件更新');
- $33:Subitems.Add('文件更名');
- end;
- if(RetId<>$33) then
- Subitems.Add(StrMsg[3])
- else
- begin
- Subitems.Add(StrMsg[3]);
- Subitems.Add(StrMsg[4]);
- end;
- end;
- end
- else if(RetId=$4) and
- ((ViewItem=2) or (ViewItem=0)) then
- begin
- with LVValues.Items.Add do
- begin
- Subitems.Add(StrMsg[0]);
- Subitems.Add(StrMSg[1]);
- Subitems.Add('内存使用');
- Subitems.Add(StrMsg[3]+'%');
- Subitems.Add(StrMsg[4]+'兆');
- end;
- end
- else if(RetId=$5) and
- ((ViewItem=1) or (ViewItem=0)) then
- begin
- with LVValues.Items.Add do
- begin
- Subitems.Add(StrMsg[0]);
- Subitems.Add(StrMSg[1]);
- Subitems.Add('CPU使用');
- Subitems.Add(StrMsg[3]+'%');
- end;
- end
- else if(RetId in [$80,$81]) and
- ((ViewItem=5) or (ViewItem=0)) then
- begin
- with LVValues.Items.Add do
- begin
- Subitems.Add(StrMsg[0]);
- Subitems.Add(StrMSg[1]);
- Subitems.Add('网络数据包');
- if(RetId=$80) then
- Subitems.Add('收')
- else
- Subitems.Add('发');
- Subitems.Add('数目'+StrMsg[3]);
- Subitems.Add('协议'+StrMsg[4]);
- Subitems.Add('远程IP'+StrMsg[5]);
- Subitems.Add('目标端口'+StrMsg[6]);
- end;
- end;
- end;
- end;
- CloseFile(LogFile);
- end;
- function ExtractStr(const StrOrg,SepChar:String):TStrings;
- var
- StrData:TStrings;
- i,n,Len:integer;
- begin
- i:=1;
- n:=0;
- Len:=Length(StrOrg)+1;
- StrData:=TStringList.Create;
- StrData.Text:='';
- while(i<Len) do
- begin
- StrData.Add('');
- while(StrOrg[i]<>SepChar) and (i<Len) do
- begin
- StrData.Strings[n]:=StrData.Strings[n]+StrOrg[i];
- i:=i+1;
- end;
- i:=i+1;
- n:=n+1;
- end;
- result:=StrData;
- end;
- end.