当前位置:   article > 正文

Unity 使用SNMP访问局域网中服务器的内存占用率_snmp占用内存

snmp占用内存

一、给服务器和pc端安装SNMP

注意:电脑只有专业版和企业版才有SNMP服务

https://blog.csdn.net/weixin_40720438/article/details/79740839

二、使用 mibbrowser工具进行SNMP测试

工具下载(内涵部分oid使用):https://pan.baidu.com/s/1Gvy3F9TPawnRoXWcPiavUQ 提取码:syq1

解压后打开文件

 win10打开SNMP服务和部分Oid 文档  里面具体操作步骤

三、Unity获取内存占用率

导入Snmp所需的dll

 https://pan.baidu.com/s/1dlUWDMd-HMPcJtjCg-Ta6A  提取码:syq1

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine.UI;
  4. using UnityEngine;
  5. using SnmpSharpNet;
  6. public class Test : MonoBehaviour {
  7. static string ip= "127.0.0.1";
  8. public InputField ipField;
  9. public Text txt;
  10. public Button btn;
  11. // Use this for initialization
  12. void Start () {
  13. btn.onClick.AddListener(BtnEvent);
  14. }
  15. void BtnEvent()
  16. {
  17. ip = ipField.text;
  18. string text = GetUsePhysicalMemoryOfSNMP().ToString();
  19. txt.text = text;
  20. Debug.Log("内存使用:" + text);
  21. }
  22. /// <summary>
  23. /// snmp获取本地当前win电脑内存使用
  24. /// snmp端口号161 团体名称:public
  25. /// 注意电脑上要开启snmp服务
  26. /// 获取数据具有延迟性,win的snmp机制所致
  27. /// </summary>
  28. /// <returns>返回系统物理内存使用(单位:G)</returns>
  29. public static float GetUsePhysicalMemoryOfSNMP()
  30. {
  31. System.Net.IPAddress ipAdd = System.Net.IPAddress.Parse(ip);
  32. UdpTarget SnmpSender = new UdpTarget(ipAdd, 161, 2000, 1);
  33. Pdu tempSendPdu = new Pdu(PduType.Get);
  34. float memoryF = float.MinValue;
  35. AgentParameters AgentParam = new AgentParameters(SnmpVersion.Ver2, new OctetString("public"));
  36. string oidRoot = ".1.3.6.1.2.1.25.2.3.1.3.";
  37. int idx = 1;
  38. SnmpV2Packet result = null;
  39. while (true)
  40. {
  41. if (idx > 1000)
  42. {
  43. return float.MinValue;
  44. }
  45. string curRepOid = oidRoot + idx;
  46. //Debug.Log("curRepOid:" + curRepOid);
  47. tempSendPdu.VbList.Clear();
  48. tempSendPdu.VbList.Add(curRepOid);
  49. result = SnmpSender.Request(tempSendPdu, AgentParam) as SnmpV2Packet;
  50. if (result != null && result.Pdu.ErrorIndex == 0)
  51. {
  52. string recVal = result.Pdu.VbList[0].Value.ToString();
  53. if (recVal.ToLower().Replace(" ", "") == "physicalmemory")
  54. {
  55. break;
  56. }
  57. else
  58. {
  59. tempSendPdu.VbList.Clear();
  60. idx++;
  61. }
  62. }
  63. else
  64. {
  65. break;
  66. }
  67. }
  68. string replayOID = "1.3.6.1.2.1.25.2.3.1.6." + idx;
  69. tempSendPdu.VbList.Clear();
  70. tempSendPdu.VbList.Add(replayOID);
  71. result = SnmpSender.Request(tempSendPdu, AgentParam) as SnmpV2Packet;
  72. if (result != null && result.Pdu.ErrorIndex == 0)
  73. {
  74. bool flag = float.TryParse(result.Pdu.VbList[0].Value.ToString(), out memoryF);
  75. if (flag)
  76. {
  77. memoryF = memoryF * 65536 / 1024 / 1024 / 1024;
  78. }
  79. }
  80. return memoryF;
  81. }
  82. }

输入服务器地址 运行:

 只要在同一个局域网中,服务器安装了Snmp服务,都可访问到

这里分享几个通用oid,如果获取不到,因为不同设备MIB的oid可能不同,需要从设备厂家要MIB库查找对应oid

1.3.6.1.2.1.1.5.0                设备名称:
1.3.6.1.2.1.25.3.3.1.2.1     cpu占用率:
1.3.6.1.2.1.25.2.2.0           系统物理内存:    获取值/1024 ----- (计算方式)
1.3.6.1.2.1.25.2.3.1.6.8     内存占用率:        获取值* 65536 / 1024 / 1024 / 1024 ----(计算方式)

1.3.6.1.2.1.25.1.6.0           系统进程数量:    本身卡顿3秒
 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/93359
推荐阅读
相关标签
  

闽ICP备14008679号