2008-04-23 23:28:55 +03:00
|
|
|
{==============================================================================|
|
2008-04-24 10:05:26 +03:00
|
|
|
| Project : Delphree - Synapse | 002.003.002 |
|
2008-04-23 23:28:55 +03:00
|
|
|
|==============================================================================|
|
|
|
|
| Content: SNMP client |
|
|
|
|
|==============================================================================|
|
2008-04-24 10:05:26 +03:00
|
|
|
| The contents of this file are subject to the Mozilla Public License Ver. 1.1 |
|
2008-04-23 23:28:55 +03:00
|
|
|
| (the "License"); you may not use this file except in compliance with the |
|
|
|
|
| License. You may obtain a copy of the License at http://www.mozilla.org/MPL/ |
|
|
|
|
| |
|
|
|
|
| Software distributed under the License is distributed on an "AS IS" basis, |
|
|
|
|
| WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for |
|
|
|
|
| the specific language governing rights and limitations under the License. |
|
|
|
|
|==============================================================================|
|
|
|
|
| The Original Code is Synapse Delphi Library. |
|
|
|
|
|==============================================================================|
|
|
|
|
| The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
|
2008-04-24 09:44:13 +03:00
|
|
|
| Portions created by Lukas Gebauer are Copyright (c)2000,2001. |
|
2008-04-23 23:28:55 +03:00
|
|
|
| All Rights Reserved. |
|
|
|
|
|==============================================================================|
|
|
|
|
| Contributor(s): |
|
2008-04-23 23:39:31 +03:00
|
|
|
| Jean-Fabien Connault (jfconnault@mail.dotcom.fr) |
|
2008-04-23 23:28:55 +03:00
|
|
|
|==============================================================================|
|
|
|
|
| History: see HISTORY.HTM from distribution package |
|
2008-04-23 23:48:39 +03:00
|
|
|
| (Found at URL: http://www.ararat.cz/synapse/) |
|
2008-04-23 23:28:55 +03:00
|
|
|
|==============================================================================}
|
|
|
|
|
2008-04-24 10:00:43 +03:00
|
|
|
{$Q-}
|
2008-04-24 10:05:26 +03:00
|
|
|
{$WEAKPACKAGEUNIT ON}
|
2008-04-24 10:00:43 +03:00
|
|
|
|
2008-04-23 23:28:55 +03:00
|
|
|
unit SNMPSend;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2008-04-24 10:05:26 +03:00
|
|
|
Classes, SysUtils,
|
|
|
|
blckSock, SynaUtil, ASN1Util;
|
2008-04-23 23:28:55 +03:00
|
|
|
|
|
|
|
const
|
2008-04-24 10:05:26 +03:00
|
|
|
cSnmpProtocol = '161';
|
2008-04-23 23:28:55 +03:00
|
|
|
|
|
|
|
//PDU type
|
2008-04-24 10:05:26 +03:00
|
|
|
PDUGetRequest = $A0;
|
|
|
|
PDUGetNextRequest = $A1;
|
|
|
|
PDUGetResponse = $A2;
|
|
|
|
PDUSetRequest = $A3;
|
|
|
|
PDUTrap = $A4;
|
2008-04-23 23:28:55 +03:00
|
|
|
|
|
|
|
//errors
|
2008-04-24 10:05:26 +03:00
|
|
|
ENoError = 0;
|
|
|
|
ETooBig = 1;
|
|
|
|
ENoSuchName = 2;
|
|
|
|
EBadValue = 3;
|
|
|
|
EReadOnly = 4;
|
|
|
|
EGenErr = 5;
|
2008-04-23 23:28:55 +03:00
|
|
|
|
|
|
|
type
|
2008-04-24 10:05:26 +03:00
|
|
|
TSNMPMib = class(TObject)
|
|
|
|
private
|
|
|
|
FOID: string;
|
|
|
|
FValue: string;
|
|
|
|
FValueType: Integer;
|
|
|
|
published
|
|
|
|
property OID: string read FOID Write FOID;
|
|
|
|
property Value: string read FValue Write FValue;
|
|
|
|
property ValueType: Integer read FValueType Write FValueType;
|
|
|
|
end;
|
2008-04-23 23:28:55 +03:00
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
TSNMPRec = class(TObject)
|
|
|
|
private
|
|
|
|
FVersion: Integer;
|
|
|
|
FCommunity: string;
|
|
|
|
FPDUType: Integer;
|
|
|
|
FID: Integer;
|
|
|
|
FErrorStatus: Integer;
|
|
|
|
FErrorIndex: Integer;
|
|
|
|
FSNMPMibList: TList;
|
2008-04-23 23:28:55 +03:00
|
|
|
public
|
|
|
|
constructor Create;
|
|
|
|
destructor Destroy; override;
|
2008-04-24 10:05:26 +03:00
|
|
|
function DecodeBuf(const Buffer: string): Boolean;
|
|
|
|
function EncodeBuf: string;
|
2008-04-23 23:28:55 +03:00
|
|
|
procedure Clear;
|
2008-04-24 10:05:26 +03:00
|
|
|
procedure MIBAdd(const MIB, Value: string; ValueType: Integer);
|
|
|
|
procedure MIBDelete(Index: Integer);
|
|
|
|
function MIBGet(const MIB: string): string;
|
|
|
|
published
|
|
|
|
property Version: Integer read FVersion Write FVersion;
|
|
|
|
property Community: string read FCommunity Write FCommunity;
|
|
|
|
property PDUType: Integer read FPDUType Write FPDUType;
|
|
|
|
property ID: Integer read FID Write FID;
|
|
|
|
property ErrorStatus: Integer read FErrorStatus Write FErrorStatus;
|
|
|
|
property ErrorIndex: Integer read FErrorIndex Write FErrorIndex;
|
|
|
|
property SNMPMibList: TList read FSNMPMibList;
|
|
|
|
end;
|
2008-04-23 23:28:55 +03:00
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
TSNMPSend = class(TObject)
|
2008-04-23 23:28:55 +03:00
|
|
|
private
|
2008-04-24 10:05:26 +03:00
|
|
|
FSock: TUDPBlockSocket;
|
|
|
|
FBuffer: string;
|
|
|
|
FTimeout: Integer;
|
|
|
|
FHost: string;
|
|
|
|
FHostIP: string;
|
|
|
|
FQuery: TSNMPRec;
|
|
|
|
FReply: TSNMPRec;
|
2008-04-23 23:28:55 +03:00
|
|
|
public
|
|
|
|
constructor Create;
|
|
|
|
destructor Destroy; override;
|
2008-04-24 10:05:26 +03:00
|
|
|
function DoIt: Boolean;
|
|
|
|
published
|
|
|
|
property Timeout: Integer read FTimeout Write FTimeout;
|
|
|
|
property Host: string read FHost Write FHost;
|
|
|
|
property HostIP: string read FHostIP;
|
|
|
|
property Query: TSNMPRec read FQuery;
|
|
|
|
property Reply: TSNMPRec read FReply;
|
|
|
|
end;
|
2008-04-23 23:28:55 +03:00
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
function SNMPGet(const Oid, Community, SNMPHost: string;
|
|
|
|
var Value: string): Boolean;
|
|
|
|
function SNMPSet(const Oid, Community, SNMPHost, Value: string;
|
|
|
|
ValueType: Integer): Boolean;
|
2008-04-23 23:28:55 +03:00
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
{==============================================================================}
|
|
|
|
|
|
|
|
constructor TSNMPRec.Create;
|
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
inherited Create;
|
|
|
|
FSNMPMibList := TList.Create;
|
|
|
|
id := 1;
|
2008-04-23 23:28:55 +03:00
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TSNMPRec.Destroy;
|
2008-04-24 09:44:13 +03:00
|
|
|
var
|
2008-04-24 10:05:26 +03:00
|
|
|
i: Integer;
|
2008-04-23 23:28:55 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
for i := 0 to FSNMPMibList.Count - 1 do
|
|
|
|
TSNMPMib(FSNMPMibList[i]).Free;
|
|
|
|
FSNMPMibList.Free;
|
|
|
|
inherited Destroy;
|
2008-04-23 23:28:55 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
function TSNMPRec.DecodeBuf(const Buffer: string): Boolean;
|
2008-04-23 23:28:55 +03:00
|
|
|
var
|
2008-04-24 10:05:26 +03:00
|
|
|
Pos: Integer;
|
|
|
|
EndPos: Integer;
|
|
|
|
sm, sv: string;
|
|
|
|
Svt: Integer;
|
2008-04-23 23:28:55 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
Result := False;
|
|
|
|
if Length(Buffer) < 2 then
|
|
|
|
Exit;
|
|
|
|
if (Ord(Buffer[1]) and $20) = 0 then
|
|
|
|
Exit;
|
|
|
|
Pos := 2;
|
|
|
|
EndPos := ASNDecLen(Pos, Buffer);
|
|
|
|
if Length(Buffer) < (EndPos + 2) then
|
|
|
|
Exit;
|
|
|
|
Self.FVersion := StrToIntDef(ASNItem(Pos, Buffer, Svt), 0);
|
|
|
|
Self.FCommunity := ASNItem(Pos, Buffer, Svt);
|
|
|
|
Self.FPDUType := StrToIntDef(ASNItem(Pos, Buffer, Svt), 0);
|
|
|
|
Self.FID := StrToIntDef(ASNItem(Pos, Buffer, Svt), 0);
|
|
|
|
Self.FErrorStatus := StrToIntDef(ASNItem(Pos, Buffer, Svt), 0);
|
|
|
|
Self.FErrorIndex := StrToIntDef(ASNItem(Pos, Buffer, Svt), 0);
|
|
|
|
ASNItem(Pos, Buffer, Svt);
|
|
|
|
while Pos < EndPos do
|
|
|
|
begin
|
|
|
|
ASNItem(Pos, Buffer, Svt);
|
|
|
|
Sm := ASNItem(Pos, Buffer, Svt);
|
|
|
|
Sv := ASNItem(Pos, Buffer, Svt);
|
|
|
|
Self.MIBAdd(sm, sv, Svt);
|
|
|
|
end;
|
|
|
|
Result := True;
|
2008-04-23 23:28:55 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
function TSNMPRec.EncodeBuf: string;
|
2008-04-23 23:28:55 +03:00
|
|
|
var
|
2008-04-24 10:05:26 +03:00
|
|
|
data, s: string;
|
2008-04-23 23:39:31 +03:00
|
|
|
SNMPMib: TSNMPMib;
|
2008-04-24 10:05:26 +03:00
|
|
|
n: Integer;
|
2008-04-23 23:28:55 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
data := '';
|
|
|
|
for n := 0 to FSNMPMibList.Count - 1 do
|
|
|
|
begin
|
|
|
|
SNMPMib := FSNMPMibList[n];
|
|
|
|
case SNMPMib.ValueType of
|
|
|
|
ASN1_INT:
|
|
|
|
s := ASNObject(MibToID(SNMPMib.OID), ASN1_OBJID) +
|
|
|
|
ASNObject(ASNEncInt(StrToIntDef(SNMPMib.Value, 0)), SNMPMib.ValueType);
|
|
|
|
ASN1_COUNTER, ASN1_GAUGE, ASN1_TIMETICKS:
|
|
|
|
s := ASNObject(MibToID(SNMPMib.OID), ASN1_OBJID) +
|
|
|
|
ASNObject(ASNEncUInt(StrToIntDef(SNMPMib.Value, 0)), SNMPMib.ValueType);
|
|
|
|
ASN1_OBJID:
|
|
|
|
s := ASNObject(MibToID(SNMPMib.OID), ASN1_OBJID) +
|
|
|
|
ASNObject(MibToID(SNMPMib.Value), SNMPMib.ValueType);
|
|
|
|
ASN1_IPADDR:
|
|
|
|
s := ASNObject(MibToID(SNMPMib.OID), ASN1_OBJID) +
|
|
|
|
ASNObject(IPToID(SNMPMib.Value), SNMPMib.ValueType);
|
|
|
|
ASN1_NULL:
|
|
|
|
s := ASNObject(MibToID(SNMPMib.OID), ASN1_OBJID) +
|
|
|
|
ASNObject('', ASN1_NULL);
|
|
|
|
else
|
|
|
|
s := ASNObject(MibToID(SNMPMib.OID), ASN1_OBJID) +
|
|
|
|
ASNObject(SNMPMib.Value, SNMPMib.ValueType);
|
2008-04-23 23:28:55 +03:00
|
|
|
end;
|
2008-04-24 10:05:26 +03:00
|
|
|
data := data + ASNObject(s, ASN1_SEQ);
|
|
|
|
end;
|
|
|
|
data := ASNObject(data, ASN1_SEQ);
|
|
|
|
data := ASNObject(ASNEncInt(Self.FID), ASN1_INT) +
|
|
|
|
ASNObject(ASNEncInt(Self.FErrorStatus), ASN1_INT) +
|
|
|
|
ASNObject(ASNEncInt(Self.FErrorIndex), ASN1_INT) +
|
|
|
|
data;
|
|
|
|
data := ASNObject(ASNEncInt(Self.FVersion), ASN1_INT) +
|
|
|
|
ASNObject(Self.FCommunity, ASN1_OCTSTR) +
|
|
|
|
ASNObject(data, Self.FPDUType);
|
|
|
|
data := ASNObject(data, ASN1_SEQ);
|
|
|
|
Result := data;
|
2008-04-23 23:28:55 +03:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TSNMPRec.Clear;
|
2008-04-23 23:39:31 +03:00
|
|
|
var
|
2008-04-24 10:05:26 +03:00
|
|
|
i: Integer;
|
2008-04-23 23:28:55 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
FVersion := 0;
|
|
|
|
FCommunity := '';
|
|
|
|
FPDUType := 0;
|
|
|
|
FErrorStatus := 0;
|
|
|
|
FErrorIndex := 0;
|
|
|
|
for i := 0 to FSNMPMibList.Count - 1 do
|
|
|
|
TSNMPMib(FSNMPMibList[i]).Free;
|
|
|
|
FSNMPMibList.Clear;
|
2008-04-23 23:28:55 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
procedure TSNMPRec.MIBAdd(const MIB, Value: string; ValueType: Integer);
|
2008-04-23 23:28:55 +03:00
|
|
|
var
|
2008-04-23 23:39:31 +03:00
|
|
|
SNMPMib: TSNMPMib;
|
2008-04-23 23:28:55 +03:00
|
|
|
begin
|
2008-04-23 23:39:31 +03:00
|
|
|
SNMPMib := TSNMPMib.Create;
|
|
|
|
SNMPMib.OID := MIB;
|
|
|
|
SNMPMib.Value := Value;
|
2008-04-23 23:46:58 +03:00
|
|
|
SNMPMib.ValueType := ValueType;
|
2008-04-24 10:05:26 +03:00
|
|
|
FSNMPMibList.Add(SNMPMib);
|
2008-04-23 23:28:55 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
procedure TSNMPRec.MIBDelete(Index: Integer);
|
2008-04-23 23:28:55 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
if (Index >= 0) and (Index < FSNMPMibList.Count) then
|
|
|
|
begin
|
|
|
|
TSNMPMib(FSNMPMibList[Index]).Free;
|
|
|
|
FSNMPMibList.Delete(Index);
|
|
|
|
end;
|
2008-04-23 23:28:55 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
function TSNMPRec.MIBGet(const MIB: string): string;
|
2008-04-23 23:28:55 +03:00
|
|
|
var
|
2008-04-24 10:05:26 +03:00
|
|
|
i: Integer;
|
2008-04-23 23:28:55 +03:00
|
|
|
begin
|
2008-04-23 23:39:31 +03:00
|
|
|
Result := '';
|
2008-04-24 10:05:26 +03:00
|
|
|
for i := 0 to FSNMPMibList.Count - 1 do
|
|
|
|
begin
|
|
|
|
if ((TSNMPMib(FSNMPMibList[i])).OID = MIB) then
|
2008-04-23 23:39:31 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
Result := (TSNMPMib(FSNMPMibList[i])).Value;
|
|
|
|
Break;
|
2008-04-23 23:39:31 +03:00
|
|
|
end;
|
2008-04-24 10:05:26 +03:00
|
|
|
end;
|
2008-04-23 23:28:55 +03:00
|
|
|
end;
|
|
|
|
|
|
|
|
{==============================================================================}
|
|
|
|
|
|
|
|
constructor TSNMPSend.Create;
|
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
inherited Create;
|
|
|
|
FQuery := TSNMPRec.Create;
|
|
|
|
FReply := TSNMPRec.Create;
|
|
|
|
FQuery.Clear;
|
|
|
|
FReply.Clear;
|
|
|
|
FSock := TUDPBlockSocket.Create;
|
|
|
|
FSock.CreateSocket;
|
|
|
|
FTimeout := 5000;
|
|
|
|
FHost := cLocalhost;
|
|
|
|
FHostIP := '';
|
2008-04-23 23:28:55 +03:00
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TSNMPSend.Destroy;
|
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
FSock.Free;
|
|
|
|
FReply.Free;
|
|
|
|
FQuery.Free;
|
|
|
|
inherited Destroy;
|
2008-04-23 23:28:55 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
function TSNMPSend.DoIt: Boolean;
|
2008-04-23 23:28:55 +03:00
|
|
|
var
|
2008-04-24 10:05:26 +03:00
|
|
|
x: Integer;
|
2008-04-23 23:28:55 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
Result := False;
|
|
|
|
FReply.Clear;
|
|
|
|
FBuffer := Query.EncodeBuf;
|
|
|
|
FSock.Connect(FHost, cSnmpProtocol);
|
|
|
|
FHostIP := FSock.GetRemoteSinIP;
|
|
|
|
FSock.SendBuffer(PChar(FBuffer), Length(FBuffer));
|
|
|
|
if FSock.CanRead(FTimeout) then
|
|
|
|
begin
|
|
|
|
x := FSock.WaitingData;
|
|
|
|
if x > 0 then
|
|
|
|
begin
|
|
|
|
SetLength(FBuffer, x);
|
|
|
|
FSock.RecvBuffer(PChar(FBuffer), x);
|
|
|
|
Result := True;
|
2008-04-23 23:28:55 +03:00
|
|
|
end;
|
2008-04-24 10:05:26 +03:00
|
|
|
end;
|
|
|
|
if Result then
|
|
|
|
Result := FReply.DecodeBuf(FBuffer);
|
2008-04-23 23:28:55 +03:00
|
|
|
end;
|
|
|
|
|
|
|
|
{==============================================================================}
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
function SNMPGet(const Oid, Community, SNMPHost: string;
|
|
|
|
var Value: string): Boolean;
|
2008-04-23 23:28:55 +03:00
|
|
|
var
|
2008-04-24 10:05:26 +03:00
|
|
|
SNMP: TSNMPSend;
|
2008-04-23 23:28:55 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
SNMP := TSNMPSend.Create;
|
2008-04-23 23:28:55 +03:00
|
|
|
try
|
2008-04-24 10:05:26 +03:00
|
|
|
SNMP.Query.Community := Community;
|
|
|
|
SNMP.Query.PDUType := PDUGetRequest;
|
|
|
|
SNMP.Query.MIBAdd(Oid, '', ASN1_NULL);
|
|
|
|
SNMP.Host := SNMPHost;
|
|
|
|
Result := SNMP.DoIt;
|
2008-04-23 23:28:55 +03:00
|
|
|
if Result then
|
2008-04-24 10:05:26 +03:00
|
|
|
Value := SNMP.Reply.MIBGet(Oid);
|
2008-04-23 23:28:55 +03:00
|
|
|
finally
|
|
|
|
SNMP.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
function SNMPSet(const Oid, Community, SNMPHost, Value: string;
|
|
|
|
ValueType: Integer): Boolean;
|
2008-04-23 23:39:31 +03:00
|
|
|
var
|
|
|
|
SNMPSend: TSNMPSend;
|
|
|
|
begin
|
|
|
|
SNMPSend := TSNMPSend.Create;
|
|
|
|
try
|
2008-04-24 10:05:26 +03:00
|
|
|
SNMPSend.Query.Community := Community;
|
2008-04-23 23:39:31 +03:00
|
|
|
SNMPSend.Query.PDUType := PDUSetRequest;
|
|
|
|
SNMPSend.Query.MIBAdd(Oid, Value, ValueType);
|
|
|
|
SNMPSend.Host := SNMPHost;
|
2008-04-24 10:05:26 +03:00
|
|
|
Result := SNMPSend.DoIt = True;
|
2008-04-23 23:39:31 +03:00
|
|
|
finally
|
|
|
|
SNMPSend.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2008-04-23 23:28:55 +03:00
|
|
|
end.
|