TrapView.cpp
上传用户:hbytqc8
上传日期:2014-07-31
资源大小:527k
文件大小:13k
- // TrapView.cpp : implementation of the CTrapView class
- //
- #include "stdafx.h"
- #include "Trap.h"
- #include "TrapDoc.h"
- #include "TrapView.h"
- #include "MainFrm.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CTrapView
- IMPLEMENT_DYNCREATE(CTrapView, CFormView)
- BEGIN_MESSAGE_MAP(CTrapView, CFormView)
- //{{AFX_MSG_MAP(CTrapView)
- ON_COMMAND(ID_START, OnStart)
- ON_COMMAND(ID_END, OnEnd)
- ON_COMMAND(ID_FINISH, OnFinish)
- ON_COMMAND(ID_CONFIG, OnConfig)
- ON_WM_DESTROY()
- //}}AFX_MSG_MAP
- // Standard printing commands
- // ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
- // ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
- // ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CTrapView construction/destruction
- CTrapView::CTrapView()
- : CFormView(CTrapView::IDD)
- {
- //{{AFX_DATA_INIT(CTrapView)
- //}}AFX_DATA_INIT
- // TODO: add construction code here
-
- }
- CTrapView::~CTrapView()
- {
- }
- void CTrapView::DoDataExchange(CDataExchange* pDX)
- {
- CFormView::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CTrapView)
- DDX_Control(pDX, IDC_LIST1, m_list1);
- DDX_Control(pDX, IDC_EDIT1, m_text);
- DDX_Control(pDX, IDC_TREE1, m_tree);
- //}}AFX_DATA_MAP
- }
- BOOL CTrapView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- return CFormView::PreCreateWindow(cs);
- }
- void CTrapView::OnInitialUpdate()
- {
- CFormView::OnInitialUpdate();
- GetParentFrame()->RecalcLayout();
- ResizeParentToFit();
- // m_hIcon=AfxGetApp()->LoadIcon(IDI_ICON1);;
- //pMain->pTrapView=this;
- TrapMessage="";
- m_list1.InsertColumn(0," 类型 ",LVCFMT_LEFT,80,0);
- m_list1.InsertColumn(1," 名称(DLCI)",LVCFMT_LEFT,130,1);
- m_list1.InsertColumn(2," 状态 ",LVCFMT_LEFT,70,2);
- m_list1.InsertColumn(3," 辅助信息 ",LVCFMT_LEFT,300,3);
- m_list1.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
- m_tree.InsertItem("被监视设备",NULL);
- m_tree.SetIndent(32);
- m_tree.SetItemHeight(26);
- // m_tree.InsertItem("aaa",m_tree.GetRootItem());
- m_iImageList.Create(16, 16, TRUE,1, 0);
- HICON hIcon;
- hIcon= NULL;
- //0,root;
- hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(),
- MAKEINTRESOURCE(IDI_ROUTER), IMAGE_ICON, 32, 32, 0);
- m_iImageList.Add(hIcon);
- //1,closed box;
- hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(),
- MAKEINTRESOURCE(IDI_LINE), IMAGE_ICON, 16, 16, 0);
- m_iImageList.Add(hIcon);
- hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(),
- MAKEINTRESOURCE(IDI_DOWN), IMAGE_ICON, 16, 16, 0);
- m_iImageList.Add(hIcon);
- m_tree.SetImageList (&m_iImageList,TVSIL_NORMAL);
- dlg.Create(IDD_DIALOG1);
- Recive=true;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CTrapView printing
- BOOL CTrapView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // default preparation
- return DoPreparePrinting(pInfo);
- }
- void CTrapView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add extra initialization before printing
- }
- void CTrapView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add cleanup after printing
- }
- void CTrapView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
- {
- // TODO: add customized printing code here
- }
- /////////////////////////////////////////////////////////////////////////////
- // CTrapView diagnostics
- #ifdef _DEBUG
- void CTrapView::AssertValid() const
- {
-
- CFormView::AssertValid();
- }
- void CTrapView::Dump(CDumpContext& dc) const
- {
- CFormView::Dump(dc);
- }
- CTrapDoc* CTrapView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTrapDoc)));
- return (CTrapDoc*)m_pDocument;
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CTrapView message handlers
- //回调函数,当有TRAP到达时,调用该函数进行进一步处理;
- void callme (int reason, Snmp *snmp, Pdu &pdu, SnmpTarget &target, void *cd)
- {
- CString str="";
- CTime time=CTime::GetCurrentTime();
- CString time_str=time.Format("%Y-%m-%d:%H:%M:%S");
- CTrapView *p;
- p=(CTrapView *)cd;
- Vb nextVb;
- GenAddress addr;
- target.get_address(addr);
- IpAddress from(addr);
- CString ipadd=from.get_printable();
- if (ipadd=="127.0.0.1")
- return;
- //结束监听时本机发送的TRAP退出循环,不做任何处理;
- Oid id,ent;
- pdu.get_notify_id(id);
- pdu.get_notify_enterprise(ent);
- CString TrapId=id.get_printable();
- // AfxMessageBox(TrapId);
- CString EnterPrise=ent.get_printable();
- //取得TRAP的ID以及enterprise ;
- //AfxMessageBox(EnterPrise);
- CString index,name;
- HTREEITEM ht;
- pdu.get_vb(nextVb, 0);
- index=nextVb.get_printable_value();
- pdu.get_vb(nextVb, 1);
- name=nextVb.get_printable_value();
- if (EnterPrise=="1.3.6.1.2.1.10.32")
- {
- for (int i=0;i<p->LineArray.GetSize();i++)
- {
- if (p->LineArray[i]->Selected)
- {
- if (p->LineArray[i]->Name==name)
- {
- ht=p->LineArray[i]->Ht;
- pdu.get_vb(nextVb,2);
- CString ud=nextVb.get_printable_value();
- if (ud=="2")
- {
- p->TrapMessage+=time_str+"设备:"+ipadd+"中PVC:"+name+"目前状态为正常!";
- p->TrapMessage+=p->LineArray[i]->Descr+"rn";
- p->m_tree.SetItemImage(ht,1,1);
- str=str+"设备:"+ipadd+"rn";
- str=str+"PVC:"+name+"rn";
- str=str+"状态正常!";
- }
- else
- {
- p->TrapMessage+=time_str+"设备:"+ipadd+"中PVC:"+name+"目前状态为不可用!";
- p->TrapMessage+=p->LineArray[i]->Descr+"rn";
- p->m_tree.SetItemImage(ht,2,2);
- str=str+"设备:"+ipadd+"rn";
- str=str+"PVC:"+name+"rn";
- str=str+"状态不可用!";
- }
- p->m_text.SetWindowText(p->TrapMessage);
- p->dlg.m_txt.SetWindowText(str);
- p->dlg.ShowWindow(SW_SHOW);
- break;
- }
- }
- }
- }
- else
- {
- for (int i=0;i<p->LineArray.GetSize();i++)
- {
- if ((p->LineArray[i]->Selected)&&(p->LineArray[i]->Type=="普通接口"))
- {
- if (p->LineArray[i]->Index==index)
- {
- ht=p->LineArray[i]->Ht;
- if (TrapId=="1.3.6.1.6.3.1.1.5.4")
- {
- p->TrapMessage+=time_str+"设备:"+ipadd+"中接口:"+name+"目前状态为UP!";
- p->TrapMessage+=p->LineArray[i]->Descr+"rn";
- p->m_tree.SetItemImage(ht,1,1);
- str=str+"设备:"+ipadd+"rn";
- str=str+"接口:"+name+"rn";
- str=str+"状态正常!";
- }
- if (TrapId=="1.3.6.1.6.3.1.1.5.3")
- {
- p->TrapMessage+=time_str+"设备:"+ipadd+"中接口:"+name+"目前状态为Down!";
- p->TrapMessage+=p->LineArray[i]->Descr+"rn";
- p->m_tree.SetItemImage(ht,2,2);
- str=str+"设备:"+ipadd+"rn";
- str=str+"接口:"+name+"rn";
- str=str+"状态不可用!";
- }
- p->m_text.SetWindowText(p->TrapMessage);
- p->dlg.m_txt.SetWindowText(str);
- p->dlg.ShowWindow(SW_SHOW);
- break;
- }
- }
- }
- }
- }
- //监听TRAP线程的处理函数,接受一个指针作为参数;
- UINT WorkerThreadProc(LPVOID Param)
- {
- Oid linkUp("1.3.6.1.6.3.1.1.5.4");
- Oid linkDown("1.3.6.1.6.3.1.1.5.3");
- CTrapView *pCV;
- pCV=(CTrapView *)Param;
- //将指针类型转换为CTrapView *类型指针;
- int trap_port;
- trap_port =162;
- //指定监听端口号;
- int status;
- Snmp::socket_startup();
- Snmp snmp( status, 0);
- // 创建Snmp对象;
- if ( status != SNMP_CLASS_SUCCESS)
- {
- AfxMessageBox("出现错误!");
- return TRUE;
- }
- OidCollection oidc;
- TargetCollection targetc;
- //创建过滤TRAP的对象;
- CNotifyEventQueue::set_listen_port(trap_port);
- //设置端口号;
- status=snmp.notify_register(oidc, targetc,callme,Param);
- //注册过滤条件,回调函数,并将Param,也就是指向CTrapView
- //对象的this指针,传递给回调函数,以方便在回调函数中访问
- //处理CTrapView对象中的成员变量;
- if (status != SNMP_CLASS_SUCCESS)
- {
- AfxMessageBox("出现错误!");
- return TRUE;
- }
- while (pCV->Recive)
- snmp.eventListHolder->SNMPProcessEvents();
- //进入SNMPProcessEvents()函数,等待TRAP触发回调用函数;
- Snmp::socket_cleanup();
- AfxMessageBox("停止监视线路!");
- return TRUE;
- }
- void CTrapView::OnStart()
- {
- if (m_tree.GetCount()<3)
- {
- MessageBox("先选择线路!");
- return;
- }
- m_list1.DeleteAllItems();
- if (Recive==false)
- Recive=true;
- AfxBeginThread(WorkerThreadProc,this,THREAD_PRIORITY_NORMAL,0,0,NULL);
- //创建一个新线程,用来在指定端口监听TRAP;将this指针作为参数传递给新线程;
- CMainFrame *pF=(CMainFrame *)AfxGetMainWnd();
- pF->m_wndStatusBar.SetPaneText(0,"开始监视......");
- // MessageBox("开始监听......");
- }
- void CTrapView::OnEnd()
- {
- if (Recive==true)
- Recive=false;
- Snmp::socket_startup();
- UdpAddress address("127.0.0.1:162");
- int status;
- Snmp snmp(status, "0.0.0.0") ;
- Pdu pdu;
- pdu.set_notify_id("1.3.6.1");
- pdu.set_notify_enterprise( "test");
- CTarget ctarget(address);
- ctarget.set_version( version1);
- ctarget.set_readcommunity("public");
- SnmpTarget *target=&ctarget;
- status = snmp.trap(pdu,*target);
- Snmp::socket_cleanup();
- //发送一个TRAP,结束监听状态;
- }
- void CTrapView::OnFinish()
- {
- HTREEITEM HRoot,HCurrent,Htemp;
- POSITION pos;
- pos=m_list1.GetFirstSelectedItemPosition();
- if (pos==NULL)
- {
- MessageBox("没有选择要监视的线路!");
- return;
- }
- HRoot=m_tree.GetRootItem();
- HCurrent=m_tree.InsertItem(TempArray[0]->Ip,0,0,HRoot);
- int nItem;
- while (pos)
- {
- nItem =m_list1.GetNextSelectedItem(pos);
- if ((nItem)<=TempArray.GetSize())
- {
- TempArray[nItem]->Selected=true;
-
- Htemp=m_tree.InsertItem(TempArray[nItem]->Name,1,1,HCurrent);
- if (TempArray[nItem]->State=="Down")
- m_tree.SetItemImage(Htemp,2,2);
- TempArray[nItem]->Ht=Htemp;
- }
- }
- m_tree.Expand(HRoot,TVE_EXPAND);
- for (int i=0;i<TempArray.GetSize();i++)
- {
- if (TempArray[i]->Selected==true)
- LineArray.Add(TempArray[i]);
- else
- delete TempArray[i];
- }
- }
- void CTrapView::OnConfig()
- {
- //number=LineArray.GetSize();
- TempArray.RemoveAll();
- CString ip,BaseOid,community;
- CScan dlg;
- if (dlg.DoModal()==IDOK)
- {
- ip=dlg.ip1;
- community=dlg.m_community;
- }
- m_snmp.clear();
- BaseOid="1.3.6.1.2.1.2.2.1.1";
- m_snmp.GetTree(ip,community,BaseOid);
- if (m_snmp.GetCount()==0)
- return;
- BaseOid="1.3.6.1.2.1.2.2.1.2";
- m_snmp.GetTree(ip,community,BaseOid);
- BaseOid="1.3.6.1.2.1.2.2.1.8";
- m_snmp.GetTree(ip,community,BaseOid);
- int row=m_snmp.GetCount()/3;
- for (int i=0;i<row;i++)
- {
- pLine=new Line;
- pLine->Type="普通接口";
- pLine->Selected=false;
- pLine->Index=m_snmp.GetValue(i);
- pLine->Name=m_snmp.GetValue(i+row);
- if (m_snmp.GetValue(i+2*row)=="1")
- pLine->State="UP";
- else pLine->State="Down";
- pLine->Ht=NULL;
- pLine->Descr="";
- pLine->Ip=ip;
- TempArray.Add(pLine);
- }
- m_snmp.clear();
- BaseOid="1.3.6.1.2.1.4.20.1.2";
- m_snmp.GetTree(ip,community,BaseOid);
- CString temp;
- for (i=0;i<m_snmp.GetCount();i++)
- {
- for (int j=0;j<TempArray.GetSize();j++)
- if (m_snmp.GetValue(i)==TempArray[j]->Index)
- {
- temp=m_snmp.GetOid(i);
- temp=temp.Right(temp.GetLength()-21);
- TempArray[j]->Descr="IP:"+temp;
- break;
- }
-
- }
- //for (int j=number;j<LineArray.GetSize();j++)
- for (int j=0;j<TempArray.GetSize();j++)
- {
- BaseOid="1.3.6.1.2.1.31.1.1.1.18."+TempArray[j]->Index;
- m_snmp.GetOne(ip,community,BaseOid,temp);
- TempArray[j]->Descr+=" 备注:"+temp;
- }
- //获取网络接口的信息并保存;
- m_snmp.clear();
- BaseOid="1.3.6.1.2.1.10.32.2.1.1";
- m_snmp.GetTree(ip,community,BaseOid);
- BaseOid="1.3.6.1.2.1.10.32.2.1.2";
- m_snmp.GetTree(ip,community,BaseOid);
- BaseOid="1.3.6.1.2.1.10.32.2.1.3";
- m_snmp.GetTree(ip,community,BaseOid);
- row=m_snmp.GetCount()/3;
- for (i=0;i<row;i++)
- {
- pLine=new Line;
- pLine->Selected=false;
- pLine->Type="帧中继PVC";
- pLine->Ip=ip;
- pLine->Ht=NULL;
- pLine->Index=m_snmp.GetValue(i);
- pLine->Name=m_snmp.GetValue(i+row);
- if (m_snmp.GetValue(i+2*row)=="2")
- pLine->State="UP";
- else pLine->State="Down";
- BaseOid="1.3.6.1.4.1.9.9.49.1.2.2.1.2."+pLine->Index+"."+pLine->Name;
- m_snmp.GetOne(ip,community,BaseOid,temp);
- if ("2"==temp)
- {
- CString rv;
- BaseOid="1.3.6.1.4.1.9.9.49.1.2.2.1.3."+pLine->Index+"."+pLine->Name;
- m_snmp.GetOne(ip,community,BaseOid,rv);
- BaseOid="1.3.6.1.2.1.31.1.1.1.18."+rv;
- m_snmp.GetOne(ip,community,BaseOid,rv);
- pLine->Descr=rv;
- }
- else
- {
- CString rv;
- BaseOid="1.3.6.1.4.1.9.9.49.1.3.1.1.3."+pLine->Index+"."+pLine->Name;
- m_snmp.GetOne(ip,community,BaseOid,rv);
- pLine->Descr=rv;
- }
- //LineArray.Add(pLine);
- TempArray.Add(pLine);
-
- }
- // if (pTrapView->m_list1.GetItemCount()>0)
- m_list1.DeleteAllItems();
- for (i=0;i<TempArray.GetSize();i++)
- {
- m_list1.InsertItem(i,TempArray[i]->Type);
- m_list1.SetItemText(i,1,TempArray[i]->Name);
- m_list1.SetItemText(i,2,TempArray[i]->State);
- m_list1.SetItemText(i,3,TempArray[i]->Descr);
- }
- }
- void CTrapView::OnDestroy()
- {
- CFormView::OnDestroy();
- for (int i=0;i<LineArray.GetSize();i++)
- {
- delete LineArray[i];
- }
- }