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