⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snmpsynapse.pas

📁 delphi写的mib browser 源码,界面友好!
💻 PAS
📖 第 1 页 / 共 2 页
字号:

{***************************************************************************}
{ FUNCTION ConvertIntegerToValueType                                        }
{***************************************************************************}

function TSNMPSynapse.ConvertIntegerToValueType(AInteger: integer):
  TSNMPMibValueType;
begin
  result := smvtNull;
  if (AInteger = ASN1_INT) then
    result := smvtInteger;
  if (AInteger = ASN1_OCTSTR) then
    result := smvtOctetString;
  if (AInteger = ASN1_NULL) then
    result := smvtNull;
  if (AInteger = ASN1_OBJID) then
    result := smvtObjectId;
  if (AInteger = ASN1_SEQ) then
    result := smvtSequence;
  if (AInteger = ASN1_IPADDR) then
    result := smvtIpAddress;
  if (AInteger = ASN1_COUNTER) then
    result := smvtCounter;
  if (AInteger = ASN1_GAUGE) then
    result := smvtGauge;
  if (AInteger = ASN1_TIMETICKS) then
    result := smvtTimeTicks;
end;

{***************************************************************************}
{ FUNCTION ConvertStringToValueType                                         }
{***************************************************************************}

function TSNMPSynapse.ConvertStringToValueType(AString: string):
  TSNMPMibValueType;
begin
  result := smvtNull;
  if (AString = 'ASN1_INT') then
    result := smvtInteger;
  if (AString = 'ASN1_OCTSTR') then
    result := smvtOctetString;
  if (AString = 'ASN1_NULL') then
    result := smvtNull;
  if (AString = 'ASN1_OBJID') then
    result := smvtObjectId;
  if (AString = 'ASN1_SEQ') then
    result := smvtSequence;
  if (AString = 'ASN1_IPADDR') then
    result := smvtIpAddress;
  if (AString = 'ASN1_COUNTER') then
    result := smvtCounter;
  if (AString = 'ASN1_GAUGE') then
    result := smvtGauge;
  if (AString = 'ASN1_TIMETICKS') then
    result := smvtTimeTicks;
end;

{***************************************************************************}
{ FUNCTION ConvertStringToHexString                                         }
{***************************************************************************}

function TSNMPSynapse.ConvertStringToHexString(AString: string): string;
var
  i, j: integer;
  S: string;
begin
  result := '';
  for i := 1 to length(AString) do
  begin
    if not odd(i) then
    begin
      S := copy(AString, i - 1, 2);
      j := strToInt('$' + S);
      result := result + char(j);
    end;
  end;
end;

{***************************************************************************}
{ FUNCTION SNMPGetOp                                                        }
{***************************************************************************}

function TSNMPSynapse.SNMPGetOp(Oid: string; Op: integer): string;
var
  r: integer;
begin
  if not ((IsHostInNoResponseList(FHost) = true) and (FBlockNoResponse = true))
    then
  begin
    FSNMPSend.Query.Clear;
    FSNMPSend.Query.Community := FCommunity;
    FSNMPSend.Query.PDUType := Op;
    FSNMPSend.Query.MIBAdd(Oid, '', ASN1_NULL);
    DoThrottle;
    for r := 0 to FRetry do
    begin
      if (FSNMPSend.DoIt = true) then
      begin
        DeleteHostFromNoResponseList(FHost);
        if (FSNMPSend.Reply.SNMPMibList.Count > 0) then
        begin
          result :=
            FSNMPSend.Reply.MIBGet(TSNMPMib(FSNMPSend.Reply.SNMPMibList[0]).OID);
          FLastOid := TSNMPMib(FSNMPSend.Reply.SNMPMibList[0]).OID;
          FVersion := FSNMPSend.Reply.Version;
          FLastValueType :=
            ConvertIntegerToValueType(TSNMPMib(FSNMPSend.Reply.SNMPMibList[0]).ValueType);
        end
        else
          result := SNMP_NONE;
        break;
      end
      else
      begin
        result := SNMP_FAILURE;
        if Assigned(OnTimeout) then
          OnTimeout(Self, r);
      end;
    end;
    if (r = FRetry) and (result = SNMP_FAILURE) then
      AddHostToNoResponseList(FHost);
    FErrorStatus := FSNMPSend.Reply.ErrorStatus;
    FErrorIndex := FSNMPSend.Reply.ErrorIndex;
    inc(FSNMPSend.Query.ID);
  end
  else
  begin
    if Assigned(OnBlockNoResponse) then
      OnBlockNoResponse(Self, FHost);
  end;
end;

{***************************************************************************}
{ FUNCTION SNMPGet                                                          }
{***************************************************************************}

function TSNMPSynapse.SNMPGet(Oid: string): string;
begin
  result := SNMPGetOp(Oid, PDUGetRequest);
end;

{***************************************************************************}
{ FUNCTION SNMPGetNext                                                      }
{***************************************************************************}

function TSNMPSynapse.SNMPGetNext: string;
begin
  result := SNMPGetOp(FLastOid, PDUGetNextRequest);
end;

{***************************************************************************}
{ FUNCTION SNMPSet                                                          }
{***************************************************************************}

function TSNMPSynapse.SNMPSet(Oid, Value: string; ValueType: TSNMPMibValueType):
  string;
