Creation of gcm_input and a first approach to a web interface
[gnucomo.git] / src / gcm_input / gcm_input.cpp
1
2 /**************************************************************************
3 **  (c) Copyright 2002, Andromeda Technology & Automation
4 ***************************************************************************
5 ** MODULE INFORMATION *
6 ***********************
7 **      FILE NAME      : gcm_input.cpp
8 **      SYSTEM NAME    : Gnucomo - Gnu Computer Monitoring
9 **      VERSION NUMBER : $Revision: 1.1 $
10 **
11 **  DESCRIPTION      :  Application to store client messages into the database
12 **                      The client message contains a log file from one of the
13 **                      system logs. Gcm_input parses the log file and enters
14 **                      the raw data into the 'log' table of the gnucomo database.
15 **
16 **                      The system log may arrive in one of several forms:
17 **                      1. Log file (archived or not) from a client machine.
18 **                      2. Log file from a client system, arriving in a clear Email.
19 **                      3. Log file from a client machine, arriving in an
20 **                         encrypted Email.
21 **
22 **                      additional information we need that may not be availble in
23 **                      the content of the log is:
24 **                      - FQDN of the client.
25 **                      - Time of arrival of the log
26 **                      - Service that created the log.
27 **
28 **                      If the log arrives in an Email, these items probably are
29 **                      available in the mail header. Otherwise, they have to be
30 **                      provided as command line arguments.
31 **
32 **                      Command line arguments:
33 **                      -c <name>        Configuration name (default = gnucomo).
34 **                      -d <date>        Date and time of log arrival.
35 **                      -h <hostname>    FQDN of the client.
36 **                      -s <service>     Service that created the log.
37 **                      -T               Test mode. Do not alter the database.
38 **                      -v               Verbose (debug) output.
39 **                      -V               Print version and exit.
40 **
41 **  EXPORTED OBJECTS : 
42 **  LOCAL    OBJECTS : 
43 **  MODULES  USED    :
44 ***************************************************************************
45 **  ADMINISTRATIVE INFORMATION *
46 ********************************
47 **      ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
48 **      CREATION DATE   : Aug 29, 2002
49 **      LAST UPDATE     : Sep 30, 2002
50 **      MODIFICATIONS   : 
51 **************************************************************************/
52
53 /*****************************
54    $Log: gcm_input.cpp,v $
55    Revision 1.1  2002-10-05 10:25:49  arjen
56    Creation of gcm_input and a first approach to a web interface
57
58 *****************************/
59
60 static const char *RCSID = "$Id: gcm_input.cpp,v 1.1 2002-10-05 10:25:49 arjen Exp $";
61
62 #include <getopt.h>
63
64 #include "message.h"
65
66 bool verbose = false;
67 bool testmode = false;
68
69 static char *Version = "gcm_input version 0.0.3 - Sep 30, 2002";
70
71
72 /*=========================================================================
73 **  NAME           : main
74 **  SYNOPSIS       : int main(int argc, char *argv[])
75 **  PARAMETERS     : 
76 **  RETURN VALUE   : 
77 **
78 **  DESCRIPTION    : Parse command line arguments and establish a connection
79 **                   to the database.
80 **                   When we have a database connection, parse the log file
81 **                   from stdin.
82 **
83 **  VARS USED      :
84 **  VARS CHANGED   :
85 **  FUNCTIONS USED :
86 **  SEE ALSO       :
87 **  LAST MODIFIED  : Sep 30, 2002
88 **=========================================================================
89 */
90
91 int main(int argc, char *argv[])
92 {
93    const char *usage = "Usage: gcm_input [-c configname] [-h hostname] [-d date]"
94                        " [-s service] [-T] [-v] [-V]\n";
95
96    gnucomo_config    cfg;
97    char             *config_name = "gnucomo";
98
99    /*   Parse command line arguments */
100
101    UTC    arrival = Now();
102    String  hostname(""), service("");
103    int     option;
104
105  
106    while ((option = getopt(argc, argv, "c:h:d:s:TvV")) != -1)
107    {
108       switch (option)
109       {
110       case 'c':
111          config_name = optarg;
112          break;
113
114       case 'h':
115          hostname = optarg;
116          break;
117
118       case 'd':
119          arrival = String(optarg);
120          if (!arrival.proper())
121          {
122             cerr << "gcm_input: Invalid date string: " << optarg << ".\n";
123             exit(1);
124          }
125          break;
126
127       case 's':
128          service = optarg;
129          break;
130
131       case 'T':
132          testmode = true;
133          break;
134
135       case 'v':
136          verbose = true;
137          break;
138
139       case 'V':
140          cout << Version << "\n";
141          exit(0);
142
143       case '?':
144       case ':':
145          cerr << usage;
146          exit(1);
147       }
148    }
149
150    if (verbose)
151    {
152       cout << "Hostname = " << hostname;
153       cout << " Arrival = " << arrival;
154       cout << " Service = " << service << "\n";
155    }
156
157    /*  Get the configuration file */
158
159    if (!cfg.read(config_name))
160    {
161       cerr << "Can not read Gnucomo configuration file for " << config_name << ".\n";
162       exit(1);
163    }
164
165    if (verbose)
166    {
167       cout << "Config OK.\n";
168    }
169
170    /*  Try to connect to the database */
171
172    gnucomo_database db(&cfg);
173
174    client_message  msg(&cin, db);
175
176    if (msg.classify(hostname, arrival, service) > 0.9)
177    {
178       msg.enter();
179    }
180
181    return 0;
182 }
183