79ceee298d8b733f198f09694f320fcd8f805fc7
[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.8 $
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     : Jul 24, 2003
24 **      MODIFICATIONS   : 
25 **************************************************************************/
26
27 /*****************************
28    $Log: database.cpp,v $
29    Revision 1.8  2003-07-31 15:44:02  arjen
30    Removed debug output.
31
32    Revision 1.7  2003/02/19 12:07:55  arjen
33    Use the SQL function currval() to obtain the identification number
34    of the most recently created notification.
35
36    Revision 1.6  2003/02/05 09:33:42  arjen
37    gnucomo_database::new_notification() retruns the id number of the
38    newly created notification record.
39
40    Revision 1.5  2003/01/20 07:31:42  arjen
41    Removed some debug output.
42
43    Revision 1.4  2003/01/18 08:52:32  arjen
44    New C++ function: gnucomo_database::new_notification()
45
46    Revision 1.3  2002/11/09 08:04:27  arjen
47    Added a reference to the GPL
48
49    Revision 1.2  2002/11/04 10:13:36  arjen
50    Use proper namespace for iostream classes
51
52    Revision 1.1  2002/10/05 10:25:49  arjen
53    Creation of gcm_input and a first approach to a web interface
54
55 *****************************/
56
57 static const char *RCSID = "$Id: database.cpp,v 1.8 2003-07-31 15:44:02 arjen Exp $";
58
59 #include <AXE/date.h>
60
61 #include "database.h"
62
63 /*=========================================================================
64 **  NAME           : gnucomo_database
65 **  SYNOPSIS       : gnucomo_database(gnucomo_config &c);
66 **  PARAMETERS     : 
67 **  RETURN VALUE   : Database constructor. Establishes a connection with
68 **                   the database server.
69 **
70 **  DESCRIPTION    : 
71 **
72 **  VARS USED      :
73 **  VARS CHANGED   :
74 **  FUNCTIONS USED :
75 **  SEE ALSO       :
76 **  LAST MODIFIED  : Jul 24, 2003
77 **=========================================================================
78 */
79
80 gnucomo_database::gnucomo_database(gnucomo_config *c)
81 {
82    cfg = c;
83
84    db = new PgDatabase(cfg->Database());
85
86    if (db->ConnectionBad())
87    {
88       std::cerr << "Can not connect to database: " << db->ErrorMessage();
89    }
90 }
91
92 /*=========================================================================
93 **  NAME           : find_host
94 **  SYNOPSIS       : String gnucomo_database::find_host(String hostname);
95 **  PARAMETERS     : 
96 **  RETURN VALUE   : Find a hostname in the 'object' table of the gnucomo database
97 **                   and return its object id.
98 **                   Return an empty string as objectid if the hostname is
99 **                   not found.
100 **
101 **  DESCRIPTION    : 
102 **
103 **  VARS USED      :
104 **  VARS CHANGED   :
105 **  FUNCTIONS USED :
106 **  SEE ALSO       :
107 **  LAST MODIFIED  : Sep 16, 2002
108 **=========================================================================
109 */
110
111 String gnucomo_database::find_host(const String hostname)
112 {
113    String objectid("");
114    String check_host("select objectid from object where ");
115
116    check_host += "objectname = '";
117    check_host += hostname;
118    check_host += "'";
119
120    if (Query(check_host) > 0)
121    {
122       objectid = String(db->GetValue(0, "objectid"));
123    }
124
125    return objectid;
126 }
127
128 /*
129  *  Create a new notification with an action_user and return the notification id
130  */
131
132 String gnucomo_database::new_notification(String objectid, String issue, String remark)
133 {
134    String qry;
135    UTC    now = Now();
136
137    String insertion;
138    String notif_id("");
139
140    String issueid("");
141
142    std::cout << "Creating notification for " << issue << ": " << remark << "\n";
143
144    qry = "select type_of_issueid, suggested_priority from type_of_issue where name='";
145    qry += issue + "'";
146    if (Query(qry) == 1)
147    {
148       issueid = Field(0, "type_of_issueid");
149       insertion = "insert into notification (objectid, type_of_issueid, timestamp, ";
150       insertion += "   statuscode, priority) values ('";
151       insertion += objectid + "', '";
152       insertion += issueid + "', '" + now.format("%Y-%m-%d %T") + "', 'new', '";
153       insertion += Field(0, "suggested_priority") + "')";
154
155       Query(insertion);
156
157       Query("select currval('notification_notificationid_seq')");
158       notif_id = Field(0, "currval");
159
160       if (notif_id != "")
161       {
162          insertion = "insert into action_user (actionid, username, notificationid,";
163          insertion += "    timestamp, statuscode, remarks) values ('1', 'gnucomo', '";
164          insertion += notif_id + "', '" + now.format("%Y-%m-%d %T") + "', 'new', '";
165          insertion += remark + "')";
166
167          Query(insertion);
168       }
169       else
170       {
171          std::cerr << "Error inserting notification.\n";
172       }
173    }
174    else
175    {
176       std::cerr << "DATABASE ERROR: Type of issue " << issue << " not found.\n";
177    }
178
179    return notif_id;
180 }
181