Report if the message type can not be detected.
[gnucomo.git] / src / gcm_input / gcm_input.cpp
index 137fb69..b1c50e6 100644 (file)
@@ -7,7 +7,7 @@
 ***********************
 **      FILE NAME      : gcm_input.cpp
 **      SYSTEM NAME    : Gnucomo - Gnu Computer Monitoring
-**      VERSION NUMBER : $Revision: 1.7 $
+**      VERSION NUMBER : $Revision: 1.13 $
 **
 **  DESCRIPTION      :  Application to store client messages into the database
 **                      The client message contains a log file from one of the
 ********************************
 **      ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
 **      CREATION DATE   : Aug 29, 2002
-**      LAST UPDATE     : Aug 11, 2003
+**      LAST UPDATE     : Nov 26, 2003
 **      MODIFICATIONS   : 
 **************************************************************************/
 
 /*****************************
    $Log: gcm_input.cpp,v $
-   Revision 1.7  2003-08-14 10:28:37  arjen
+   Revision 1.13  2003-12-22 10:20:21  arjen
+   Report if the message type can not be detected.
+
+   Revision 1.12  2003/12/04 10:38:09  arjen
+   Major redesign. All input is handled through XML. Raw input data is first
+   transformed into an XML document for further processing.
+   A collection of polymorphic classes handle the transformation of various
+   input formats into XML.
+   Classifying input data is done with a finite improbability calculation.
+
+   Revision 1.11  2003/10/27 13:00:15  arjen
+   Catch exceptions from the database library
+
+   Revision 1.10  2003/09/03 06:58:31  arjen
+   Changed version string to 0.0.8
+
+   Revision 1.9  2003/09/01 06:59:26  arjen
+   A date without the time for the '-d <date> option will
+   assume midnight on that date.
+
+   Revision 1.8  2003/08/16 15:30:19  arjen
+   Fixed a gcc 2 vs. gcc 3 problem
+
+   Revision 1.7  2003/08/14 10:28:37  arjen
    Use parameters from a new section 'logging' with three configuration parameters:
       method       - Output method to use for logging.
       destination  - Name of the log output destination.
 
 *****************************/
 
-static const char *RCSID = "$Id: gcm_input.cpp,v 1.7 2003-08-14 10:28:37 arjen Exp $";
+static const char *RCSID = "$Id: gcm_input.cpp,v 1.13 2003-12-22 10:20:21 arjen Exp $";
 
 #include <fstream>
 
 #include <getopt.h>
 
 #include "message.h"
+#include "log_filter.h"
+#include "rpm_filter.h"
+
+#include "xml_cooker.h"
 #include "syslog_cooker.h"
 #include "irix_syslog_cooker.h"
 #include "access_cooker.h"
@@ -105,9 +132,9 @@ static const char *RCSID = "$Id: gcm_input.cpp,v 1.7 2003-08-14 10:28:37 arjen E
 bool verbose = false;
 bool testmode = false;
 bool incremental = false;
-std::ostream *log = &std::cerr;
+std::ostream *Log = &std::cerr;
 
-static char *Version = "gcm_input version 0.0.7 - Jul 24, 2003";
+static char *Version = "gcm_input version 0.0.9 - Dec 22, 2003";
 
 
 /*=========================================================================
@@ -160,11 +187,16 @@ int main(int argc, char *argv[])
 
       case 'd':
          arrival = String(optarg);
-         if (!arrival.proper())
+         if (!date(arrival).proper())
          {
-            std::cerr << "gcm_input: Invalid date string: " << optarg << ".\n";
+            std::cerr << "gcm_input: Invalid date string: " << optarg
+                      << "(" << arrival << ").\n";
             exit(1);
          }
+         else if (!hour(arrival).proper())
+         {
+            arrival = UTC(date(arrival), hour(0,0,0));
+         }
          break;
 
       case 's':
@@ -203,32 +235,32 @@ int main(int argc, char *argv[])
 
    String log_method      = cfg.find_parameter("logging", "method");
    String log_destination = cfg.find_parameter("logging", "destination");
-   int    level           = cfg.find_parameter("gcm_input", "level");
+   int    level           = cfg.find_parameter("logging", "level");
 
    if (log_method == "file" && log_destination != "")
    {
-      std::cerr << "Logging to " << log_destination << ".\n";
+#if __GNUC__ == 2
       logfile.open(log_destination, _IO_APPEND); // for gcc 2
-      //logfile.open(logfile_name, std::ios_base::app); // for gcc 3
+#else
+      logfile.open(log_destination, std::ios_base::app); // for gcc 3
+#endif
       if (!logfile)
       {
          std::cerr << "Can't open logfile " << log_destination << " for writing.\n";
       }
       else
       {
-         log = &logfile;
-         verbose = verbose || level > 0;
+         Log = &logfile;
       }
    }
-
-   *log << "Gcm_input starting at " << Now() << ".\n";
+   verbose = verbose || level > 0;
 
    if (verbose)
    {
-      *log << "Hostname = " << hostname;
-      *log << " Arrival = " << arrival;
-      *log << " Service = " << service << "\n";
-      *log << "Config OK.\n";
+      *Log << "Hostname = " << hostname;
+      *Log << " Arrival = " << arrival;
+      *Log << " Service = " << service << "\n";
+      *Log << "Config OK.\n";
    }
 
    /*  Try to connect to the database */
@@ -239,27 +271,46 @@ int main(int argc, char *argv[])
    {
 
       client_message      msg(&std::cin, db);
+
+      message_filter      shortcircuit(hostname, arrival, service);
+      log_filter          lf(hostname, arrival, service);
+      rpm_filter          rf(hostname, arrival, service);
+
       syslog_cooker       slc;
       irix_syslog_cooker  islc;
       access_cooker       alc;
       error_cooker        elc;
+      xml_cooker          xlc;
+      rpm_cooker          rlc;
 
-      msg.add_cooker(&slc);
-      msg.add_cooker(&islc);
-      msg.add_cooker(&alc);
-      msg.add_cooker(&elc);
+      msg.add_cooker(&xlc,  &shortcircuit);
+      msg.add_cooker(&slc,  &lf);
+      msg.add_cooker(&islc, &lf);
+      msg.add_cooker(&alc,  &lf);
+      msg.add_cooker(&elc,  &lf);
+      msg.add_cooker(&rlc,  &rf);
 
       if (msg.classify(hostname, arrival, service) > 0.9)
       {
-         msg.enter();
+         try
+         {
+            msg.enter();
+         }
+         catch (std::exception &e)
+         {
+            *Log << "Caught an exception: " << e.what() << "\n";
+         }
+      }
+      else
+      {
+         *Log << "Cannot determine message type with sufficient certainty.\n";
       }
-      *log << "Gcm_input finished at " << Now() << ".\n";
       return 0;
    }
    else
    {
-      *log << "gcm_input: Can not connect to database.\n";
-      *log << "Gcm_input finished at " << Now() << ".\n";
+      *Log << "gcm_input: Can not connect to database.\n";
+      *Log << "Gcm_input finished at " << Now() << ".\n";
       return 1;
    }
 }