Release 10

git-svn-id: https://svn.code.sf.net/p/synalist/code/trunk@21 7c85be65-684b-0410-a082-b2ed4fbef004
This commit is contained in:
geby 2008-04-23 20:39:31 +00:00
parent 58c87ee0e9
commit fb0759c8f2
4 changed files with 188 additions and 129 deletions

View File

@ -1,5 +1,5 @@
{==============================================================================| {==============================================================================|
| Project : Delphree - Synapse | 001.001.000 | | Project : Delphree - Synapse | 002.000.000 |
|==============================================================================| |==============================================================================|
| Content: SNMP client | | Content: SNMP client |
|==============================================================================| |==============================================================================|
@ -18,6 +18,7 @@
| All Rights Reserved. | | All Rights Reserved. |
|==============================================================================| |==============================================================================|
| Contributor(s): | | Contributor(s): |
| Jean-Fabien Connault (jfconnault@mail.dotcom.fr) |
|==============================================================================| |==============================================================================|
| History: see HISTORY.HTM from distribution package | | History: see HISTORY.HTM from distribution package |
| (Found at URL: http://www.mlp.cz/space/gebauerl/synapse/) | | (Found at URL: http://www.mlp.cz/space/gebauerl/synapse/) |
@ -49,9 +50,23 @@ EGenErr=5;
type type
TSNMPMibValueType = (smvtInteger,
smvtOctetString,
smvtNull,
smvtObjectId,
smvtSequence,
smvtIpAddress,
smvtCounter,
smvtGauge,
smvtTimeTicks);
TSNMPMib = class
OID: string;
Value: string;
ValueType: integer;
end;
TSNMPRec=class(TObject) TSNMPRec=class(TObject)
private
procedure SyncMIB;
public public
version:integer; version:integer;
community:string; community:string;
@ -59,16 +74,16 @@ TSNMPRec=class(TObject)
ID:integer; ID:integer;
ErrorStatus:integer; ErrorStatus:integer;
ErrorIndex:integer; ErrorIndex:integer;
MIBOID:TStringList; SNMPMibList: TList;
MIBValue:TStringList;
constructor Create; constructor Create;
destructor Destroy; override; destructor Destroy; override;
procedure DecodeBuf(Buffer:string); procedure DecodeBuf(Buffer:string);
function EncodeBuf:string; function EncodeBuf:string;
procedure Clear; procedure Clear;
procedure MIBAdd(MIB,Value:string); procedure MIBAdd(MIB,Value:string; ValueType:TSNMPMibValueType);
procedure MIBdelete(Index:integer); procedure MIBdelete(Index:integer);
function MIBGet(MIB:string):string; function MIBGet(MIB:string):string;
function ConvertValueType(ValueType: TSNMPMibValueType): integer;
end; end;
TSNMPSend=class(TObject) TSNMPSend=class(TObject)
@ -76,8 +91,8 @@ TSNMPSend=class(TObject)
Sock:TUDPBlockSocket; Sock:TUDPBlockSocket;
Buffer:string; Buffer:string;
public public
timeout:integer; Timeout:integer;
SNMPhost:string; Host:string;
Query:TSNMPrec; Query:TSNMPrec;
Reply:TSNMPrec; Reply:TSNMPrec;
constructor Create; constructor Create;
@ -85,7 +100,8 @@ TSNMPSend=class(TObject)
function DoIt:boolean; function DoIt:boolean;
end; end;
function SNMPget (Mib, Community, SNMPHost:string; var Value:string):Boolean; function SNMPget (Oid, Community, SNMPHost:string; var Value:string):Boolean;
function SNMPSet (Oid, Community, SNMPHost, Value: string; ValueType: TSNMPMibValueType): boolean;
implementation implementation
@ -95,34 +111,23 @@ implementation
constructor TSNMPRec.Create; constructor TSNMPRec.Create;
begin begin
inherited create; inherited create;
MIBOID:=TStringList.create; SNMPMibList := TList.create;
MIBValue:=TStringList.create;
end; end;
{TSNMPRec.Destroy} {TSNMPRec.Destroy}
destructor TSNMPRec.Destroy; destructor TSNMPRec.Destroy;
begin begin
MIBValue.Free; SNMPMibList.free;
MIBOID.Free;
inherited destroy; inherited destroy;
end; end;
{TSNMPRec.SyncMIB}
procedure TSNMPRec.SyncMIB;
var
n,x:integer;
begin
x:=MIBValue.Count;
for n:=x to MIBOID.Count-1 do
MIBValue.Add('');
end;
{TSNMPRec.DecodeBuf} {TSNMPRec.DecodeBuf}
procedure TSNMPRec.DecodeBuf(Buffer:string); procedure TSNMPRec.DecodeBuf(Buffer:string);
var var
Pos:integer; Pos:integer;
endpos:integer; endpos:integer;
sm,sv:string; sm,sv:string;
svt: TSNMPMibValueType;
begin begin
Pos:=2; Pos:=2;
Endpos:=ASNDecLen(Pos,buffer); Endpos:=ASNDecLen(Pos,buffer);
@ -138,21 +143,32 @@ begin
ASNItem(Pos,buffer); ASNItem(Pos,buffer);
Sm:=ASNItem(Pos,buffer); Sm:=ASNItem(Pos,buffer);
Sv:=ASNItem(Pos,buffer); Sv:=ASNItem(Pos,buffer);
Self.MIBadd(sm,sv); Svt:=smvtNull;
Self.MIBadd(sm,sv, svt);
end; end;
end; end;
{TSNMPRec.EncodeBuf} {TSNMPRec.EncodeBuf}
function TSNMPRec.EncodeBuf:string; function TSNMPRec.EncodeBuf:string;
var var
data,s:string; data,s,t:string;
SNMPMib: TSNMPMib;
n:integer; n:integer;
begin begin
data:=''; data:='';
SyncMIB; for n:=0 to SNMPMibList.Count-1 do
for n:=0 to Self.MIBOID.Count-1 do
begin begin
s:=ASNObject(MibToID(Self.MIBOID[n]),6)+ASNObject(Self.MIBValue[n],4); SNMPMib := SNMPMibList[n];
case (SNMPMib.ValueType) of
ASN1_INT, ASN1_COUNTER, ASN1_GAUGE, ASN1_TIMETICKS:
begin
t := chr(strToInt('$'+copy(inttohex(strToInt(SNMPMib.Value),4),1,2)));
t := t+chr(strToInt('$'+copy(inttohex(strToInt(SNMPMib.Value),4),3,2)));
s := ASNObject(MibToID(SNMPMib.OID),6) + ASNObject(t,SNMPMib.ValueType);
end;
else
s := ASNObject(MibToID(SNMPMib.OID),6) + ASNObject(SNMPMib.Value,SNMPMib.ValueType);
end;
data := data + ASNObject(s, $30); data := data + ASNObject(s, $30);
end; end;
data:=ASNObject(data,$30); data:=ASNObject(data,$30);
@ -169,6 +185,8 @@ end;
{TSNMPRec.Clear} {TSNMPRec.Clear}
procedure TSNMPRec.Clear; procedure TSNMPRec.Clear;
var
i:integer;
begin begin
version:=0; version:=0;
community:=''; community:='';
@ -176,40 +194,64 @@ begin
ID:=0; ID:=0;
ErrorStatus:=0; ErrorStatus:=0;
ErrorIndex:=0; ErrorIndex:=0;
MIBOID.Clear; for i := 0 to SNMPMibList.count - 1 do
MIBValue.Clear; TSNMPMib(SNMPMibList[i]).Free;
SNMPMibList.Clear;
end; end;
{TSNMPRec.MIBAdd} {TSNMPRec.MIBAdd}
procedure TSNMPRec.MIBAdd(MIB,Value:string); procedure TSNMPRec.MIBAdd(MIB,Value:string; ValueType:TSNMPMibValueType);
var var
x:integer; SNMPMib: TSNMPMib;
begin begin
SyncMIB; SNMPMib := TSNMPMib.Create;
MIBOID.Add(MIB); SNMPMib.OID := MIB;
x:=MIBOID.Count; SNMPMib.Value := Value;
if MIBValue.Count>x then MIBvalue[x-1]:=value SNMPMib.ValueType := ConvertValueType(ValueType);
else MIBValue.Add(Value); SNMPMibList.Add(SNMPMib);
end; end;
{TSNMPRec.MIBdelete} {TSNMPRec.MIBdelete}
procedure TSNMPRec.MIBdelete(Index:integer); procedure TSNMPRec.MIBdelete(Index:integer);
begin begin
SyncMIB; if (Index >= 0) and (Index < SNMPMibList.count) then
MIBOID.Delete(Index); begin
if (MIBValue.Count-1)>= Index then MIBValue.Delete(Index); TSNMPMib(SNMPMibList[Index]).Free;
SNMPMibList.Delete(Index);
end;
end; end;
{TSNMPRec.MIBGet} {TSNMPRec.MIBGet}
function TSNMPRec.MIBGet(MIB:string):string; function TSNMPRec.MIBGet(MIB:string):string;
var var
x:integer; i: integer;
begin begin
SyncMIB; Result := '';
x:=MIBOID.IndexOf(MIB); for i := 0 to SNMPMibList.count - 1 do
if x<0 then Result:='' begin
else Result:=MIBValue[x]; if ((TSNMPMib(SNMPMibList[i])).OID = MIB) then
begin
Result := (TSNMPMib(SNMPMibList[i])).Value;
break;
end; end;
end;
end;
{TSNMPRec.GetValueType}
function TSNMPRec.ConvertValueType(ValueType: TSNMPMibValueType): integer;
begin
result := ASN1_NULL;
if (ValueType = smvtInteger) then result := ASN1_INT;
if (ValueType = smvtOctetString) then result := ASN1_OCTSTR;
if (ValueType = smvtNull) then result := ASN1_NULL;
if (ValueType = smvtObjectId) then result := ASN1_OBJID;
if (ValueType = smvtSequence) then result := ASN1_SEQ;
if (ValueType = smvtIpAddress) then result := ASN1_IPADDR;
if (ValueType = smvtCounter) then result := ASN1_COUNTER;
if (ValueType = smvtGauge) then result := ASN1_GAUGE;
if (ValueType = smvtTimeTicks) then result := ASN1_TIMETICKS;
end;
{==============================================================================} {==============================================================================}
@ -224,7 +266,7 @@ begin
sock:=TUDPBlockSocket.create; sock:=TUDPBlockSocket.create;
sock.createsocket; sock.createsocket;
timeout:=5; timeout:=5;
SNMPhost:='localhost'; host:='localhost';
end; end;
{TSNMPSend.Destroy} {TSNMPSend.Destroy}
@ -244,7 +286,7 @@ begin
Result:=false; Result:=false;
reply.clear; reply.clear;
Buffer:=Query.Encodebuf; Buffer:=Query.Encodebuf;
sock.connect(SNMPhost,'161'); sock.connect(host,'161');
sock.SendBuffer(PChar(Buffer),Length(Buffer)); sock.SendBuffer(PChar(Buffer),Length(Buffer));
if sock.canread(timeout) if sock.canread(timeout)
then begin then begin
@ -256,12 +298,13 @@ begin
result:=true; result:=true;
end; end;
end; end;
if Result then reply.DecodeBuf(Buffer); if Result
then reply.DecodeBuf(Buffer);
end; end;
{==============================================================================} {==============================================================================}
function SNMPget (Mib, Community, SNMPHost:string; var Value:string):Boolean; function SNMPget (Oid, Community, SNMPHost:string; var Value:string):Boolean;
var var
SNMP:TSNMPSend; SNMP:TSNMPSend;
begin begin
@ -270,15 +313,31 @@ begin
try try
Snmp.Query.community:=Community; Snmp.Query.community:=Community;
Snmp.Query.PDUType:=PDUGetRequest; Snmp.Query.PDUType:=PDUGetRequest;
Snmp.Query.MIBAdd(MIB,''); Snmp.Query.MIBAdd(Oid,'',smvtNull);
Snmp.SNMPhost:=SNMPHost; Snmp.host:=SNMPHost;
Result:=Snmp.DoIt; Result:=Snmp.DoIt;
if Result then if Result then
Value:=Snmp.Reply.MIBGet(MIB); Value:=Snmp.Reply.MIBGet(Oid);
finally finally
SNMP.Free; SNMP.Free;
end; end;
end; end;
function SNMPSet(Oid, Community, SNMPHost, Value: string; ValueType: TSNMPMibValueType): boolean;
var
SNMPSend: TSNMPSend;
begin
SNMPSend := TSNMPSend.Create;
try
SNMPSend.Query.community := Community;
SNMPSend.Query.PDUType := PDUSetRequest;
SNMPSend.Query.MIBAdd(Oid, Value, ValueType);
SNMPSend.Host := SNMPHost;
result:= SNMPSend.DoIt=true;
finally
SNMPSend.Free;
end;
end;
end. end.

View File

@ -1,5 +1,5 @@
{==============================================================================| {==============================================================================|
| Project : Delphree - Synapse | 001.002.000 | | Project : Delphree - Synapse | 001.002.001 |
|==============================================================================| |==============================================================================|
| Content: SNMP traps | | Content: SNMP traps |
|==============================================================================| |==============================================================================|
@ -223,7 +223,7 @@ var
x: integer; x: integer;
begin begin
Result := 0; Result := 0;
Sock.Connect(SNMPHost, IntToStr(Trap.TrapPort)); Sock.Bind('0.0.0.0', IntToStr(Trap.TrapPort));
if Sock.CanRead(Timeout) then if Sock.CanRead(Timeout) then
begin begin
x := Sock.WaitingData; x := Sock.WaitingData;

View File

@ -29,7 +29,7 @@ unit SNTPsend;
interface interface
uses uses
winsock, SysUtils, windows, blcksock, Synautil, dialogs; winsock, SysUtils, windows, blcksock;
type type