Está en la página 1de 10

Chng

Lp trnh ng dng SNMP vi Delphi 2010


Chun b lp trnh cc phn mm SNMP Thit k phn mm nhn trap : SNMP Trap Receiver. Thit k phn mm gim st lu lng thit b : SNMP Traffic Monitor Thit k phn mm SNMP agent cho Windows server, h tr ly cc thng tin t to. Abstract Syntax Notation One (ASN.1)

SNMP ton tp

Chng 5: Lp trnh ng dng SNMP vi Delphi 2010

Chng ny tc gi s trnh by cch vit cc phn mm SNMP s dng ngn ng lp trnh Delphi phin bn 14 (Delphi 2010). Mc ch chng ny l trnh by tng tng bc trong vic vit cc ng dng SNMP hn l trnh by c php lp trnh SNMP trn Delphi. Trn cc ngn ng khc th ch khc nhau cc hm khi to, gi, nhn bn tin, cn v trnh t thc hin th vn ging nhau. Cc on code ca tc gi c vit mc n gin c th d dng c hiu v chuyn i, nn chng khng phi l khun mu c tc cao nht hay by li tt nht. Source code ca ton b cc project c th download ti trang ch ca quyn ti liu ny. Ti sao bn cn phi lp trnh SNMP ? Nhiu thit b, ng dng c cc hng thit k mib ring, bn khng th gim st chng bng ng dng snmp thng thng. Bn c th dng phn mm ca chnh hng thit b gim st. Nhng nu bn c nhiu chng loi thit b khc nhau th bt buc bn phi dng tng phn mm ring. By gi lm th no dng mt ng dng duy nht gim st tt c chng ? Lc ny bn cn bit cch lp trnh ng dng gim st SNMP. Cng c mt s phn mm cho php gim st custom mib nhng cha hn chng p ng hon ton nhu cu ca bn. Cc thit b gi cc event dng trap n mt trap host. nh ngha trap chun ch c mt s event rt ngho nn, do cc dng sn phm khc nhau u c nh ngha rt nhiu trap enterpriseSpecific m phi dng sn phm ca chnh hng mi c th c c. Nu bn c file mib m t event ca cc thit b, lm th no dng mt ng dng duy nht lm host nhn event v cnh bo cho tt c cc chng loi thit b ? Lc ny bn cn bit cch lp trnh ng dng SNMP Trap receiver. Gi s bn vit mt ng dng no , ng dng ny chy trn rt nhiu server. Ngi qun tr cn gim st hiu nng ng dng ca bn trn tt c cc server m khng cn phi truy cp vo tng server ly thng tin. Bn c th thit k giao thc v phn mm gim st ring, nhng nu s dng SNMP th ngi dng c th dng cc phn mm c sn tnh nng custom mib nh Solarwinds gim st ng dng ca bn. Lc ny bn cn bit cch lp trnh ng dng SNMP Agent b sung tnh nng ny vo ng dng ca bn. Nu bn khng phi l ngi pht trin ng dng, hoc vic dng cc phn mm gim st c sn p ng c nhu cu cng vic th bn khng cn phi c chng ny.

1. Chun b lp trnh SNMP


Delphi 2010 Delphi l ngn ng lp trnh hng i tng, c php ging vi Pascal. Mi trng lp trnh Delphi h tr thit k form dng ko th tng t nh Visual Studio. Cc i tng giao din c ng gi gi l VCL (Visual Component Library), tng t nh Controls trong C# hay Java Beans ca Java. C hng trm component trong Delphi, v chng h tr Unicode hon ton. ng dng ca Delphi vit ra l ng dng native Windows nn khng s dng .NET Framework. Bn cn ci t Delphi 2009 hoc 2010 vit cc ng dng SNMP. Indy Project Indy l mt b th vin cc component h tr lp trnh mng mc application (layer 7 trong m hnh OSI), ngha l nhng g ng dng ca bn phi x l l phn data sau khi tch ht cc header ca cc giao thc lp application. Indy h tr hu ht cc giao thc ph bin nh : TCP, UDP, IPMulticast, DNS, Echo, FTP, HTTP, IMAP4, SMTP, POP3, Telnet, ICMP, Syslog, SNMP, . Bn c th vit mt web server ch vi vi dng lnh. Indy l mt d n m ngun m c tch hp vo Delphi. M ngun Indy c vit bng Delphi bi cc tnh nguyn vin. 1 Nu khng s dng th vin Indy vit ng dng mng, bn c th s dng cc component c sn trong Delphi l TTCPServer, TTCPClient, TUDPSocket thay th. Tuy nhin lc ny bn phi t vit phn m x l d liu cc lp cao hn. V nu khng mun dng nhng component ca Delphi na th bn c th dng cc hm Windows API trong th vin Winsock.

