www.pudn.com > SnmpExpriment1.rar > SnmpExprimentDlg.cpp
// SnmpExprimentDlg.cpp : implementation file
//
#include "stdafx.h"
#include "SnmpExpriment.h"
#include "SnmpExprimentDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
//UINT _StartThread(LPVOID param);
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSnmpExprimentDlg dialog
CSnmpExprimentDlg::CSnmpExprimentDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSnmpExprimentDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSnmpExprimentDlg)
m_FileType = _T("");
m_CurIn = _T("");
m_CurOut = _T("");
m_OrigIn = _T("");
m_OrigOut = _T("");
m_RealIn = _T("");
m_RealOut = _T("");
m_AgentIP = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSnmpExprimentDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSnmpExprimentDlg)
DDX_Control(pDX, IDC_END, m_BtnEnd);
DDX_Control(pDX, IDC_COMBOTYPE, m_ComboType);
DDX_CBString(pDX, IDC_COMBOTYPE, m_FileType);
DDX_Text(pDX, IDC_CURIN, m_CurIn);
DDX_Text(pDX, IDC_CUROUT, m_CurOut);
DDX_Text(pDX, IDC_ORIGIN, m_OrigIn);
DDX_Text(pDX, IDC_ORIGOUT, m_OrigOut);
DDX_Text(pDX, IDC_REALIN, m_RealIn);
DDX_Text(pDX, IDC_REALOUT, m_RealOut);
DDX_Text(pDX, IDC_AGENTIP, m_AgentIP);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSnmpExprimentDlg, CDialog)
//{{AFX_MSG_MAP(CSnmpExprimentDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_END, OnEnd)
ON_MESSAGE(wMsg,OnRecv)
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSnmpExprimentDlg message handlers
BOOL CSnmpExprimentDlg::OnInitDialog()//初始化对话框
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
//m_ComboType.SetExtendedUI(FALSE);
//m_ComboType.ShowDropDown(FALSE);
//m_ComboType.SetDroppedWidth(40);
m_AgentIP="127.0.0.1";//初始要监测的IP地址
m_in=FALSE;
m_out=FALSE;
//GetDlgItem(IDC_AGENTIP).
SetDlgItemText(IDC_AGENTIP,m_AgentIP);
for(int i=0;i<=9;i++)
{
m_initOid[i]=new char[100];
}
m_initOid[1]="1.3.6.1.2.1.1.1.0"; //网络设备的系统描述对象实例标识符
m_initOid[2]="1.3.6.1.2.1.1.2.0"; //找出Router的ID以及WAN Interface的Object ID
m_initOid[3]="1.3.6.1.2.1.1.3.0";
m_initOid[4]="1.3.6.1.2.1.1.5.0"; //查询系统的名称
m_initOid[5]="1.3.6.1.2.1.4.3.0"; //累计从接口收到的IP输入数据报的总数,包括出错的数据报
m_initOid[6]="1.3.6.1.2.1.4.10.0"; //累计IP的上层协议(如TCP、UDP或ICMP)提供给IP传送的全部数据报个数
CString path="D:\\SNMP";
if(-1==::GetFileAttributes(path))//如果没有找到相关的目录
{
::CreateDirectory(path,NULL); //创建文件夹,存放各系统文件
//::SetFileAttributes(path,FILE_ATTRIBUTE_HIDDEN);
}
CString logPath="D:\\SNMP\\log.txt";
if(-1==::GetFileAttributes(logPath))//如果没有找到相应的文件名,则创建新的文件
{
CFileException ex;//保存出错信息
CFile log;
if(!log.Open(logPath,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary,&ex))//创建文件失败
{
TCHAR szError[1024];
ex.GetErrorMessage(szError, 1024);
::AfxMessageBox(szError);
return FALSE;
}
}
for(i=0;i<=9;i++ )
{
m_value[i]=new smiVALUE[100];
m_sOid[i]=new char[100];
str[i]=new char[100];
}
m_BtnEnd.EnableWindow(FALSE);//按钮“结束”不可用
return TRUE; // return TRUE unless you set the focus to a control
}
void CSnmpExprimentDlg::OnSysCommand(UINT nID, LPARAM lParam)//系统关于对话框的显示
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CSnmpExprimentDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CSnmpExprimentDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CSnmpExprimentDlg::OnOK() //按下“开始”时的响应
{
// TODO: Add extra validation here
UpdateData();
CFileException ex;
if((0==m_AgentIP.GetLength())||(""==m_AgentIP))
{
MessageBox("请填写AgentIP !");
}
else
{
if((0==m_FileType.GetLength())||(""==m_FileType))
{
MessageBox("请选择文件类型!");
return ;
}
else
{
CFile log;
if(!log.Open("D:\\SNMP\\Log.txt", CFile::modeWrite|CFile::typeBinary,&ex))//定义一个日志文件,用于记录系统日志
{
TCHAR szError[1024];
ex.GetErrorMessage(szError, 1024);
::AfxMessageBox(szError);
//AfxMessageBox("打开log文件失败!");
}
else
{
CTime time=CTime::GetCurrentTime();//定义一个时间变量,赋值为当前系统时间
CString strTime=time.Format("%Y.%m.%d %H-%M-%S");
CString strFold="D:\\SNMP\\";
strFold+=m_FileType+_T("[")+strTime+_T("]");
::CreateDirectory(strFold,NULL); //以当前的时间做为文件名来保存流日志
CString strPath=strFold+_T("\\");
CString fileName1=strPath+m_FileType+"SIN"; //存放每秒钟采集的输入数据
// CString fileName2=strPath+m_FileType+"MIN"; //存放每分钟采集的输入数据
CString fileName3=strPath+m_FileType+"SOUT"; //存放每秒钟采集的输出数据
// CString fileName4=strPath+m_FileType+"MOUT"; //存放每秒钟采集的输出数据
// CString fileName5=strPath+m_FileType+"WIN"; //存放每100微秒钟采集的输入数据,暂时未用
// CString fileName6=strPath+m_FileType+"WOUT"; //存放每100秒钟采集的输出数据,暂时未用
CString str=_T("[")+strTime+_T("]")+".txt";
fileName1+=str;
// fileName2+=str;
fileName3+=str;
// fileName4+=str;
// fileName5+=str;
// fileName6+=str;
if(!m_sFileIn.Open(fileName1,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary,&ex))
{
TCHAR szError[1024];
ex.GetErrorMessage(szError, 1024);
::AfxMessageBox(szError);
//AfxMessageBox("创建文件"+fileName1+"失败!");
}
else
{
log.SeekToEnd();
log.Write(fileName1+"\r\n",fileName1.GetLength()+2);
log.SeekToBegin();
}
if(!m_sFileOut.Open(fileName3,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary,&ex))
{
TCHAR szError[1024];
ex.GetErrorMessage(szError, 1024);
::AfxMessageBox(szError);
//AfxMessageBox("创建文件"+fileName1+"失败!");
}
else
{
log.SeekToEnd();
log.Write(fileName3+"\r\n\r\n",fileName3.GetLength()+4);
log.SeekToBegin();
}
/* if(!m_mFileIn.Open(fileName2,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary,&ex))
{
TCHAR szError[1024];
ex.GetErrorMessage(szError, 1024);
::AfxMessageBox(szError);
//AfxMessageBox("创建文件"+fileName2+"失败!");
}
else
{
log.SeekToEnd();
log.Write(fileName2+"\r\n",fileName2.GetLength()+2);
log.SeekToBegin();
}
if(!m_mFileOut.Open(fileName4,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary,&ex))
{
TCHAR szError[1024];
ex.GetErrorMessage(szError, 1024);
::AfxMessageBox(szError);
//AfxMessageBox("创建文件"+fileName2+"失败!");
}
else
{
log.SeekToEnd();
log.Write(fileName4+"\r\n\r\n",fileName4.GetLength()+4);
log.SeekToBegin();
}
if(!m_wFileIn.Open(fileName5,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary,&ex))
{
TCHAR szError[1024];
ex.GetErrorMessage(szError, 1024);
::AfxMessageBox(szError);
}
else
{
log.SeekToEnd();
log.Write(fileName5+"\r\n",fileName5.GetLength()+2);
log.SeekToBegin();
}
if(!m_wFileOut.Open(fileName6,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary,&ex))
{
TCHAR szError[1024];
ex.GetErrorMessage(szError, 1024);
::AfxMessageBox(szError);
//AfxMessageBox("创建文件"+fileName2+"失败!");
}
else
{
log.SeekToEnd();
log.Write(fileName6+"\r\n\r\n",fileName6.GetLength()+4);
log.SeekToBegin();
}
*/
log.Close();
m_Agent=new char;
strcpy(m_Agent,m_AgentIP);
Start(); //开始监测
//SetTimer(1,200,NULL);
SetTimer(2,10,NULL); //每50微秒钟采集一次数据
// SetTimer(3,600000,NULL); //每分钟采集一次数据
SetTimer(4,7200000,NULL); //每次采集2小时
}
}
}
m_BtnEnd.EnableWindow();
//CDialog::OnOK();
}
void CSnmpExprimentDlg::OnCancel() //按钮“取消”的响应
{
// TODO: Add extra cleanup here
//OnEnd();
CDialog::OnCancel();
}
void CSnmpExprimentDlg::OnEnd() //按钮“结束”的响应
{
// TODO: Add your control notification handler code here
pSnmp.CloseSnmp();//关闭SNMP服务
// KillTimer(1);
KillTimer(2);//关闭记时器2
// KillTimer(3);
KillTimer(4);//关闭记时器4
m_sFileIn.Close();
m_sFileOut.Close();
// m_mFileIn.Close();
// m_mFileOut.Close();
m_BtnEnd.EnableWindow(FALSE);//让按钮“结束”失效
// m_FileIn.Close();
// m_FileOut.Close();
}
void CSnmpExprimentDlg::Start()//开始监测
{
if(pSnmp.sessionID==FALSE)
{
pSnmp.CreateSession(CSnmpExprimentDlg::m_hWnd,wMsg);
pSnmp.sessionID=TRUE;
}
//pSnmp.Register();
pSnmp.CreateVbl(m_initOid[1],NULL);
for(int i=2;i<=6;i++)
{
pSnmp.SetVbl(m_initOid[i]);
}
pSnmp.CreatePdu(SNMP_PDU_GET,NULL,NULL,NULL);
pSnmp.Send(m_Agent,"public");
}
void CSnmpExprimentDlg::OnRecv()
{
CTime time=CTime::GetCurrentTime();
CString strTime=time.Format("%Y.%m.%d %H:%M:%S");
int nIpin;
CString strIp;
CString strTemp;
pSnmp.Receive(m_sOid,m_value);
for(int i=1;i<=pSnmp.nCount;i++)
{
switch(m_value[i]->syntax)
{
case SNMP_SYNTAX_INT:
//case SNMP_SYNTAX_INT32:
smiINT sNumber;
sNumber=m_value[i]->value.sNumber;
nIpin=sNumber;
wsprintf(str[i],"%d",sNumber);
break;
case SNMP_SYNTAX_UINT32:
case SNMP_SYNTAX_CNTR32:
case SNMP_SYNTAX_GAUGE32:
case SNMP_SYNTAX_TIMETICKS:
smiUINT32 uNumber;
uNumber=m_value[i]->value.uNumber;
nIpin=uNumber;
wsprintf(str[i],"%d",uNumber);
break;
case SNMP_SYNTAX_CNTR64:
smiCNTR64 hNumber;
hNumber=m_value[i]->value.hNumber;
break;
case SNMP_SYNTAX_OCTETS:
case SNMP_SYNTAX_OPAQUE:
case SNMP_SYNTAX_NSAPADDR:
str[i]=(char *)m_value[i]->value.string.ptr;
break;
case SNMP_SYNTAX_IPADDR:
strIp.Format("%d",*m_value[i]->value.string.ptr);
strIp+=".";
strTemp.Format("%d",*(m_value[i]->value.string.ptr+1));
strIp+=strTemp;
strIp+=".";
strTemp.Format("%d",*(m_value[i]->value.string.ptr+2));
strIp+=strTemp;
strIp+=".";
strTemp.Format("%d",*(m_value[i]->value.string.ptr+3));
strIp+=strTemp;
strcpy(str[i],strIp);
break;
case SNMP_SYNTAX_OID:
smiOID oid;
oid=m_value[i]->value.oid;
SnmpOidToStr(&oid,30,str[i]);
//SnmpFreeDescriptor (SNMP_SYNTAX_OID, (smiLPOPAQUE)&oid);
break;
case SNMP_SYNTAX_NULL:
case SNMP_SYNTAX_NOSUCHOBJECT:
case SNMP_SYNTAX_NOSUCHINSTANCE:
case SNMP_SYNTAX_ENDOFMIBVIEW:
smiBYTE empty;
empty=m_value[i]->value.empty;
str[i]="No Value Returned";
break;
default:
break;
}
if(strcmp(m_sOid[i],m_initOid[1])==0) //网络设备的系统描述对象实例标识符
{
//m_sDesr=CString(str[i]);
}
else if (strcmp(m_sOid[i],m_initOid[2])==0) //找出Router的ID以及WAN Interface的Object ID
{
//m_sSysOid=CString(str[i]);
}
else if (strcmp(m_sOid[i],m_initOid[3])==0)
{
//m_sSysTime=CString(str[i]);
}
else if (strcmp(m_sOid[i],m_initOid[4])==0) //查询系统的名称
{
//m_sName=str[i];
}
else if (strcmp(m_sOid[i],m_initOid[5])==0) //累计从接口收到的IP输入数据报的总数,包括出错的数据报
{
if(!m_in)
{
m_OrigIn=str[i];
m_in=TRUE;
}
m_CurIn=str[i];
unsigned long oriIn=_ttol(m_OrigIn);
unsigned long curIn=_ttol(m_CurIn);
m_RealIn.Format("%d",curIn-oriIn);
/* CString strIn=strTime;
strIn+=" In = "+m_RealIn;
m_FileIn.SeekToEnd();
m_FileIn.Write(strIn+"\r\n",strIn.GetLength()+2);
m_FileIn.SeekToBegin();
*/
//m_nIpin=nIpin;
}
else if(strcmp(m_sOid[i],m_initOid[6])==0) //累计IP的上层协议(如TCP、UDP或ICMP)提供给IP传送的全部数据报个数
{
if(!m_out)
{
m_OrigOut=str[i];
m_out=TRUE;
}
m_CurOut=str[i];
unsigned long oriOut=_ttol(m_OrigOut);
unsigned long curOut=_ttol(m_CurOut);
m_RealOut.Format("%d",curOut-oriOut);
/*
CString strOut=strTime;
strOut+=" Out = "+m_RealOut;
m_FileOut.SeekToEnd();
m_FileOut.Write(strOut+"\r\n",strOut.GetLength()+2);
m_FileOut.SeekToBegin();
*/
//m_sIpout=str[i];
}
}
// UpdateData(FALSE);
if(m_bNext==TRUE)
Next(m_sOid[1]);
}
void CSnmpExprimentDlg::Next(LPTSTR Oid)
{
CString str(Oid);
static i=0;
//AfxMessageBox(str.Left(20));
if(!strcmp(str.Left(lstrlen(m_OidNext1)),m_OidNext1))
{
m_OidNext[i]=new char[50];
m_valueNext[i]=new char[50];
strcpy(m_OidNext[i],m_sOid[1]);
strcpy(m_valueNext[i],CSnmpExprimentDlg::str[1]);
//*(m_valueNext+i)=CSnmpManagerDlg::str[1];
pSnmp.CreateVbl(Oid,NULL);
pSnmp.CreatePdu(SNMP_PDU_GETNEXT,NULL,NULL,NULL);
pSnmp.Send(m_IpNext,"public");
i++;
}
else
{
m_bNext=FALSE;
}
}
void CSnmpExprimentDlg::OnTimer(UINT nIDEvent) //计时时间片处理
{
// TODO: Add your message handler code here and/or call default
CTime curTime=CTime::GetCurrentTime();
CString strTime=curTime.Format("%Y.%m.%d %H:%M:%S");
/* CString sIn=strTime;
CString sOut=strTime;
CString mIn=strTime;
CString mOut=strTime;
sIn+=" In = "+m_RealIn;
sOut+=" Out = "+m_RealOut;
mIn+=" In = "+m_RealIn;
mOut+=" Out = "+m_RealOut;
*/
CString sIn=m_RealIn;
CString sOut=m_RealOut;
// CString mIn=m_RealIn;
// CString mOut=m_RealOut;
switch(nIDEvent) {
/* case 1:
m_wFileIn.SeekToEnd();
m_wFileIn.Write(sIn+"\r\n",sIn.GetLength()+2);
m_wFileIn.SeekToBegin();
m_wFileOut.SeekToEnd();
m_wFileOut.Write(sOut+"\r\n",sOut.GetLength()+2);
m_wFileOut.SeekToBegin();
Start();
break;
*/
case 2:
m_sFileIn.SeekToEnd();
m_sFileIn.Write(sIn+"\r\n",sIn.GetLength()+2);
m_sFileIn.SeekToBegin();
m_sFileOut.SeekToEnd();
m_sFileOut.Write(sOut+"\r\n",sOut.GetLength()+2);
m_sFileOut.SeekToBegin();
UpdateData(FALSE);
Start();
break;
/* case 3:
m_mFileIn.SeekToEnd();
m_mFileIn.Write(sIn+"\r\n",sIn.GetLength()+2);
m_mFileIn.SeekToBegin();
m_mFileOut.SeekToEnd();
m_mFileOut.Write(sOut+"\r\n",sOut.GetLength()+2);
m_mFileOut.SeekToBegin();
break;*/
case 4:
/*pSnmp.CloseSnmp();
KillTimer(1);
KillTimer(2);
KillTimer(3);*/
OnEnd();
CDialog::OnCancel();
break;
default:
break;
}
CDialog::OnTimer(nIDEvent);
}
/*UINT _StartThread(LPVOID param)
{
CSnmpExprimentDlg* pDlg=(CSnmpExprimentDlg*)param;
while(TRUE)
{
pDlg->Start();
Sleep(300);
}
}*/