TrafficView.cpp
上传用户:hbytqc8
上传日期:2014-07-31
资源大小:527k
文件大小:7k
源码类别:

SNMP编程

开发平台:

Visual C++

  1. // TrafficView.cpp : implementation of the CTrafficView class
  2. //
  3. #include "stdafx.h"
  4. #include "Traffic.h"
  5. #include "Input.h"
  6. #include "TrafficDoc.h"
  7. #include "TrafficView.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CTrafficView
  15. IMPLEMENT_DYNCREATE(CTrafficView, CFormView)
  16. BEGIN_MESSAGE_MAP(CTrafficView, CFormView)
  17. //{{AFX_MSG_MAP(CTrafficView)
  18. ON_COMMAND(ID_SCAN, OnScan)
  19. ON_COMMAND(ID_START, OnStart)
  20. ON_WM_TIMER()
  21. ON_COMMAND(ID_END, OnEnd)
  22. //}}AFX_MSG_MAP
  23. // Standard printing commands
  24. ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
  25. ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
  26. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
  27. END_MESSAGE_MAP()
  28. /////////////////////////////////////////////////////////////////////////////
  29. // CTrafficView construction/destruction
  30. CTrafficView::CTrafficView()
  31. : CFormView(CTrafficView::IDD)
  32. {
  33. //{{AFX_DATA_INIT(CTrafficView)
  34. //}}AFX_DATA_INIT
  35. // TODO: add construction code here
  36. ip="127.0.0.1";
  37. community="public";
  38. oid="1.3.6.1.2.1.2.2.1";
  39. last_in=0;
  40. last_out=0;
  41. last_time=0;
  42. }
  43. CTrafficView::~CTrafficView()
  44. {
  45. }
  46. void CTrafficView::DoDataExchange(CDataExchange* pDX)
  47. {
  48. CFormView::DoDataExchange(pDX);
  49. //{{AFX_DATA_MAP(CTrafficView)
  50. DDX_Control(pDX, IDC_LIST1, m_list1);
  51. //}}AFX_DATA_MAP
  52. }
  53. BOOL CTrafficView::PreCreateWindow(CREATESTRUCT& cs)
  54. {
  55. // TODO: Modify the Window class or styles here by modifying
  56. //  the CREATESTRUCT cs
  57. return CFormView::PreCreateWindow(cs);
  58. }
  59. void CTrafficView::OnInitialUpdate()
  60. {
  61. CFormView::OnInitialUpdate();
  62. GetParentFrame()->RecalcLayout();
  63. ResizeParentToFit();
  64.     m_list1.InsertColumn(0,"  类型   ",LVCFMT_LEFT,80,0);
  65. m_list1.InsertColumn(1,"  名称(DLCI)",LVCFMT_LEFT,130,1);
  66.     m_list1.InsertColumn(2,"  状态  ",LVCFMT_LEFT,70,2);
  67. m_list1.InsertColumn(3,"  辅助信息  ",LVCFMT_LEFT,300,3);
  68. m_list1.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
  69. }
  70. /////////////////////////////////////////////////////////////////////////////
  71. // CTrafficView printing
  72. BOOL CTrafficView::OnPreparePrinting(CPrintInfo* pInfo)
  73. {
  74. // default preparation
  75. return DoPreparePrinting(pInfo);
  76. }
  77. void CTrafficView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  78. {
  79. // TODO: add extra initialization before printing
  80. }
  81. void CTrafficView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  82. {
  83. // TODO: add cleanup after printing
  84. }
  85. void CTrafficView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
  86. {
  87. // TODO: add customized printing code here
  88. }
  89. /////////////////////////////////////////////////////////////////////////////
  90. // CTrafficView diagnostics
  91. #ifdef _DEBUG
  92. void CTrafficView::AssertValid() const
  93. {
  94. CFormView::AssertValid();
  95. }
  96. void CTrafficView::Dump(CDumpContext& dc) const
  97. {
  98. CFormView::Dump(dc);
  99. }
  100. CTrafficDoc* CTrafficView::GetDocument() // non-debug version is inline
  101. {
  102. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTrafficDoc)));
  103. return (CTrafficDoc*)m_pDocument;
  104. }
  105. #endif //_DEBUG
  106. /////////////////////////////////////////////////////////////////////////////
  107. // CTrafficView message handlers
  108. void CTrafficView::OnScan() 
  109. {
  110. CString BaseOid;
  111. CInput dlg;
  112. if (dlg.DoModal()==IDOK)
  113. {
  114. ip=dlg.ip;
  115. community=dlg.m_community;
  116. }
  117. m_snmp.clear();
  118. BaseOid="1.3.6.1.2.1.2.2.1.1";
  119. m_snmp.GetTree(ip,community,BaseOid);
  120. BaseOid="1.3.6.1.2.1.2.2.1.2";
  121. m_snmp.GetTree(ip,community,BaseOid);
  122. BaseOid="1.3.6.1.2.1.2.2.1.8";
  123. m_snmp.GetTree(ip,community,BaseOid);
  124. int row=m_snmp.GetCount()/3;
  125. for (int i=0;i<row;i++)
  126. {
  127. pLine=new Line;
  128.         pLine->Type="普通接口";
  129. pLine->Index=m_snmp.GetValue(i);
  130. pLine->Name=m_snmp.GetValue(i+row);
  131. if (m_snmp.GetValue(i+2*row)=="1")
  132. pLine->State="UP";
  133. else 
  134. pLine->State="Down";
  135.       pLine->Descr="";
  136. pLine->Ip=ip;
  137. LineArray.Add(pLine);
  138. }
  139. m_snmp.clear();
  140. BaseOid="1.3.6.1.2.1.4.20.1.2";
  141. m_snmp.GetTree(ip,community,BaseOid);
  142. CString temp;
  143. for (i=0;i<m_snmp.GetCount();i++)
  144. {
  145. for (int j=0;j<LineArray.GetSize();j++)
  146. if (m_snmp.GetValue(i)==LineArray[j]->Index)
  147. {
  148. temp=m_snmp.GetOid(j);
  149. temp=temp.Right(temp.GetLength()-21);
  150. LineArray[j]->Descr="IP:"+temp;
  151. break;
  152. }
  153. }
  154. for (int j=0;j<LineArray.GetSize();j++)
  155. {
  156. BaseOid="1.3.6.1.2.1.31.1.1.1.18."+LineArray[j]->Index;
  157. m_snmp.GetOne(ip,community,BaseOid,temp);
  158. LineArray[j]->Descr+=" 备注:"+temp;
  159. }
  160. //获取网络接口的信息并保存;
  161. m_snmp.clear();
  162. BaseOid="1.3.6.1.2.1.10.32.2.1.1";
  163. m_snmp.GetTree(ip,community,BaseOid);
  164. BaseOid="1.3.6.1.2.1.10.32.2.1.2";
  165. m_snmp.GetTree(ip,community,BaseOid);
  166. BaseOid="1.3.6.1.2.1.10.32.2.1.3";
  167. m_snmp.GetTree(ip,community,BaseOid);
  168. row=m_snmp.GetCount()/3;
  169. for (i=0;i<row;i++)
  170. {
  171. pLine=new Line;
  172. pLine->Type="帧中继PVC";
  173. pLine->Ip=ip;
  174. pLine->Index=m_snmp.GetValue(i);
  175. pLine->Name=m_snmp.GetValue(i+row);
  176. if (m_snmp.GetValue(i+2*row)=="2")
  177. pLine->State="UP";
  178. else  pLine->State="Down";
  179. BaseOid="1.3.6.1.4.1.9.9.49.1.2.2.1.2."+pLine->Index+"."+pLine->Name;
  180. m_snmp.GetOne(ip,community,BaseOid,temp);
  181. if ("2"==temp)
  182. {
  183. CString rv;
  184. BaseOid="1.3.6.1.4.1.9.9.49.1.2.2.1.3."+pLine->Index+"."+pLine->Name;
  185. m_snmp.GetOne(ip,community,BaseOid,rv);
  186. BaseOid="1.3.6.1.2.1.31.1.1.1.18."+rv;
  187. m_snmp.GetOne(ip,community,BaseOid,rv);
  188. pLine->Descr=rv;
  189. }
  190. else
  191. {
  192. CString rv;
  193. BaseOid="1.3.6.1.4.1.9.9.49.1.3.1.1.3."+pLine->Index+"."+pLine->Name;
  194. m_snmp.GetOne(ip,community,BaseOid,rv);
  195. pLine->Descr=rv;
  196. }
  197. LineArray.Add(pLine);
  198.     
  199. }
  200.     m_list1.DeleteAllItems();
  201. for (i=0;i<LineArray.GetSize();i++)
  202. {
  203.      m_list1.InsertItem(i,LineArray[i]->Type);
  204.  m_list1.SetItemText(i,1,LineArray[i]->Name);
  205.      m_list1.SetItemText(i,2,LineArray[i]->State);
  206.      m_list1.SetItemText(i,3,LineArray[i]->Descr);
  207. }
  208. }
  209. void CTrafficView::OnStart() 
  210. {
  211. SetTimer(1,60000,NULL);
  212. m_snmp.GetThree(ip,community,oid,index,last_in,last_out,last_time);
  213. }
  214. void CTrafficView::OnTimer(UINT nIDEvent) 
  215. {
  216. unsigned long InBits,OutBits;
  217. unsigned long in_v,out_v,time_v,in_diff,out_diff,time_diff;
  218. CTime t=CTime::GetCurrentTime();
  219. CString time=t.Format("%H:%M");
  220. m_snmp.GetThree(ip,community,oid,index,in_v,out_v,time_v);
  221. in_diff=in_v-last_in;
  222. if (in_diff<0)
  223. in_diff+=4294967295;
  224. out_diff=out_v-last_out;
  225. if (out_diff<0)
  226. out_diff+=4294967295;
  227. time_diff=time_v-last_time;
  228. if (time_diff<0)
  229. time_diff+=4294967295;
  230. InBits=(in_diff*8)/(time_diff/100);
  231. OutBits=(out_diff*8)/(time_diff/100);
  232. last_in=in_v;
  233. last_out=out_v;
  234. last_time=time_v;
  235. //write to a txt file
  236. /*CString s;
  237. s.Format("%d",v1);
  238. MessageBox(s);
  239. s.Format("%d",v3);
  240. MessageBox(s);*/
  241. CFormView::OnTimer(nIDEvent);
  242. }
  243. void CTrafficView::OnEnd() 
  244. {
  245. KillTimer(1);
  246. }