Trang ch Indy Project : http://www.indyproject.org


Trang|

DIP THANH NGUYN, 2010

SNMP ton tp

Chng 5: Lp trnh ng dng SNMP vi Delphi 2010

Patch Indy Tiburon B Indy km theo Delphi 2010 c li trong component IdSNMP, bn cn update ln phin bn mi nht l Indy Tiburon v li, nu khng IdSNMP s hot ng sai. Indy Tiburon c th c download ti trang ch quyn ti liu ny hoc ti link gc http://indy.fulgan.com/ZIP/ Qu trnh update thc cht l xa bn Indy trong Delphi 2010 v thay th bng bn Indy Tiburon, trnh t update nh sau : + Gii nn bn Indy mi ra folder IndyTiburon. + Khi ng Delphi 2010. + M v bin dch package IndyTiburon\Lib\System\IndySystem140.dpk. + Bin dch package IndyTiburon\Lib\Core\IndyCore140.dpk. + Bin dch package IndyTiburon\Lib\Core\dclIndyCore140.dpk. + Bin dch package IndyTiburon\Lib\Protocols\IndyProtocols140.dpk. + Bin dch package IndyTiburon\Lib\Protocols\dclIndyProtocols140.dpk. + Tt Delphi. + Xa tt c file trong C:\Program Files\Embarcadero\RAD Studio\7.0\lib\Indy10 + Copy tt c cc file *.dcu trong folder IndyTiburon (k c trong subfolder) sang C:\Program Files\Embarcadero\RAD Studio\7.0\lib\Indy10 (c khong 325 file). + Copy 5 file *.bpl trong C:\Users\Public\Documents\RAD Studio\7.0\Bpl (Win 7) hoc C:\Documents and Settings\All Users\Documents\RAD Studio\7.0\Bpl (Win XP) v ghi vo cc file trong folder C:\Program Files\Embarcadero\RAD Studio\7.0\bin. + Khi ng li Delphi. Lu : phin bn Indy ti thi im vit ti liu ny l 10.5.5, l mt phin bn vn cha h tr SNMPv2c. Do cc ng dng c vit ch hot ng ng vi SNMPv1.

2. SNMP Traffic Monitor


Gii thiu SNMP Traffic Monitor l phn mm gim st lin tc lu lng ca interface trn thit b. Cc tnh nng demo bao gm : + Ly c cc thng tin m t thit b (nhm mib-2.system). + Ly danh sch cc interface v cho php ngi dng chn 1 interface gim st. + Cho php chn cc chu k ly mu khc nhau. + V lu lng ra biu , 2 ng lu lng in/out ring, t ng zoom biu khi lu lng tng. Do ch l demo tp trung vo SNMP nn phn mm c cc hn ch sau : + Khng gim st c cng lc nhiu interface. + Khng ghi nh kt qu gim st, khng in c biu . tng thc hin ly thng tin v h thng (tn, m t, thi gian hot ng, ) ta ly tt c OID nm di .iso.org.dod.internet.mgmt.mib-2.system (.1.3.6.1.2.1.1). Bn thn thit b khng cung cp thng tin v tc lu lng ca interface nn ta khng th ly trc tip bng SNMP. Ta phi ly tng s byte m interface nhn ti OID .iso.org.dod.internet.mgmt.mib2.interfaces.ifTable.ifEntry.ifInOctets (.1.3.6.1.2.1.2.2.1.10) v tng s byte truyn ti ifOutOctets (.1.3.6.1.2.1.2.2.1.16). Vic ly thng tin c thc hin lin tc v tnh (gi_tr_sau gi_tr_trc)/thi_gian_gia_cc_ln_ly_mu c c tc lu lng ca interface. khng nh hng n chng trnh chnh, phn code qut lu lng lin tc c t trong mt thread.

DIP THANH NGUYN, 2010

Trang|

SNMP ton tp

Chng 5: Lp trnh ng dng SNMP vi Delphi 2010

Thit k giao din

Ly thng tin v thit b Sau khi nhp IP v community string, nhn nt Ly thng tin phn mm ly v cc thng tin ca thit b trong nhm mib-2.system nh name, description, contact, .

mmInfo.Clear; SNMP.Host := edHost.Text; SNMP.Community := edCommunity.Text; SNMP.ReceiveTimeout := 1000; // timeout = 1000 ms // Bc 1 : dng hm QuickSend ly cc thng tin thuc group mib-2.system // mt s thit b khng h tr sub-id nn ta ly c 2 object sysDescr v sysDescr.0 // hm QuickSend s tr v TRUE nu ly thng tin thnh cng, FALSE nu timeout // kt qu ly v l mt chui cha trong bin v s := 'sysDescr : '; // sysDescr c oid l 1.3.6.1.2.1.1.1 if SNMP.QuickSend('1.3.6.1.2.1.1.1', SNMP.Community, SNMP.Host, v) or SNMP.QuickSend('1.3.6.1.2.1.1.1.0', SNMP.Community, SNMP.Host, v) then s := s + v; mmInfo.Lines.Add(s); // xut kt qu ra mmInfo s := 'sysUptime : '; if SNMP.QuickSend('1.3.6.1.2.1.1.3', SNMP.Community, SNMP.Host, v) or SNMP.QuickSend('1.3.6.1.2.1.1.3.0', SNMP.Community, SNMP.Host, v) then s := s + v; mmInfo.Lines.Add(s); s := 'sysContact : '; if SNMP.QuickSend('1.3.6.1.2.1.1.4', SNMP.Community, SNMP.Host, v) or SNMP.QuickSend('1.3.6.1.2.1.1.4.0', SNMP.Community, SNMP.Host, v) then s := s + v; mmInfo.Lines.Add(s); s := 'sysName : '; if SNMP.QuickSend('1.3.6.1.2.1.1.5', SNMP.Community, SNMP.Host, v) or SNMP.QuickSend('1.3.6.1.2.1.1.5.0', SNMP.Community, SNMP.Host, v) then s := s + v; mmInfo.Lines.Add(s); s := 'sysLocation : '; if SNMP.QuickSend('1.3.6.1.2.1.1.6', SNMP.Community, SNMP.Host, v) or

DIP THANH NGUYN, 2010

Trang|

SNMP ton tp

Chng 5: Lp trnh ng dng SNMP vi Delphi 2010

SNMP.QuickSend('1.3.6.1.2.1.1.6.0', SNMP.Community, SNMP.Host, v) then s := s + v; mmInfo.Lines.Add(s);

Cc thng tin ly c s c xut ra mn hnh nh v d sau

Ly s lng interface ca thit b

// Bc 2 : ly tng s interface ang c trn thit b s := 'ifNumber : '; if SNMP.QuickSend('1.3.6.1.2.1.2.1', SNMP.Community, SNMP.Host, v) or SNMP.QuickSend('1.3.6.1.2.1.2.1.0', SNMP.Community, SNMP.Host, v) then s := s + v; mmInfo.Lines.Add(s); // nu s ifNumber l rng hoc khng phi l kiu s th gn = 0 if not TryStrToInt(v, ifNumber) then ifNumber := 0;

Ly danh sch interface index


