📄 idsnmp.pas
字号:
(*----------------------------------------------------------------------------*
| function TSNMPInfo.MIBGet |
| |
| Get a string representation of tha value of the specified MIBOID |
| |
| Parameters: |
| MIB:string The MIBOID to query |
| |
| The function returns the string representation of the value. |
*----------------------------------------------------------------------------*)
function TSNMPInfo.MIBGet(MIB:string):string;
var
x:integer;
begin
SyncMIB;
x:=MIBOID.IndexOf(MIB);
if x<0 then Result:='' {Do not Localize}
else Result:=MIBValue[x];
end;
{======================= TRAPS =====================================}
(*----------------------------------------------------------------------------*
| procedure TSNMPInfo.EncodeTrap |
| |
| Encode the trap details into an ASN string - the 'Buffer' member | {Do not Localize}
| |
| The function returns 1 for historical reasons! |
*----------------------------------------------------------------------------*)
function TSNMPInfo.EncodeTrap: integer;
var
s: string;
n: integer;
begin
Buffer := ''; {Do not Localize}
for n:=0 to MIBOID.Count-1 do
begin
s := ASNObject(MibToID(MIBOID[n]), ASN1_OBJID)
+ ASNObject(MIBValue[n], ASN1_OCTSTR);
Buffer := Buffer + ASNObject(s, ASN1_SEQ);
end;
Buffer := ASNObject(Buffer, ASN1_SEQ);
Buffer := ASNObject(ASNEncInt(GenTrap), ASN1_INT)
+ ASNObject(ASNEncInt(SpecTrap), ASN1_INT)
+ ASNObject(ASNEncInt(TimeTicks), ASN1_TIMETICKS) + Buffer;
Buffer := ASNObject(MibToID(Enterprise), ASN1_OBJID)
+ ASNObject(IPToID(Host), ASN1_IPADDR) + Buffer;
Buffer := ASNObject(Char(Version), ASN1_INT)
+ ASNObject(Community, ASN1_OCTSTR) + ASNObject(Buffer, Self.PDUType);
Buffer := ASNObject(Buffer, ASN1_SEQ);
Result := 1;
end;
(*----------------------------------------------------------------------------*
| procedure TSNMPInfo.DecodeTrap |
| |
| Decode the 'Buffer' trap string to fil in our member variables. | {Do not Localize}
| |
| The function returns 1. |
*----------------------------------------------------------------------------*)
function TSNMPInfo.DecodeTrap: integer;
var
Pos, EndPos, vt: integer;
Sm, Sv: string;
begin
Pos := 2;
EndPos := ASNDecLen(Pos, Buffer);
Version := StrToIntDef(ASNItem(Pos, Buffer,vt), 0);
Community := ASNItem(Pos, Buffer,vt);
PDUType := StrToIntDef(ASNItem(Pos, Buffer,vt), PDUTRAP);
Enterprise := IdToMIB(ASNItem(Pos, Buffer,vt));
Host := ASNItem(Pos, Buffer,vt);
GenTrap := StrToIntDef(ASNItem(Pos, Buffer,vt), 0);
Spectrap := StrToIntDef(ASNItem(Pos, Buffer,vt), 0);
TimeTicks := StrToIntDef(ASNItem(Pos, Buffer,vt), 0);
ASNItem(Pos, Buffer,vt);
while (Pos < EndPos) do
begin
ASNItem(Pos, Buffer,vt);
Sm := ASNItem(Pos, Buffer,vt);
Sv := ASNItem(Pos, Buffer,vt);
MIBAdd(Sm, Sv, vt);
end;
Result := 1;
end;
(*----------------------------------------------------------------------------*
| TSNMPInfo.SetCommunity |
| |
| Set the community. |
| |
| Parameters: |
| const Value: string The new community value |
*----------------------------------------------------------------------------*)
procedure TSNMPInfo.SetCommunity(const Value: string);
begin
if Community <> Value then
begin
Clear;
fCommunity := Value
end
end;
{ TIdSNMP }
{============================== IdSNMP OBJECT ================================}
(*----------------------------------------------------------------------------*
| constructor TIdSNMP.Create |
| |
| Contructor for TIdSNMP component |
| |
| Parameters: |
| aOwner : TComponent |
*----------------------------------------------------------------------------*)
constructor TIdSNMP.Create(aOwner : TComponent);
begin
inherited;
Port := 161;
fTrapPort := 162;
fCommunity := 'public'; {Do not Localize}
Query:=TSNMPInfo.Create (Self);
Reply:=TSNMPInfo.Create (Self);
Trap :=TSNMPInfo.Create (Self);
Query.Clear;
Reply.Clear;
Trap.Clear;
FReceiveTimeout:=5000;
end;
(*----------------------------------------------------------------------------*
| destructor TIdSNMP.Destroy |
| |
| Destructor for TIdSNMP component |
*----------------------------------------------------------------------------*)
destructor TIdSNMP.Destroy;
begin
Reply.Free;
Query.Free;
Trap.Free;
inherited destroy;
end;
(*----------------------------------------------------------------------------*
| function TIdSNMP.SendQuery |
| |
| Send an SNMP query and receive a reply. |
| |
| nb. Before calling this, ensure that the following members are set: |
| |
| Community The SNMP community being queried - eg. 'public' | {Do not Localize}
| Host The IP address being queried. 127.0.0.1 for the |
| local machine. |
| |
| The call Query.Clear, then set: |
| |
| Query.PDUType PDUGetRequest to get a single set of MIBOID |
| value(s) or PDUGetNextRequest to start walking |
| the MIB |
| |
| Next call Query.Clear, call MIBAdd to add the MIBOID(s) you require. |
| |
| The function returns True if a response was received. IF a response was |
| received, it will be decoded into Reply.Value |
*----------------------------------------------------------------------------*)
function TIdSNMP.SendQuery:boolean;
begin
reply.clear;
Query.Buffer:=Query.Encodebuf;
Send(Query.host, Query.port, Query.buffer);
try
reply.Buffer := ReceiveString(Query.host, Query.port, FReceiveTimeout);
except
on e : EIdSocketError do
if e.LastError <> 10054 then
raise
else
reply.buffer := ''; {Do not Localize}
end;
if reply.Buffer<>'' then reply.DecodeBuf(reply.Buffer); {Do not Localize}
Result := (reply.Buffer <> '') and (reply.ErrorStatus = 0) {Do not Localize}
end;
(*----------------------------------------------------------------------------*
| TIdSNMP.QuickSend |
| |
| Query a single MIBOID value. |
| |
| Parameters: |
| Mib : string The MIBOID to query |
| Community : string The SNMP comunity |
| Host : string The SNMP host |
| var value : string String representation of the returned value. |
| |
| The function returns true if a value was returned for the MIB OID |
*----------------------------------------------------------------------------*)
function TIdSNMP.QuickSend (const Mib, Community,Host:string; var Value:string):Boolean;
begin
self.Community := Community;
self.Host := Host;
Query.Clear;
Query.PDUType:=PDUGetRequest;
Query.MIBAdd(MIB,''); {Do not Localize}
Result:=SendQuery;
if Result then Value:=Reply.MIBGet(MIB);
end;
(*----------------------------------------------------------------------------*
| TIdSNMP.SendTrap |
| |
| Send an SNMP trap. |
| |
| The function returns 1 |
*----------------------------------------------------------------------------*)
function TIdSNMP.SendTrap: integer;
begin
Trap.PDUType := PDUTrap;
Trap.EncodeTrap;
Send(Trap.Host, Trap.Port, Trap.Buffer);
Result := 1;
end;
function TIdSNMP.ReceiveTrap: integer;
begin
Trap.PDUType := PDUTrap;
Result := 0;
Trap.Buffer := ReceiveString(trap.host, trap.port, FReceiveTimeout);
if Trap.Buffer <> '' then begin {Do not Localize}
Trap.DecodeTrap;
Result := 1;
end;
end;
function TIdSNMP.QuickSendTrap(const Dest, Enterprise, Community: string;
Port, Generic, Specific: integer; MIBName, MIBValue: TStringList): integer;
var
i: integer;
begin
Trap.Host := Dest;
Trap.Enterprise := Enterprise;
Trap.GenTrap := Generic;
Trap.SpecTrap := Specific;
for i:=0 to (MIBName.Count - 1) do
Trap.MIBAdd(MIBName[i], MIBValue[i], Integer (MibValue.Objects [i]));
Result := SendTrap;
end;
function TIdSNMP.QuickReceiveTrap(var Source, Enterprise, Community: string;
var Port, Generic, Specific, Seconds: integer; var MIBName, MIBValue: TStringList): integer;
var
i: integer;
begin
Result := ReceiveTrap;
if (Result <> 0) then
begin
Source := Trap.Host;
Port := Trap.Port;
Enterprise := Trap.Enterprise;
Community := Trap.Community;
Generic := Trap.GenTrap;
Specific := Trap.SpecTrap;
Seconds := Trap.TimeTicks;
MIBName.Clear;
MIBValue.Clear;
for i:=0 to (Trap.MIBOID.Count - 1) do
begin
MIBName.Add(Trap.MIBOID[i]);
MIBValue.Add(Trap.MIBValue[i]);
end;
end;
end;
(*----------------------------------------------------------------------------*
| TSNMPInfo.GetValue |
| |
| Return string representation of value 'idx' | {Do not Localize}
| |
| Parameters: |
| idx : Integer The value to get |
| |
| The function returns the string representation of the value. |
*----------------------------------------------------------------------------*)
function TSNMPInfo.GetValue (idx : Integer) : string;
begin
Result := MIBValue [idx]
end;
(*----------------------------------------------------------------------------*
| TSNMPInfo.GetValueCount |
| |
| Get the number of values. |
| |
| The function returns the number of values. |
*----------------------------------------------------------------------------*)
function TSNMPInfo.GetValueCount: Integer;
begin
Result := MIBValue.Count
end;
(*----------------------------------------------------------------------------*
| TSNMPInfo.GetValueType |
| |
| Return the 'type' of value 'idx' | {Do not Localize}
| |
| Parameters: |
| idx : Integer The value type to get |
| |
| The function returns the value type. |
*----------------------------------------------------------------------------*)
function TSNMPInfo.GetValueType (idx : Integer): Integer;
begin
Result := Integer (MIBValue.Objects [idx]);
if Result = 0 then
Result := ASN1_OCTSTR
end;
(*----------------------------------------------------------------------------*
| TSNMPInfo.GetValueOID |
| |
| Get the MIB OID for value 'idx' | {Do not Localize}
| |
| Parameters: |
| idx: Integer The MIB OID to gey |
| |
| The function returns the specified MIB OID |
*----------------------------------------------------------------------------*)
function TSNMPInfo.GetValueOID(idx: Integer): string;
begin
Result := MIBOID [idx];
end;
(*----------------------------------------------------------------------------*
| TIdSNMP.SetCommunity |
| |
| Setter for the Community property. |
| |
| Parameters: |
| const Value: string The new community value |
*----------------------------------------------------------------------------*)
procedure TIdSNMP.SetCommunity(const Value: string);
begin
if fCommunity <> Value then
begin
fCommunity := Value;
Query.Community := Value;
Reply.Community := Value;
Trap.Community := Value
end
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -