Removed some debug output.
[gnucomo.git] / src / lib / database.cpp
1
2 /**************************************************************************
3 **  (c) Copyright 2002, Andromeda Technology & Automation
4 ** This is free software; you can redistribute it and/or modify it under the
5 ** terms of the GNU General Public License, see the file COPYING.
6 ***************************************************************************
7 ** MODULE INFORMATION *
8 ***********************
9 **      FILE NAME      : database.cpp
10 **      SYSTEM NAME    : Gnucomo - Gnu Computer Monitoring
11 **      VERSION NUMBER : $Revision: 1.5 $
12 **
13 **  DESCRIPTION      :  Implementation of the gnucomo database classes
14 **
15 **  EXPORTED OBJECTS : 
16 **  LOCAL    OBJECTS : 
17 **  MODULES  USED    :
18 ***************************************************************************
19 **  ADMINISTRATIVE INFORMATION *
20 ********************************
21 **      ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
22 **      CREATION DATE   : Sep 10, 2002
23 **      LAST UPDATE     : Jan 17, 2003
24 **      MODIFICATIONS   : 
25 **************************************************************************/
26
27 /*****************************
28    $Log: database.cpp,v $
29    Revision 1.5  2003-01-20 07:31:42  arjen
30    Removed some debug output.
31
32    Revision 1.4  2003/01/18 08:52:32  arjen
33    New C++ function: gnucomo_database::new_notification()
34
35    Revision 1.3  2002/11/09 08:04:27  arjen
36    Added a reference to the GPL
37
38    Revision 1.2  2002/11/04 10:13:36  arjen
39    Use proper namespace for iostream classes
40
41    Revision 1.1  2002/10/05 10:25:49  arjen
42    Creation of gcm_input and a first approach to a web interface
43
44 *****************************/
45
46 static const char *RCSID = "$Id: database.cpp,v 1.5 2003-01-20 07:31:42 arjen Exp $";
47
48 #include <AXE/date.h>
49
50 #include "database.h"
51
52 extern bool verbose;   /*  Defined in the main application */
53
54 /*=========================================================================
55 **  NAME           : gnucomo_database
56 **  SYNOPSIS       : gnucomo_database(gnucomo_config &c);
57 **  PARAMETERS     : 
58 **  RETURN VALUE   : Database constructor. Establishes a connection with
59 **                   the database server.
60 **
61 **  DESCRIPTION    : 
62 **
63 **  VARS USED      :
64 **  VARS CHANGED   :
65 **  FUNCTIONS USED :
66 **  SEE ALSO       :
67 **  LAST MODIFIED  : Sep 26, 2002
68 **=========================================================================
69 */
70
71 gnucomo_database::gnucomo_database(gnucomo_config *c)
72 {
73    cfg = c;
74
75    if (verbose)
76    {
77       std::cout <<  "Database connection string = " << cfg->Database() << "\n";
78    }
79
80    db = new PgDatabase(cfg->Database());
81
82    if (db->ConnectionBad())
83    {
84       std::cerr << "Can not connect to database: " << db->ErrorMessage();
85    }
86 }
87
88 /*=========================================================================
89 **  NAME           : find_host
90 **  SYNOPSIS       : String gnucomo_database::find_host(String hostname);
91 **  PARAMETERS     : 
92 **  RETURN VALUE   : Find a hostname in the 'object' table of the gnucomo database
93 **                   and return its object id.
94 **                   Return an empty string as objectid if the hostname is
95 **                   not found.
96 **
97 **  DESCRIPTION    : 
98 **
99 **  VARS USED      :
100 **  VARS CHANGED   :
101 **  FUNCTIONS USED :
102 **  SEE ALSO       :
103 **  LAST MODIFIED  : Sep 16, 2002
104 **=========================================================================
105 */
106
107 String gnucomo_database::find_host(const String hostname)
108 {
109    String objectid("");
110    String check_host("select objectid from object where ");
111
112    check_host += "objectname = '";
113    check_host += hostname;
114    check_host += "'";
115
116    if (Query(check_host) > 0)
117    {
118       objectid = String(db->GetValue(0, "objectid"));
119    }
120
121    return objectid;
122 }
123
124 void gnucomo_database::new_notification(String objectid, String issue, String remark)
125 {
126    String qry;
127    UTC    now = Now();
128
129    String insertion;
130    String notif_id;
131
132    String issueid("");
133
134    std::cout << "Creating notification for " << issue << ": " << remark << "\n";
135
136    qry = "select type_of_issueid, suggested_priority from type_of_issue where name='";
137    qry += issue + "'";
138    if (Query(qry) == 1)
139    {
140       issueid = Field(0, "type_of_issueid");
141       insertion = "insert into notification (objectid, type_of_issueid, timestamp, ";
142       insertion += "   statuscode, priority) values ('";
143       insertion += objectid + "', '";
144       insertion += issueid + "', '" + now.format("%Y-%m-%d %T") + "', 'new', '";
145       insertion += Field(0, "suggested_priority") + "')";
146
147       qry = "select notificationid from notification where objectid='";
148       qry += objectid + "' and type_of_issueid = '";
149       qry += issueid + "' order by notificationid";
150
151       Query(insertion);
152       int tuples = Query(qry);
153
154       if (tuples > 0)
155       {
156          notif_id = Field(tuples - 1, "notificationid");
157
158          insertion = "insert into action_user (actionid, username, notificationid,";
159          insertion += "    timestamp, statuscode, remarks) values ('1', 'gnucomo', '";
160          insertion += notif_id + "', '" + now.format("%Y-%m-%d %T") + "', 'new', '";
161          insertion += remark + "')";
162
163          Query(insertion);
164       }
165       else
166       {
167          std::cerr << "Error inserting notification.\n";
168          exit(1);
169       }
170    }
171 }
172