#include "listen.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
fd_set fdSet,testFdSet;
fd_set zeroSet;
int HTTPPacketNum = 0;
int HTTPTotalStat = 0;
int DNSPacketNum = 0;
int DNSTotalStat = 0;
int POP3PacketNum = 0;
int POP3TotalStat = 0;
int SMTPPacketNum = 0;
int SMTPTotalStat = 0;
int buildSocket(char *s,int *sockFd,int port)
{
int servFd;
int servLen;
struct sockaddr_in servAddr;
int res;
servFd = socket(AF_INET,SOCK_STREAM,0);
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
servAddr.sin_port = htons(port);
servLen = sizeof(servAddr);
res = bind(servFd,(struct sockaddr*)&servAddr,servLen);
if (res == -1)
{
char tmp[64];
sprintf(tmp,"[FAILURE] Build %s monitor socket failure\n",s);
display(textBuff,tmp,LogFd);
return -1;
}
res = listen(servFd,5);
if (res == -1)
{
char tmp[64];
sprintf(tmp,"[FAILURE] Build %s monitor socket failure\n",s);
display(textBuff,tmp,LogFd);
return -1;
}
*sockFd = servFd;
char tmp[64];
sprintf(tmp,"[OK] Build %s monitor socket success\n",s);
display(textBuff,tmp,LogFd);
return 0;
}
void analysisPacket(pMHDR pPacket)
{
if (strncmp(pPacket->protocol,"HTTP",4) == 0)
{
HTTPPacketNum++;
HTTPTotalStat += pPacket->stat;
return ;
}
if (strncmp(pPacket->protocol,"DNS",3) == 0)
{
DNSPacketNum++;
DNSTotalStat += pPacket->stat;
return;
}
if (strncmp(pPacket->protocol,"SMTP",4) == 0)
{
SMTPPacketNum++;
SMTPTotalStat += pPacket->stat;
return;
}
if (strncmp(pPacket->protocol,"POP3",4) == 0)
{
POP3PacketNum++;
POP3TotalStat += pPacket->stat;
return ;
}
char tmp[64] = "[ERROR] Can not analysis the packet\n";
display(textBuff,tmp,LogFd);
}
void handleSelect()
{
int fd;
struct sockaddr_in clientAddr;
int clientSock;
int clientLen;
for (fd = 0;fd < FD_SETSIZE;fd++)
{
if(FD_ISSET(fd,&testFdSet))
{
if (fd == HTTPServFd || fd == DNSServFd || fd == SMTPServFd || fd == POP3ServFd)
{
clientLen = sizeof(clientAddr);
clientSock = accept(fd,(struct sockaddr*)&clientAddr,&clientLen);
if (clientSock != -1)
{
FD_SET(clientSock,&fdSet);
char tmp[64] = "[OK] A new sensor connect the console\n";
display(textBuff,tmp,LogFd);
}
}
else
{
int nread;
ioctl(fd,FIONREAD,&nread);
if (nread == 0)
{
close(fd);
FD_CLR(fd,&fdSet);
char tmp[64] = "[OK] A sensor leave\n";
display(textBuff,tmp,LogFd);
}
else
{
pMHDR pPacket = (pMHDR)malloc(sizeof(MHDR));
int res = read(fd,pPacket,sizeof(MHDR));
if (res == -1)
return;
analysisPacket(pPacket); /*analysis the data packet*/
}
}
}
}
}
void analysis(char *s,int packetNum,int totalStat)
{
/*************************************************
*
* stat : 0 1 2
* avgStat 0~2
* define avgStat of 0~0.5 is server down
* 0.5~1.5 is server not smooth
* 1.5~2.0 is server run well
*
* ***********************************************/
float avgStat;
if (packetNum == 0)
return ;
avgStat = (float)totalStat/packetNum;
char tmp[64];
if (avgStat < 0.5)
{
sprintf(tmp,"[FAILURE] %s protocol is down\n",s);
display(textBuff,tmp,LogFd);
}
if (avgStat >= 0.5 && avgStat <= 1.5)
{
sprintf(tmp,"[WARNING] %s protocol is not smooth\n",s);
display(textBuff,tmp,LogFd);
}
if (avgStat > 1.5)
{
sprintf(tmp,"[OK] %s protocol is running well\n",s);
display(textBuff,tmp,LogFd);
}
}
void monitor()
{
struct timeval timeout;
time_t sTime,eTime;
int res;
HTTPPacketNum = 0;
HTTPTotalStat = 0;
DNSPacketNum = 0;
DNSTotalStat = 0;
POP3PacketNum = 0;
POP3TotalStat = 0;
SMTPPacketNum = 0;
SMTPTotalStat = 0;
fdSet = zeroSet;
FD_ZERO(&fdSet);
if (HTTPrun)
{
res = buildSocket("HTTP",&HTTPServFd,CHTTP_PORT);
if (res == 0)
FD_SET(HTTPServFd,&fdSet);
}
if (DNSrun)
{
res = buildSocket("DNS ",&DNSServFd,CDNS_PORT);
if (res == 0)
FD_SET(DNSServFd,&fdSet);
}
if (SMTPrun)
{
res = buildSocket("SMTP",&SMTPServFd,CSMTP_PORT);
if (res == 0)
FD_SET(SMTPServFd,&fdSet);
}
if (POP3run)
{
res = buildSocket("POP3",&POP3ServFd,CPOP3_PORT);
if (res == 0)
FD_SET(POP3ServFd,&fdSet);
}
while (running)
{
sTime = time((time_t *)0);
/*handle*/
while (running)
{
testFdSet = fdSet;
timeout.tv_sec = SELECT_TIME_SEC;
timeout.tv_usec = SELECT_TIME_USEC;
res = select(FD_SETSIZE,&testFdSet,(fd_set*)NULL,(fd_set*)NULL,&timeout);
switch (res)
{
case 0:
// printf("time out\n");
break;
case -1:
printf("select\n");
break;
default:
handleSelect();
}
eTime = time((time_t *)0);
double diff;
diff = difftime(eTime,sTime);
if (diff >= SHOW_TIME_VAL)
break;
}
/*TODO*/
if (HTTPrun)
{
analysis("HTTP",HTTPPacketNum,HTTPTotalStat);
HTTPPacketNum = HTTPTotalStat =0;
}
if (DNSrun)
{
analysis("DNS ",DNSPacketNum,DNSTotalStat);
DNSPacketNum = DNSTotalStat = 0;
}
if (SMTPrun)
{
analysis("SMTP",SMTPPacketNum,SMTPTotalStat);
SMTPPacketNum = SMTPTotalStat = 0;
}
if (POP3run)
{
analysis("POP3",POP3PacketNum,POP3TotalStat);
POP3PacketNum = POP3TotalStat = 0;
}
}
}
评论0