SNMP 是应用在 IP 网络管理网络节点(服务器、OLT、路由器、交换机等)的一种标准协议,属于应用层协议,通过 SNMP 可以获得设备的主要运行参数信息,帮助管理员解决出现的问题,我用到的开发工具包括C#、JavaScript、Echarts等。
本次介绍如何监控OLT设备的主要工作参数指标,包括上行口状态、板卡cpu利用率、内存利用率和温度等,其他需要监控的指标自己可以摸索。
OLT设备的oid信息由于保密的需要做了隐藏
public string zxAnOltsysName = "1.3.6.1.2.1.1.5.0"; //C320设备名称
public string zxAnOltsysDescr = "1.3.6.1.2.1.1.1.0"; //C320设备描述
public string zxAnOltGei1Status = "1.3.6.1.2.1.2.2.*"; //C320上行口
public string zxAnCardOperStatus = "1.3.6.1.4.1.3902.*";//块卡状态
public string zxAnCardCpuLoad = "1.3.6.1.4.1.3902.*";//板卡cpu利用率
public string zxAnCardMemUsage = "1.3.6.1.4.1.3902.*";//内存利用率
public string zxAnOltTemperature = "1.3.6.1.4.1.3902.*"; //温度
public string zxAnOltOnuAddress = "1.3.6.1.4.1.3902.*"; //带的ONUmac
根据oid调用getOIDValue()函数获得对应的参数值。
public Dictionary<string, string> getOIDValue(string host, string singleoid, string mcmtsCommunity)
{
Dictionary<string, string> dic = new Dictionary<string, string>();
try
{
OctetString community = new OctetString(mcmtsCommunity);
AgentParameters param = new AgentParameters(community);
param.Version = SnmpVersion.Ver2;
IpAddress agent = new IpAddress(host);
UdpTarget target = new UdpTarget((IPAddress)agent, 161, 1000, 1);
Pdu pdu = new Pdu(PduType.Get);
if (!singleoid.Contains("SNMP No-Such-Instance"))
{
pdu.VbList.Add(singleoid);
SnmpV2Packet result = (SnmpV2Packet)target.Request(pdu, param);
// If result is null then agent didn't reply or we couldn't parse the reply.
if (result != null)
{
// ErrorStatus other then 0 is an error returned by
// the Agent - see SnmpConstants for error definitions
if (result.Pdu.ErrorStatus == 0)
{
for (int i = 0; i < result.Pdu.VbList.Count; i++)
{
dic.Add(result.Pdu.VbList[i].Oid.ToString(), result.Pdu.VbList[i].Value.ToString());
}
}
}
target.Close();
}
else
{
dic = null;
}
}
catch (Exception e)
{
dic = null;
}
return dic;
}
public Dictionary<string, string> getWalkValueV2(string host, string irootOid, string mcmtsCommunity)
{
Dictionary<string, string> dic = new Dictionary<string, string>();
try
{
OctetString community = new OctetString(mcmtsCommunity);
AgentParameters param = new AgentParameters(community);
param.Version = SnmpVersion.Ver2;
IpAddress agent = new IpAddress(host);
UdpTarget target = new UdpTarget((IPAddress)agent, 161, 1000, 1);
// Define Oid that is the root of the MIB
// tree you wish to retrieve
Oid rootOid = new Oid(irootOid); // ifDescr
Oid lastOid = (Oid)rootOid.Clone();
Pdu pdu = new Pdu(PduType.GetBulk);
pdu.NonRepeaters = 0;
pdu.MaxRepetitions = 5;
while (lastOid != null)
{
if (pdu.RequestId != 0)
{
pdu.RequestId += 1;
}
// Clear Oids from the Pdu class.
pdu.VbList.Clear();
// Initialize request PDU with the last retrieved Oid
pdu.VbList.Add(lastOid);
// Make SNMP request
SnmpV2Packet result = (SnmpV2Packet)target.Request(pdu, param);
// You should catch exceptions in the Request if using in real application.
// If result is null then agent didn't reply or we couldn't parse the reply.
if (result != null)
{
// ErrorStatus other then 0 is an error returned by
// the Agent - see SnmpConstants for error definitions
if (result.Pdu.ErrorStatus != 0)
{
// agent reported an error with the request
Console.WriteLine("Error in SNMP reply. Error {0} index {1}",
result.Pdu.ErrorStatus,
result.Pdu.ErrorIndex);
lastOid = null;
break;
}
else
{
// Walk through returned variable bindings
foreach (Vb v in result.Pdu.VbList)
{
// Check that retrieved Oid is "child" of the root OID
if (rootOid.IsRootOf(v.Oid))
{
//Console.WriteLine("{0} ({1}): {2}",v.Oid.ToString(),SnmpConstants.GetTypeName(v.Value.Type),v.Value.ToString());
dic.Add(v.Oid.ToString(), v.Value.ToString());
if (v.Value.Type == SnmpConstants.SMI_ENDOFMIBVIEW)
lastOid = null;
else
lastOid = v.Oid;
}
else
{
// we have reached the end of the requested
// MIB tree. Set lastOid to null and exit loop
lastOid = null;
}
}
}
}
else
{
Console.WriteLine("No response received from SNMP agent.");
}
}
target.Close();
}
catch (Exception e)
{
dic = null;
}
return dic;
}
把获得的参数值利用页面进行展示,如果发现指标异常,通过微信平台进行相应的报警,让维修人员尽快处理(由于时间关系,下一步在写相关的内容)
效果图