home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / VP2_409E.SZH / STATS.C < prev    next >
C/C++ Source or Header  |  1991-06-27  |  14KB  |  525 lines

  1.  
  2. /*
  3.  
  4.                           The Conference Mail System
  5.  
  6.                                                Statistics and Logging Module
  7.  
  8.                           This module was originally written by Graham Stair
  9.                                              Sysop of FidoNet node 3:711/409
  10.                                                           26th February 1991
  11.  
  12. */
  13.  
  14.  
  15. #define  RELEASE "1a"
  16.  
  17. #include <stdio.h>
  18. #include <stdarg.h>
  19. #include <stdlib.h>
  20. #include <time.h>
  21. #include <string.h>
  22. #include <malloc.h>
  23. #include <sys\types.h>
  24. #include <sys\stat.h>
  25. #include "types.h"
  26.  
  27. #if OS2
  28.    #define INCL_DOSFILEMGR
  29.    #include <os2.h>
  30.  
  31.    #define FSINFOBUF       FSInfoBuf
  32.    #define AVAILCLUSTERS   cUnitAvail
  33.    #define SECTPERCLUSTER  cSectorUnit
  34.    #define BYTESPERSECTOR  cbSector
  35. #else
  36.    #include <dos.h>
  37.  
  38.    #define FSINFOBUF       DiskInfo
  39.    #define AVAILCLUSTERS   avail_clusters
  40.    #define SECTPERCLUSTER  sectors_per_cluster
  41.    #define BYTESPERSECTOR  bytes_per_sector
  42. #endif
  43.  
  44. /* -- struct's and tyepdef's --------------------------------------------- */
  45. struct _packet_header
  46.    {
  47.    int   OrigNode;
  48.    int   DestNode;
  49.    int   CreateYear;
  50.    int   CreateMonth;
  51.    int   CreateDay;
  52.    int   CreateHour;
  53.    int   CreateMinute;
  54.    int   CreateSecond;
  55.    int   Baud;
  56.    int   PacketType;
  57.    int   OrigNet;
  58.    int   DestNet;
  59.    char  ProductCode;
  60.    char  SerialNum;
  61.    char  Password[8];
  62.    int   OrigZone;
  63.    int   DestZone;
  64.    char  Filler[20];
  65.    };
  66.  
  67. /* -- Global Variables --------------------------------------------------- */
  68.        int   mail_msgs = 0;
  69.        long  mail_size = 0;
  70.        int   security  = 0;
  71. extern char *REV;
  72.  
  73. /* -- Local Prototypes --------------------------------------------------- */
  74. void LogDateStamp(char *DateStamp);
  75. char LogOpen(void);
  76. char LogClose(void);
  77.  
  78. /* -- Local Variables ---------------------------------------------------- */
  79. static           char     *LogEntry  = NULL;
  80. static    signed char     LogOpened  = -1;
  81. static           FILE     *LogHandle = NULL;
  82. static           char     *LogName   = NULL;
  83. static  unsigned int      TotMsgs    = 0;
  84. static  unsigned int      TotDupes   = 0;
  85. static  unsigned int      TotSec     = 0;
  86. static  unsigned int      TotSent    = 0;
  87. static           time_t   BeginTime;
  88. static           char     Marker[60];
  89. static           int      MarkerLen;
  90. static           char     *Message   = NULL;
  91. static           char     Type[9];
  92.  
  93. /* ----------------------------------------------------------------------- */
  94. void      LogIt(int status, char *message)
  95. {
  96. char      Date[16];
  97. int       MessageLen;
  98. char      StatusCharacter;
  99.  
  100.  
  101. switch (status)
  102.    {
  103.    case 1:
  104.       StatusCharacter = '!';        /* Error */
  105.       break;
  106.    case 2:
  107.       StatusCharacter = '#';
  108.       break;
  109.    case 3:
  110.       StatusCharacter = '*';
  111.       break;
  112.    case 4:
  113.       StatusCharacter = '+';
  114.       break;
  115.    default:
  116.       StatusCharacter = ' ';
  117.       break;
  118.    }
  119.  
  120. LogDateStamp(Date);
  121.  
  122. MessageLen = strlen(message);
  123. for ( ; MessageLen > 0; MessageLen--)
  124.     if (message[MessageLen] == '\n')
  125.        {
  126.        message[MessageLen] = ' ';
  127.        break;
  128.        }
  129.  
  130. #if OS2
  131.    sprintf(LogEntry, "%c %s VP2  %s\n", StatusCharacter, Date, message);
  132. #else
  133.    sprintf(LogEntry, "%c %s VP   %s\n", StatusCharacter, Date, message);
  134. #endif
  135.  
  136. fwrite(LogEntry, strlen(LogEntry), 1, LogHandle);
  137.  
  138. if (MessageLen > 0)
  139.    message[MessageLen] = '\n';
  140.  
  141. }
  142.  
  143. /* ----------------------------------------------------------------------- */
  144. char *LogMessage(int status, char *fmt, ...)
  145. {
  146. va_list arg;
  147.  
  148. if (Message == NULL)
  149.    if ((Message = (char *) malloc ( (size_t) (260) )) == NULL)
  150.       {
  151.       printf("Problem getting 260 bytes for Message\n");
  152.       exit(1);
  153.       }
  154.  
  155. va_start (arg, fmt);
  156. vsprintf (Message, fmt, arg);
  157. va_end (arg);
  158.  
  159. LogIt(status, Message);
  160.  
  161. return(Message);
  162. }
  163.  
  164. /* ----------------------------------------------------------------------- */
  165. void     LogDateStamp(char *DateStamp)
  166. {
  167. time_t            current_time_sec;
  168. struct      tm    *current_time_str;
  169.  
  170. char        months[12][4] = { "Jan",
  171.                               "Feb",
  172.                               "Mar",
  173.                               "Apr",
  174.                               "May",
  175.                               "Jun",
  176.                               "Jul",
  177.                               "Aug",
  178.                               "Sep",
  179.                               "Oct",
  180.                               "Nov",
  181.                               "Dec" };
  182.  
  183.  
  184. time(¤t_time_sec);
  185. current_time_str = localtime(¤t_time_sec);
  186.  
  187. sprintf(DateStamp, "%02d %3s %02d:%02d:%02d",
  188.               current_time_str->tm_mday,
  189.                    months[current_time_str->tm_mon],
  190.                        current_time_str->tm_hour,
  191.                            current_time_str->tm_min,
  192.                                  current_time_str->tm_sec);
  193.  
  194. }
  195.  
  196. /* ----------------------------------------------------------------------- */
  197. unsigned char LogOpen()
  198. {
  199. LogName = getenv("VP2.LOG");
  200. if (LogName == NULL)
  201.    {
  202.    LogName = getenv("VP.LOG");
  203.    if (LogName == NULL)
  204.       LogName = getenv("BBS.LOG");
  205.    }
  206.  
  207. if (LogName == NULL)
  208.    return(99);         /* Log NOT wanted */
  209.  
  210. if ((LogHandle = fopen(LogName, "a")) == NULL)
  211.    fprintf(stderr, "\aERROR, couldn't open log file '%s'\n\r\n\r", LogName);
  212.  
  213. if (Message == NULL)
  214.    if ((Message = (char *) malloc ( (size_t) (260) )) == NULL)
  215.       {
  216.       printf("Problem getting 260 bytes for Message\n");
  217.       exit(1);
  218.       }
  219.  
  220. if (LogEntry == NULL)
  221.    if ((LogEntry = (char *) malloc ( (size_t) (260) )) == NULL)
  222.       {
  223.       printf("Problem getting 260 bytes for LogEntry\n");
  224.       exit(1);
  225.       }
  226.  
  227.  
  228. return(1);
  229. }
  230.  
  231. /* ----------------------------------------------------------------------- */
  232. unsigned char LogClose()
  233. {
  234. return(-1);
  235. }
  236.  
  237. /* ----------------------------------------------------------------------- */
  238. void LogBegin(char *type)
  239. {
  240.          long  DiskSize = 0;
  241. unsigned int   Drive;
  242. unsigned int   i;
  243.          char  buff1[20], buff2[20];
  244.  
  245. #if OS2
  246.    long              DriveMap;
  247.    FSALLOCATE        FSINFOBUF;
  248. #else
  249.    struct diskfree_t FSINFOBUF;
  250. #endif
  251.  
  252.  
  253. if (LogOpened == -1)        /* Logging not installed yet */
  254.    LogOpened = LogOpen();
  255.  
  256. if (LogOpened == 99)        /* No logging wanted */
  257.    return;
  258.  
  259.  
  260. /* OK, Logging wanted, file found and opened. Let's tell the world */
  261. printf ("Modified for Logging and Statistics by Graham Stair (Release %s)\n", RELEASE);
  262. printf ("Logging active to file [%s]\n\n", LogName);
  263.  
  264. #if OS2
  265.    DosQCurDisk(&Drive, &DriveMap);
  266.    if (DosQFSInfo( Drive, 1, (PBYTE)&FSINFOBUF, sizeof(FSINFOBUF)) == 0)
  267. #else
  268.    _dos_getdrive(&Drive);
  269.    if (_dos_getdiskfree( Drive, &FSINFOBUF) == 0)
  270. #endif
  271.       DiskSize = ((long)FSINFOBUF.AVAILCLUSTERS
  272.                 * (long)FSINFOBUF.SECTPERCLUSTER
  273.                 * (long)FSINFOBUF.BYTESPERSECTOR/1024L);
  274.  
  275. time(&BeginTime);
  276.  
  277. sscanf (REV, "$%s %s", buff1, buff2);
  278.  
  279. for (i = 0; i < sizeof(Marker); i++)
  280.    Marker[i] = '*';
  281. Marker[sizeof(Marker)-1] = 0;
  282.  
  283. /* Save 'type' for LogExit's use */
  284. strcpy(Type, type);
  285.  
  286. sprintf(Message, "%s Begin disk[%c]=%lukb free; rel=%s(%s);",
  287.                          Type, Drive+64, DiskSize, buff2, RELEASE);
  288.  
  289. strncat(Message, Marker, (56 - strlen(Message)) );
  290.  
  291. LogIt(5, Message);
  292. }
  293.  
  294. /* ----------------------------------------------------------------------- */
  295. void LogExit(int rc)
  296. {
  297.          long     DiskSize = 0;
  298. unsigned int      Drive;
  299.          time_t   EndTime;
  300.          double   et;
  301.  
  302. #if OS2
  303.    long              DriveMap;
  304.    FSALLOCATE        FSINFOBUF;
  305. #else
  306.    struct diskfree_t FSINFOBUF;
  307. #endif
  308.  
  309.  
  310.  
  311. if (LogOpened == 99)        /* No logging wanted */
  312.    exit(rc);
  313.  
  314. #if OS2
  315.    DosQCurDisk(&Drive, &DriveMap);
  316.    if (DosQFSInfo( Drive, 1, (PBYTE)&FSINFOBUF, sizeof(FSINFOBUF)) == 0)
  317. #else
  318.    _dos_getdrive(&Drive);
  319.    if (_dos_getdiskfree( Drive, &FSINFOBUF) == 0)
  320. #endif
  321.       DiskSize = ((long)FSINFOBUF.AVAILCLUSTERS
  322.                 * (long)FSINFOBUF.SECTPERCLUSTER
  323.                 * (long)FSINFOBUF.BYTESPERSECTOR/1024L);
  324.  
  325. time(&EndTime);
  326. et = difftime(EndTime, BeginTime);
  327.  
  328. if ( (TotMsgs + TotDupes + TotSec) > 0)
  329.    {
  330.    sprintf(Message, "I-Summary; msgs=%u; dupes=%u; sec=%u; msgs/sec=%0.1f;",
  331.                       TotMsgs, TotDupes, TotSec,
  332.              (double) ((TotMsgs+TotDupes+TotSec)/(et < 1 ? (double) 1 : et)) );
  333.    LogIt(3, Message);
  334.    }
  335.  
  336. if ( TotSent > 0)
  337.    {
  338.    sprintf(Message, "E-Summary; msgs=%u; msgs/sec=%0.1f;",
  339.                    TotSent, (double) ((TotSent)/(et < 1 ? (double) 1 : et)) );
  340.    LogIt(3, Message);
  341.    }
  342.  
  343. sprintf(Message, "%s End disk[%c]=%lukb free; rc=%u; et=%0.f;",
  344.                   Type, Drive+64, DiskSize, rc, et);
  345. LogIt(5, Message);
  346.  
  347. LogOpened = LogClose();
  348.  
  349. exit(rc);
  350. }
  351.  
  352. /* ----------------------------------------------------------------------- */
  353. void LogArchive(char *fn, int net, int node)
  354. {
  355. struct   stat  FnStats;
  356. struct   tm    *date;
  357. unsigned int   i;
  358.  
  359. if (LogOpened == 99)        /* No logging wanted */
  360.    return;
  361.  
  362. for (i = 0; i < sizeof(Marker); i++)
  363.    Marker[i] = '=';
  364. Marker[sizeof(Marker)-1] = 0;
  365.  
  366. sprintf(Message, "Archive=[%s] ", strlwr(fn));
  367. strncat(Message, Marker, (56 - strlen(Message)) );
  368.  
  369. LogIt(4, Message);
  370.  
  371. stat(fn, &FnStats);
  372. date = localtime(&FnStats.st_mtime);
  373.  
  374. sprintf(Message, "Archive date=%u/%02u/%02u %02u:%02u:%02u; size=%lub;",
  375.                   date->tm_year, date->tm_mon+1, date->tm_mday,
  376.                       date->tm_hour, date->tm_min, date->tm_sec,
  377.                        FnStats.st_size);
  378. LogIt(4, Message);
  379.  
  380. sprintf(Message, "From=%d/%d;", net, node);
  381. LogIt(4, Message);
  382. }
  383.  
  384.  
  385. /* ----------------------------------------------------------------------- */
  386. void LogAreas(AREAS_PTR areas[], int tot_areas)
  387. {
  388. int i;
  389.  
  390. if (LogOpened == 99)        /* No logging wanted */
  391.    return;
  392.  
  393. if (security != 0)
  394.    {
  395.    sprintf(Message, "Area=$Security$; added=%u;", security);
  396.    LogIt(1, Message);
  397.  
  398.    TotSec += security;
  399.  
  400.    security = 0;
  401.    }
  402.  
  403. if (mail_msgs != 0)
  404.    {
  405.    sprintf(Message, "Area=$NetMail$; added=%u; size=%lub;", mail_msgs, mail_size);
  406.    LogIt(4, Message);
  407.  
  408.    TotMsgs += mail_msgs;
  409.  
  410.    mail_msgs = 0;
  411.    mail_size = 0;
  412.    }
  413.  
  414. for (i = 0; i < tot_areas; i++)
  415.    if ((areas[i]->msgs_proc   != 0)
  416.    ||  (areas[i]->msgs_sec    != 0)
  417.    ||  (areas[i]->msg_dupes   != 0) )
  418.       {
  419.       sprintf(Message, "Area=%s; added=%u; size=%lub; dupes=%u; sec=%u;",
  420.                          areas[i]->area_name,
  421.                           areas[i]->msgs_proc, areas[i]->msgs_size,
  422.                            areas[i]->msg_dupes, areas[i]->msgs_sec);
  423.  
  424.       if ((areas[i]->msg_dupes != 0)
  425.       ||  (areas[i]->msgs_sec  != 0) )
  426.          LogIt(1, Message);
  427.       else
  428.          LogIt(4, Message);
  429.  
  430.       TotMsgs  += areas[i]->msgs_proc;
  431.       TotDupes += areas[i]->msg_dupes;
  432.       TotSec   += areas[i]->msgs_sec;
  433.  
  434.       areas[i]->msgs_proc   = 0;
  435.       areas[i]->msgs_size   = 0;
  436.       areas[i]->msg_dupes   = 0;
  437.       areas[i]->msgs_sec    = 0;
  438.       }
  439.  
  440. }
  441.  
  442.  
  443. /* ----------------------------------------------------------------------- */
  444. void LogScanArea(char *Area, unsigned int HighMsg, unsigned int LastMsg)
  445. {
  446.  
  447. if (LogOpened == 99)        /* No logging wanted */
  448.    return;
  449.  
  450. sprintf(Message, "Area=%s; high=%u; last=%u;", Area, HighMsg, LastMsg);
  451. LogIt(4, Message);
  452.  
  453. }
  454.  
  455.  
  456. /* ----------------------------------------------------------------------- */
  457. void LogScanNodes(AREAS_PTR area)
  458. {
  459. int i;
  460.  
  461. if (LogOpened == 99)        /* No logging wanted */
  462.    return;
  463.  
  464. for (i = 0; i < area->num_nodes; i++)
  465.    if (area->msgs_scanned[i] > 0)
  466.       {
  467.       sprintf(Message, "Sent=%u; to=%d:%d/%d;",
  468.                         area->msgs_scanned[i],
  469.                          area->zone[i], area->net[i], area->node[i]);
  470.  
  471.       TotSent += area->msgs_scanned[i];
  472.  
  473.       LogIt(4, Message);
  474.       }
  475. }
  476.  
  477.  
  478. /* ----------------------------------------------------------------------- */
  479. void LogPacket (char *pktname)
  480. {
  481. struct   stat            FnStats;
  482. struct  _packet_header   PktHeader;
  483.          FILE            *PktHandle;
  484. unsigned int             i;
  485.  
  486. if (LogOpened == 99)        /* No logging wanted */
  487.    return;
  488.  
  489. for (i = 0; i < sizeof(Marker); i++)
  490.    Marker[i] = '-' ;
  491. Marker[sizeof(Marker)-1] = 0;
  492.  
  493. sprintf(Message, "Packet=[%s] ", strlwr(pktname));
  494. strncat(Message, Marker, (56 - strlen(Message)) );
  495. LogIt(4, Message);
  496.  
  497. if ((PktHandle = fopen(pktname, "rb")) == NULL)
  498.    {
  499.    printf("Problem opening packet '%s\n", pktname);
  500.    exit(1);
  501.    }
  502.  
  503. if (fread(&PktHeader, sizeof(PktHeader), 1, PktHandle) != 1)
  504.    {
  505.    printf("Problem reading header of packet '%s\n", pktname);
  506.    exit(1);
  507.    }
  508.  
  509. fclose(PktHandle);
  510.  
  511. stat(pktname, &FnStats);
  512.  
  513. sprintf(Message, "Packed=%d/%02d/%02d %02d:%02d:%02d; size=%lub;",
  514.                    PktHeader.CreateYear, PktHeader.CreateMonth+1, PktHeader.CreateDay,
  515.                     PktHeader.CreateHour, PktHeader.CreateMinute, PktHeader.CreateSecond,
  516.                       FnStats.st_size);
  517. LogIt(4, Message);
  518.  
  519. sprintf(Message, "From=%d:%d/%d; to=%d:%d/%d; pcode=0x%02x; serial#=0x%02x;",
  520.                    PktHeader.OrigZone, PktHeader.OrigNet, PktHeader.OrigNode,
  521.                     PktHeader.DestZone, PktHeader.DestNet, PktHeader.DestNode,
  522.                       PktHeader.ProductCode, PktHeader.SerialNum);
  523. LogIt(4, Message);
  524. }
  525.