免費的 SNMP LIB => http://sourceforge.net/projects/snmpsharpnet/files/
API Document =>http://www.docs.snmpsharpnet.com/docs-0-9-1/
在 Examples 頁籤中,有簡單的範例:
Demonstrate how to use SNMP#Net library.
- How to make an SNMP Set request
- Receive SNMP version 1 and 2c Trap notifications
- SNMP Version 1 or 2c Get request
- Walk operation with SNMP version 1 and 2c
- SimpleSnmp with VB.Net
- Simple SNMP with SimpleSnmp class
- Sending SNMP Version 3 traps
- Send SNMP version 1, 2c and 3 traps
- SNMP Version 1 Low Level Packet Class
- SNMP Version 2 Low Level Packet Class
- SNMP Version 3 Low Level Packet Class
- SNMP version 3 notifications (traps and informs)
- Async Receive SNMP version 1 traps and 2c traps and informs
- How to get a table from a SNMP Agent using SNMPv2c and GetBulk
- SnmpSharpNet in IronPython
以下,我使用該Lib ,為ASI_Guard 撰寫了一個僅使用Request 資訊的使用者物件
如何使用:
1:
2: /* IP 預設值 */
3: string IP_AsiGuard1 = "10.5.120.101";
4: string IP_AsiGuard2 = "10.5.120.102";
5: string IP_IPGuard1 = "10.5.120.100";
6:
7: private void StartSnmpPulling()
8: {
9: /* 啟動 SNMP pulling */
10: uiAsiGuard1.run(IP_AsiGuard1);
11: uiAsiGuard2.run(IP_AsiGuard2);
12: uiIpGuard1.run(IP_IPGuard1);
13: }
uiAsiGuard.Designer.cs
1: namespace TVBS_SSNMP
2: {
3: partial class uiAsiGuard
4: {
5: /// <summary>
6: /// 設計工具所需的變數。
7: /// </summary>
8: private System.ComponentModel.IContainer components = null;
9:
10: /// <summary>
11: /// 清除任何使用中的資源。
12: /// </summary>
13: /// <param name="disposing">如果應該處置 Managed 資源則為 true,否則為 false。</param>
14: protected override void Dispose(bool disposing)
15: {
16: if (disposing && (components != null))
17: {
18: components.Dispose();
19: }
20: base.Dispose(disposing);
21: }
22:
23: #region 元件設計工具產生的程式碼
24:
25: /// <summary>
26: /// 此為設計工具支援所需的方法 - 請勿使用程式碼編輯器
27: /// 修改這個方法的內容。
28: /// </summary>
29: private void InitializeComponent()
30: {
31: this.components = new System.ComponentModel.Container();
32: this.panel4 = new System.Windows.Forms.Panel();
33: this.label_IpAddr = new System.Windows.Forms.Label();
34: this.label9 = new System.Windows.Forms.Label();
35: this.panel7 = new System.Windows.Forms.Panel();
36: this.lab_opMode = new System.Windows.Forms.Label();
37: this.lab_swMode = new System.Windows.Forms.Label();
38: this.label12 = new System.Windows.Forms.Label();
39: this.label111 = new System.Windows.Forms.Label();
40: this.panel5 = new System.Windows.Forms.Panel();
41: this.panel9 = new System.Windows.Forms.Panel();
42: this.shapeContainer2 = new Microsoft.VisualBasic.PowerPacks.ShapeContainer();
43: this.lineShape3 = new Microsoft.VisualBasic.PowerPacks.LineShape();
44: this.lineShape2 = new Microsoft.VisualBasic.PowerPacks.LineShape();
45: this.lineShape1 = new Microsoft.VisualBasic.PowerPacks.LineShape();
46: this.labIP2 = new System.Windows.Forms.Label();
47: this.label5 = new System.Windows.Forms.Label();
48: this.label6 = new System.Windows.Forms.Label();
49: this.label1 = new System.Windows.Forms.Label();
50: this.label2 = new System.Windows.Forms.Label();
51: this.labIP1 = new System.Windows.Forms.Label();
52: this.shapeContainer3 = new Microsoft.VisualBasic.PowerPacks.ShapeContainer();
53: this.LED_Run = new Microsoft.VisualBasic.PowerPacks.OvalShape();
54: this.timer1 = new System.Windows.Forms.Timer(this.components);
55: this.panel4.SuspendLayout();
56: this.panel7.SuspendLayout();
57: this.panel5.SuspendLayout();
58: this.panel9.SuspendLayout();
59: this.SuspendLayout();
60: //
61: // panel4
62: //
63: this.panel4.BackgroundImage = global::TVBS_SSNMP.Properties.Resources.ASI_Guard;
64: this.panel4.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
65: this.panel4.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
66: this.panel4.Controls.Add(this.label_IpAddr);
67: this.panel4.Controls.Add(this.label9);
68: this.panel4.Controls.Add(this.panel7);
69: this.panel4.Controls.Add(this.panel5);
70: this.panel4.Controls.Add(this.shapeContainer3);
71: this.panel4.Location = new System.Drawing.Point(0, 0);
72: this.panel4.Name = "panel4";
73: this.panel4.Padding = new System.Windows.Forms.Padding(5);
74: this.panel4.Size = new System.Drawing.Size(970, 80);
75: this.panel4.TabIndex = 6;
76: this.panel4.DoubleClick += new System.EventHandler(this.panel4_DoubleClick);
77: //
78: // label_IpAddr
79: //
80: this.label_IpAddr.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
81: | System.Windows.Forms.AnchorStyles.Right)));
82: this.label_IpAddr.BackColor = System.Drawing.Color.Transparent;
83: this.label_IpAddr.Font = new System.Drawing.Font("新細明體", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(136)));
84: this.label_IpAddr.ForeColor = System.Drawing.SystemColors.ControlDarkDark;
85: this.label_IpAddr.Location = new System.Drawing.Point(759, 58);
86: this.label_IpAddr.Name = "label_IpAddr";
87: this.label_IpAddr.Size = new System.Drawing.Size(200, 13);
88: this.label_IpAddr.TabIndex = 10;
89: this.label_IpAddr.Text = "0.0.0.0";
90: //
91: // label9
92: //
93: this.label9.AutoSize = true;
94: this.label9.BackColor = System.Drawing.Color.Transparent;
95: this.label9.Font = new System.Drawing.Font("新細明體", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(136)));
96: this.label9.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(192)))), ((int)(((byte)(0)))));
97: this.label9.Location = new System.Drawing.Point(170, 7);
98: this.label9.Name = "label9";
99: this.label9.Size = new System.Drawing.Size(40, 12);
100: this.label9.TabIndex = 7;
101: this.label9.Text = "Inputs";
102: //
103: // panel7
104: //
105: this.panel7.BackColor = System.Drawing.Color.Transparent;
106: this.panel7.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
107: this.panel7.Controls.Add(this.lab_opMode);
108: this.panel7.Controls.Add(this.lab_swMode);
109: this.panel7.Controls.Add(this.label12);
110: this.panel7.Controls.Add(this.label111);
111: this.panel7.Location = new System.Drawing.Point(366, 20);
112: this.panel7.Name = "panel7";
113: this.panel7.Size = new System.Drawing.Size(254, 47);
114: this.panel7.TabIndex = 6;
115: //
116: // lab_opMode
117: //
118: this.lab_opMode.AutoSize = true;
119: this.lab_opMode.BackColor = System.Drawing.Color.Transparent;
120: this.lab_opMode.Font = new System.Drawing.Font("新細明體", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(136)));
121: this.lab_opMode.ForeColor = System.Drawing.Color.YellowGreen;
122: this.lab_opMode.Location = new System.Drawing.Point(121, 9);
123: this.lab_opMode.Name = "lab_opMode";
124: this.lab_opMode.Size = new System.Drawing.Size(121, 12);
125: this.lab_opMode.TabIndex = 0;
126: this.lab_opMode.Text = " ";
127: //
128: // lab_swMode
129: //
130: this.lab_swMode.AutoSize = true;
131: this.lab_swMode.BackColor = System.Drawing.Color.Transparent;
132: this.lab_swMode.Font = new System.Drawing.Font("新細明體", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(136)));
133: this.lab_swMode.ForeColor = System.Drawing.Color.YellowGreen;
134: this.lab_swMode.Location = new System.Drawing.Point(123, 29);
135: this.lab_swMode.Name = "lab_swMode";
136: this.lab_swMode.Size = new System.Drawing.Size(121, 12);
137: this.lab_swMode.TabIndex = 0;
138: this.lab_swMode.Text = " ";
139: //
140: // label12
141: //
142: this.label12.AutoSize = true;
143: this.label12.BackColor = System.Drawing.Color.Transparent;
144: this.label12.Font = new System.Drawing.Font("新細明體", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(136)));
145: this.label12.ForeColor = System.Drawing.SystemColors.ButtonHighlight;
146: this.label12.Location = new System.Drawing.Point(12, 9);
147: this.label12.Name = "label12";
148: this.label12.Size = new System.Drawing.Size(103, 12);
149: this.label12.TabIndex = 0;
150: this.label12.Text = "Operation Mode :";
151: //
152: // label111
153: //
154: this.label111.AutoSize = true;
155: this.label111.BackColor = System.Drawing.Color.Transparent;
156: this.label111.Font = new System.Drawing.Font("新細明體", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(136)));
157: this.label111.ForeColor = System.Drawing.SystemColors.ButtonHighlight;
158: this.label111.Location = new System.Drawing.Point(14, 29);
159: this.label111.Name = "label111";
160: this.label111.Size = new System.Drawing.Size(89, 12);
161: this.label111.TabIndex = 0;
162: this.label111.Text = "Switch Mode : ";
163: //
164: // panel5
165: //
166: this.panel5.BackColor = System.Drawing.Color.Transparent;
167: this.panel5.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
168: this.panel5.Controls.Add(this.panel9);
169: this.panel5.Controls.Add(this.labIP2);
170: this.panel5.Controls.Add(this.label5);
171: this.panel5.Controls.Add(this.label6);
172: this.panel5.Controls.Add(this.label1);
173: this.panel5.Controls.Add(this.label2);
174: this.panel5.Controls.Add(this.labIP1);
175: this.panel5.Location = new System.Drawing.Point(171, 20);
176: this.panel5.Name = "panel5";
177: this.panel5.Size = new System.Drawing.Size(181, 47);
178: this.panel5.TabIndex = 6;
179: //
180: // panel9
181: //
182: this.panel9.BackColor = System.Drawing.Color.Transparent;
183: this.panel9.Controls.Add(this.shapeContainer2);
184: this.panel9.Dock = System.Windows.Forms.DockStyle.Right;
185: this.panel9.Location = new System.Drawing.Point(143, 0);
186: this.panel9.Name = "panel9";
187: this.panel9.Size = new System.Drawing.Size(36, 45);
188: this.panel9.TabIndex = 6;
189: //
190: // shapeContainer2
191: //
192: this.shapeContainer2.Location = new System.Drawing.Point(0, 0);
193: this.shapeContainer2.Margin = new System.Windows.Forms.Padding(0);
194: this.shapeContainer2.Name = "shapeContainer2";
195: this.shapeContainer2.Shapes.AddRange(new Microsoft.VisualBasic.PowerPacks.Shape[] {
196: this.lineShape3,
197: this.lineShape2,
198: this.lineShape1});
199: this.shapeContainer2.Size = new System.Drawing.Size(36, 45);
200: this.shapeContainer2.TabIndex = 0;
201: this.shapeContainer2.TabStop = false;
202: //
203: // lineShape3
204: //
205: this.lineShape3.BorderColor = System.Drawing.Color.YellowGreen;
206: this.lineShape3.BorderWidth = 6;
207: this.lineShape3.Cursor = System.Windows.Forms.Cursors.Default;
208: this.lineShape3.Name = "lineShape3";
209: this.lineShape3.Visible = false;
210: this.lineShape3.X1 = 16;
211: this.lineShape3.X2 = 49;
212: this.lineShape3.Y1 = 26;
213: this.lineShape3.Y2 = 26;
214: //
215: // lineShape2
216: //
217: this.lineShape2.BorderColor = System.Drawing.Color.YellowGreen;
218: this.lineShape2.BorderWidth = 6;
219: this.lineShape2.Cursor = System.Windows.Forms.Cursors.Default;
220: this.lineShape2.Name = "lineShape1";
221: this.lineShape2.Visible = false;
222: this.lineShape2.X1 = -3;
223: this.lineShape2.X2 = 18;
224: this.lineShape2.Y1 = 43;
225: this.lineShape2.Y2 = 27;
226: //
227: // lineShape1
228: //
229: this.lineShape1.BorderColor = System.Drawing.Color.YellowGreen;
230: this.lineShape1.BorderWidth = 6;
231: this.lineShape1.Name = "lineShape1";
232: this.lineShape1.Visible = false;
233: this.lineShape1.X1 = -1;
234: this.lineShape1.X2 = 19;
235: this.lineShape1.Y1 = 11;
236: this.lineShape1.Y2 = 27;
237: //
238: // labIP2
239: //
240: this.labIP2.BackColor = System.Drawing.Color.Transparent;
241: this.labIP2.ForeColor = System.Drawing.SystemColors.ButtonHighlight;
242: this.labIP2.Location = new System.Drawing.Point(50, 29);
243: this.labIP2.Name = "labIP2";
244: this.labIP2.Size = new System.Drawing.Size(50, 12);
245: this.labIP2.TabIndex = 0;
246: this.labIP2.Text = "0";
247: this.labIP2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
248: //
249: // label5
250: //
251: this.label5.AutoSize = true;
252: this.label5.BackColor = System.Drawing.Color.Transparent;
253: this.label5.ForeColor = System.Drawing.SystemColors.ButtonHighlight;
254: this.label5.Location = new System.Drawing.Point(106, 9);
255: this.label5.Name = "label5";
256: this.label5.Size = new System.Drawing.Size(31, 12);
257: this.label5.TabIndex = 0;
258: this.label5.Text = "Mbps";
259: //
260: // label6
261: //
262: this.label6.AutoSize = true;
263: this.label6.BackColor = System.Drawing.Color.Transparent;
264: this.label6.ForeColor = System.Drawing.SystemColors.ButtonHighlight;
265: this.label6.Location = new System.Drawing.Point(106, 29);
266: this.label6.Name = "label6";
267: this.label6.Size = new System.Drawing.Size(31, 12);
268: this.label6.TabIndex = 0;
269: this.label6.Text = "Mbps";
270: //
271: // label1
272: //
273: this.label1.AutoSize = true;
274: this.label1.BackColor = System.Drawing.Color.Transparent;
275: this.label1.Font = new System.Drawing.Font("新細明體", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(136)));
276: this.label1.ForeColor = System.Drawing.SystemColors.ButtonHighlight;
277: this.label1.Location = new System.Drawing.Point(14, 9);
278: this.label1.Name = "label1";
279: this.label1.Size = new System.Drawing.Size(45, 12);
280: this.label1.TabIndex = 0;
281: this.label1.Text = "ASI1 : ";
282: //
283: // label2
284: //
285: this.label2.AutoSize = true;
286: this.label2.BackColor = System.Drawing.Color.Transparent;
287: this.label2.Font = new System.Drawing.Font("新細明體", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(136)));
288: this.label2.ForeColor = System.Drawing.SystemColors.ButtonHighlight;
289: this.label2.Location = new System.Drawing.Point(14, 29);
290: this.label2.Name = "label2";
291: this.label2.Size = new System.Drawing.Size(45, 12);
292: this.label2.TabIndex = 0;
293: this.label2.Text = "ASI2 : ";
294: //
295: // labIP1
296: //
297: this.labIP1.BackColor = System.Drawing.Color.Transparent;
298: this.labIP1.ForeColor = System.Drawing.SystemColors.ButtonHighlight;
299: this.labIP1.Location = new System.Drawing.Point(50, 9);
300: this.labIP1.Name = "labIP1";
301: this.labIP1.Size = new System.Drawing.Size(50, 12);
302: this.labIP1.TabIndex = 0;
303: this.labIP1.Text = "0";
304: this.labIP1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
305: //
306: // shapeContainer3
307: //
308: this.shapeContainer3.Location = new System.Drawing.Point(5, 5);
309: this.shapeContainer3.Margin = new System.Windows.Forms.Padding(0);
310: this.shapeContainer3.Name = "shapeContainer3";
311: this.shapeContainer3.Shapes.AddRange(new Microsoft.VisualBasic.PowerPacks.Shape[] {
312: this.LED_Run});
313: this.shapeContainer3.Size = new System.Drawing.Size(956, 66);
314: this.shapeContainer3.TabIndex = 8;
315: this.shapeContainer3.TabStop = false;
316: //
317: // LED_Run
318: //
319: this.LED_Run.BackColor = System.Drawing.Color.Lime;
320: this.LED_Run.BackStyle = Microsoft.VisualBasic.PowerPacks.BackStyle.Opaque;
321: this.LED_Run.BorderColor = System.Drawing.Color.Lime;
322: this.LED_Run.FillColor = System.Drawing.Color.Lime;
323: this.LED_Run.FillGradientColor = System.Drawing.Color.Lime;
324: this.LED_Run.Location = new System.Drawing.Point(9, 9);
325: this.LED_Run.Name = "LED_Run";
326: this.LED_Run.Size = new System.Drawing.Size(16, 12);
327: this.LED_Run.Visible = false;
328: //
329: // timer1
330: //
331: this.timer1.Interval = 800;
332: this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
333: //
334: // uiAsiGuard
335: //
336: this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
337: this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
338: this.Controls.Add(this.panel4);
339: this.Name = "uiAsiGuard";
340: this.Size = new System.Drawing.Size(970, 80);
341: this.panel4.ResumeLayout(false);
342: this.panel4.PerformLayout();
343: this.panel7.ResumeLayout(false);
344: this.panel7.PerformLayout();
345: this.panel5.ResumeLayout(false);
346: this.panel5.PerformLayout();
347: this.panel9.ResumeLayout(false);
348: this.ResumeLayout(false);
349:
350: }
351:
352: #endregion
353:
354: private System.Windows.Forms.Panel panel4;
355: private System.Windows.Forms.Label label9;
356: private System.Windows.Forms.Panel panel7;
357: private System.Windows.Forms.Label label12;
358: private System.Windows.Forms.Label label111;
359: private System.Windows.Forms.Panel panel5;
360: private System.Windows.Forms.Panel panel9;
361: private Microsoft.VisualBasic.PowerPacks.ShapeContainer shapeContainer2;
362: private Microsoft.VisualBasic.PowerPacks.LineShape lineShape3;
363: private Microsoft.VisualBasic.PowerPacks.LineShape lineShape2;
364: private Microsoft.VisualBasic.PowerPacks.LineShape lineShape1;
365: private System.Windows.Forms.Label labIP2;
366: private System.Windows.Forms.Label label5;
367: private System.Windows.Forms.Label label6;
368: private System.Windows.Forms.Label label1;
369: private System.Windows.Forms.Label label2;
370: private System.Windows.Forms.Label labIP1;
371: private Microsoft.VisualBasic.PowerPacks.ShapeContainer shapeContainer3;
372: private Microsoft.VisualBasic.PowerPacks.OvalShape LED_Run;
373: private System.Windows.Forms.Timer timer1;
374: private System.Windows.Forms.Label lab_opMode;
375: private System.Windows.Forms.Label lab_swMode;
376: private System.Windows.Forms.Label label_IpAddr;
377: }
378: }
uiAsiGuard.cs
1: using System;
2: using System.Collections.Generic;
3: using System.ComponentModel;
4: using System.Drawing;
5: using System.Data;
6: using System.Linq;
7: using System.Text;
8: using System.Windows.Forms;
9:
10: using System.Threading;
11: using SnmpSharpNet;
12: using System.Net;
13:
14: namespace TVBS_SSNMP
15: {
16: public partial class uiAsiGuard : UserControl
17: {
18: const int almTIMES = 2; /* n 次無法連通 ,當成IP 有問題*/
19:
20: /* 建構式 */
21: public uiAsiGuard()
22: {
23: InitializeComponent();
24: }
25:
26: /* 欄位 */
27: public DataAsiGuard Device;
28: private int cnt = almTIMES;
29: private bool m_IpAlarm = false; /* for IP文字 Alarm=>變紅*/
30:
31: /* 屬性 */
32: public int IpErrorTimes
33: {
34: get { return cnt; }
35: set
36: {
37: switch (value)
38: {
39: case 0:
40: IpAlarm = true;
41: break;
42: default:
43: IpAlarm = false;
44: cnt = value;
45: break;
46: }
47: }
48: }
49: public bool IpAlarm
50: {
51: get { return m_IpAlarm; }
52: set {
53: m_IpAlarm = value;
54: label_IpAddr.ForeColor = value ? Color.Crimson : Color.YellowGreen;
55: }
56: }
57:
58: /* 方法 */
59: public void run(string ip)
60: {
61: Device = new DataAsiGuard(this, ip);
62: Device.onRequestResult += RequestResult; //.. 加入事件處理程序
63: label_IpAddr.Text = "Device IP : " + Device.IP;
64: timer1.Enabled = true;
65: }
66: private void show()
67: {
68: labIP1.Text = string.Format("{0:f}", Device.inputTotalBitrateASI1/1000);
69: labIP2.Text = string.Format("{0:f}", Device.inputTotalBitrateASI2 / 1000);
70: lab_opMode.Text = Device.operationMode;
71: lab_swMode.Text = Device.switchMode;
72:
73: switch (Device.inputSelected)
74: {
75: case 0:
76: lineShape1.Visible = true;
77: lineShape2.Visible = false;
78: lineShape3.Visible = true;
79: break;
80: case 1:
81: lineShape1.Visible = false;
82: lineShape2.Visible = true;
83: lineShape3.Visible = true;
84: break;
85: default:
86: lineShape1.Visible = false;
87: lineShape2.Visible = false;
88: lineShape3.Visible = false;
89: break;
90: }
91: }
92:
93: /* 事件處理程式 */
94: private void panel4_DoubleClick(object sender, EventArgs e)
95: {
96: try
97: {
98: Win32.RunExe("iexplore.exe", Device.IP);
99: }
100: catch { }
101:
102: }
103: private void timer1_Tick(object sender, EventArgs e)
104: {
105: timer1.Enabled = false;
106:
107: /* SNMP.Request 開始 */
108: Device.GetRequest();
109: }
110: private void RequestResult(Object sender, EventArgs e) /* 當 SNMP.Request 完成時執行 */
111: {
112: try
113: {
114: switch (Device.flag)
115: {
116: case 0: // 正常取得數據
117: LED_Run.Visible = !LED_Run.Visible; // 通訊閃燈
118: IpErrorTimes = almTIMES;
119: show();
120: break;
121: case -1: // error
122: break;
123: }
124: }
125: catch { }
126: finally
127: {
128: timer1.Enabled = true;
129: IpErrorTimes = IpErrorTimes - 1;
130: }
131: }
132: }
133:
134:
135:
136: public class DataAsiGuard
137: {
138: /* 建構式 */
139: public DataAsiGuard(object owner, string ip)
140: {
141: mOwner = owner;
142: IP = ip;
143: }
144:
145: /* 欄位 */
146: private int m_switchMode;
147: private int m_operationMode;
148: private object mOwner; // 儲存擁有者
149:
150: /* 屬性 */
151: public object Owner // 儲存擁有者
152: {
153: set { mOwner = value; }
154: get { return mOwner; }
155: }
156: public string IP { get; set; }
157: public int inputSelected { get; set; }
158: public float inputTotalBitrateASI1 { get; set; }
159: public float inputTotalBitrateASI2 { get; set; }
160: public int outputTotalBitrate{get;set;}
161: public string operationMode
162: {
163: get
164: {
165: switch (m_operationMode)
166: {
167: case 0: return "Seamless SFN Switch(0)";
168: case 1: return "Seamless Switch(1)";
169: case 2: return "Basic Switch(2)";
170: case 3: return "T2mi Switch(3)";
171:
172: default:
173: return "Seamless SFN Switch(0)";
174: break;
175: }
176: }
177: }
178: public string switchMode
179: {
180: get
181: {
182: switch (m_switchMode)
183: {
184: case 0: return "Auto Switch(0)";
185: case 1: return "Semi Auto ASI1(1)";
186: case 2: return "Semi Auto ASI2(2)";
187: case 3: return "ASI1(3)";
188: case 4: return "ASI2(4)";
189: case 5: return "GP Input(5)";
190:
191: default:
192: return "Auto Switch(0)";
193: break;
194: }
195: }
196: }
197: public string ErrorString
198: {
199: get;
200: set;
201: }
202: public int flag{get; set;}
203:
204: /* Event */
205: public event EventHandler onRequestResult;
206:
207: /* 方法 */
208: public void GetRequest()
209: {
210: ThreadPool.QueueUserWorkItem(new WaitCallback(Request));
211: }
212: private void Request(object obj)
213: {
214: try
215: {
216: OctetString community = new OctetString("public"); // SNMP community name
217: AgentParameters param = new AgentParameters(community); // Define agent parameters class
218: param.Version = SnmpVersion.Ver1; // Set SNMP version to 1 (or 2)
219:
220: // Construct the agent address object
221: // IpAddress class is easy to use here because
222: // it will try to resolve constructor parameter if it doesn't
223: // parse to an IP address
224: IpAddress agent = new IpAddress(this.IP);
225: UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1); // Construct target
226: Pdu pdu = new Pdu(PduType.Get); // Pdu class used for all requests
227:
228: setPdu(pdu);
229:
230: SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param); // Make SNMP request
231:
232: // If result is null then agent didn't reply or we couldn't parse the reply.
233: if (result != null)
234: {
235: // ErrorStatus other then 0 is an error returned by
236: // the Agent - see SnmpConstants for error definitions
237: if (result.Pdu.ErrorStatus != 0)
238: {
239: // agent reported an error with the request
240: ErrorString = string.Format("Error in SNMP reply. Error {0} index {1}", result.Pdu.ErrorStatus, result.Pdu.ErrorIndex);
241: flag = -1;
242: }
243: else
244: {
245: anaRequest(result);
246: flag = 0;
247: }
248: }
249: else
250: {
251: ErrorString = ("No response received from SNMP agent. \r\n");
252: flag = -1;
253: }
254: target.Close();
255: }
256: catch
257: {
258: ErrorString = ("Time out. \r\n");
259: flag = -1;
260: }
261: finally
262: {
263: ((System.Windows.Forms.Control)mOwner).Invoke(new RequestResultHandler(RequestResult));
264: }
265: }
266: private static void setPdu(Pdu pdu)
267: {
268: pdu.VbList.Add("1.3.6.1.4.1.23986.11.5.1.1.1.0"); //operationMode
269: pdu.VbList.Add("1.3.6.1.4.1.23986.11.5.1.1.2.0"); //switchMode
270: pdu.VbList.Add("1.3.6.1.4.1.23986.11.5.2.1.1.0"); //inputSelected
271: pdu.VbList.Add("1.3.6.1.4.1.23986.11.5.2.2.1.0"); //inputTotalBitrateASI1
272: pdu.VbList.Add("1.3.6.1.4.1.23986.11.5.2.2.3.0"); //inputTotalBitrateASI2
273: pdu.VbList.Add("1.3.6.1.4.1.23986.11.5.2.2.5.0"); //outputTotalBitrate
274: }
275: private void anaRequest(SnmpV1Packet result)
276: {
277: StringBuilder sb = new StringBuilder();
278:
279: // Reply variables are returned in the same order as they were added
280: // to the VbList
281:
282: this.m_operationMode = Convert.ToInt32(result.Pdu.VbList[0].Value.ToString());
283: this.m_switchMode = Convert.ToInt32(result.Pdu.VbList[1].Value.ToString());
284: this.inputSelected = Convert.ToInt32(result.Pdu.VbList[2].Value.ToString());
285: this.inputTotalBitrateASI1 = Convert.ToInt32(result.Pdu.VbList[3].Value.ToString());
286: this.inputTotalBitrateASI2 = Convert.ToInt32(result.Pdu.VbList[4].Value.ToString());
287: this.outputTotalBitrate = Convert.ToInt32(result.Pdu.VbList[5].Value.ToString());
288: }
289:
290: /* 事件處理程式 */
291: private void RequestResult()
292: {
293: /* 觸發 onLoad 事件 */
294: if (onRequestResult != null) { onRequestResult(this, new EventArgs()); }
295: }
296:
297: /* delegate */
298: public delegate void RequestResultHandler();
299: }
300:
301:
302: }
沒有留言:
張貼留言