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

@ -33,14 +33,14 @@ uses
SysUtils, SynaUtil; SysUtils, SynaUtil;
const const
ASN1_INT = $02; ASN1_INT = $02;
ASN1_OCTSTR = $04; ASN1_OCTSTR = $04;
ASN1_NULL = $05; ASN1_NULL = $05;
ASN1_OBJID = $06; ASN1_OBJID = $06;
ASN1_SEQ = $30; ASN1_SEQ = $30;
ASN1_IPADDR = $40; ASN1_IPADDR = $40;
ASN1_COUNTER = $41; ASN1_COUNTER = $41;
ASN1_GAUGE = $42; ASN1_GAUGE = $42;
ASN1_TIMETICKS = $43; ASN1_TIMETICKS = $43;
function ASNEncLen(Len: integer): string; function ASNEncLen(Len: integer): string;
@ -61,11 +61,11 @@ begin
if (Len < $FF) then if (Len < $FF) then
Result := Char($81) + Char(Len) Result := Char($81) + Char(Len)
else else
begin begin
x := Len div $FF; x := Len div $FF;
y := Len mod $FF; y := Len mod $FF;
Result := Char($82) + Char(x) + Char(y); Result := Char($82) + Char(x) + Char(y);
end; end;
end; end;
function ASNDecLen(var Start: integer; Buffer: string): integer; function ASNDecLen(var Start: integer; Buffer: string): integer;
@ -74,25 +74,25 @@ var
begin begin
x := Ord(Buffer[Start]); x := Ord(Buffer[Start]);
if (x < $80) then if (x < $80) then
begin begin
Inc(Start); Inc(Start);
Result := x; Result := x;
end end
else else
if (x = $81) then if (x = $81) then
begin begin
Inc(Start); Inc(Start);
Result := Ord(Buffer[Start]); Result := Ord(Buffer[Start]);
Inc(Start); Inc(Start);
end end
else else
begin begin
Inc(Start); Inc(Start);
x := Ord(Buffer[Start]); x := Ord(Buffer[Start]);
Inc(Start); Inc(Start);
Result := x * $FF + Ord(Buffer[Start]); Result := x * $FF + Ord(Buffer[Start]);
Inc(Start); Inc(Start);
end; end;
end; end;
function ASNEncInt(Len: integer): string; function ASNEncInt(Len: integer): string;
@ -104,17 +104,17 @@ begin
y := Len div $FFFFFF; y := Len div $FFFFFF;
Len := Len - (y * $FFFFFF); Len := Len - (y * $FFFFFF);
if ((y > 0) or (j = 1)) then if ((y > 0) or (j = 1)) then
begin begin
j := 1; j := 1;
Result := Result + Char(y); Result := Result + Char(y);
end; end;
y := Len div $FFFF; y := Len div $FFFF;
Len := Len - (y * $FFFF); Len := Len - (y * $FFFF);
if ((y > 0) or (j = 1)) then if ((y > 0) or (j = 1)) then
begin begin
j := 1; j := 1;
Result := Result + Char(y); Result := Result + Char(y);
end; end;
y := Len div $FF; y := Len div $FF;
Len := Len - (y * $FF); Len := Len - (y * $FF);
if ((y > 0) or (j = 1)) then if ((y > 0) or (j = 1)) then
@ -140,60 +140,60 @@ begin
ASNSize := ASNDecLen(Start, Buffer); ASNSize := ASNDecLen(Start, Buffer);
Result := ''; Result := '';
if ((ASNType and $20) > 0) then if ((ASNType and $20) > 0) then
begin begin
Result := '$' + IntToHex(ASNType, 2); Result := '$' + IntToHex(ASNType, 2);
end end
else else
case ASNType of case ASNType of
ASN1_INT, ASN1_COUNTER, ASN1_GAUGE, ASN1_TIMETICKS: ASN1_INT, ASN1_COUNTER, ASN1_GAUGE, ASN1_TIMETICKS:
begin begin
y := 0; y := 0;
for n:=1 to ASNSize do for n := 1 to ASNSize do
begin begin
y := y * 256 + Ord(Buffer[Start]); y := y * 256 + Ord(Buffer[Start]);
Inc(Start); Inc(Start);
end; end;
Result := IntToStr(y); Result := IntToStr(y);
end; end;
ASN1_OCTSTR, $44: ASN1_OCTSTR, $44:
begin begin
for n:=1 to ASNSize do for n := 1 to ASNSize do
begin begin
c := Char(Buffer[Start]); c := Char(Buffer[Start]);
Inc(Start); Inc(Start);
s := s + c; s := s + c;
end; end;
Result := s; Result := s;
end; end;
ASN1_OBJID: ASN1_OBJID:
begin begin
for n:=1 to ASNSize do for n := 1 to ASNSize do
begin begin
c := Char(Buffer[Start]); c := Char(Buffer[Start]);
Inc(Start); Inc(Start);
s := s + c; s := s + c;
end; end;
Result := IdToMib(s); Result := IdToMib(s);
end; end;
ASN1_IPADDR: ASN1_IPADDR:
begin begin
s:=''; s := '';
for n:=1 to ASNSize do for n := 1 to ASNSize do
begin begin
if (n<>1) then if (n <> 1) then
s := s + '.'; s := s + '.';
y := Ord(Buffer[Start]); y := Ord(Buffer[Start]);
Inc(Start); Inc(Start);
s := s + IntToStr(y); s := s + IntToStr(y);
end; end;
Result := s; Result := s;
end; end;
else // NULL else // NULL
begin begin
Result := ''; Result := '';
Inc(Start); Inc(Start);
Start := Start + ASNSize; Start := Start + ASNSize;
end; end;
end; end;
end; end;

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,22 +143,33 @@ 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];
data:=data+ASNObject(s,$30); 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);
end; end;
data:=ASNObject(data,$30); data:=ASNObject(data,$30);
data:=ASNObject(char(Self.ID),2) data:=ASNObject(char(Self.ID),2)
@ -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,41 +194,65 @@ 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;
{==============================================================================} {==============================================================================}
{TSNMPSend.Create} {TSNMPSend.Create}
@ -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;
@ -261,7 +261,7 @@ begin
end; end;
function RecvTrap(var Dest, Source, Enterprise, Community: string; function RecvTrap(var Dest, Source, Enterprise, Community: string;
var Generic, Specific, Seconds: integer; var MIBName, MIBValue: TStringList): var Generic, Specific, Seconds: integer; var MIBName, MIBValue: TStringList):
integer; integer;
var var
SNMP: TTrapSNMP; SNMP: TTrapSNMP;

View File

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