A date without the time for the '-d <date> option will
[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.9 $
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     : Aug 11, 2003
52 **      MODIFICATIONS   : 
53 **************************************************************************/
54
55 /*****************************
56    $Log: gcm_input.cpp,v $
57    Revision 1.9  2003-09-01 06:59:26  arjen
58    A date without the time for the '-d <date> option will
59    assume midnight on that date.
60
61    Revision 1.8  2003/08/16 15:30:19  arjen
62    Fixed a gcc 2 vs. gcc 3 problem
63
64    Revision 1.7  2003/08/14 10:28:37  arjen
65    Use parameters from a new section 'logging' with three configuration parameters:
66       method       - Output method to use for logging.
67       destination  - Name of the log output destination.
68       level        - Log level: Verbose output if greater than 0.
69
70    Revision 1.6  2003/08/11 16:56:16  arjen
71    Different kinds of log files are parsed by a collection of objects
72    of different classes, derived from the base class line_cooker
73    Depending on the message content or the message_type element in
74    XML, one of these objects is selected.
75
76    Logrunner is integrated with gcm_input. Although its functionality
77    is still limited, a connection between logrunner and gcm_input
78    is beginning to form.
79
80    Revision 1.5  2003/08/05 08:11:06  arjen
81    Added two configuration parameters:
82       logfile   - Log to this file instead of stderr.
83       verbosity - Verbose output if greater than 0.
84    Added '-i' option for incremental parameter updates
85
86    Revision 1.4  2003/03/29 08:42:00  arjen
87    Exit without reading any input if the database connection fails.
88
89    Revision 1.3  2002/11/09 08:04:27  arjen
90    Added a reference to the GPL
91
92    Revision 1.2  2002/11/04 10:13:36  arjen
93    Use proper namespace for iostream classes
94
95    Revision 1.1  2002/10/05 10:25:49  arjen
96    Creation of gcm_input and a first approach to a web interface
97
98 *****************************/
99
100 static const char *RCSID = "$Id: gcm_input.cpp,v 1.9 2003-09-01 06:59:26 arjen Exp $";
101
102 #include <fstream>
103
104 #include <getopt.h>
105
106 #include "message.h"
107 #include "syslog_cooker.h"
108 #include "irix_syslog_cooker.h"
109 #include "access_cooker.h"
110 #include "error_cooker.h"
111
112 bool verbose = false;
113 bool testmode = false;
114 bool incremental = false;
115 std::ostream *log = &std::cerr;
116
117 static char *Version = "gcm_input version 0.0.7 - Jul 24, 2003";
118
119
120 /*=========================================================================
121 **  NAME           : main
122 **  SYNOPSIS       : int main(int argc, char *argv[])
123 **  PARAMETERS     : 
124 **  RETURN VALUE   : 
125 **
126 **  DESCRIPTION    : Parse command line arguments and establish a connection
127 **                   to the database.
128 **                   When we have a database connection, parse the log file
129 **                   from stdin.
130 **
131 **  VARS USED      :
132 **  VARS CHANGED   :
133 **  FUNCTIONS USED :
134 **  SEE ALSO       :
135 **  LAST MODIFIED  : Aug 11, 2003
136 **=========================================================================
137 */
138
139 int main(int argc, char *argv[])
140 {
141    const char *usage = "Usage: gcm_input [-c configname] [-h hostname] [-i] [-d date]"
142                        " [-s service] [-T] [-v] [-V]\n";
143
144    gnucomo_config    cfg;
145    char             *config_name = "gnucomo";
146    std::ofstream    logfile;
147
148
149    /*   Parse command line arguments */
150
151    UTC    arrival = Now();
152    String  hostname(""), service("");
153    int     option;
154
155
156    while ((option = getopt(argc, argv, "c:h:d:s:iTvV")) != -1)
157    {
158       switch (option)
159       {
160       case 'c':
161          config_name = optarg;
162          break;
163
164       case 'h':
165          hostname = optarg;
166          break;
167
168       case 'd':
169          arrival = String(optarg);
170          if (!date(arrival).proper())
171          {
172             std::cerr << "gcm_input: Invalid date string: " << optarg
173                       << "(" << arrival << ").\n";
174             exit(1);
175          }
176          else if (!hour(arrival).proper())
177          {
178             arrival = UTC(date(arrival), hour(0,0,0));
179          }
180          break;
181
182       case 's':
183          service = optarg;
184          break;
185
186       case 'i':
187          incremental = true;
188          break;
189
190       case 'T':
191          testmode = true;
192          break;
193
194       case 'v':
195          verbose = true;
196          break;
197
198       case 'V':
199          std::cout << Version << "\n";
200          exit(0);
201
202       case '?':
203       case ':':
204          std::cerr << usage;
205          exit(1);
206       }
207    }
208    /*  Get the configuration file */
209
210    if (!cfg.read(config_name))
211    {
212       std::cerr << "Can not read Gnucomo configuration file for " << config_name << ".\n";
213       exit(1);
214    }
215
216    String log_method      = cfg.find_parameter("logging", "method");
217    String log_destination = cfg.find_parameter("logging", "destination");
218    int    level           = cfg.find_parameter("gcm_input", "level");
219
220    if (log_method == "file" && log_destination != "")
221    {
222       std::cerr << "Logging to " << log_destination << ".\n";
223 #if __GNUC__ == 2
224       logfile.open(log_destination, _IO_APPEND); // for gcc 2
225 #else
226       logfile.open(log_destination, std::ios_base::app); // for gcc 3
227 #endif
228       if (!logfile)
229       {
230          std::cerr << "Can't open logfile " << log_destination << " for writing.\n";
231       }
232       else
233       {
234          log = &logfile;
235          verbose = verbose || level > 0;
236       }
237    }
238
239    if (verbose)
240    {
241       *log << "Hostname = " << hostname;
242       *log << " Arrival = " << arrival;
243       *log << " Service = " << service << "\n";
244       *log << "Config OK.\n";
245    }
246
247    /*  Try to connect to the database */
248
249    gnucomo_database db(&cfg);
250
251    if (db.is_connected())
252    {
253
254       client_message      msg(&std::cin, db);
255       syslog_cooker       slc;
256       irix_syslog_cooker  islc;
257       access_cooker       alc;
258       error_cooker        elc;
259
260       msg.add_cooker(&slc);
261       msg.add_cooker(&islc);
262       msg.add_cooker(&alc);
263       msg.add_cooker(&elc);
264
265       if (msg.classify(hostname, arrival, service) > 0.9)
266       {
267          msg.enter();
268       }
269       return 0;
270    }
271    else
272    {
273       *log << "gcm_input: Can not connect to database.\n";
274       *log << "Gcm_input finished at " << Now() << ".\n";
275       return 1;
276    }
277 }
278