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

SNMP编程

开发平台:

Visual C++

  1. // TrafficDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "Traffic.h"
  5. #include "TrafficDlg.h"
  6. #include "input.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CAboutDlg dialog used for App About
  14. class CAboutDlg : public CDialog
  15. {
  16. public:
  17. CAboutDlg();
  18. // Dialog Data
  19. //{{AFX_DATA(CAboutDlg)
  20. enum { IDD = IDD_ABOUTBOX };
  21. //}}AFX_DATA
  22. // ClassWizard generated virtual function overrides
  23. //{{AFX_VIRTUAL(CAboutDlg)
  24. protected:
  25. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  26. //}}AFX_VIRTUAL
  27. // Implementation
  28. protected:
  29. //{{AFX_MSG(CAboutDlg)
  30. //}}AFX_MSG
  31. DECLARE_MESSAGE_MAP()
  32. };
  33. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  34. {
  35. //{{AFX_DATA_INIT(CAboutDlg)
  36. //}}AFX_DATA_INIT
  37. }
  38. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  39. {
  40. CDialog::DoDataExchange(pDX);
  41. //{{AFX_DATA_MAP(CAboutDlg)
  42. //}}AFX_DATA_MAP
  43. }
  44. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  45. //{{AFX_MSG_MAP(CAboutDlg)
  46. // No message handlers
  47. //}}AFX_MSG_MAP
  48. END_MESSAGE_MAP()
  49. /////////////////////////////////////////////////////////////////////////////
  50. // CTrafficDlg dialog
  51. CTrafficDlg::CTrafficDlg(CWnd* pParent /*=NULL*/)
  52. : CDialog(CTrafficDlg::IDD, pParent)
  53. {
  54. //{{AFX_DATA_INIT(CTrafficDlg)
  55. //}}AFX_DATA_INIT
  56. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  57. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  58. }
  59. void CTrafficDlg::DoDataExchange(CDataExchange* pDX)
  60. {
  61. CDialog::DoDataExchange(pDX);
  62. //{{AFX_DATA_MAP(CTrafficDlg)
  63. DDX_Control(pDX, IDC_EDIT2, m_community);
  64. DDX_Control(pDX, IDC_IPADDRESS1, m_ipadd);
  65. DDX_Control(pDX, IDC_LIST2, m_list1);
  66. //}}AFX_DATA_MAP
  67. }
  68. BEGIN_MESSAGE_MAP(CTrafficDlg, CDialog)
  69. //{{AFX_MSG_MAP(CTrafficDlg)
  70. ON_WM_SYSCOMMAND()
  71. ON_WM_PAINT()
  72. ON_WM_QUERYDRAGICON()
  73. ON_WM_DESTROY()
  74. ON_BN_CLICKED(IDC_BUTTON1, OnScan)
  75. ON_BN_CLICKED(IDC_BUTTON2, OnSure)
  76. ON_BN_CLICKED(IDC_BUTTON3, OnModify)
  77. ON_BN_CLICKED(IDC_BUTTON4, OnStart)
  78. ON_WM_TIMER()
  79. ON_BN_CLICKED(IDC_BUTTON5, OnEnd)
  80. ON_BN_CLICKED(IDC_BUTTON6, OnWorkData)
  81. ON_BN_CLICKED(IDC_BUTTON7, OnOpenFile)
  82. ON_BN_CLICKED(IDC_BUTTON8, OnToLeft)
  83. ON_BN_CLICKED(IDC_BUTTON9, OnToRight)
  84. ON_BN_CLICKED(IDC_CHECK1, OnChangeUse)
  85. //}}AFX_MSG_MAP
  86. END_MESSAGE_MAP()
  87. /////////////////////////////////////////////////////////////////////////////
  88. // CTrafficDlg message handlers
  89. BOOL CTrafficDlg::OnInitDialog()
  90. {
  91. CDialog::OnInitDialog();
  92. // Add "About..." menu item to system menu.
  93. // IDM_ABOUTBOX must be in the system command range.
  94. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  95. ASSERT(IDM_ABOUTBOX < 0xF000);
  96. CMenu* pSysMenu = GetSystemMenu(FALSE);
  97. if (pSysMenu != NULL)
  98. {
  99. CString strAboutMenu;
  100. strAboutMenu.LoadString(IDS_ABOUTBOX);
  101. if (!strAboutMenu.IsEmpty())
  102. {
  103. pSysMenu->AppendMenu(MF_SEPARATOR);
  104. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  105. }
  106. }
  107. // Set the icon for this dialog.  The framework does this automatically
  108. //  when the application's main window is not a dialog
  109. SetIcon(m_hIcon, TRUE); // Set big icon
  110. SetIcon(m_hIcon, FALSE); // Set small icon
  111. // TODO: Add extra initialization here
  112.     m_list1.InsertColumn(0,"  类型   ",LVCFMT_LEFT,80,0);
  113. m_list1.InsertColumn(1,"  名称(DLCI)",LVCFMT_LEFT,130,1);
  114.     m_list1.InsertColumn(2,"  最大速率",LVCFMT_LEFT,70,2);
  115.     m_list1.InsertColumn(3,"  状态  ",LVCFMT_LEFT,70,3);
  116. m_list1.InsertColumn(4,"  辅助信息  ",LVCFMT_LEFT,300,4);
  117. m_list1.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
  118.     CRect cr(25,12,518,150);
  119. CSize c(388,150);
  120. pDay=NULL;
  121. DayNumber=288;
  122. pDay=new CLineChart(c,DayNumber);
  123. pDay->Create(NULL,WS_CHILD|WS_VISIBLE|SS_NOTIFY|SS_BLACKFRAME, cr,this,50000);
  124. pDay->SetTitle("日流量图");
  125. pDay->SetLegend("流入","流出");
  126.     CRect cr1(25,169,518,170);
  127. CSize c1(388,150);
  128. pWeek=NULL;
  129. pWeek=new CLineChart(c1,DayNumber);
  130. pWeek->Create(NULL,WS_CHILD|WS_VISIBLE|SS_NOTIFY|SS_BLACKFRAME, cr1,this,50001);
  131. pWeek->SetTitle("周流量图");
  132. pWeek->SetLegend("流入","流出");
  133. index="";
  134. SampleNumber=0;
  135. return TRUE;  // return TRUE  unless you set the focus to a control
  136. }
  137. void CTrafficDlg::OnSysCommand(UINT nID, LPARAM lParam)
  138. {
  139. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  140. {
  141. CAboutDlg dlgAbout;
  142. dlgAbout.DoModal();
  143. }
  144. else
  145. {
  146. CDialog::OnSysCommand(nID, lParam);
  147. }
  148. }
  149. // If you add a minimize button to your dialog, you will need the code below
  150. //  to draw the icon.  For MFC applications using the document/view model,
  151. //  this is automatically done for you by the framework.
  152. void CTrafficDlg::OnPaint() 
  153. {
  154. if (IsIconic())
  155. {
  156. CPaintDC dc(this); // device context for painting
  157. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  158. // Center icon in client rectangle
  159. int cxIcon = GetSystemMetrics(SM_CXICON);
  160. int cyIcon = GetSystemMetrics(SM_CYICON);
  161. CRect rect;
  162. GetClientRect(&rect);
  163. int x = (rect.Width() - cxIcon + 1) / 2;
  164. int y = (rect.Height() - cyIcon + 1) / 2;
  165. // Draw the icon
  166. dc.DrawIcon(x, y, m_hIcon);
  167. }
  168. else
  169. {
  170. CDialog::OnPaint();
  171. }
  172. }
  173. // The system calls this to obtain the cursor to display while the user drags
  174. //  the minimized window.
  175. HCURSOR CTrafficDlg::OnQueryDragIcon()
  176. {
  177. return (HCURSOR) m_hIcon;
  178. }
  179. void CTrafficDlg::OnDestroy() 
  180. {
  181. CDialog::OnDestroy();
  182. if (pDay!=NULL) delete pDay;
  183. if (pWeek!=NULL) delete pWeek;
  184. }
  185. void CTrafficDlg::OnScan() 
  186. {
  187. CString BaseOid;
  188. m_ipadd.GetWindowText(ip);
  189. m_community.GetWindowText(community);
  190. if ((community.GetLength()==0)||(ip.GetLength()==0))
  191. {
  192. MessageBox("地址或口令错误!");
  193. return;
  194. }
  195.     m_snmp.clear();
  196. BaseOid="1.3.6.1.2.1.2.2.1.1";
  197. m_snmp.GetTree(ip,community,BaseOid);
  198. BaseOid="1.3.6.1.2.1.2.2.1.2";
  199. m_snmp.GetTree(ip,community,BaseOid);
  200. BaseOid="1.3.6.1.2.1.2.2.1.8";
  201. m_snmp.GetTree(ip,community,BaseOid);
  202. BaseOid="1.3.6.1.2.1.2.2.1.5";
  203. m_snmp.GetTree(ip,community,BaseOid);
  204. int row=m_snmp.GetCount()/4;
  205. for (int i=0;i<row;i++)
  206. {
  207. pLine=new Line;
  208.         pLine->Type="普通接口";
  209. pLine->Index=m_snmp.GetValue(i);
  210. pLine->Name=m_snmp.GetValue(i+row);
  211. pLine->Speed=m_snmp.GetValue(i+3*row);
  212. if (m_snmp.GetValue(i+2*row)=="1")
  213. pLine->State="UP";
  214. else 
  215. pLine->State="Down";
  216.       pLine->Descr="";
  217. pLine->Ip=ip;
  218. LineArray.Add(pLine);
  219. }
  220. m_snmp.clear();
  221. BaseOid="1.3.6.1.2.1.4.20.1.2";
  222. m_snmp.GetTree(ip,community,BaseOid);
  223. CString temp;
  224. for (i=0;i<m_snmp.GetCount();i++)
  225. {
  226. for (int j=0;j<LineArray.GetSize();j++)
  227. if (m_snmp.GetValue(i)==LineArray[j]->Index)
  228. {
  229. temp=m_snmp.GetOid(i);
  230. temp=temp.Right(temp.GetLength()-21);
  231. LineArray[j]->Descr="IP:"+temp;
  232. break;
  233. }
  234. }
  235. for (int j=0;j<LineArray.GetSize();j++)
  236. {
  237. BaseOid="1.3.6.1.2.1.31.1.1.1.18."+LineArray[j]->Index;
  238. m_snmp.GetOne(ip,community,BaseOid,temp);
  239. LineArray[j]->Descr+=" 备注:"+temp;
  240. }
  241. //获取网络接口的信息并保存;
  242. m_snmp.clear();
  243. BaseOid="1.3.6.1.2.1.10.32.2.1.1";
  244. m_snmp.GetTree(ip,community,BaseOid);
  245. BaseOid="1.3.6.1.2.1.10.32.2.1.2";
  246. m_snmp.GetTree(ip,community,BaseOid);
  247. BaseOid="1.3.6.1.2.1.10.32.2.1.3";
  248. m_snmp.GetTree(ip,community,BaseOid);
  249. row=m_snmp.GetCount()/3;
  250. for (i=0;i<row;i++)
  251. {
  252. pLine=new Line;
  253. pLine->Type="帧中继PVC";
  254. pLine->Ip=ip;
  255. pLine->Speed="65535";
  256. pLine->Index=m_snmp.GetValue(i);
  257. pLine->Name=m_snmp.GetValue(i+row);
  258. if (m_snmp.GetValue(i+2*row)=="2")
  259. pLine->State="UP";
  260. else  pLine->State="Down";
  261. BaseOid="1.3.6.1.4.1.9.9.49.1.2.2.1.2."+pLine->Index+"."+pLine->Name;
  262. m_snmp.GetOne(ip,community,BaseOid,temp);
  263. if ("2"==temp)
  264. {
  265. CString rv;
  266. BaseOid="1.3.6.1.4.1.9.9.49.1.2.2.1.3."+pLine->Index+"."+pLine->Name;
  267. m_snmp.GetOne(ip,community,BaseOid,rv);
  268. BaseOid="1.3.6.1.2.1.31.1.1.1.18."+rv;
  269. m_snmp.GetOne(ip,community,BaseOid,rv);
  270. pLine->Descr=rv;
  271. }
  272. else
  273. {
  274. CString rv;
  275. BaseOid="1.3.6.1.4.1.9.9.49.1.3.1.1.3."+pLine->Index+"."+pLine->Name;
  276. m_snmp.GetOne(ip,community,BaseOid,rv);
  277. pLine->Descr=rv;
  278. }
  279. LineArray.Add(pLine);
  280.     
  281. }
  282.     m_list1.DeleteAllItems();
  283. for (i=0;i<LineArray.GetSize();i++)
  284. {
  285.      m_list1.InsertItem(i,LineArray[i]->Type);
  286.  m_list1.SetItemText(i,1,LineArray[i]->Name);
  287.      m_list1.SetItemText(i,2,LineArray[i]->Speed);
  288.      m_list1.SetItemText(i,3,LineArray[i]->State);
  289.      m_list1.SetItemText(i,4,LineArray[i]->Descr);
  290. }
  291. }
  292. void CTrafficDlg::OnSure() 
  293. {
  294. if (LineArray.GetSize()==0)
  295.     {
  296. MessageBox("无可用数据!");
  297. return;
  298. }
  299.   POSITION pos;
  300.   pos=m_list1.GetFirstSelectedItemPosition();
  301.   if (pos==NULL)
  302.   {
  303.   MessageBox("没有选择线路!");
  304.   return;
  305.   }
  306.   int n=m_list1.GetNextSelectedItem(pos);
  307.   if (LineArray[n]->Type=="帧中继PVC")
  308.   {
  309.     index=LineArray[n]->Index+"."+LineArray[n]->Name;
  310. oid="1.3.6.1.2.1.10.32.2.1";
  311.   }
  312.   else
  313.   {
  314. index=LineArray[n]->Index;
  315.     oid="1.3.6.1.2.1.2.2.1";
  316.   }
  317.   MaxSpeed=atoi(LineArray[n]->Speed);
  318.  for (int i=0;i<LineArray.GetSize();i++)
  319.  {
  320.  if (LineArray[i]!=NULL) delete LineArray[i];
  321.  }  
  322.  m_list1.DeleteAllItems();
  323. }
  324. void CTrafficDlg::OnModify() 
  325. {
  326. char ch[10];
  327. itoa(MaxSpeed,ch,10);
  328. Cinput dlg;
  329. dlg.m_old=ch;
  330. if (dlg.DoModal()==IDOK)
  331. {   
  332. if (dlg.m_new.GetLength()>0)
  333. MaxSpeed=atoi((LPCTSTR)dlg.m_new);
  334. MessageBox("速率更改成功!");
  335. }
  336. }
  337. void CTrafficDlg::OnStart() 
  338. {
  339. if (index.GetLength()==0)
  340. {
  341. MessageBox("OID错误!");
  342. return;
  343. }
  344. pDay->Init();
  345. pWeek->Init();
  346. CTime t=CTime::GetCurrentTime();
  347. fDayName=t.Format("%m-%d-%H");
  348. fDayName=fDayName+"Day.txt";
  349. DataFile.Open(fDayName,CFile::modeCreate);
  350. DataFile.Close();
  351. fWeekName=t.Format("%m-%d-%H");
  352. fWeekName=fWeekName+"Week.txt";
  353. DataFile.Open(fWeekName,CFile::modeCreate);
  354. DataFile.Close();
  355. SetTimer(1,300000,NULL);
  356. m_snmp.GetThree(ip,community,oid,index,last_in,last_out,last_time);
  357. CButton* m_pB=(CButton*)GetDlgItem(IDC_BUTTON4);
  358. m_pB->EnableWindow(FALSE);
  359. }
  360. void CTrafficDlg::OnTimer(UINT nIDEvent) 
  361. {
  362. unsigned long InBits,OutBits;
  363. unsigned long in_v,out_v,time_v,in_diff,out_diff,time_diff;
  364. CTime t=CTime::GetCurrentTime();
  365. CString time=t.Format("%H:%M");
  366. m_snmp.GetThree(ip,community,oid,index,in_v,out_v,time_v);
  367. in_diff=in_v-last_in;
  368. if (in_diff<0)
  369. in_diff+=4294967295;
  370. out_diff=out_v-last_out;
  371. if (out_diff<0)
  372. out_diff+=4294967295;
  373. time_diff=time_v-last_time;
  374. if (time_diff<0)
  375. time_diff+=4294967295;
  376. InBits=(in_diff*8)/(time_diff/100);
  377. OutBits=(out_diff*8)/(time_diff/100);
  378. int In_use=InBits*100/MaxSpeed;
  379. int Out_use=OutBits*100/MaxSpeed;
  380. pDay->Append(InBits,OutBits,time,In_use,Out_use);
  381. SampleNumber+=1;
  382. CString line,s;
  383. line="";
  384. if ((SampleNumber % 6)==0)
  385. {
  386. CString time1=t.Format("%a");
  387. pWeek->Append(pDay->GetXAverage(),pDay->GetYAverage(),time1,0,0);
  388. line+=time1;
  389. if(DataFile.Open(fWeekName,CFile::modeWrite))
  390. {
  391. s.Format("%d",pDay->GetXAverage());
  392. line=line+"@"+s;
  393. s.Format("%d",pDay->GetYAverage());
  394. line=line+"#"+s;
  395. s.Format("%d",0);
  396. line=line+"$"+s;
  397. s.Format("%d",0);
  398. line=line+"&"+s+"rn";
  399. DataFile.SeekToEnd();
  400. DataFile.WriteString(line);
  401. line="";
  402. DataFile.Close();
  403. }
  404. }
  405. last_in=in_v;
  406. last_out=out_v;
  407. last_time=time_v;
  408. line+=time;
  409. if(DataFile.Open(fDayName,CFile::modeWrite))
  410. {
  411. s.Format("%d",InBits);
  412. line=line+"@"+s;
  413. s.Format("%d",OutBits);
  414. line=line+"#"+s;
  415. s.Format("%d",In_use);
  416. line=line+"$"+s;
  417. s.Format("%d",Out_use);
  418. line=line+"&"+s+"rn";
  419. DataFile.SeekToEnd();
  420. DataFile.WriteString(line);
  421. DataFile.Close();
  422. }
  423. if (SampleNumber==DayNumber)
  424. {
  425. CTime t=CTime::GetCurrentTime();
  426. fDayName=t.Format("%m-%d-%H");
  427. fDayName=fDayName+".txt";
  428. DataFile.Open(fDayName,CFile::modeCreate);
  429. DataFile.Close();
  430. SampleNumber=0;
  431. }
  432. CDialog::OnTimer(nIDEvent);
  433. }
  434. void CTrafficDlg::OnEnd() 
  435. {
  436. KillTimer(1);
  437. CButton* m_pB=(CButton*)GetDlgItem(IDC_BUTTON4);
  438. m_pB->EnableWindow(TRUE);
  439. }
  440. void CTrafficDlg::OnWorkData() 
  441. {
  442. CButton* m_pB=(CButton*)GetDlgItem(IDC_BUTTON4);
  443. if (!(m_pB->IsWindowEnabled()))
  444. {
  445. MessageBox("先停止流量测试!");
  446. return;
  447. }
  448. pDay->Init();
  449. pWeek->Init();
  450. pDay->SetCross(true);
  451. }
  452. void CTrafficDlg::OnOpenFile() 
  453. {   
  454. bool show;
  455.     unsigned long x,y;
  456. int number,Usex,Usey;
  457.     CStdioFile dataFile;
  458. CString line,t;
  459. pDay->Init();
  460. CFileDialog openfile(TRUE,"*.txt",NULL,
  461.  OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
  462.  "Text files(*.txt)|*.txt");
  463. if (openfile.DoModal()==IDOK)
  464. {
  465. if(dataFile.Open(openfile.GetPathName(),CFile::modeRead))
  466. {   
  467. number=0;
  468. while (dataFile.ReadString(line))
  469. {
  470. int n0=line.Find('@');
  471. CString str=line.Mid(0,n0);
  472. t=str;
  473.                 int n1=line.Find('#');
  474. str=line.Mid(n0+1,n1-n0-1);
  475. x=atoi((LPCTSTR)str);
  476. n0=line.Find('$');
  477. str=line.Mid(n1+1,n0-n1-1);
  478.                 y=atoi((LPCTSTR)str);
  479. n1=line.Find('&');
  480.                 str=line.Mid(n0+1,n1-n0-1);
  481.                 Usex=atoi((LPCTSTR)str);
  482. str=line.Mid(n1+1,10);
  483.                 Usey=atoi((LPCTSTR)str);
  484. if ((number % 36)==0)
  485. show=true;
  486. else 
  487. show=false;
  488. pDay->AddData(x,y,t,Usex,Usey,number,show);
  489.     number+=1;
  490. }
  491. }
  492.    dataFile.Close();
  493. }
  494. pDay->Invalidate();
  495. }
  496. void CTrafficDlg::OnToLeft() 
  497. {
  498. pDay->ToLeft();
  499. }
  500. void CTrafficDlg::OnToRight() 
  501. {
  502. pDay->ToRight();
  503. }
  504. void CTrafficDlg::OnChangeUse() 
  505. {
  506. CButton* m_pCheck=(CButton*)GetDlgItem(IDC_CHECK1);
  507. if(m_pCheck->GetCheck())
  508. pDay->SetUse(true);
  509. else
  510. pDay->SetUse(false);
  511. }