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.10 $
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 : Aug 11, 2003
53 **************************************************************************/
55 /*****************************
56 $Log: gcm_input.cpp,v $
57 Revision 1.10 2003-09-03 06:58:31 arjen
58 Changed version string to 0.0.8
60 Revision 1.9 2003/09/01 06:59:26 arjen
61 A date without the time for the '-d <date> option will
62 assume midnight on that date.
64 Revision 1.8 2003/08/16 15:30:19 arjen
65 Fixed a gcc 2 vs. gcc 3 problem
67 Revision 1.7 2003/08/14 10:28:37 arjen
68 Use parameters from a new section 'logging' with three configuration parameters:
69 method - Output method to use for logging.
70 destination - Name of the log output destination.
71 level - Log level: Verbose output if greater than 0.
73 Revision 1.6 2003/08/11 16:56:16 arjen
74 Different kinds of log files are parsed by a collection of objects
75 of different classes, derived from the base class line_cooker
76 Depending on the message content or the message_type element in
77 XML, one of these objects is selected.
79 Logrunner is integrated with gcm_input. Although its functionality
80 is still limited, a connection between logrunner and gcm_input
83 Revision 1.5 2003/08/05 08:11:06 arjen
84 Added two configuration parameters:
85 logfile - Log to this file instead of stderr.
86 verbosity - Verbose output if greater than 0.
87 Added '-i' option for incremental parameter updates
89 Revision 1.4 2003/03/29 08:42:00 arjen
90 Exit without reading any input if the database connection fails.
92 Revision 1.3 2002/11/09 08:04:27 arjen
93 Added a reference to the GPL
95 Revision 1.2 2002/11/04 10:13:36 arjen
96 Use proper namespace for iostream classes
98 Revision 1.1 2002/10/05 10:25:49 arjen
99 Creation of gcm_input and a first approach to a web interface
101 *****************************/
103 static const char *RCSID = "$Id: gcm_input.cpp,v 1.10 2003-09-03 06:58:31 arjen Exp $";
110 #include "syslog_cooker.h"
111 #include "irix_syslog_cooker.h"
112 #include "access_cooker.h"
113 #include "error_cooker.h"
115 bool verbose = false;
116 bool testmode = false;
117 bool incremental = false;
118 std::ostream *log = &std::cerr;
120 static char *Version = "gcm_input version 0.0.8 - Sep 04, 2003";
123 /*=========================================================================
125 ** SYNOPSIS : int main(int argc, char *argv[])
129 ** DESCRIPTION : Parse command line arguments and establish a connection
131 ** When we have a database connection, parse the log file
138 ** LAST MODIFIED : Aug 11, 2003
139 **=========================================================================
142 int main(int argc, char *argv[])
144 const char *usage = "Usage: gcm_input [-c configname] [-h hostname] [-i] [-d date]"
145 " [-s service] [-T] [-v] [-V]\n";
148 char *config_name = "gnucomo";
149 std::ofstream logfile;
152 /* Parse command line arguments */
155 String hostname(""), service("");
159 while ((option = getopt(argc, argv, "c:h:d:s:iTvV")) != -1)
164 config_name = optarg;
172 arrival = String(optarg);
173 if (!date(arrival).proper())
175 std::cerr << "gcm_input: Invalid date string: " << optarg
176 << "(" << arrival << ").\n";
179 else if (!hour(arrival).proper())
181 arrival = UTC(date(arrival), hour(0,0,0));
202 std::cout << Version << "\n";
211 /* Get the configuration file */
213 if (!cfg.read(config_name))
215 std::cerr << "Can not read Gnucomo configuration file for " << config_name << ".\n";
219 String log_method = cfg.find_parameter("logging", "method");
220 String log_destination = cfg.find_parameter("logging", "destination");
221 int level = cfg.find_parameter("gcm_input", "level");
223 if (log_method == "file" && log_destination != "")
225 std::cerr << "Logging to " << log_destination << ".\n";
227 logfile.open(log_destination, _IO_APPEND); // for gcc 2
229 logfile.open(log_destination, std::ios_base::app); // for gcc 3
233 std::cerr << "Can't open logfile " << log_destination << " for writing.\n";
238 verbose = verbose || level > 0;
244 *log << "Hostname = " << hostname;
245 *log << " Arrival = " << arrival;
246 *log << " Service = " << service << "\n";
247 *log << "Config OK.\n";
250 /* Try to connect to the database */
252 gnucomo_database db(&cfg);
254 if (db.is_connected())
257 client_message msg(&std::cin, db);
259 irix_syslog_cooker islc;
263 msg.add_cooker(&slc);
264 msg.add_cooker(&islc);
265 msg.add_cooker(&alc);
266 msg.add_cooker(&elc);
268 if (msg.classify(hostname, arrival, service) > 0.9)
276 *log << "gcm_input: Can not connect to database.\n";
277 *log << "Gcm_input finished at " << Now() << ".\n";