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