forked from fangnes/networkManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
207 lines (189 loc) · 7.45 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using System;
using System.Net;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Lextm.SharpSnmpLib;
using Lextm.SharpSnmpLib.Security;
using Lextm.SharpSnmpLib.Messaging;
using System.Windows.Forms.DataVisualization.Charting;
namespace networkManager
{
public partial class Form1 : Form
{
const string LOCALHOST = "127.0.0.1";
const string SERIE = "Valor";
string[] ids =
{
// Falhas
"1.3.6.1.2.1.11.8.0", //snmpInTooBigs
// Configuração
"1.3.6.1.2.1.6.9.0", //tcpCurrEstab
"1.3.6.1.2.1.2.2.1.4.1", //ifMtu
// Desempenho
"1.3.6.1.2.1.2.2.1.15.1", //ifInUnknownProtos
"1.3.6.1.2.1.4.5.0", //IpInAddrErrors
// Accounting
"1.3.6.1.2.1.6.5.0", //tcpActiveOpens
"1.3.6.1.2.1.6.6.0", //tcpPassiveOpens
// Segurança
"1.3.6.1.2.1.2.1.0", //snmpInBadCommunityNames
// Utilização do link
"1.3.6.1.2.1.2.2.1.5.1", //ifSpeed 8
"1.3.6.1.2.1.2.2.1.10.1", //ifInOctets
"1.3.6.1.2.1.2.2.1.16.1", //ifOutOctets
// Quantidade de pacotes
"1.3.6.1.2.1.4.10.0", //ipOutRequests - ip enviado
"1.3.6.1.2.1.4.3.0", //ipInReceives - ip recebido
"1.3.6.1.2.1.6.10.0", //tcpInSegs - tpc recebidos
"1.3.6.1.2.1.6.11.0", //tcpOutSegs - tcp enviados
"1.3.6.1.2.1.7.1.0", //udpInDatagrams - pacotes udp recebidos
"1.3.6.1.2.1.7.4.0" //udpOutDatagrams - pacotes udp enviados
};
enum IDS_INDEX
{
SNMP_IN_TOO_BIGS,
TCP_CURR_ESTAB,
IF_MTU,
IF_UNKNOWN_PROTOS,
IP_IN_ADDR_ERRORS,
TCP_ACTIVE_OPENS,
TCP_PASSIVE_OPENS,
SNMP_IN_BAD_COMMUNITY_NAMES,
IF_SPEED,
IF_IN_OCTETS,
IF_OUT_OCTETS,
IP_OUT_REQUESTS,
IP_IN_RECEIVES,
TCP_IN_SEGS,
TCP_OUT_SEGS,
UDP_IN_DATAGRAMS,
UDP_OUT_DATAGRAMS,
MAX_IDS
};
public Form1()
{
InitializeComponent();
}
string Get(string id)
{
GetRequestMessage request = new GetRequestMessage(Messenger.NextRequestId,
VersionCode.V2,
new OctetString("public"),
new List<Variable> { new Variable(new ObjectIdentifier(id)) });
ISnmpMessage reply = request.GetResponse(60000, new IPEndPoint(IPAddress.Parse(LOCALHOST), 161));
if (reply.Pdu().ErrorStatus.ToInt32() != 0) // != ErrorCode.NoError
{
throw ErrorException.Create(
"error in response",
IPAddress.Parse(LOCALHOST),
reply);
}
return reply.Pdu().Variables[0].Data.ToString();
}
void Set(string id, int value)
{
SetRequestMessage request = new SetRequestMessage(Messenger.NextRequestId,
VersionCode.V2,
new OctetString("public"),
new List<Variable> { new Variable(new ObjectIdentifier(id), new OctetString(value.ToString())) });
ISnmpMessage reply = request.GetResponse(60000, new IPEndPoint(IPAddress.Parse(LOCALHOST), 161));
if (reply.Pdu().ErrorStatus.ToInt32() != 0) // != ErrorCode.NoError
{
throw ErrorException.Create(
"error in response",
IPAddress.Parse(LOCALHOST),
reply);
}
}
void Set(string id, string value)
{
SetRequestMessage request = new SetRequestMessage(Messenger.NextRequestId,
VersionCode.V2,
new OctetString("public"),
new List<Variable> { new Variable(new ObjectIdentifier(id), new OctetString(value)) });
ISnmpMessage reply = request.GetResponse(60000, new IPEndPoint(IPAddress.Parse(LOCALHOST), 161));
if (reply.Pdu().ErrorStatus.ToInt32() != 0) // != ErrorCode.NoError
{
throw ErrorException.Create(
"error in response",
IPAddress.Parse(LOCALHOST),
reply);
}
}
List<Variable> Walk(string id)
{
var result = new List<Variable>();
#pragma warning disable CS0618 // O tipo ou membro é obsoleto
Messenger.BulkWalk(VersionCode.V2,
new IPEndPoint(IPAddress.Parse(LOCALHOST), 161),
new OctetString("public"),
new ObjectIdentifier(id),
result,
60000,
10,
WalkMode.WithinSubtree,
null,
null);
#pragma warning restore CS0618 // O tipo ou membro é obsoleto
return result;
}
private void timer1_Tick(object sender, EventArgs e)
{
string[] results = new string[ids.Length];
int ifInOctectsY = 0;
int ifInOctectsX = 0;
int ifOutOctectsY = 0;
int ifOutOctectsX = 0;
int bytesRate = 0;
int linkUsage = 0;
for (int i = 0; i < ids.Length; i++)
results[i] = Get(ids[i]);
ifInOctectsY = ifInOctectsX;
ifOutOctectsY = ifOutOctectsX;
ifInOctectsX = Int32.Parse(results[(int)IDS_INDEX.IF_IN_OCTETS]);
ifOutOctectsX = Int32.Parse(results[(int)IDS_INDEX.IF_OUT_OCTETS]);
bytesRate = (ifInOctectsY - ifInOctectsX) + (ifOutOctectsY - ifOutOctectsX);
linkUsage = (bytesRate * 8) / Int32.Parse(results[(int)IDS_INDEX.IF_SPEED]);
int j = 0;
foreach (TabPage tempPage in tabControl1.Controls.OfType<TabPage>())
{
foreach (Chart tempChart in tempPage.Controls.OfType<Chart>())
{
if (j == (int)IDS_INDEX.IF_SPEED)
{
tempChart.Series[SERIE].Points.AddY(linkUsage);
j = (int)IDS_INDEX.IP_OUT_REQUESTS;
}
else
{
tempChart.Series[SERIE].Points.AddY(results[j]);
j++;
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
string result = Get(textBox1.Text).ToString();
textBox1.Text = result;
}
private void button2_Click(object sender, EventArgs e)
{
string temp = comboBox1.Text;
List<Variable> result = Walk(temp);
int i = 0;
comboBox1.Items.Clear();
while (i < result.Count)
{
comboBox1.Items.Add(i + ": " + result[i].Data.ToString());
i++;
}
}
}
}