📄 snmpsynapse.pas
字号:
{ -------------------------------------------------------------------------------------}
{ A "SNMP" component for Delphi32. }
{ Copyright 2000-2001, Jean-Fabien Connault. All Rights Reserved. }
{ This component can be freely used and distributed in commercial and private }
{ environments, provided this notice is not modified in any way. }
{ -------------------------------------------------------------------------------------}
{ Feel free to contact me if you have any questions, comments or suggestions at }
{ JFConnault@mail.dotcom.fr (Jean-Fabien Connault) }
{ You can always find the latest version of this component at: }
{ http://www.worldnet.net/~cycocrew/delphi/ }
{ -------------------------------------------------------------------------------------}
{ Date last modified: 03/30/01 }
{ -------------------------------------------------------------------------------------}
{ -------------------------------------------------------------------------------------}
{ TSNMPSynapse v1.06 }
{ -------------------------------------------------------------------------------------}
{ Description: }
{ A component that encapsulates the Synapse SNMP library }
{ Properties: }
{ Public }
{ property ErrorStatus: integer; }
{ property ErrorIndex: integer; }
{ property HostIP: string; }
{ property LastOid: string; }
{ property LastValueType: TSNMPMibValueType; }
{ property NoResponseList: TStringList; }
{ property Version: integer; }
{ Published }
{ property BlockNoResponse: boolean; }
{ property Community: string; }
{ property Host: string; }
{ property Retry: integer; }
{ property Throttle: integer; }
{ property Timeout: integer; }
{ Procedures and Functions: }
{ function ConvertValueTypeToString(ASNMPMibValueType: TSNMPMibValueType): string; }
{ function ConvertStringToValueType(AString: string): TSNMPMibValueType; }
{ function ConvertStringToHexString(AString: string): string; }
{ procedure ResetNoResponseList; }
{ function SNMPGet(Oid: string): string; }
{ function SNMPGetNext: string; }
{ function SNMPSet(Oid, Value: string; ValueType: TSNMPMibValueType): string; }
{ procedure SNMPMibAdd(Oid, Value: string; SNMPMibValueType: TSNMPMibValueType); }
{ procedure SNMPMibClear; }
{ procedure SNMPMibDelete(i: integer); }
{ function SNMPMibSet: string; }
{ Needs: }
{ Synapse package from Lukas Gebauer (gebauerl@mlp.cz) }
{ }
{ See example contained in example.zip file for more details. }
{ -------------------------------------------------------------------------------------}
{ Revision History: }
{ 1.00: + Initial release (08/10/00) }
{ 1.01: + Moved some constants from SNMPSend to SNMPSynapse (08/21/00) }
{ 1.02: + Moved TSNMPMibValueType from SNMPSend to SNMPSynapse (09/17/00) }
{ 1.03: + Added ConvertValueTypeToString function (02/04/01) }
{ + Added SNMPGetNext function (02/04/01) }
{ Thanks to Adrian Gallero (Adrian.Gallero@telefonica-data.com.uy) }
{ + Added HostIP, LastOid, LastValueType and Version properties (02/04/01) }
{ Thanks to Adrian Gallero (Adrian.Gallero@telefonica-data.com.uy) }
{ 1.04: + Added SNMPMibAdd, SNMPMibClear and SNMPMibDelete procedures (03/17/01) }
{ + Added ConvertStringToValueType and SNMPMibSet functions (03/17/01) }
{ 1.05: + Added Retry and Throttle properties (03/28/01) }
{ + Added OnThrottle and OnTimeout events (03/28/01) }
{ + Changed default timeout value to 2000 milliseconds (03/28/01) }
{ 1.06: + Added BlockNoResponse and NoResponseList properties (03/30/01) }
{ + Added OnBlockNoResponse event (03/30/01) }
{ + Added ResetNoResponseList procedure (03/30/01) }
{ + Added ConvertStringToHexString function (03/30/01) }
{--------------------------------------------------------------------------------------}
unit SNMPSynapse;
{$IFNDEF WIN32}
ERROR! This unit only available on Win32!
{$ENDIF}
interface
uses
SysUtils, Windows, Classes, StdCtrls, Forms, Dialogs, Controls, SNMPSend,
ASN1Util;
type
TSNMPMibValueType = (smvtInteger, smvtOctetString, smvtNull, smvtObjectId,
smvtSequence, smvtIpAddress, smvtCounter, smvtGauge, smvtTimeTicks);
TBlockNoResponseEvent = procedure(Sender: TObject; Host: string) of object;
TThrottleEvent = procedure(Sender: TObject) of object;
TTimeoutEvent = procedure(Sender: TObject; RetryCount: integer) of object;
TSNMPSynapse = class(TComponent)
private
FBlockNoResponse: boolean;
FCommunity: string;
FErrorStatus: integer;
FErrorIndex: integer;
FHost: string;
FLastOid: string;
FLastValueType: TSNMPMibValueType;
FNoResponseList: TStringList;
FOnBlockNoResponse: TBlockNoResponseEvent;
FOnThrottle: TThrottleEvent;
FOnTimeout: TTimeoutEvent;
FRetry: integer;
FSNMPMibList: TList;
FSNMPSend: TSNMPSend;
FThrottle: integer;
FTimeout: integer;
FTimeStamp: TTimeStamp;
FVersion: integer;
procedure AddHostToNoResponseList(AHost: string);
procedure DeleteHostFromNoResponseList(AHost: string);
function IsHostInNoResponseList(AHost: string): boolean;
procedure DoThrottle;
function ConvertValueTypeToInteger(ASNMPMibValueType: TSNMPMibValueType):
integer;
function ConvertIntegerToValueType(AInteger: integer): TSNMPMibValueType;
function SNMPGetOp(Oid: string; Op: integer): string;
function GetHostIP: string;
procedure SetHost(Value: string);
procedure SetTimeout(Value: integer);
protected
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function ConvertValueTypeToString(ASNMPMibValueType: TSNMPMibValueType):
string;
function ConvertStringToValueType(AString: string): TSNMPMibValueType;
function ConvertStringToHexString(AString: string): string;
procedure ResetNoResponseList;
function SNMPGet(Oid: string): string;
function SNMPGetNext: string;
function SNMPSet(Oid, Value: string; ValueType: TSNMPMibValueType): string;
procedure SNMPMibAdd(Oid, Value: string; SNMPMibValueType:
TSNMPMibValueType);
procedure SNMPMibClear;
procedure SNMPMibDelete(i: integer);
function SNMPMibSet: string;
property ErrorStatus: integer read FErrorStatus;
property ErrorIndex: integer read FErrorIndex;
property HostIP: string read GetHostIP;
property LastOid: string read FLastOid write FLastOid;
property LastValueType: TSNMPMibValueType read FLastValueType;
property NoResponseList: TStringList read FNoResponseList write
FNoResponseList;
property Version: integer read FVersion;
published
property BlockNoResponse: boolean read FBlockNoResponse write
FBlockNoResponse;
property Community: string read FCommunity write FCommunity;
property Host: string read FHost write SetHost;
property Retry: integer read FRetry write FRetry;
property Throttle: integer read FThrottle write FThrottle;
property Timeout: integer read FTimeout write SetTimeout;
property OnBlockNoResponse: TBlockNoResponseEvent read FOnBlockNoResponse
write FOnBlockNoResponse;
property OnThrottle: TThrottleEvent read FOnThrottle write FOnThrottle;
property OnTimeout: TTimeoutEvent read FOnTimeout write FOnTimeout;
end;
procedure Register;
const
SNMP_SUCCESS = 'Success';
SNMP_FAILURE = 'Failure';
SNMP_NONE = 'None';
implementation
{***************************************************************************}
{ CONSTRUCTOR Create }
{***************************************************************************}
constructor TSNMPSynapse.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FBlockNoResponse := false;
FCommunity := 'public';
FSNMPMibList := TList.Create;
FSNMPSend := TSNMPSend.Create;
FNoResponseList := TStringList.Create;
FRetry := 1;
FThrottle := 20;
FTimeout := 2000;
FTimeStamp := DateTimeToTimeStamp(now);
FOnThrottle := nil;
FLastOid := '';
end;
{***************************************************************************}
{ DESTRUCTOR Destroy }
{***************************************************************************}
destructor TSNMPSynapse.Destroy;
var
i: integer;
begin
for i := 0 to FSNMPMibList.count - 1 do
TSNMPMib(FSNMPMibList[i]).Free;
FSNMPMibList.Free;
FSNMPSend.Free;
FNoResponseList.Free;
inherited Destroy;
end;
{***************************************************************************}
{ PROCEDURE AddHostToNoResponseList }
{***************************************************************************}
procedure TSNMPSynapse.AddHostToNoResponseList(AHost: string);
var
itempos: integer;
begin
itempos := FNoResponseList.indexOf(AHost);
if (itempos = -1) then
FNoResponseList.add(AHost);
end;
{***************************************************************************}
{ PROCEDURE DeleteHostFromNoResponseList }
{***************************************************************************}
procedure TSNMPSynapse.DeleteHostFromNoResponseList(AHost: string);
var
itempos: integer;
begin
itempos := FNoResponseList.indexOf(AHost);
if (itempos > -1) then
FNoResponseList.delete(itempos);
end;
{***************************************************************************}
{ FUNCTION IsHostInNoResponseList }
{***************************************************************************}
function TSNMPSynapse.IsHostInNoResponseList(AHost: string): boolean;
var
itempos: integer;
begin
itempos := FNoResponseList.indexOf(AHost);
if (itempos > -1) then
result := true
else
result := false;
end;
{***************************************************************************}
{ PROCEDURE ResetNoResponseList }
{***************************************************************************}
procedure TSNMPSynapse.ResetNoResponseList;
begin
FNoResponseList.Clear;
end;
{***************************************************************************}
{ PROCEDURE DoThrottle }
{***************************************************************************}
procedure TSNMPSynapse.DoThrottle;
begin
if ((DateTimeToTimeStamp(now)).Time < (FTimeStamp.Time + (1000 div FThrottle)))
then
begin
Sleep((FTimeStamp.Time + (1000 div FThrottle)) -
(DateTimeToTimeStamp(now)).Time);
if Assigned(OnThrottle) then
OnThrottle(Self);
end;
FTimeStamp := DateTimeToTimeStamp(now);
end;
{***************************************************************************}
{ FUNCTION ConvertValueTypeToInteger }
{***************************************************************************}
function TSNMPSynapse.ConvertValueTypeToInteger(ASNMPMibValueType:
TSNMPMibValueType): integer;
begin
result := ASN1_NULL;
if (ASNMPMibValueType = smvtInteger) then
result := ASN1_INT;
if (ASNMPMibValueType = smvtOctetString) then
result := ASN1_OCTSTR;
if (ASNMPMibValueType = smvtNull) then
result := ASN1_NULL;
if (ASNMPMibValueType = smvtObjectId) then
result := ASN1_OBJID;
if (ASNMPMibValueType = smvtSequence) then
result := ASN1_SEQ;
if (ASNMPMibValueType = smvtIpAddress) then
result := ASN1_IPADDR;
if (ASNMPMibValueType = smvtCounter) then
result := ASN1_COUNTER;
if (ASNMPMibValueType = smvtGauge) then
result := ASN1_GAUGE;
if (ASNMPMibValueType = smvtTimeTicks) then
result := ASN1_TIMETICKS;
end;
{***************************************************************************}
{ FUNCTION ConvertValueTypeToString }
{***************************************************************************}
function TSNMPSynapse.ConvertValueTypeToString(ASNMPMibValueType:
TSNMPMibValueType): string;
begin
result := 'ASN1_NULL';
if (ASNMPMibValueType = smvtInteger) then
result := 'ASN1_INT';
if (ASNMPMibValueType = smvtOctetString) then
result := 'ASN1_OCTSTR';
if (ASNMPMibValueType = smvtNull) then
result := 'ASN1_NULL';
if (ASNMPMibValueType = smvtObjectId) then
result := 'ASN1_OBJID';
if (ASNMPMibValueType = smvtSequence) then
result := 'ASN1_SEQ';
if (ASNMPMibValueType = smvtIpAddress) then
result := 'ASN1_IPADDR';
if (ASNMPMibValueType = smvtCounter) then
result := 'ASN1_COUNTER';
if (ASNMPMibValueType = smvtGauge) then
result := 'ASN1_GAUGE';
if (ASNMPMibValueType = smvtTimeTicks) then
result := 'ASN1_TIMETICKS';
end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -