Professional Documents
Culture Documents
SNMP ton tp
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.
SNMP ton tp
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.
Trang|
SNMP ton tp
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
Trang|
SNMP ton tp
// 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;
Trang|
SNMP ton tp
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 }
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 :
Trang|
SNMP ton tp
+ 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.
Trang|
SNMP ton tp
var genTrap: array[0..6] of string = ( 'coldStart(0)', 'warmStart(1)', 'linkDown(2)', 'linkUp(3)', authenticationFailure(4)', 'egpNeighborloss(5)', 'enterpriseSpecific(6)');
Nhn trap Khi click nt [Start], chng trnh s thc hin vng lp lin tc nhn trap
Trang|
SNMP ton tp
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)
Trang|
SNMP ton tp
Tm tt
+
Trang|
10