// Bc 3 : Ln lt ly index ca cc interface entry, bng cch GetNext lin tc, bt u t ifIndex SNMP.Query.Host := edHost.Text; SNMP.Query.Community := edCommunity.Text; SNMP.Query.Port := 161; SNMP.Query.Version := 0; // s dng SNMP v1 (0=v1; 1=v2c) SNMP.Query.PDUType := PDUGetNextRequest; List.Clear; // ifIndex c OID l 1.3.6.1.2.1.2.1.1, khi GetNext s ly c ifIndex ca interface u tin OID := '1.3.6.1.2.1.2.1.1'; // v tng s interface ca thit b = ifNumber nn ta lp ifNumber ln for i := 1 to ifNumber do begin SNMP.Query.MIBOID.Clear; // u tin xa sanh sch OID cn query SNMP.Query.MIBOID.Add(OID); // sau thm ifIndex ca interface cn query j := 1; while (j<=3) and (SNMP.SendQuery=False) do inc(j); // nu fail th cho php lp li n ln th 3 if j <= 3 then begin L := List.Items.Add; // thm 1 dng mi vo danh sch interface L.Caption := SNMP.Reply.MIBValue[0]; // gn ifIndex vo Caption ca ListItem { t OID = ifIndex ca interface hin ti, vng lp sau s GetNext ly ifIndex ca interface tip theo } OID := SNMP.Reply.MIBOID[0]; end; end;

Ly thng tin ca interface


// Bc 4 : ly cc thng tin khc ca tng interface SNMP.Query.PDUType := PDUGetRequest; for i := 0 to List.Items.Count - 1 do

DIP THANH NGUYN, 2010

Trang|

SNMP ton tp

Chng 5: Lp trnh ng dng SNMP vi Delphi 2010

begin L := List.Items[i]; { a cc oid cn ly vo query, sau thc hin 1 GetRequest ly ton b thng tin ca 1 interface cng lc } SNMP.Query.MIBOID.Clear; SNMP.Query.MIBOID.Add('1.3.6.1.2.1.2.2.1.2.' + L.Caption); // ifDescr SNMP.Query.MIBOID.Add('1.3.6.1.2.1.2.2.1.3.' + L.Caption); // ifType SNMP.Query.MIBOID.Add('1.3.6.1.2.1.2.2.1.5.' + L.Caption); // ifSpeed SNMP.Query.MIBOID.Add('1.3.6.1.2.1.2.2.1.6.' + L.Caption); // ifPhysAddress SNMP.Query.MIBOID.Add('1.3.6.1.2.1.2.2.1.7.' + L.Caption); // ifAdminStatus SNMP.Query.MIBOID.Add('1.3.6.1.2.1.2.2.1.8.' + L.Caption); // ifOperStatus // nu fail th cho php lp li n ln th 3 j := 1; while (j<=3) and (SNMP.SendQuery=False) do inc(j); if j <= 3 then try L.SubItems.Add(SNMP.Reply.MIBValue[0]); // ifDescr l kiu OctetString t := StrToInt(SNMP.Reply.MIBValue[1]); // ifType l kiu INTEGER if t <= Length(ifTypeArray) then L.SubItems.Add(ifTypeArray[t]) else L.SubItems.Add(InttoStr(t)); // ifSpeed l kiu GAUGE nn phi chia 100 L.SubItems.Add(InttoStr(StrToInt(SNMP.Reply.MIBValue[2]) div 100)); // ifPhysAddress l kiu PhysicalAddress nn phi chuyn i thnh chui c c L.SubItems.Add(PhysAddressToStr(SNMP.Reply.MIBValue[3])); t := StrToInt(SNMP.Reply.MIBValue[4]); // ifAdminStatus, kiu INTEGER if t <= Length(ifStatusArray) then L.SubItems.Add(ifStatusArray[t]) else L.SubItems.Add(InttoStr(t)); t := StrToInt(SNMP.Reply.MIBValue[5]); // ifOperStatus, kiu INTEGER if t <= Length(ifStatusArray) then L.SubItems.Add(ifStatusArray[t]) else L.SubItems.Add(InttoStr(t)); except end; { thng thng tt c cc oid nm trong request u c snmp agent tr v y v ng th t, on code trn khng kim tra s lng value tr v v khng kim tra th t }

Danh sch cc interface xut ra mn hnh nh v d sau

Qut lu lng Sau khi c danh sch cc interface index, bn click vo mt interface trong danh sch v nhn nt Bt u gim st, phn mm s to snmp query ly thng tin ifInOctets v ifOutOctets ca interface index ang chn, qut lin tc theo chu k. on code nh sau :

snmp.Query.PDUType := PDUGetRequest; snmp.Query.MIBOID.Clear; // ifInOctets c oid l 1.3.6.1.2.1.2.2.1.10, dng ang c chn trong List l i tng List.Selected, caption ca dng ny chnh l interface index snmp.Query.MIBOID.Add('1.3.6.1.2.1.2.2.1.10.' + List.Selected.Caption); snmp.Query.MIBOID.Add('1.3.6.1.2.1.2.2.1.16.' + List.Selected.Caption); // gi snmp query, ly 2 item ifInOctets v ifOutOctets cng lc snmp.SendQuery;

Sau khi c thng tin tng s byte truyn v nhn theo tng chu k ly mu, bn v chng ra trn biu . Cch x l thng tin v v biu khng nm trong phm vi quyn ti liu ny, bn hy xem thm trong source code. Hnh minh ha biu nh sau :

DIP THANH NGUYN, 2010

Trang|

SNMP ton tp

Chng 5: Lp trnh ng dng SNMP vi Delphi 2010

3. SNMP Trap Receiver


Gii thiu SNMP Trap receiver l phn mm c kh nng nhn v hin th cc trap version 1. tng thc hin + Dng component IdSNMP ca Delphi nhn trap port 162. + Khi mt trap c nhn th phn mm s xem c phi version ca trap l v1 hay khng, nu l v1 th bn tin trap s c tip tc x l. + Bn tin trap s c hin th ra mn hnh vi y cc thuc tnh. Chng 4 trnh by cu trc ca mt bn tin trap v1, gm : enterprise, agent-addr, generic-trap, specific-trap, time-stamp v cc variable-bindings. + Nu trap c kiu generic th phn mm s hin th tn trap. Chng 1 lit k 7 gi tr generic gm : coldStart(0), warmStart(1), linkDown(2), linkUp(3), authenticationFailure(4), egpNeighborloss(5), enterpriseSpecific(6). + Trong cc oid c trong bn tin trap, nu oid no l con ca mib-2.system v mib-2.interfaces th n s c hin th tn thay v oid. Thit k giao din

+ Nt [Start] bt u nhn v x l trap. + Checkbox [Nhn trap enterpriseSpecific] l ty chn c hin th cc trap c generic = 6 hay khng. Cu trc d liu c th hin th tn ca cc trap generic - v d hin th Generic : linkDown(2) thay v Generic : 2 th ta cn mt mng nh ngha chng.

DIP THANH NGUYN, 2010

Trang|

SNMP ton tp

Chng 5: Lp trnh ng dng SNMP vi Delphi 2010

