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