Added a new filter which can directly read the output
[gnucomo.git] / src / gcm_input / gcm_input.cpp
1 /**************************************************************************
2 **  (c) Copyright 2002, Andromeda Technology & Automation
3 ** This is free software; you can redistribute it and/or modify it under the
4 ** terms of the GNU General Public License, see the file COPYING.
5 ***************************************************************************
6 ** MODULE INFORMATION *
7 ***********************
8 **      FILE NAME      : gcm_input.cpp
9 **      SYSTEM NAME    : Gnucomo - Gnu Computer Monitoring
10 **      VERSION NUMBER : $Revision: 1.14 $
11 **
12 **  DESCRIPTION      :  Application to store client messages into the database
13 **                      The client message contains a log file from one of the
14 **                      system logs. Gcm_input parses the log file and enters
15 **                      the raw data into the 'log' table of the gnucomo database.
16 **
17 **                      The system log may arrive in one of several forms:
18 **                      1. Log file (archived or not) from a client machine.
19 **                      2. Log file from a client system, arriving in a clear Email.
20 **                      3. Log file from a client machine, arriving in an
21 **                         encrypted Email.
22 **
23 **                      additional information we need that may not be availble in
24 **                      the content of the log is:
25 **                      - FQDN of the client.
26 **                      - Time of arrival of the log
27 **                      - Service that created the log.
28 **
29 **                      If the log arrives in an Email, these items probably are
30 **                      available in the mail header. Otherwise, they have to be
31 **                      provided as command line arguments.
32 **
33 **                      Command line arguments:
34 **                      -c <name>        Configuration name (default = gnucomo).
35 **                      -d <date>        Date and time of log arrival.
36 **                      -h <hostname>    FQDN of the client.
37 **                      -i               Incremental - partial list of parameters.
38 **                      -s <service>     Service that created the log.
39 **                      -T               Test mode. Do not alter the database.
40 **                      -v               Verbose (debug) output.
41 **                      -V               Print version and exit.
42 **
43 **  EXPORTED OBJECTS : 
44 **  LOCAL    OBJECTS : 
45 **  MODULES  USED    :
46 ***************************************************************************
47 **  ADMINISTRATIVE INFORMATION *
48 ********************************
49 **      ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
50 **      CREATION DATE   : Aug 29, 2002
51 **      LAST UPDATE     : Nov 26, 2003
52 **      MODIFICATIONS   : 
53 **************************************************************************/
54
55 /*****************************
56    $Log: gcm_input.cpp,v $
57    Revision 1.14  2007-11-03 10:26:13  arjen
58    Added a new filter which can directly read the output
59    of the UNIX df command. A brief description is added in
60    the user manual.
61
62    Revision 1.13  2003/12/22 10:20:21  arjen
63    Report if the message type can not be detected.
64
65    Revision 1.12  2003/12/04 10:38:09  arjen
66    Major redesign. All input is handled through XML. Raw input data is first
67    transformed into an XML document for further processing.
68    A collection of polymorphic classes handle the transformation of various
69    input formats into XML.
70    Classifying input data is done with a finite improbability calculation.
71
72    Revision 1.11  2003/10/27 13:00:15  arjen
73    Catch exceptions from the database library
74
75    Revision 1.10  2003/09/03 06:58:31  arjen
76    Changed version string to 0.0.8
77
78    Revision 1.9  2003/09/01 06:59:26  arjen
79    A date without the time for the '-d <date> option will
80    assume midnight on that date.
81
82    Revision 1.8  2003/08/16 15:30:19  arjen
83    Fixed a gcc 2 vs. gcc 3 problem
84
85    Revision 1.7  2003/08/14 10:28:37  arjen
86    Use parameters from a new section 'logging' with three configuration parameters:
87       method       - Output method to use for logging.
88       destination  - Name of the log output destination.
89       level        - Log level: Verbose output if greater than 0.
90
91    Revision 1.6  2003/08/11 16:56:16  arjen
92    Different kinds of log files are parsed by a collection of objects
93    of different classes, derived from the base class line_cooker
94    Depending on the message content or the message_type element in
95    XML, one of these objects is selected.
96
97    Logrunner is integrated with gcm_input. Although its functionality
98    is still limited, a connection between logrunner and gcm_input
99    is beginning to form.
100
101    Revision 1.5  2003/08/05 08:11:06  arjen
102    Added two configuration parameters:
103       logfile   - Log to this file instead of stderr.
104       verbosity - Verbose output if greater than 0.
105    Added '-i' option for incremental parameter updates
106
107    Revision 1.4  2003/03/29 08:42:00  arjen
108    Exit without reading any input if the database connection fails.
109
110    Revision 1.3  2002/11/09 08:04:27  arjen
111    Added a reference to the GPL
112
113    Revision 1.2  2002/11/04 10:13:36  arjen
114    Use proper namespace for iostream classes
115
116    Revision 1.1  2002/10/05 10:25:49  arjen
117    Creation of gcm_input and a first approach to a web interface
118
119 *****************************/
120
121 static const char *RCSID = "$Id: gcm_input.cpp,v 1.14 2007-11-03 10:26:13 arjen Exp $";
122
123 #include <fstream>
124
125 #include <getopt.h>
126
127 #include "message.h"
128 #include "log_filter.h"
129 #include "rpm_filter.h"
130 #include "df_filter.h"
131
132 #include "xml_cooker.h"
133 #include "syslog_cooker.h"
134 #include "irix_syslog_cooker.h"
135 #include "access_cooker.h"
136 #include "error_cooker.h"
137
138 bool verbose = false;
139 bool testmode = false;
140 bool incremental = false;
141 std::ostream *Log = &std::cerr;
142
143 static char *Version = "gcm_input version 0.0.11 - Nov 22, 2007";
144
145
146 /*=========================================================================
147 **  NAME           : main
148 **  SYNOPSIS       : int main(int argc, char *argv[])
149 **  PARAMETERS     : 
150 **  RETURN VALUE   : 
151 **
152 **  DESCRIPTION    : Parse command line arguments and establish a connection
153 **                   to the database.
154 **                   When we have a database connection, parse the log file
155 **                   from stdin.
156 **
157 **  VARS USED      :
158 **  VARS CHANGED   :
159 **  FUNCTIONS USED :
160 **  SEE ALSO       :
161 **  LAST MODIFIED  : Aug 11, 2003
162 **=========================================================================
163 */
164
165 int main(int argc, char *argv[])
166 {
167    const char *usage = "Usage: gcm_input [-c configname] [-h hostname] [-i] [-d date]"
168                        " [-s service] [-T] [-v] [-V]\n";
169
170    gnucomo_config    cfg;
171    char             *config_name = "gnucomo";
172    std::ofstream    logfile;
173
174
175    /*   Parse command line arguments */
176
177    UTC    arrival = Now();
178    String  hostname(""), service("");
179    int     option;
180
181
182    while ((option = getopt(argc, argv, "c:h:d:s:iTvV")) != -1)
183    {
184       switch (option)
185       {
186       case 'c':
187          config_name = optarg;
188          break;
189
190       case 'h':
191          hostname = optarg;
192          break;
193
194       case 'd':
195          arrival = String(optarg);
196          if (!date(arrival).proper())
197          {
198             std::cerr << "gcm_input: Invalid date string: " << optarg
199                       << "(" << arrival << ").\n";
200             exit(1);
201          }
202          else if (!hour(arrival).proper())
203          {
204             arrival = UTC(date(arrival), hour(0,0,0));
205          }
206          break;
207
208       case 's':
209          service = optarg;
210          break;
211
212       case 'i':
213          incremental = true;
214          break;
215
216       case 'T':
217          testmode = true;
218          break;
219
220       case 'v':
221          verbose = true;
222          break;
223
224       case 'V':
225          std::cout << Version << "\n";
226          exit(0);
227
228       case '?':
229       case ':':
230          std::cerr << usage;
231          exit(1);
232       }
233    }
234    /*  Get the configuration file */
235
236    if (!cfg.read(config_name))
237    {
238       std::cerr << "Can not read Gnucomo configuration file for " << config_name << ".\n";
239       exit(1);
240    }
241
242    String log_method      = cfg.find_parameter("logging", "method");
243    String log_destination = cfg.find_parameter("logging", "destination");
244    int    level           = cfg.find_parameter("logging", "level");
245
246    if (log_method == "file" && log_destination != "")
247    {
248 #if __GNUC__ == 2
249       logfile.open(log_destination, _IO_APPEND); // for gcc 2
250 #else
251       logfile.open(log_destination, std::ios_base::app); // for gcc 3
252 #endif
253       if (!logfile)
254       {
255          std::cerr << "Can't open logfile " << log_destination << " for writing.\n";
256       }
257       else
258       {
259          Log = &logfile;
260       }
261    }
262    verbose = verbose || level > 0;
263
264    if (verbose)
265    {
266       *Log << "Hostname = " << hostname;
267       *Log << " Arrival = " << arrival;
268       *Log << " Service = " << service << "\n";
269       *Log << "Config OK.\n";
270    }
271
272    /*  Try to connect to the database */
273
274    gnucomo_database db(&cfg);
275
276    if (db.is_connected())
277    {
278
279       client_message      msg(&std::cin, db);
280
281       double              message_probability;
282
283       message_filter      shortcircuit(hostname, arrival, service);
284       log_filter          lf(hostname, arrival, service);
285       rpm_filter          rf(hostname, arrival, service);
286       df_filter           df(hostname, arrival, service);
287
288       syslog_cooker       slc;
289       irix_syslog_cooker  islc;
290       access_cooker       alc;
291       error_cooker        elc;
292       xml_cooker          xlc;
293       rpm_cooker          rlc;
294       df_cooker           dlc;
295
296       msg.add_cooker(&xlc,  &shortcircuit);
297       msg.add_cooker(&slc,  &lf);
298       msg.add_cooker(&islc, &lf);
299       msg.add_cooker(&alc,  &lf);
300       msg.add_cooker(&elc,  &lf);
301       msg.add_cooker(&rlc,  &rf);
302       msg.add_cooker(&dlc,  &df);
303
304       message_probability = msg.classify(hostname, arrival, service);
305       *Log << "Message type probability = " << message_probability << "\n";
306       if (message_probability > 0.75)
307       {
308          try
309          {
310             msg.enter();
311          }
312          catch (std::exception &e)
313          {
314             *Log << "Caught an exception: " << e.what() << "\n";
315          }
316       }
317       else
318       {
319          *Log << "Cannot determine message type with sufficient certainty.\n";
320       }
321       return 0;
322    }
323    else
324    {
325       *Log << "gcm_input: Can not connect to database.\n";
326       *Log << "Gcm_input finished at " << Now() << ".\n";
327       return 1;
328    }
329 }
330