cyeh%netscape.com 0f787fde0e added blat url to usage output
git-svn-id: svn://10.0.0.236/trunk@22215 18797224-902f-48f8-a5cc-f745e15eee43
1999-02-26 20:15:03 +00:00

333 lines
10 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
/***************************************************************************************
* MODULE NOTES -- MTBF -- Mean Time Between Failures
* @update gess 01/04/99
*
* MTBF is a simple WinNT application that runs our viewer.exe remotely.
* After viewer crashes (inevitably) the total elapsed time is recorded, and send
* in an HTML mail message to the appropriate recipient. By default, the message
* is sent to rickg, but you can cause it to be sent to anywhere you like.
*
* Note: To compile this program using the existing project file, simply create a
* subst drive on your system using s:\ as the root. Place this project
* there as s:\mtbf. Of course, if this doesn't work for you, it's easy
* to change since there's only one file in the whole project anyway.
*
* Note: We depend on the availability of the blat.exe somewhere in your path.
* You can get your own copy of BLAT at http://gepasi.dbs.aber.ac.uk/softw/Blat.html.
*
***************************************************************************************/
#include <iostream.h>
#include <fstream.h>
#include <process.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <direct.h>
const char* gStatus[]={"abnormally","normally"};
bool gSendMail=false;
const char* gDefDir="s:";
char gMTBFDirectory[500]="";
char gDirectory[10];
const char* gDefProgram="/mozilla/dist/win32_d.obj/bin/viewer.exe";
char gProgram[500];
char gSubject[]="\"Automated MTBF Report\"";
char gRecipient[500]="rgess@san.rr.com";
char gReportFilename[20]="report.html";
char gReportPath[500]="";
const char* gURLFilename="urls.txt";
char gURLPath[500];
void ExitWithError(const char* aMsg1,const char* aMsg2){
cout << endl << "Error: " << aMsg1;
if(aMsg2)
cout<< " " << aMsg2;
cout <<"."<< endl;
cout << "Process terminated." << endl;
exit(2000);
}
//-d 10 -f c:/apps/utils/urls.txt -m
/**
*
* @update gess 01/04/99
* @param
* @return
*/
void ComputeEXEDirectory(char* aProgramPath,char* anOutputFilename){
char* thePtr=strrchr(aProgramPath,'\\');
if(thePtr) {
strcpy(anOutputFilename,aProgramPath);
anOutputFilename[thePtr-aProgramPath+1]=0;
}
thePtr=strchr(anOutputFilename,'\\');
while(thePtr) {
*thePtr='/';
thePtr=strchr(anOutputFilename,'\\');
}
}
/**
*
* @update gess 01/04/99
* @param
* @return
*/
void initializeSettings(int argc,char* argv[]){
bool theURLFileIsValid=false;
ComputeEXEDirectory(argv[0],gMTBFDirectory);
sprintf(gReportPath,"%s%s",gMTBFDirectory,gReportFilename);
sprintf(gURLPath,"%s%s",gMTBFDirectory,gURLFilename);
for(int i=0;i<argc;i++){
if(0==strnicmp(argv[i],"-f",2)){
//determine which url file to use...
fstream in(gURLPath,ios::in);
char buf[500];
buf[0]=0;
in>>buf;
if(!buf[0]){
ExitWithError("Unable to read the given URL file",gURLPath);
}
else theURLFileIsValid=true;
}
else if(0==strnicmp(argv[i],"-r",2)){
//determine email recipient...
strcpy(gRecipient,argv[i+1]);
gSendMail=true;
}
}
if(!theURLFileIsValid)
ExitWithError("You must provide a valid URL textfile",0);
}
/**
*
* @update gess 01/04/99
* @param
* @return
*/
void PrintResultsAsHTML(ostream& aStream,time_t& aStart,time_t& aEnd,int aResult){
time_t ltime;
time( &ltime );
//begin by dumping the MIME header...
/*
aStream << "MIME-Version: 1.0" << endl;
aStream << "Content-Type: text/html; charset=us-ascii" << endl;
aStream << "From: rickg@netscape.com" << endl;
aStream << "Subject: Automated MTBF Report" << endl;
aStream << "To: rickg@netscape.com rgess@san.rr.com" << endl;
// aStream << "To: rick@gessner.com rickg@netscape.com mozilla-layout@mozilla.org" << endl;
*/
//now dump the document in html format...
int total=aEnd-aStart;
int mins=(aEnd-aStart)/60;
char buffer[50];
sprintf(buffer,"%i mins %i sec",mins,total%60);
aStream << "<!doctype html public \"-//w3c//dtd html 4.0 transitional//en\">" << endl;
aStream << "<html>"<<endl;
aStream << " <body>"<<endl;
aStream << " <font face=\"Verdana\" size=2>"<<endl;
aStream << " <table border=0 cellspacing=1 cellpadding=1>"<<endl;
aStream << " <tbody>"<<endl;
aStream << " <tr bgcolor=ffcccc>"<<endl;
aStream << " <td></td><td align=center><font face=\"Verdana\" size=2><b>Automated MTBF Report</b></td>"<< endl;
aStream << " </tr>"<<endl;
aStream << " <tr>"<<endl;
aStream << " <td><font face=\"Verdana\" size=2>Date:</td>"<< endl;
aStream << " <td bgcolor=e8e8e8><font face=\"Verdana\" size=2>" << ctime(&ltime) << "</td>" << endl;
aStream << " </tr>"<<endl;
aStream << " <tr>"<<endl;
aStream << " <td><font face=\"Verdana\" size=2>Running:</td>"<< endl;
aStream << " <td bgcolor=cccccc><font face=\"Verdana\" size=2>" << gDirectory << gProgram << "</td>" << endl;
aStream << " </tr>"<<endl;
aStream << " <tr>"<<endl;
aStream << " <td><font face=\"Verdana\" size=2>Args:</td>"<< endl;
aStream << " <td bgcolor=e8e8e8><font face=\"Verdana\" size=2>-f " << gURLPath << "</td>" << endl;
aStream << " </tr>"<<endl;
aStream << " <tr>"<<endl;
aStream << " <td><font face=\"Verdana\" size=2>Elapsed:</td>"<< endl;
aStream << " <td bgcolor=cccccc><font face=\"Verdana\" size=2>"<< buffer << "</td>" << endl;
aStream << " </tr>"<<endl;
aStream << " <tr>"<<endl;
aStream << " <td><font face=\"Verdana\" size=2>Exit: </td>"<<endl;
aStream << " <td bgcolor=e8e8e8><font face=\"Verdana\" size=2>"<< gStatus[0==aResult] << endl;
aStream << " </tr>"<<endl;
aStream << " </tbody>"<<endl;
aStream << " </table>" << endl;
aStream << " </body>"<<endl;
aStream << "</html>"<<endl;
//now dump my signature file...
aStream << "<hr><font size=1>Last update: Rick Gessner<br>" << endl;
aStream << "Date: 01.04.99<br>" << endl;
//now close the document...
aStream << "</body></html>" << endl;
}
/**
*
* @update gess 01/04/99
* @param
* @return
*/
void PrintResults(ostream& aStream,time_t& aStart,time_t& aEnd,int aResult){
time_t ltime;
time( &ltime );
aStream << "=========================================" << endl;
aStream << "Gecko \"Mean Time Between Failure\" Report" << endl;
aStream << "=========================================" << endl;
aStream << "Date: " << ctime( &ltime);
aStream << "Running: " << gDirectory << gProgram << endl;
aStream << "File: " << gURLPath << endl;
int total=aEnd-aStart;
int mins=(aEnd-aStart)/60;
aStream << "MTBF: ";
if(mins>0){
aStream << mins << " min ";
}
const char* status[]={"abnormally","normally"};
aStream << total%60 << " seconds" << endl;
aStream << "Exit: Program terminated " << status[0==aResult] << endl;
}
/**
*
* @update gess 01/04/99
* @param
* @return
*/
bool getPath(char* aBuffer,const char* aFilename,int index){
bool result=false;
if(aFilename){
int len=strlen(aFilename);
if(len>0){
int hit=0;
int pos=len;
while((hit<index) && (--pos>-1)){
if((aFilename[pos]=='\\') || (aFilename[pos]=='/')){
hit++;
}//if
}//while
if(hit==index){
strncpy(aBuffer,aFilename,pos);
aBuffer[pos]=0;
strcat(aBuffer,"\\cache");
result=true;
}
}//if
}//if
return result;
}
/**
*
* @update gess 01/04/99
* @param
* @return
*/
int main(int argc,char* argv[]) {
int result=0;
//result=system("sendmail -t -messagefile=s:/mtbf/msg.txt ");
if(argc>1) {
char* theDir = getenv("moz_src");
if(!theDir)
theDir=getenv("homedrive");
if(theDir)
strcpy(gDirectory,theDir);
else strcpy(gDirectory,gDefDir);
strcpy(gProgram,gDefProgram);
initializeSettings(argc,argv);
// example for CTime::CTime
time_t startTime; // C run-time time (defined in <time.h>)
time( &startTime ) ; // Get the current time from the
char buffer[500];
sprintf(buffer,"%s%s -f %s",gDirectory,gProgram,gURLPath);
result=system(buffer);
time_t endTime; // C run-time time (defined in <time.h>)
time( &endTime ) ; // Get the current time from system
if(gSendMail) {
{
//Write the message out in html form...
fstream out(gReportPath,ios::out);
PrintResultsAsHTML(out,startTime,endTime,result);
}
sprintf(buffer,"blat %s -s %s -t %s -uuencode",gReportPath,gSubject,gRecipient);
result=system(buffer);
}
else {
cout << endl << endl;
PrintResults(cout,startTime,endTime,result);
}
int err=errno;
switch(errno){
case E2BIG:
cout << "too big" << endl; break;
case EACCES:
cout << "access" << endl; break;
case EMFILE:
cout << "file" << endl; break;
case ENOENT:
cout << "bad file or directory" << endl; break;
case ENOEXEC:
cout << "exec" << endl; break;
case ENOMEM:
cout << "no mem" << endl; break;
default:
break;
}
}
else {
printf("MTBF (Mean time between failures) version 1.1.\n");
printf("This program requires BLAT.EXE, which you can find on the net.\n");
printf("http://gepasi.dbs.aber.ac.uk/softw/Blat.html \n");
printf("Optional arguments: \n");
printf(" -f filename containing URL list\n");
printf(" -r email recipient\n");
}
return 0;
}