~scarneiro/ubuntu/oneiric/opendrim-lmp-recordlog/fix-for-756108

« back to all changes in this revision

Viewing changes to OpenDRIM_LogEntry/OpenDRIM_LogEntryAccess.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Guillaume BOTTEX
  • Date: 2009-08-19 17:08:36 UTC
  • Revision ID: james.westby@ubuntu.com-20090819170836-4pdxix40ulw5xqds
Tags: upstream-1.1.1
ImportĀ upstreamĀ versionĀ 1.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*###############################################################################
 
2
# Linux Management Providers (LMP), OpenDRIM_RecordLogPackage provider package
 
3
# Copyright (C) 2007 Ilsoo Byun, ETRI <widepis@etri.re.kr, widepis@empal.com>
 
4
#
 
5
# This program is being developed under the "OpenDRIM" project.
 
6
# The "OpenDRIM" project web page: http://opendrim.sourceforge.net
 
7
# The "OpenDRIM" project mailing list: opendrim@googlegroups.com
 
8
#
 
9
# This program is free software; you can redistribute it and/or
 
10
# modify it under the terms of the GNU General Public License
 
11
# as published by the Free Software Foundation; version 2
 
12
# of the License.
 
13
#
 
14
# This program is distributed in the hope that it will be useful,
 
15
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
# GNU General Public License for more details.
 
18
#
 
19
# You should have received a copy of the GNU General Public License
 
20
# along with this program; if not, write to the Free Software
 
21
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
22
#################################################################################
 
23
 
 
24
#################################################################################
 
25
# To contributors, please leave your contact information in this section
 
26
# AND comment your changes in the source code.
 
27
#
 
28
# Modified by 2008 Guillaume BOTTEX, ETRI <guillaumebottex@etri.re.kr, guillaumebottex@gmail.com>
 
29
###############################################################################*/
 
30
 
 
31
#include "OpenDRIM_LogEntryAccess.h"
 
32
#include "../OpenDRIM_RecordLog/OpenDRIM_RecordLogAccess.h"
 
33
#include "CMPIBroking.h"
 
34
 
 
35
#define LOG_TIME_LIMIT 15
 
36
 
 
37
int OpenDRIM_RecordLogPackage_OpenDRIM_LogEntry_load(const CMPIBroker* broker, string& errorMessage) {
 
38
        _E_;
 
39
        _L_;
 
40
        return OK;
 
41
}
 
42
 
 
43
int OpenDRIM_RecordLogPackage_OpenDRIM_LogEntry_unload(string& errorMessage) {
 
44
        _E_;
 
45
        _L_;
 
46
        return OK;
 
47
}
 
48
 
 
49
int OpenDRIM_RecordLogPackage_OpenDRIM_LogEntry_makeInstances(const string& filename, vector<OpenDRIM_LogEntry>& result, const string& discriminant,  string& errorMessage) {
 
50
        _E_;
 
51
        ifstream ifs(filename.c_str());
 
52
        if (ifs.bad()) {
 
53
                ifs.close();
 
54
                errorMessage = "Failed to open the file: "+filename;
 
55
                return FAILED;
 
56
        }
 
57
 
 
58
        time_t timep;
 
59
        time(&timep);
 
60
        struct tm *tp = localtime(&timep);
 
61
 
 
62
        OpenDRIM_LogEntry instance;
 
63
        string line;
 
64
        while (getline(ifs, line)) {
 
65
                if (line.size() < 15)
 
66
                        continue;
 
67
                CF_assert(OpenDRIM_RecordLogPackage_OpenDRIM_LogEntry_populate(instance, filename, line, tp, discriminant, errorMessage));
 
68
                result.push_back(instance);
 
69
        }
 
70
        ifs.close();
 
71
        _L_;
 
72
        return OK;
 
73
}
 
74
 
 
75
 
 
76
int OpenDRIM_RecordLogPackage_OpenDRIM_LogEntry_retrieve(const CMPIBroker* broker, const CMPIContext* ctx, vector<OpenDRIM_LogEntry>& result, const char** properties, string& errorMessage, const string& discriminant) {
 
77
        _E_;
 
78
        vector<OpenDRIM_RecordLog> recordLogs;
 
79
        CF_assert(OpenDRIM_RecordLogPackage_OpenDRIM_RecordLog_retrieve(broker, ctx, recordLogs, NULL, errorMessage, "ein"));
 
80
 
 
81
        for (size_t i=0; i<recordLogs.size(); i++) {
 
82
                string logInstanceID;
 
83
                recordLogs[i].getInstanceID(logInstanceID);
 
84
                CF_assert(OpenDRIM_RecordLogPackage_OpenDRIM_LogEntry_makeInstances(logInstanceID, result, discriminant, errorMessage));
 
85
        }
 
86
        _L_;
 
87
        return OK;
 
88
}
 
