Skip to content

Commit 7f51378

Browse files
committed
Enable scalar object emulation
1 parent 927ce4f commit 7f51378

File tree

10 files changed

+2270
-4862
lines changed

10 files changed

+2270
-4862
lines changed

Samples.Engine/Pipeline/ObjectStore.cs

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@
1717
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
1818
// DEALINGS IN THE SOFTWARE.
1919

20+
using System;
2021
using System.Collections.Generic;
22+
using System.Globalization;
23+
using System.IO;
2124
using System.Linq;
25+
using System.Text;
2226
using Lextm.SharpSnmpLib;
2327

2428
namespace Samples.Pipeline
@@ -66,5 +70,159 @@ public virtual void Add(ISnmpObject newObject)
6670
}
6771
List.Add(newObject);
6872
}
73+
74+
public void LoadData(string dataFile)
75+
{
76+
if (!File.Exists(dataFile))
77+
{
78+
Console.WriteLine($"Warning: Data file '{dataFile}' not found. Skipping data loading.");
79+
return;
80+
}
81+
82+
try
83+
{
84+
var lines = File.ReadAllLines(dataFile);
85+
var loadedCount = 0;
86+
87+
foreach (var line in lines)
88+
{
89+
if (string.IsNullOrWhiteSpace(line) || line.StartsWith("#"))
90+
continue;
91+
92+
var parts = line.Split('|');
93+
if (parts.Length != 3)
94+
continue;
95+
96+
var oidString = parts[0].Trim();
97+
var typeString = parts[1].Trim();
98+
var valueString = parts[2].Trim();
99+
100+
try
101+
{
102+
var oid = new ObjectIdentifier(oidString);
103+
var data = ParseSnmpData(typeString, valueString);
104+
105+
if (data != null)
106+
{
107+
SetObjectData(oid, data);
108+
loadedCount++;
109+
}
110+
}
111+
catch (Exception ex)
112+
{
113+
Console.WriteLine($"Warning: Failed to parse line '{line}': {ex.Message}");
114+
}
115+
}
116+
117+
Console.WriteLine($"Successfully loaded {loadedCount} SNMP values from '{dataFile}'");
118+
}
119+
catch (Exception ex)
120+
{
121+
Console.WriteLine($"Error loading data file '{dataFile}': {ex.Message}");
122+
}
123+
}
124+
125+
private ISnmpData ParseSnmpData(string typeString, string valueString)
126+
{
127+
switch (typeString)
128+
{
129+
case "2": // Integer32
130+
if (int.TryParse(valueString, out var intValue))
131+
return new Integer32(intValue);
132+
break;
133+
134+
case "4": // OctetString (ASCII)
135+
return new OctetString(valueString);
136+
137+
case "4x": // OctetString (Hex)
138+
try
139+
{
140+
var bytes = new byte[valueString.Length / 2];
141+
for (int i = 0; i < bytes.Length; i++)
142+
{
143+
bytes[i] = Convert.ToByte(valueString.Substring(i * 2, 2), 16);
144+
}
145+
return new OctetString(bytes);
146+
}
147+
catch
148+
{
149+
return new OctetString(valueString);
150+
}
151+
152+
case "6": // ObjectIdentifier
153+
try
154+
{
155+
return new ObjectIdentifier(valueString);
156+
}
157+
catch
158+
{
159+
return new ObjectIdentifier("0.0");
160+
}
161+
162+
case "64x": // IpAddress (Hex)
163+
try
164+
{
165+
var bytes = new byte[valueString.Length / 2];
166+
for (int i = 0; i < bytes.Length; i++)
167+
{
168+
bytes[i] = Convert.ToByte(valueString.Substring(i * 2, 2), 16);
169+
}
170+
return new IP(bytes);
171+
}
172+
catch
173+
{
174+
return new IP("127.0.0.1");
175+
}
176+
177+
case "65": // Counter32
178+
if (uint.TryParse(valueString, out var counterValue))
179+
return new Counter32(counterValue);
180+
break;
181+
182+
case "66": // Gauge32
183+
if (uint.TryParse(valueString, out var gaugeValue))
184+
return new Gauge32(gaugeValue);
185+
break;
186+
187+
case "67": // TimeTicks
188+
if (uint.TryParse(valueString, out var ticksValue))
189+
return new TimeTicks(ticksValue);
190+
break;
191+
192+
case "70": // Counter64
193+
if (ulong.TryParse(valueString, out var counter64Value))
194+
return new Counter64(counter64Value);
195+
break;
196+
197+
default:
198+
Console.WriteLine($"Warning: Unknown SNMP type '{typeString}' for value '{valueString}'");
199+
break;
200+
}
201+
202+
return null;
203+
}
204+
205+
private void SetObjectData(ObjectIdentifier oid, ISnmpData data)
206+
{
207+
// Find the object that matches this OID
208+
var obj = GetObject(oid);
209+
if (obj != null)
210+
{
211+
try
212+
{
213+
obj.CheckAccess = false;
214+
obj.Data = data;
215+
obj.CheckAccess = true;
216+
}
217+
catch (Exception ex)
218+
{
219+
Console.WriteLine($"Warning: Failed to set data for OID {oid}: {ex.Message}");
220+
}
221+
}
222+
else
223+
{
224+
Console.WriteLine($"Warning: No object found for OID {oid}. Cannot set data.");
225+
}
226+
}
69227
}
70228
}

Samples.Engine/Pipeline/ScalarObject.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,7 @@ public override ScalarObject MatchGet(ObjectIdentifier id)
9292
{
9393
return Id == id ? this : null;
9494
}
95+
96+
public bool CheckAccess { get; set; }
9597
}
9698
}

Samples/CSharpCore/snmpd/Customized/.customized

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)