var genTrap: array[0..6] of string = ( 'coldStart(0)', 'warmStart(1)', 'linkDown(2)', 'linkUp(3)', authenticationFailure(4)', 'egpNeighborloss(5)', 'enterpriseSpecific(6)');

c th hin th tn ca cc oid v d hin th sysDescr thay v .1.3.6.1.2.1.1.1 ta cn khi to th tc nh ngha chng


var oid: TstringList; procedure TfrmMain.InitCommonOID; begin oid := TStringList.Create; oid.Delimiter := '='; oid.Add('1.3.6.1.2.1.1.1=sysDescr'); oid.Add('1.3.6.1.2.1.1.2=sysObjectID'); oid.Add('1.3.6.1.2.1.1.3=sysUptime'); oid.Add('1.3.6.1.2.1.1.4=sysContact'); oid.Add('1.3.6.1.2.1.1.5=sysName'); oid.Add('1.3.6.1.2.1.1.6=sysLocation'); oid.Add('1.3.6.1.2.1.1.7=sysServices'); oid.Add('1.3.6.1.2.1.2.1=ifNumber'); oid.Add('1.3.6.1.2.1.2.2.1.1=ifIndex'); oid.Add('1.3.6.1.2.1.2.2.1.2=ifDescr'); oid.Add('1.3.6.1.2.1.2.2.1.3=ifType'); oid.Add('1.3.6.1.2.1.2.2.1.4=ifMtu'); oid.Add('1.3.6.1.2.1.2.2.1.5=ifSpeed'); oid.Add('1.3.6.1.2.1.2.2.1.6=ifPhysAddress'); oid.Add('1.3.6.1.2.1.2.2.1.7=ifAdminStatus'); oid.Add('1.3.6.1.2.1.2.2.1.8=ifOperStatus'); oid.Add('1.3.6.1.2.1.2.2.1.9=ifLastChange'); oid.Add('1.3.6.1.2.1.2.2.1.10=ifInOctets'); oid.Add('1.3.6.1.2.1.2.2.1.11=ifInUcastPkts'); oid.Add('1.3.6.1.2.1.2.2.1.12=ifInNUcastPkts'); oid.Add('1.3.6.1.2.1.2.2.1.13=ifInDiscards'); oid.Add('1.3.6.1.2.1.2.2.1.14=ifInErrors'); oid.Add('1.3.6.1.2.1.2.2.1.15=ifInUknownProtos'); oid.Add('1.3.6.1.2.1.2.2.1.16=ifOutOctets'); oid.Add('1.3.6.1.2.1.2.2.1.17=ifOutUcastPkts'); oid.Add('1.3.6.1.2.1.2.2.1.18=ifOutNUcastPkts'); oid.Add('1.3.6.1.2.1.2.2.1.19=ifOutDiscards'); oid.Add('1.3.6.1.2.1.2.2.1.20=ifOutErrors'); oid.Add('1.3.6.1.2.1.2.2.1.21=ifOutQLen'); oid.Add('1.3.6.1.2.1.2.2.1.22=ifSpecific'); end;

Nhn trap Khi click nt [Start], chng trnh s thc hin vng lp lin tc nhn trap

DIP THANH NGUYN, 2010

Trang|

SNMP ton tp

Chng 5: Lp trnh ng dng SNMP vi Delphi 2010

procedure TfrmMain.bbtStartClick(Sender: TObject); begin snmp.ReceiveTimeout := 100; // thi gian timeout cho hm ReceiveTrap l 100ms snmp.Active := True; bbtStart.Enabled := False; bbtStop.Enabled := not bbtStart.Enabled; while snmp.Active do begin while snmp.ReceiveTrap = 1 do // =1 nu nhn c trap trong vng 100ms if (ckTrapv1.Checked and (snmp.Trap.Version = 0)) then begin ProcessTrapv1(snmp.Trap); // hm x l bn tin trap v1 end; Sleep(1); // nu khng tm dng 1ms th vng lp s chim 100% CPU Application.ProcessMessages; end; end;

Hm x l trap v1 nh sau
procedure TfrmMain.ProcessTrapv1(var trap: TSNMPInfo); var i,j: integer; s: string; begin // nu nhn bn tin trap c generic = 6 (enterpriseSpecific) m checkbox [Nhn trap enterpriseSpecific] khng c chn th ngng x l if (not ckSpecific.Checked) and (Trap.GenTrap = 6) then Exit; mm.Lines.Add('+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+'); mm.Lines.Add( 'Host: ' + Trap.Host + TAB + 'Version: ' + GetTrapVersionStr(Trap.Version) + TAB + 'Enterprise: ' + trap.Enterprise); mm.Lines.Add( 'Generic: ' + genTrap[Trap.GenTrap] + TAB + 'Specific: ' + InttoStr(Trap.SpecTrap) + TAB + 'TimeStamp: ' + InttoStr(Trap.TimeTicks)); // nu bn tin trap c cha variable-bindings (cc cp oid-value) th ValueCount l tng s cc oid-value cha trong trap if Trap.ValueCount = 0 then mm.Lines.Add('(no values)') else for i := 0 to Trap.ValueCount - 1 do begin // nu oid l mt trong cc oid nm di mib-2.system v mib-2.interfaces th hin th tn ca oid j := isCommonOID(Trap.ValueOID[i]); if j > -1 then s := Replacestr(Trap.ValueOID[i], oid.Names[j], oid.ValueFromIndex[j]) else s := Trap.ValueOID[i]; mm.Lines.Add(' ' + s + TAB + '=' + TAB + Trap.Value[i]); end; mm.Lines.Add(''); Trap.Clear; end;

4. SNMP Agent
(in progress)

5. Abstract Syntax Notation One (ASN.1)


(in progress)

DIP THANH NGUYN, 2010

Trang|

SNMP ton tp

Chng 5: Lp trnh ng dng SNMP vi Delphi 2010

Tm tt
+

DIP THANH NGUYN, 2010

Trang|

10