89
 
 
90
int OpenDRIM_RecordLogPackage_OpenDRIM_LogEntry_getInstance(const CMPIBroker* broker, const CMPIContext* ctx, OpenDRIM_LogEntry& instance, const char** properties, string& errorMessage) {
 
91
        _E_;
 
92
        if (!instance.InstanceID_isNULL) {
 
93
                string filename = instance.InstanceID.substr(0, instance.InstanceID.find(':'));
 
94
                string subject_timestamp = instance.InstanceID.substr(instance.InstanceID.find(':')+1);
 
95
                vector<OpenDRIM_LogEntry> result;
 
96
 
 
97
                ifstream ifs(filename.c_str());
 
98
                if (ifs.bad()) {
 
99
                        ifs.close();
 
100
                        errorMessage = "Failed to open the file: "+filename;
 
101
                        return FAILED;
 
102
                }
 
103
 
 
104
                time_t timep;
 
105
                time(&timep);
 
106
                struct tm *tp = localtime(&timep);
 
107
 
 
108
                string line;
 
109
                while (getline(ifs, line)) {
 
110
                        if (line.size() < 15)
 
111
                                continue;
 
112
                        string timestamp = CF_logTimeFormat(line.substr(0, LOG_TIME_LIMIT), tp);
 
113
                        if (subject_timestamp == timestamp) {
 
114
                                ifs.close();
 
115
                                CF_assert(OpenDRIM_RecordLogPackage_OpenDRIM_LogEntry_populate(instance, filename, line, tp, "ei", errorMessage));
 
116
                                return OK;
 
117
                        }
 
118
                }
 
119
                ifs.close();
 
120
 
 
121
                errorMessage = "No instance";
 
122
                return NOT_FOUND;
 
123
        } else {
 
124
                errorMessage = "No key property";
 
125
                return NOT_FOUND;
 
126
        }
 
127
        _L_;
 
128
        return OK;
 
129
}
 
130
 
 
131
int OpenDRIM_RecordLogPackage_OpenDRIM_LogEntry_setInstance(const CMPIBroker* broker, const CMPIContext* ctx, const OpenDRIM_LogEntry& newInstance, const OpenDRIM_LogEntry& oldInstance, const char** properties, string& errorMessage) {
 
132
        _E_;
 
133
        _L_;
 
134
        return NOT_SUPPORTED;
 
135
}
 
136
 
 
137
int OpenDRIM_RecordLogPackage_OpenDRIM_LogEntry_createInstance(const CMPIBroker* broker, const CMPIContext* ctx, const OpenDRIM_LogEntry& instance, string& errorMessage) {
 
138
        _E_;
 
139
        _L_;
 
140
        return NOT_SUPPORTED;
 
141
}
 
142
 
 
143
int OpenDRIM_RecordLogPackage_OpenDRIM_LogEntry_deleteInstance(const CMPIBroker* broker, const CMPIContext* ctx, const OpenDRIM_LogEntry& instance, string& errorMessage) {
 
144
        _E_;
 
145
        _L_;
 
146
        return NOT_SUPPORTED;
 
147
}
 
148
 
 
149
int OpenDRIM_RecordLogPackage_OpenDRIM_LogEntry_populate(OpenDRIM_LogEntry& instance, const string& filename, string& line, struct tm* tp, const string& discriminant, string& errorMessage) {
 
150
        _E_;
 
151
 
 
152
        /*
 
153
         * Properties to fill from profile
 
154
         * + Mandatory:
 
155
         * [X] InstanceID                       [KEY]
 
156
         * [X] RecordID
 
157
         * [X] CreationTimestamp
 
158
         * [X] ElementName
 
159
         * + Optional:
 
160
         * [X] LogInstanceID
 
161
         * [X] LogName
 
162
         * [X] RecordData
 
163
         * [X] RecordFormat
 
164
         */
 
165
 
 
166
        string timestamp = CF_logTimeFormat(line.substr(0, LOG_TIME_LIMIT), tp);
 
167
        string id = filename+":"+timestamp;
 
168
        instance.setInstanceID(id);
 
169
        instance.setCreationTimeStamp(timestamp);
 
170
        instance.setRecordData(line);
 
171
        instance.setLogInstanceID(filename);
 
172
        instance.setRecordID(timestamp);
 
173
        instance.setRecordFormat(": string Month uint8 Day uint8 Hour:uint8 Minute:uint8 Second string HostName string Origin: string Message");
 
174
        instance.setLogName(filename);
 
175
        instance.setElementName(id);
 
176
        _L_;
 
177
        return OK;
 
178
}
 
179
 
 
180
// added by Frederic Desmons (2007/11/15)
 
181
int OpenDRIM_RecordLogPackage_OpenDRIM_LogEntry_retrieveInDatetimeBoundaries(const string& log_file, const string& datetime_lower_limit, const string& datetime_upper_limit, vector<OpenDRIM_LogEntry>& log_entries, string& errorMessage) {
 
182
        _E_;
 
183
        ifstream ifs(log_file.c_str());
 
184
        if (ifs.bad()) {
 
185
                ifs.close();
 
186
                errorMessage = "Failed to open the file: "+log_file;
 
187
                return FAILED;
 
188
        }
 
189
 
 
190
        time_t timep;
 
191
        time(&timep);
 
192
        struct tm *tp = localtime(&timep);
 
193
 
 
194
        string line;
 
195
        while (getline(ifs, line)) {
 
196
                if (line.size() < 15)
 
197
                        continue;
 
198
                string timestamp = CF_logTimeFormat(line.substr(0, LOG_TIME_LIMIT), tp);
 
199
                int upper_sign, lower_sign;
 
200
                CF_assert(CF_datetimeCmp(upper_sign, timestamp, datetime_upper_limit, errorMessage));
 
201
                CF_assert(CF_datetimeCmp(lower_sign, timestamp, datetime_lower_limit, errorMessage));
 
202
                if (upper_sign <= 0 && lower_sign >=0) {
 
203
                        OpenDRIM_LogEntry instance;
 
204
                        CF_assert(OpenDRIM_RecordLogPackage_OpenDRIM_LogEntry_populate(instance, log_file, line, tp, "ei", errorMessage));
 
205
                        log_entries.push_back(instance);
 
206
                }
 
207
        }
 
208
        ifs.close();
 
209
        _L_;
 
210
        return OK;
 
211
}