var
  r: integer;
begin
  if not ((IsHostInNoResponseList(FHost) = true) and (FBlockNoResponse = true))
    then
  begin
    FSNMPSend.Query.Clear;
    FSNMPSend.Query.Community := FCommunity;
    FSNMPSend.Query.PDUType := PDUSetRequest;
    DoThrottle;
    FSNMPSend.Query.MIBAdd(Oid, Value, ConvertValueTypeToInteger(ValueType));
    for r := 0 to FRetry do
    begin
      if (FSNMPSend.DoIt = true) then
      begin
        DeleteHostFromNoResponseList(FHost);
        result := SNMP_SUCCESS;
        break;
      end
      else
      begin
        result := SNMP_FAILURE;
        if Assigned(OnTimeout) then
          OnTimeout(Self, r);
      end;
    end;
    if (r = FRetry) and (result = SNMP_FAILURE) then
      AddHostToNoResponseList(FHost);
    FErrorStatus := FSNMPSend.Query.ErrorStatus;
    FErrorIndex := FSNMPSend.Query.ErrorIndex;
    inc(FSNMPSend.Query.ID);
  end
  else
  begin
    if Assigned(OnBlockNoResponse) then
      OnBlockNoResponse(Self, FHost);
  end;
end;

{***************************************************************************}
{ FUNCTION GetHostIP                                                        }
{***************************************************************************}

function TSNMPSynapse.GetHostIP: string;
begin
  result := FSNMPSend.HostIP;
end;

{***************************************************************************}
{ PROCEDURE SNMPMibAdd                                                      }
{***************************************************************************}

procedure TSNMPSynapse.SNMPMibAdd(Oid, Value: string; SNMPMibValueType:
  TSNMPMibValueType);
var
  ASNMPMib: TSNMPMib;
begin
  ASNMPMib := TSNMPMib.Create;
  ASNMPMib.OID := Oid;
  ASNMPMib.Value := Value;
  ASNMPMib.ValueType := ConvertValueTypeToInteger(SNMPMibValueType);
  FSNMPMibList.Add(ASNMPMib);
end;

{***************************************************************************}
{ PROCEDURE SNMPMibClear                                                    }
{***************************************************************************}

procedure TSNMPSynapse.SNMPMibClear;
var
  i: integer;
begin
  for i := 0 to FSNMPMibList.count - 1 do
    TSNMPMib(FSNMPMibList[i]).Free;
  FSNMPMibList.Clear;
end;

{***************************************************************************}
{ PROCEDURE SNMPMibDelete                                                   }
{***************************************************************************}

procedure TSNMPSynapse.SNMPMibDelete(i: integer);
begin
  if (i >= 0) and (i < FSNMPMibList.count) then
  begin
    TSNMPMib(FSNMPMibList[i]).Free;
    FSNMPMibList.Delete(i);
  end;
end;

{***************************************************************************}
{ FUNCTION SNMPMibSet                                                       }
{***************************************************************************}

function TSNMPSynapse.SNMPMibSet: string;
var
  i, r: integer;
begin
  if (FSNMPMibList.count > 0) then
  begin
    if not ((IsHostInNoResponseList(FHost) = true) and (FBlockNoResponse = true))
      then
    begin
      FSNMPSend.Query.Clear;
      FSNMPSend.Query.Community := FCommunity;
      FSNMPSend.Query.PDUType := PDUSetRequest;
      for i := 0 to FSNMPMibList.count - 1 do
        FSNMPSend.Query.MIBAdd(TSNMPMib(FSNMPMibList[i]).Oid,
          TSNMPMib(FSNMPMibList[i]).Value, TSNMPMib(FSNMPMibList[i]).ValueType);
      DoThrottle;
      for r := 0 to FRetry do
      begin
        if (FSNMPSend.DoIt = true) then
        begin
          DeleteHostFromNoResponseList(FHost);
          result := SNMP_SUCCESS;
          break;
        end
        else
        begin
          result := SNMP_FAILURE;
          if Assigned(OnTimeout) then
            OnTimeout(Self, r);
        end;
      end;
      if (r = FRetry) and (result = SNMP_FAILURE) then
        AddHostToNoResponseList(FHost);
      FErrorStatus := FSNMPSend.Query.ErrorStatus;
      FErrorIndex := FSNMPSend.Query.ErrorIndex;
      inc(FSNMPSend.Query.ID);
    end
    else
    begin
      if Assigned(OnBlockNoResponse) then
        OnBlockNoResponse(Self, FHost);
    end;
  end
  else
    result := SNMP_NONE;
end;

{*****************************************************************************}
{* PROCEDURE SetHost                                                         *}
{*****************************************************************************}

procedure TSNMPSynapse.SetHost(Value: string);
begin
  FHost := Value;
  FSNMPSend.Host := Value;
end;

{*****************************************************************************}
{* PROCEDURE SetTimeout                                                      *}
{*****************************************************************************}

procedure TSNMPSynapse.SetTimeout(Value: integer);
begin
  FTimeout := Value;
  FSNMPSend.Timeout := Value;
end;

{***************************************************************************}
{ PROCEDURE Register                                                        }
{***************************************************************************}

procedure Register;
begin
  RegisterComponents('System', [TSNMPSynapse]);
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -