Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Net;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Net.Sockets;
- using SnmpSharpNet;
- using System.Threading.Tasks;
- namespace WindowsFormsApplication2
- {
- public partial class Form1 : Form
- {
- public class Globals
- {
- public static string P1 = "";
- public static string P2 = "";
- public static string P3 = "";
- public static string P4 = "";
- public static string P5 = "";
- public static string P6 = "";
- public static string P7 = "";
- public static string P8 = "";
- public static string P9 = "";
- public static string P10 = "";
- public static string P11 = "";
- public static string P12 = "";
- public static string P13 = "";
- public static string P14 = "";
- public static string P15 = "";
- public static string P16 = "";
- public static string P17 = "";
- public static string P18 = "";
- public static string P19 = "";
- public static string P20 = "";
- public static string P21 = "";
- public static string P22 = "";
- public static string P23 = "";
- public static string P24 = "";
- public static string PL1 = "";
- public static string PL2 = "";
- public static string PL3 = "";
- public static string PL4 = "";
- public static string PL5 = "";
- public static string PL6 = "";
- public static string PL7 = "";
- public static string PL8 = "";
- public static string PL9 = "";
- public static string PL10 = "";
- public static string PL11 = "";
- public static string PL12 = "";
- public static string PL13 = "";
- public static string PL14 = "";
- public static string PL15 = "";
- public static string PL16 = "";
- public static string PL17 = "";
- public static string PL18 = "";
- public static string PL19 = "";
- public static string PL20 = "";
- public static string PL21 = "";
- public static string PL22 = "";
- public static string PL23 = "";
- public static string PL24 = "";
- }
- public Form1()
- {
- InitializeComponent();
- timer1.Stop();
- timer1.Enabled = false;
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- portReset();
- datelabel.Text = DateTime.Now.ToLongDateString();
- timelabel.Text = DateTime.Now.ToLongTimeString();
- }
- private void portReset()
- {
- port1.Image = Properties.Resources.port_nostatus;
- port2.Image = Properties.Resources.port_nostatus;
- port3.Image = Properties.Resources.port_nostatus;
- port4.Image = Properties.Resources.port_nostatus;
- port5.Image = Properties.Resources.port_nostatus;
- port6.Image = Properties.Resources.port_nostatus;
- port7.Image = Properties.Resources.port_nostatus;
- port8.Image = Properties.Resources.port_nostatus;
- port9.Image = Properties.Resources.port_nostatus;
- port10.Image = Properties.Resources.port_nostatus;
- port11.Image = Properties.Resources.port_nostatus;
- port12.Image = Properties.Resources.port_nostatus;
- port13.Image = Properties.Resources.port_nostatus;
- port14.Image = Properties.Resources.port_nostatus;
- port15.Image = Properties.Resources.port_nostatus;
- port16.Image = Properties.Resources.port_nostatus;
- port17.Image = Properties.Resources.port_nostatus;
- port18.Image = Properties.Resources.port_nostatus;
- port19.Image = Properties.Resources.port_nostatus;
- port20.Image = Properties.Resources.port_nostatus;
- port21.Image = Properties.Resources.port_nostatus;
- port22.Image = Properties.Resources.port_nostatus;
- port23.Image = Properties.Resources.port_nostatus;
- port24.Image = Properties.Resources.port_nostatus;
- p1textBox1.Text = null;
- p2textBox1.Text = null;
- p3textBox1.Text = null;
- p4textBox1.Text = null;
- p5textBox1.Text = null;
- p6textBox1.Text = null;
- p7textBox1.Text = null;
- p8textBox1.Text = null;
- p9textBox1.Text = null;
- p10textBox1.Text = null;
- p11textBox1.Text = null;
- p12textBox1.Text = null;
- p13textBox1.Text = null;
- p14textBox1.Text = null;
- p15textBox1.Text = null;
- p16textBox1.Text = null;
- p17textBox1.Text = null;
- p18textBox1.Text = null;
- p19textBox1.Text = null;
- p20textBox1.Text = null;
- p21textBox1.Text = null;
- p22textBox1.Text = null;
- p23textBox1.Text = null;
- p24textBox1.Text = null;
- }
- private async void snmpGetFunc()
- {
- timer1.Enabled = true;
- this.Invoke((MethodInvoker)delegate
- {
- lastUpdateLabel.Text = "Last Updated @" + DateTime.Now.ToLongDateString() + DateTime.Now.ToLongTimeString();
- });
- OctetString community = new OctetString("PUBLIC");
- AgentParameters param = new AgentParameters(community);
- param.Version = SnmpVersion.Ver2;
- IpAddress agent = new IpAddress("10.0.0.1");
- UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1);
- Pdu pdu = new Pdu(PduType.Get);
- // Ports
- pdu.VbList.Add("1.3.6.1.2.1.2.2.1.8.10101");
- pdu.VbList.Add("1.3.6.1.2.1.2.2.1.8.10102");
- pdu.VbList.Add("1.3.6.1.2.1.2.2.1.8.10103");
- pdu.VbList.Add("1.3.6.1.2.1.2.2.1.8.10104");
- pdu.VbList.Add("1.3.6.1.2.1.2.2.1.8.10105");
- pdu.VbList.Add("1.3.6.1.2.1.2.2.1.8.10106");
- pdu.VbList.Add("1.3.6.1.2.1.2.2.1.8.10107");
- pdu.VbList.Add("1.3.6.1.2.1.2.2.1.8.10108");
- pdu.VbList.Add("1.3.6.1.2.1.2.2.1.8.10109");
- pdu.VbList.Add("1.3.6.1.2.1.2.2.1.8.10110");
- pdu.VbList.Add("1.3.6.1.2.1.2.2.1.8.10111");
- pdu.VbList.Add("1.3.6.1.2.1.2.2.1.8.10112");
- pdu.VbList.Add("1.3.6.1.2.1.2.2.1.8.10113");
- pdu.VbList.Add("1.3.6.1.2.1.2.2.1.8.10114");
- pdu.VbList.Add("1.3.6.1.2.1.2.2.1.8.10115");
- pdu.VbList.Add("1.3.6.1.2.1.2.2.1.8.10116");
- pdu.VbList.Add("1.3.6.1.2.1.2.2.1.8.10117");
- pdu.VbList.Add("1.3.6.1.2.1.2.2.1.8.10118");
- pdu.VbList.Add("1.3.6.1.2.1.2.2.1.8.10119");
- pdu.VbList.Add("1.3.6.1.2.1.2.2.1.8.10120");
- pdu.VbList.Add("1.3.6.1.2.1.2.2.1.8.10121");
- pdu.VbList.Add("1.3.6.1.2.1.2.2.1.8.10122");
- pdu.VbList.Add("1.3.6.1.2.1.2.2.1.8.10123");
- pdu.VbList.Add("1.3.6.1.2.1.2.2.1.8.10605");
- // Ports End
- // Now quering switch related data
- pdu.VbList.Add("1.3.6.1.2.1.31.1.1.1.18.10101");
- pdu.VbList.Add("1.3.6.1.2.1.31.1.1.1.18.10102");
- pdu.VbList.Add("1.3.6.1.2.1.31.1.1.1.18.10103");
- pdu.VbList.Add("1.3.6.1.2.1.31.1.1.1.18.10104");
- pdu.VbList.Add("1.3.6.1.2.1.31.1.1.1.18.10105");
- pdu.VbList.Add("1.3.6.1.2.1.31.1.1.1.18.10106");
- pdu.VbList.Add("1.3.6.1.2.1.31.1.1.1.18.10107");
- pdu.VbList.Add("1.3.6.1.2.1.31.1.1.1.18.10108");
- pdu.VbList.Add("1.3.6.1.2.1.31.1.1.1.18.10109");
- pdu.VbList.Add("1.3.6.1.2.1.31.1.1.1.18.10110");
- pdu.VbList.Add("1.3.6.1.2.1.31.1.1.1.18.10111");
- pdu.VbList.Add("1.3.6.1.2.1.31.1.1.1.18.10112");
- pdu.VbList.Add("1.3.6.1.2.1.31.1.1.1.18.10113");
- pdu.VbList.Add("1.3.6.1.2.1.31.1.1.1.18.10114");
- pdu.VbList.Add("1.3.6.1.2.1.31.1.1.1.18.10115");
- pdu.VbList.Add("1.3.6.1.2.1.31.1.1.1.18.10116");
- pdu.VbList.Add("1.3.6.1.2.1.31.1.1.1.18.10117");
- pdu.VbList.Add("1.3.6.1.2.1.31.1.1.1.18.10118");
- pdu.VbList.Add("1.3.6.1.2.1.31.1.1.1.18.10119");
- pdu.VbList.Add("1.3.6.1.2.1.31.1.1.1.18.10120");
- pdu.VbList.Add("1.3.6.1.2.1.31.1.1.1.18.10121");
- pdu.VbList.Add("1.3.6.1.2.1.31.1.1.1.18.10122");
- pdu.VbList.Add("1.3.6.1.2.1.31.1.1.1.18.10123");
- pdu.VbList.Add("1.3.6.1.2.1.31.1.1.1.18.10605");
- // Make SNMP request
- try
- {
- SnmpV2Packet result = (SnmpV2Packet)target.Request(pdu, param);
- }
- catch (SnmpException ex)
- {
- this.Invoke((MethodInvoker)delegate
- {
- this.StatusTextBox1.AppendText("\r\n" + ex.Message + "Either Device or SNMP not responding. Retrying again ...");
- });
- return;
- }
- if (timer1.Enabled == true)
- {
- SnmpV2Packet result = (SnmpV2Packet)target.Request(pdu, param);
- Globals.P1 = result.Pdu.VbList[0].Value.ToString();
- Globals.P2 = result.Pdu.VbList[1].Value.ToString();
- Globals.P3 = result.Pdu.VbList[2].Value.ToString();
- Globals.P4 = result.Pdu.VbList[3].Value.ToString();
- Globals.P5 = result.Pdu.VbList[4].Value.ToString();
- Globals.P6 = result.Pdu.VbList[5].Value.ToString();
- Globals.P7 = result.Pdu.VbList[6].Value.ToString();
- Globals.P8 = result.Pdu.VbList[7].Value.ToString();
- Globals.P9 = result.Pdu.VbList[8].Value.ToString();
- Globals.P10 = result.Pdu.VbList[9].Value.ToString();
- Globals.P11 = result.Pdu.VbList[10].Value.ToString();
- Globals.P12 = result.Pdu.VbList[11].Value.ToString();
- Globals.P13 = result.Pdu.VbList[12].Value.ToString();
- Globals.P14 = result.Pdu.VbList[13].Value.ToString();
- Globals.P15 = result.Pdu.VbList[14].Value.ToString();
- Globals.P16 = result.Pdu.VbList[15].Value.ToString();
- Globals.P17 = result.Pdu.VbList[16].Value.ToString();
- Globals.P18 = result.Pdu.VbList[17].Value.ToString();
- Globals.P19 = result.Pdu.VbList[18].Value.ToString();
- Globals.P20 = result.Pdu.VbList[19].Value.ToString();
- Globals.P21 = result.Pdu.VbList[20].Value.ToString();
- Globals.P22 = result.Pdu.VbList[21].Value.ToString();
- Globals.P23 = result.Pdu.VbList[22].Value.ToString();
- Globals.P24 = result.Pdu.VbList[23].Value.ToString();
- Globals.PL1 = result.Pdu.VbList[24].Value.ToString();
- Globals.PL2 = result.Pdu.VbList[25].Value.ToString();
- Globals.PL3 = result.Pdu.VbList[26].Value.ToString();
- Globals.PL4 = result.Pdu.VbList[27].Value.ToString();
- Globals.PL5 = result.Pdu.VbList[28].Value.ToString();
- Globals.PL6 = result.Pdu.VbList[29].Value.ToString();
- Globals.PL7 = result.Pdu.VbList[30].Value.ToString();
- Globals.PL8 = result.Pdu.VbList[31].Value.ToString();
- Globals.PL9 = result.Pdu.VbList[32].Value.ToString();
- Globals.PL10 = result.Pdu.VbList[33].Value.ToString();
- Globals.PL11 = result.Pdu.VbList[34].Value.ToString();
- Globals.PL12 = result.Pdu.VbList[35].Value.ToString();
- Globals.PL13 = result.Pdu.VbList[36].Value.ToString();
- Globals.PL14 = result.Pdu.VbList[37].Value.ToString();
- Globals.PL15 = result.Pdu.VbList[38].Value.ToString();
- Globals.PL16 = result.Pdu.VbList[39].Value.ToString();
- Globals.PL17 = result.Pdu.VbList[40].Value.ToString();
- Globals.PL18 = result.Pdu.VbList[41].Value.ToString();
- Globals.PL19 = result.Pdu.VbList[42].Value.ToString();
- Globals.PL20 = result.Pdu.VbList[43].Value.ToString();
- Globals.PL21 = result.Pdu.VbList[44].Value.ToString();
- Globals.PL22 = result.Pdu.VbList[45].Value.ToString();
- Globals.PL23 = result.Pdu.VbList[46].Value.ToString();
- Globals.PL24 = result.Pdu.VbList[47].Value.ToString();
- this.Invoke((MethodInvoker)delegate
- {
- //textBox1.BackColor = Color.Gray;
- plabel1.Text = Globals.PL1;
- plabel2.Text = Globals.PL2;
- plabel3.Text = Globals.PL3;
- plabel4.Text = Globals.PL4;
- plabel5.Text = Globals.PL5;
- plabel6.Text = Globals.PL6;
- plabel7.Text = Globals.PL7;
- plabel8.Text = Globals.PL8;
- plabel9.Text = Globals.PL9;
- plabel10.Text = Globals.PL10;
- plabel11.Text = Globals.PL11;
- plabel12.Text = Globals.PL12;
- plabel13.Text = Globals.PL13;
- plabel14.Text = Globals.PL14;
- plabel15.Text = Globals.PL15;
- plabel16.Text = Globals.PL16;
- plabel17.Text = Globals.PL17;
- plabel18.Text = Globals.PL18;
- plabel19.Text = Globals.PL19;
- plabel20.Text = Globals.PL20;
- plabel21.Text = Globals.PL21;
- plabel22.Text = Globals.PL22;
- plabel23.Text = Globals.PL23;
- plabel24.Text = Globals.PL24;
- //});
- if (Globals.P1 == "1")
- {
- port1.Image = Properties.Resources.port_up;
- p1textBox1.Text = Globals.P1;
- }
- else
- {
- port1.Image = Properties.Resources.port_down;
- }
- if (Globals.P2 == "1")
- {
- port2.Image = Properties.Resources.port_up;
- p2textBox1.Text = Globals.P2;
- }
- else
- {
- port2.Image = Properties.Resources.port_down;
- }
- if (Globals.P3 == "1")
- {
- port3.Image = Properties.Resources.port_up;
- p3textBox1.Text = Globals.P3;
- }
- else
- {
- port3.Image = Properties.Resources.port_down;
- }
- if (Globals.P4 == "1")
- {
- port4.Image = Properties.Resources.port_up;
- p4textBox1.Text = Globals.P4;
- }
- else
- {
- port4.Image = Properties.Resources.port_down;
- }
- if (Globals.P5 == "1")
- {
- port5.Image = Properties.Resources.port_up;
- p5textBox1.Text = Globals.P5;
- }
- else
- {
- port5.Image = Properties.Resources.port_down;
- //this.StatusTextBox1.Text += string.Format("Port 5 is now Down!\r\n");
- }
- if (Globals.P6 == "1")
- {
- port6.Image = Properties.Resources.port_up;
- p6textBox1.Text = Globals.P6;
- }
- else
- {
- port6.Image = Properties.Resources.port_down;
- }
- if (Globals.P7 == "1")
- {
- port7.Image = Properties.Resources.port_up;
- }
- else
- {
- port7.Image = Properties.Resources.port_down;
- }
- if (Globals.P8 == "1")
- {
- port8.Image = Properties.Resources.port_up;
- }
- else
- {
- port8.Image = Properties.Resources.port_down;
- }
- if (Globals.P9 == "1")
- {
- port9.Image = Properties.Resources.port_up;
- }
- else
- {
- port9.Image = Properties.Resources.port_down;
- }
- if (Globals.P10 == "1")
- {
- port10.Image = Properties.Resources.port_up;
- }
- else
- {
- port10.Image = Properties.Resources.port_down;
- }
- if (Globals.P11 == "1")
- {
- port11.Image = Properties.Resources.port_up;
- }
- else
- {
- port11.Image = Properties.Resources.port_down;
- }
- if (Globals.P12 == "1")
- {
- port12.Image = Properties.Resources.port_up;
- }
- else
- {
- port12.Image = Properties.Resources.port_down;
- }
- if (Globals.P13 == "1")
- {
- port13.Image = Properties.Resources.port_up;
- }
- else
- {
- port13.Image = Properties.Resources.port_down;
- }
- if (Globals.P14 == "1")
- {
- port14.Image = Properties.Resources.port_up;
- }
- else
- {
- port14.Image = Properties.Resources.port_down;
- }
- if (Globals.P15 == "1")
- {
- port15.Image = Properties.Resources.port_up;
- }
- else
- {
- port15.Image = Properties.Resources.port_down;
- }
- if (Globals.P16 == "1")
- {
- port16.Image = Properties.Resources.port_up;
- }
- else
- {
- port16.Image = Properties.Resources.port_down;
- }
- if (Globals.P17 == "1")
- {
- port17.Image = Properties.Resources.port_up;
- }
- else
- {
- port17.Image = Properties.Resources.port_down;
- }
- if (Globals.P18 == "1")
- {
- port18.Image = Properties.Resources.port_up;
- }
- else
- {
- port18.Image = Properties.Resources.port_down;
- }
- if (Globals.P19 == "1")
- {
- port19.Image = Properties.Resources.port_up;
- }
- else
- {
- port19.Image = Properties.Resources.port_down;
- }
- if (Globals.P20 == "1")
- {
- port20.Image = Properties.Resources.port_up;
- }
- else
- {
- port20.Image = Properties.Resources.port_down;
- }
- if (Globals.P21 == "1")
- {
- port21.Image = Properties.Resources.port_up;
- }
- else
- {
- port21.Image = Properties.Resources.port_down;
- }
- if (Globals.P22 == "1")
- {
- port22.Image = Properties.Resources.port_up;
- }
- else
- {
- port22.Image = Properties.Resources.port_down;
- }
- if (Globals.P23 == "1")
- {
- port23.Image = Properties.Resources.port_up;
- }
- else
- {
- port23.Image = Properties.Resources.port_down;
- }
- if (Globals.P24 == "1")
- {
- port24.Image = Properties.Resources.port_up;
- p24textBox1.Text = Globals.P24;
- }
- else
- {
- port24.Image = Properties.Resources.port_down;
- p24textBox1.Text = Globals.P24;
- }
- });
- }
- }
- private async void button1_Click(object sender, EventArgs e)
- {
- if (timer1.Enabled == true)
- {
- this.StatusTextBox1.Text += string.Format("Monitoring already started. Stop it to first to start new instance !\r\n");
- }
- else
- {
- timer1.Enabled = true;
- this.statusLoadinBar.Image = Properties.Resources.loading_gif;
- await Task.Run(() =>
- {
- snmpGetFunc();
- });
- StatusTextBox1.Text = String.Empty;
- this.StatusTextBox1.Text += string.Format("Starting monitoring of Cisco Switch Ports. \r\n");
- }
- }
- private void exitToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Application.Exit();
- }
- private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
- {
- Application.Exit();
- }
- private void pictureBox1_Click(object sender, EventArgs e)
- {
- Application.Exit();
- }
- private void timer4dt_Tick(object sender, EventArgs e)
- {
- timelabel.Text = DateTime.Now.ToLongTimeString();
- timer4dt.Start();
- }
- private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
- {
- }
- private void stopButton_Click(object sender, EventArgs e)
- {
- if (timer1.Enabled == true)
- {
- timer1.Enabled = false;
- timer1.Stop();
- portReset();
- this.statusLoadinBar.Image = null;
- this.StatusTextBox1.AppendText("\r\nMonitoring of switch have been Stopped on user request \r\n");
- }
- else
- {
- this.StatusTextBox1.AppendText("\r\nMonitoring not started yet !\r\n");
- }
- }
- private void timer1_Tick(object sender, EventArgs e)
- {
- snmpGetFunc();
- }
- private void exitButton_Click(object sender, EventArgs e)
- {
- this.statusLoadinBar.Image = null;
- timer1.Stop();
- portReset();
- this.StatusTextBox1.AppendText("\r\nExiting on user request \r\n");
- Application.Exit();
- }
- private void StatusTextBox1_TextChanged(object sender, EventArgs e)
- {
- StatusTextBox1.SelectionStart = StatusTextBox1.Text.Length;
- StatusTextBox1.ScrollToCaret();
- }
- private void p1textBox1_TextChanged(object sender, EventArgs e)
- {
- if (Globals.P1 == "1")
- this.StatusTextBox1.AppendText("\r\nPort 1 is now UP @" + DateTime.Now.ToLongDateString() + " / " + DateTime.Now.ToLongTimeString());
- else
- this.StatusTextBox1.AppendText("\r\nPort 1 is now DOWN! @" + DateTime.Now.ToLongDateString() + " / " + DateTime.Now.ToLongTimeString());
- }
- private void p5textBox1_TextChanged_1(object sender, EventArgs e)
- {
- if (Globals.P5 == "1")
- this.StatusTextBox1.AppendText("\r\nPort 5 is now UP @" + DateTime.Now.ToLongDateString() + " / " + DateTime.Now.ToLongTimeString());
- else
- this.StatusTextBox1.AppendText("\r\nPort 5 is now DOWN! @" + DateTime.Now.ToLongDateString() + " / " + DateTime.Now.ToLongTimeString());
- }
- private void p24textBox1_TextChanged(object sender, EventArgs e)
- {
- if (Globals.P24 == "1")
- this.StatusTextBox1.AppendText("\r\nPort 24 is now UP @" + DateTime.Now.ToLongDateString() + " / " + DateTime.Now.ToLongTimeString());
- else
- this.StatusTextBox1.AppendText("\r\nPort 24 is now DOWN! @" + DateTime.Now.ToLongDateString() + " / " + DateTime.Now.ToLongTimeString());
- }
- private void p2textBox1_TextChanged(object sender, EventArgs e)
- {
- if (Globals.P2 == "1")
- this.StatusTextBox1.AppendText("\r\nPort 2 is now UP @" + DateTime.Now.ToLongDateString() + " / " + DateTime.Now.ToLongTimeString());
- else
- this.StatusTextBox1.AppendText("\r\nPort 2 is now DOWN! @" + DateTime.Now.ToLongDateString() + " / " + DateTime.Now.ToLongTimeString());
- }
- }
- }
Add Comment
Please, Sign In to add comment