180 lines
5.2 KiB
C++
180 lines
5.2 KiB
C++
#include <access/access.hpp>
|
|
#include <access/arecord.hpp>
|
|
#include <common/openfile.hpp>
|
|
#include <common/systime.hpp>
|
|
#include <thread/sleep.hpp>
|
|
|
|
AccessManager::AccessManager(void)
|
|
: mStrPathAccessLog("/var/log/httpd/access_log"), mStrPathMessageLog("var/log/messages")
|
|
{
|
|
}
|
|
|
|
AccessManager::AccessManager(const AccessManager &someAccessManager)
|
|
{ // private implementation
|
|
}
|
|
|
|
AccessManager::~AccessManager()
|
|
{
|
|
}
|
|
|
|
AccessManager &AccessManager::operator=(const AccessManager &someAccessManager)
|
|
{ // private implementation
|
|
}
|
|
|
|
DWORD AccessManager::getAccessLineCount(void)
|
|
{
|
|
String strLine;
|
|
DWORD lineCount(0);
|
|
|
|
File accessFile(mStrPathAccessLog);
|
|
if(!accessFile.isOkay())return FALSE;
|
|
while(accessFile.getLine(strLine))lineCount++;
|
|
return lineCount;
|
|
}
|
|
|
|
void AccessManager::accessDelta(DWORD prevLines,DWORD currLines)
|
|
{
|
|
Block<String> accessLines;
|
|
String strLine;
|
|
File accessLog;
|
|
|
|
accessLog.open(mStrPathAccessLog);
|
|
if(!accessLog.isOkay())return;
|
|
while(accessLog.getLine(strLine))accessLines.insert(strLine);
|
|
accessLog.close();
|
|
for(int indexLine=0;indexLine<accessLines.size();indexLine++)
|
|
{
|
|
if(indexLine<prevLines)continue;
|
|
String &currLine=accessLines[indexLine];
|
|
if(!currLine.strstr("GET"))continue;
|
|
AccessRecord accessRecord;
|
|
accessRecord.ip(currLine.betweenString(0,' '));
|
|
accessRecord.date(SDate().dayMonthYearStringToDate(currLine.betweenString('[',':')));
|
|
accessRecord.time(currLine.betweenString('[',' ').betweenString(':',0));
|
|
getHost(accessRecord);
|
|
mAccessFile.insert(accessRecord);
|
|
}
|
|
if(mAccessFile.isDirty())
|
|
{
|
|
mAccessFile.flush();
|
|
accessPage();
|
|
}
|
|
}
|
|
|
|
void AccessManager::accessPage(void)
|
|
{
|
|
Block<AccessRecord> accessRecords;
|
|
SystemTime systemTime;
|
|
String strPostfix;
|
|
String strPrefix;
|
|
String strLine;
|
|
File outFile;
|
|
SDate currDate;
|
|
|
|
mAccessFile.getEntries(accessRecords);
|
|
createCounter(accessRecords.size());
|
|
strPrefix="<FONT SIZE=\"+1\" COLOR=\"Lime\"><B><I>";
|
|
strPostfix="</I></B></FONT><BR>";
|
|
outFile.open("/home/httpd/html/access/access.html","wb");
|
|
if(!outFile.isOkay())return;
|
|
outFile.writeLine("<HTML>");
|
|
outFile.writeLine("<HEAD>");
|
|
outFile.writeLine("<TITLE></TITLE>");
|
|
outFile.writeLine("</HEAD>");
|
|
outFile.writeLine("<BODY BACKGROUND=\"setup.gif\" TEXT=\"#000000\" LINK=\"#00AA00\" VLINK=\"#808080\" ALINK=\"ff00ff\">");
|
|
outFile.writeLine("<P ALIGN=\"CENTER\"><FONT COLOR=\"Lime\" SIZE=\"+3\"><B><I>Ganymede System Monitor</I></B></FONT></P>");
|
|
outFile.writeLine("<P> </P>");
|
|
outFile.writeLine(String("<P><B><I><FONT COLOR=\"Lime\" SIZE=\"+3\">HTTP Access Log, ")+(String)systemTime+String("</FONT><FONT COLOR=\"Lime\"><BR>"));
|
|
outFile.writeLine("</FONT></I></B>");
|
|
for(int index=0;index<accessRecords.size();index++)
|
|
{
|
|
AccessRecord &accessRecord=accessRecords[index];
|
|
if(!index)currDate=accessRecord.date();
|
|
else if(!(currDate==accessRecord.date())){outFile.writeLine("<BR>");currDate=accessRecord.date();}
|
|
outFile.writeLine(strPrefix+accessRecord.ip()+String(" (")+accessRecord.host()+String(") on ")+accessRecord.date().makeString()+String(" at ")+accessRecord.time()+strPostfix);
|
|
}
|
|
outFile.writeLine("</P>");
|
|
outFile.writeLine("</BODY>");
|
|
outFile.writeLine("</HTML>");
|
|
}
|
|
|
|
void AccessManager::getHost(AccessRecord &accessRecord)
|
|
{
|
|
String strPathResultFile("host.rst");
|
|
String strSystem("/usr/bin/host ");
|
|
String strHostNotFoundTryAgain("Host not found, try again.");
|
|
String strHostNotFound("Host not found.");
|
|
String strLine;
|
|
char *ptrLine;
|
|
File inFile;
|
|
|
|
strSystem+=accessRecord.ip();
|
|
strSystem+=String(" &>")+strPathResultFile;
|
|
::unlink(strPathResultFile);
|
|
::system(strSystem);
|
|
inFile.open(strPathResultFile);
|
|
if(!inFile.isOkay()){accessRecord.host(strHostNotFound);return;}
|
|
inFile.getLine(strLine);
|
|
if(strLine.isNull()||strLine==strHostNotFound||strLine==strHostNotFound){accessRecord.host(strHostNotFound);return;}
|
|
ptrLine=(char*)strLine+(strLine.length()-1);
|
|
while(ptrLine>(char*)strLine&&*ptrLine!=' ')ptrLine--;
|
|
if(' '==*ptrLine)ptrLine++;
|
|
accessRecord.host(ptrLine);
|
|
}
|
|
|
|
void AccessManager::createCounter(int accessCount)
|
|
{
|
|
File outFile;
|
|
String strCount;
|
|
|
|
::sprintf(strCount,"%06d",accessCount);
|
|
outFile.open("/home/httpd/html/log/access.cnt","wb");
|
|
if(!outFile.isOkay())return;
|
|
outFile.writeLine(strCount);
|
|
outFile.close();
|
|
}
|
|
|
|
void AccessManager::perform(void)
|
|
{
|
|
DWORD accessLines(getAccessLineCount());
|
|
DWORD newAccessLines;
|
|
SDate currDate(SDate::TodaysDate);
|
|
SDate nextDate;
|
|
Sleep sleeper;
|
|
BOOL isInit(TRUE);
|
|
|
|
while(TRUE)
|
|
{
|
|
::printf("AccessManager is sleeping\n");
|
|
if(!isInit)sleeper.sleep(TimeOut);
|
|
isInit=FALSE;
|
|
::printf("AccessManager is awake\n");
|
|
nextDate=SDate(SDate::TodaysDate);
|
|
if(DayCount<=nextDate.daysBetweenActual(currDate,nextDate))
|
|
{
|
|
currDate=nextDate;
|
|
mAccessFile.reset();
|
|
}
|
|
newAccessLines=getAccessLineCount();
|
|
if(newAccessLines<accessLines)accessLines=newAccessLines;
|
|
if(newAccessLines==accessLines)continue;
|
|
if(newAccessLines!=accessLines)
|
|
{
|
|
::printf("AccessManager is processing the change log...\n");
|
|
accessDelta(accessLines,newAccessLines);
|
|
accessLines=newAccessLines;
|
|
::printf("AccessManager is finished processing the change log.\n");
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|