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