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.15 $
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.
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
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.
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.
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.
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
53 **************************************************************************/
55 /*****************************
56 $Log: gcm_input.cpp,v $
57 Revision 1.15 2007-11-14 16:19:25 arjen
58 Bugfix: Segmentation fault when reading an rpm package list
61 Revision 1.14 2007/11/03 10:26:13 arjen
62 Added a new filter which can directly read the output
63 of the UNIX df command. A brief description is added in
66 Revision 1.13 2003/12/22 10:20:21 arjen
67 Report if the message type can not be detected.
69 Revision 1.12 2003/12/04 10:38:09 arjen
70 Major redesign. All input is handled through XML. Raw input data is first
71 transformed into an XML document for further processing.
72 A collection of polymorphic classes handle the transformation of various
73 input formats into XML.
74 Classifying input data is done with a finite improbability calculation.
76 Revision 1.11 2003/10/27 13:00:15 arjen
77 Catch exceptions from the database library
79 Revision 1.10 2003/09/03 06:58:31 arjen
80 Changed version string to 0.0.8
82 Revision 1.9 2003/09/01 06:59:26 arjen
83 A date without the time for the '-d <date> option will
84 assume midnight on that date.
86 Revision 1.8 2003/08/16 15:30:19 arjen
87 Fixed a gcc 2 vs. gcc 3 problem
89 Revision 1.7 2003/08/14 10:28:37 arjen
90 Use parameters from a new section 'logging' with three configuration parameters:
91 method - Output method to use for logging.
92 destination - Name of the log output destination.
93 level - Log level: Verbose output if greater than 0.
95 Revision 1.6 2003/08/11 16:56:16 arjen
96 Different kinds of log files are parsed by a collection of objects
97 of different classes, derived from the base class line_cooker
98 Depending on the message content or the message_type element in
99 XML, one of these objects is selected.
101 Logrunner is integrated with gcm_input. Although its functionality
102 is still limited, a connection between logrunner and gcm_input
103 is beginning to form.
105 Revision 1.5 2003/08/05 08:11:06 arjen
106 Added two configuration parameters:
107 logfile - Log to this file instead of stderr.
108 verbosity - Verbose output if greater than 0.
109 Added '-i' option for incremental parameter updates
111 Revision 1.4 2003/03/29 08:42:00 arjen
112 Exit without reading any input if the database connection fails.
114 Revision 1.3 2002/11/09 08:04:27 arjen
115 Added a reference to the GPL
117 Revision 1.2 2002/11/04 10:13:36 arjen
118 Use proper namespace for iostream classes
120 Revision 1.1 2002/10/05 10:25:49 arjen
121 Creation of gcm_input and a first approach to a web interface
123 *****************************/
125 static const char *RCSID = "$Id: gcm_input.cpp,v 1.15 2007-11-14 16:19:25 arjen Exp $";
132 #include "log_filter.h"
133 #include "rpm_filter.h"
134 #include "df_filter.h"
136 #include "xml_cooker.h"
137 #include "syslog_cooker.h"
138 #include "irix_syslog_cooker.h"
139 #include "access_cooker.h"
140 #include "error_cooker.h"
142 bool verbose = false;
143 bool testmode = false;
144 bool incremental = false;
145 std::ostream *Log = &std::cerr;
147 static char *Version = "gcm_input version 0.0.11 - Nov 22, 2007";
150 /*=========================================================================
152 ** SYNOPSIS : int main(int argc, char *argv[])
156 ** DESCRIPTION : Parse command line arguments and establish a connection
158 ** When we have a database connection, parse the log file
165 ** LAST MODIFIED : Aug 11, 2003
166 **=========================================================================
169 int main(int argc, char *argv[])
171 const char *usage = "Usage: gcm_input [-c configname] [-h hostname] [-i] [-d date]"
172 " [-s service] [-T] [-v] [-V]\n";
175 char *config_name = "gnucomo";
176 std::ofstream logfile;
179 /* Parse command line arguments */
182 String hostname(""), service("");
186 while ((option = getopt(argc, argv, "c:h:d:s:iTvV")) != -1)
191 config_name = optarg;
199 arrival = String(optarg);
200 if (!date(arrival).proper())
202 std::cerr << "gcm_input: Invalid date string: " << optarg
203 << "(" << arrival << ").\n";
206 else if (!hour(arrival).proper())
208 arrival = UTC(date(arrival), hour(0,0,0));
229 std::cout << Version << "\n";
238 /* Get the configuration file */
240 if (!cfg.read(config_name))
242 std::cerr << "Can not read Gnucomo configuration file for " << config_name << ".\n";
246 String log_method = cfg.find_parameter("logging", "method");
247 String log_destination = cfg.find_parameter("logging", "destination");
248 int level = cfg.find_parameter("logging", "level");
250 if (log_method == "file" && log_destination != "")
253 logfile.open(log_destination, _IO_APPEND); // for gcc 2
255 logfile.open(log_destination, std::ios_base::app); // for gcc 3
259 std::cerr << "Can't open logfile " << log_destination << " for writing.\n";
266 verbose = verbose || level > 0;
270 *Log << "Hostname = " << hostname;
271 *Log << " Arrival = " << arrival;
272 *Log << " Service = " << service << "\n";
273 *Log << "Config OK.\n";
276 /* Try to connect to the database */
278 gnucomo_database db(&cfg);
280 if (db.is_connected())
283 client_message msg(&std::cin, db);
285 double message_probability;
287 message_filter shortcircuit(hostname, arrival, service);
288 log_filter lf(hostname, arrival, service);
289 rpm_filter rf(hostname, arrival, service);
290 df_filter df(hostname, arrival, service);
293 irix_syslog_cooker islc;
300 msg.add_cooker(&xlc, &shortcircuit);
301 msg.add_cooker(&slc, &lf);
302 msg.add_cooker(&islc, &lf);
303 msg.add_cooker(&alc, &lf);
304 msg.add_cooker(&elc, &lf);
305 msg.add_cooker(&rlc, &rf);
306 msg.add_cooker(&dlc, &df);
308 message_probability = msg.classify(hostname, arrival, service);
309 if (message_probability > 0.75)
315 catch (std::exception &e)
317 *Log << "Caught an exception: " << e.what() << "\n";
322 *Log << "Cannot determine message type with sufficient certainty.\n";
328 *Log << "gcm_input: Can not connect to database.\n";
329 *Log << "Gcm_input finished at " << Now() << ".\n";