9e47d481eb006232287c3e6dd8a8a476d4e0defd
[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.4 $
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 **                      -s <service>     Service that created the log.
38 **                      -T               Test mode. Do not alter the database.
39 **                      -v               Verbose (debug) output.
40 **                      -V               Print version and exit.
41 **
42 **  EXPORTED OBJECTS : 
43 **  LOCAL    OBJECTS : 
44 **  MODULES  USED    :
45 ***************************************************************************
46 **  ADMINISTRATIVE INFORMATION *
47 ********************************
48 **      ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
49 **      CREATION DATE   : Aug 29, 2002
50 **      LAST UPDATE     : Mar 28, 2003
51 **      MODIFICATIONS   : 
52 **************************************************************************/
53
54 /*****************************
55    $Log: gcm_input.cpp,v $
56    Revision 1.4  2003-03-29 08:42:00  arjen
57    Exit without reading any input if the database connection fails.
58
59    Revision 1.3  2002/11/09 08:04:27  arjen
60    Added a reference to the GPL
61
62    Revision 1.2  2002/11/04 10:13:36  arjen
63    Use proper namespace for iostream classes
64
65    Revision 1.1  2002/10/05 10:25:49  arjen
66    Creation of gcm_input and a first approach to a web interface
67
68 *****************************/
69
70 static const char *RCSID = "$Id: gcm_input.cpp,v 1.4 2003-03-29 08:42:00 arjen Exp $";
71
72 #include <getopt.h>
73
74 #include "message.h"
75
76 bool verbose = false;
77 bool testmode = false;
78
79 static char *Version = "gcm_input version 0.0.4 - Nov 05, 2002";
80
81
82 /*=========================================================================
83 **  NAME           : main
84 **  SYNOPSIS       : int main(int argc, char *argv[])
85 **  PARAMETERS     : 
86 **  RETURN VALUE   : 
87 **
88 **  DESCRIPTION    : Parse command line arguments and establish a connection
89 **                   to the database.
90 **                   When we have a database connection, parse the log file
91 **                   from stdin.
92 **
93 **  VARS USED      :
94 **  VARS CHANGED   :
95 **  FUNCTIONS USED :
96 **  SEE ALSO       :
97 **  LAST MODIFIED  : Mar 28, 2003
98 **=========================================================================
99 */
100
101 int main(int argc, char *argv[])
102 {
103    const char *usage = "Usage: gcm_input [-c configname] [-h hostname] [-d date]"
104                        " [-s service] [-T] [-v] [-V]\n";
105
106    gnucomo_config    cfg;
107    char             *config_name = "gnucomo";
108
109    /*   Parse command line arguments */
110
111    UTC    arrival = Now();
112    String  hostname(""), service("");
113    int     option;
114
115
116    while ((option = getopt(argc, argv, "c:h:d:s:TvV")) != -1)
117    {
118       switch (option)
119       {
120       case 'c':
121          config_name = optarg;
122          break;
123
124       case 'h':
125          hostname = optarg;
126          break;
127
128       case 'd':
129          arrival = String(optarg);
130          if (!arrival.proper())
131          {
132             std::cerr << "gcm_input: Invalid date string: " << optarg << ".\n";
133             exit(1);
134          }
135          break;
136
137       case 's':
138          service = optarg;
139          break;
140
141       case 'T':
142          testmode = true;
143          break;
144
145       case 'v':
146          verbose = true;
147          break;
148
149       case 'V':
150          std::cout << Version << "\n";
151          exit(0);
152
153       case '?':
154       case ':':
155          std::cerr << usage;
156          exit(1);
157       }
158    }
159    if (verbose)
160    {
161       std::cout << "Hostname = " << hostname;
162       std::cout << " Arrival = " << arrival;
163       std::cout << " Service = " << service << "\n";
164    }
165
166    /*  Get the configuration file */
167
168    if (!cfg.read(config_name))
169    {
170       std::cerr << "Can not read Gnucomo configuration file for " << config_name << ".\n";
171       exit(1);
172    }
173
174    if (verbose)
175    {
176       std::cout << "Config OK.\n";
177    }
178
179    /*  Try to connect to the database */
180
181    gnucomo_database db(&cfg);
182
183    if (db.is_connected())
184    {
185
186       client_message  msg(&std::cin, db);
187
188       if (msg.classify(hostname, arrival, service) > 0.9)
189       {
190          msg.enter();
191       }
192       return 0;
193    }
194    else
195    {
196       std::cerr << "gcm_input: Can not connect to database.\n";
197       return 1;
198    }
199 }
200