Revision 14d30d82

View differences:

include/Host.h
114 114
    int update_info(string &parse_str);
115 115

  
116 116
    /**
117
     * Inserts the last monitoring, and deletes old monitoring entries.
118
     *
119
     * @param db pointer to the db
120
     * @return 0 on success
121
     */
122
    int update_monitoring(SqlDB * db);
123

  
124
    /**
125
     * Deletes all monitoring entries.
126
     *
127
     * @param db pointer to the db
128
     * @return 0 on success
129
     */
130
    int clean_monitoring(SqlDB * db);
131

  
132
    /**
117 133
     * Retrives host state
118 134
     *    @return HostState code number
119 135
     */
......
355 371

  
356 372
    static const char * table;
357 373

  
374
    static const char * monit_db_names;
375

  
376
    static const char * monit_db_bootstrap;
377

  
378
    static const char * monit_table;
379

  
358 380
    /**
359 381
     *  Execute an INSERT or REPLACE Sql query.
360 382
     *    @param db The SQL DB
......
370 392
     */
371 393
    static int bootstrap(SqlDB * db)
372 394
    {
395
        int rc;
396

  
373 397
        ostringstream oss_host(Host::db_bootstrap);
398
        ostringstream oss_monit(Host::monit_db_bootstrap);
399

  
400
        rc =  db->exec(oss_host);
401
        rc += db->exec(oss_monit);
374 402

  
375
        return db->exec(oss_host);
403
        return rc;
376 404
    };
377 405

  
378 406
    /**
include/HostPool.h
197 197
        return PoolSQL::search(oids, Host::table, where);
198 198
    };
199 199

  
200
    /**
201
     *  Dumps the host monitoring information entries in XML format. A filter
202
     *  can be also added to the query.
203
     *
204
     *  @param oss the output stream to dump the pool contents
205
     *  @param where filter for the objects, defaults to all
206
     *
207
     *  @return 0 on success
208
     */
209
    int dump_monitoring(ostringstream& oss,
210
                        const string&  where);
211

  
212
    /**
213
     * Inserts the last monitoring, and deletes old monitoring entries for this
214
     * host
215
     *
216
     * @param host pointer to the host object
217
     * @return 0 on success
218
     */
219
    int update_monitoring(Host * host)
220
    {
221
        if ( _host_monitoring_history <= 0 )
222
        {
223
            return 0;
224
        }
225

  
226
        return host->update_monitoring(db);
227
    };
228

  
229
    /**
230
     * Deletes all monitoring entries for this host
231
     *
232
     * @param host pointer to the virtual machine object
233
     * @return 0 on success
234
     */
235
    int clean_monitoring(Host * host)
236
    {
237
        return host->clean_monitoring(db);
238
    };
239

  
240
    /**
241
     *  Get the size, in seconds, of the historical monitoring information
242
     *  @return the seconds
243
     */
244
    static const int& host_monitoring_history()
245
    {
246
        return _host_monitoring_history;
247
    };
248

  
200 249
private:
201 250

  
202 251
    /**
......
217 266
     *    @return 0 on success
218 267
     */
219 268
    int discover_cb(void * _map, int num, char **values, char **names);
269

  
270
    /**
271
     * Size, in seconds, of the historical monitoring information
272
     */
273
    static int _host_monitoring_history;
220 274
};
221 275

  
222 276
#endif /*HOST_POOL_H_*/
include/RequestManagerHost.h
68 68
                         RequestAttributes& att);
69 69
};
70 70

  
71
/* ------------------------------------------------------------------------- */
72
/* ------------------------------------------------------------------------- */
73

  
74
class HostMonitoring : public RequestManagerHost
75
{
76
public:
77
    HostMonitoring():
78
        RequestManagerHost("HostMonitoring",
79
                            "Returns the host monitoring records",
80
                            "A:si")
81
    {
82
        auth_op = AuthRequest::USE;
83
    };
84

  
85
    ~HostMonitoring(){};
86

  
87
    void request_execute(xmlrpc_c::paramList const& _paramList,
88
                         RequestAttributes& att);
89
};
90

  
71 91
/* -------------------------------------------------------------------------- */
72 92
/* -------------------------------------------------------------------------- */
73 93
/* -------------------------------------------------------------------------- */
include/RequestManagerPoolInfoFilter.h
238 238
/* ------------------------------------------------------------------------- */
239 239
/* ------------------------------------------------------------------------- */
240 240

  
241
class HostPoolMonitoring : public RequestManagerPoolInfoFilter
242
{
243
public:
244

  
245
    HostPoolMonitoring():
246
        RequestManagerPoolInfoFilter("HostPoolMonitoring",
247
                                     "Returns the host monitoring records",
248
                                     "A:s")
249
    {
250
        Nebula& nd  = Nebula::instance();
251
        pool        = nd.get_hpool();
252
        auth_object = PoolObjectSQL::HOST;
253
    };
254

  
255
    ~HostPoolMonitoring(){};
256

  
257
    /* -------------------------------------------------------------------- */
258

  
259
    void request_execute(
260
            xmlrpc_c::paramList const& paramList, RequestAttributes& att);
261
};
262

  
263
/* ------------------------------------------------------------------------- */
264
/* ------------------------------------------------------------------------- */
265

  
241 266
class GroupPoolInfo: public RequestManagerPoolInfoFilter
242 267
{
243 268
public:
include/VirtualMachinePool.h
147 147
     * @param vm pointer to the virtual machine object
148 148
     * @return 0 on success
149 149
     */
150

  
151 150
    int clean_monitoring(
152 151
            VirtualMachine * vm)
153 152
    {
src/host/Host.cc
69 69
    "last_mon_time INTEGER, uid INTEGER, gid INTEGER, owner_u INTEGER, "
70 70
    "group_u INTEGER, other_u INTEGER, UNIQUE(name))";
71 71

  
72

  
73
const char * Host::monit_table = "host_monitoring";
74

  
75
const char * Host::monit_db_names = "hid, last_mon_time, body";
76

  
77
const char * Host::monit_db_bootstrap = "CREATE TABLE IF NOT EXISTS "
78
    "host_monitoring (hid INTEGER, last_mon_time INTEGER, body TEXT, "
79
    "PRIMARY KEY(hid, last_mon_time))";
72 80
/* ------------------------------------------------------------------------ */
73 81
/* ------------------------------------------------------------------------ */
74 82

  
......
153 161
    goto error_generic;
154 162

  
155 163
error_generic:
156
    error_str = "Error inserting Group in DB.";
164
    error_str = "Error inserting Host in DB.";
157 165
error_common:
158 166
    return -1;
159 167
}
......
188 196
    return 0;
189 197
}
190 198

  
199
/* -------------------------------------------------------------------------- */
200
/* -------------------------------------------------------------------------- */
201

  
202
int Host::update_monitoring(SqlDB * db)
203
{
204
    ostringstream   oss;
205
    int             rc;
206

  
207
    string xml_body;
208
    string error_str;
209
    char * sql_xml;
210

  
211
    sql_xml = db->escape_str(to_xml(xml_body).c_str());
212

  
213
    if ( sql_xml == 0 )
214
    {
215
        goto error_body;
216
    }
217

  
218
    if ( validate_xml(sql_xml) != 0 )
219
    {
220
        goto error_xml;
221
    }
222

  
223
    oss << "DELETE FROM " << monit_table
224
        << " WHERE hid=" << oid
225
        << " AND last_mon_time < (" << last_monitored
226
        << " - " << HostPool::host_monitoring_history() << ")";
227

  
228
    db->exec(oss);
229

  
230
    oss.str("");
231
    oss << "INSERT INTO " << monit_table << " ("<< monit_db_names <<") VALUES ("
232
        <<          oid             << ","
233
        <<          last_monitored       << ","
234
        << "'" <<   sql_xml         << "')";
235

  
236
    db->free_str(sql_xml);
237

  
238
    rc = db->exec(oss);
239

  
240
    return rc;
241

  
242
error_xml:
243
    db->free_str(sql_xml);
244

  
245
    error_str = "could not transform the Host to XML.";
246

  
247
    goto error_common;
248

  
249
error_body:
250
    error_str = "could not insert the Host in the DB.";
251

  
252
error_common:
253
    oss.str("");
254
    oss << "Error updating Host monitoring information, " << error_str;
255

  
256
    NebulaLog::log("ONE",Log::ERROR, oss);
257

  
258
    return -1;
259
}
260

  
261
/* -------------------------------------------------------------------------- */
262
/* -------------------------------------------------------------------------- */
263

  
264
int Host::clean_monitoring(SqlDB * db)
265
{
266
    ostringstream   oss;
267
    int             rc;
268

  
269
    oss << "DELETE FROM " << monit_table << " WHERE hid=" << oid;
270

  
271
    rc = db->exec(oss);
272

  
273
    return rc;
274
}
275

  
191 276
/* ************************************************************************ */
192 277
/* Host :: Misc                                                             */
193 278
/* ************************************************************************ */
src/host/HostPool.cc
29 29
/* -------------------------------------------------------------------------- */
30 30
/* -------------------------------------------------------------------------- */
31 31

  
32
int HostPool::_host_monitoring_history;
33

  
34
/* -------------------------------------------------------------------------- */
35
/* -------------------------------------------------------------------------- */
36

  
32 37
HostPool::HostPool(SqlDB*                    db,
33 38
                   vector<const Attribute *> hook_mads,
34 39
                   const string&             hook_location,
35 40
                   const string&             remotes_location,
36
                   int                       host_monitoring_history)   // TODO
41
                   int                       host_monitoring_history)
37 42
                        : PoolSQL(db, Host::table, true)
38 43
{
39 44
    // ------------------ Initialize Hooks for the pool ----------------------
......
143 148

  
144 149
        add_hook(hook);
145 150
    }
151

  
152
    _host_monitoring_history = host_monitoring_history;
146 153
}
147 154

  
148 155
/* -------------------------------------------------------------------------- */
......
290 297

  
291 298
    return rc;
292 299
}
300

  
301
/* -------------------------------------------------------------------------- */
302
/* -------------------------------------------------------------------------- */
303

  
304
int HostPool::dump_monitoring(
305
        ostringstream& oss,
306
        const string&  where)
307
{
308
    ostringstream cmd;
309

  
310
    cmd << "SELECT " << Host::monit_table << ".body FROM " << Host::monit_table
311
        << " INNER JOIN " << Host::table
312
        << " WHERE hid = oid";
313

  
314
    if ( !where.empty() )
315
    {
316
        cmd << " AND " << where;
317
    }
318

  
319
    cmd << " ORDER BY hid, " << Host::monit_table << ".last_mon_time;";
320

  
321
    return PoolSQL::dump(oss, "MONITORING_DATA", cmd);
322
}
src/im/InformationManagerDriver.cc
128 128
        host->touch(true);
129 129

  
130 130
        hpool->update(host);
131
        hpool->update_monitoring(host);
131 132

  
132 133
        host->unlock();
133 134
    }
src/rm/RequestManager.cc
306 306

  
307 307
    // Host Methods
308 308
    xmlrpc_c::methodPtr host_enable(new HostEnable());
309
    xmlrpc_c::methodPtr host_monitoring(new HostMonitoring());
310
    xmlrpc_c::methodPtr host_pool_monitoring(new HostPoolMonitoring());
309 311

  
310 312
    // Image Methods
311 313
    xmlrpc_c::methodPtr image_persistent(new ImagePersistent());
......
373 375
    RequestManagerRegistry.addMethod("one.host.allocate", host_allocate);   
374 376
    RequestManagerRegistry.addMethod("one.host.delete", host_delete);
375 377
    RequestManagerRegistry.addMethod("one.host.info", host_info);
378
    RequestManagerRegistry.addMethod("one.host.monitoring", host_monitoring);
376 379

  
377 380
    RequestManagerRegistry.addMethod("one.hostpool.info", hostpool_info); 
381
    RequestManagerRegistry.addMethod("one.hostpool.monitoring", host_pool_monitoring);
378 382

  
379 383
    /* Group related methods */
380 384
    RequestManagerRegistry.addMethod("one.group.allocate",  group_allocate);
src/rm/RequestManagerHost.cc
67 67
/* -------------------------------------------------------------------------- */
68 68
/* -------------------------------------------------------------------------- */
69 69

  
70
void HostMonitoring::request_execute(
71
        xmlrpc_c::paramList const&  paramList,
72
        RequestAttributes&          att)
73
{
74
    int id  = xmlrpc_c::value_int(paramList.getInt(1));
75

  
76
    ostringstream   oss;
77
    string          where;
78
    int             rc;
79

  
80
    if ( basic_authorization(id, att) == false )
81
    {
82
        return;
83
    }
84

  
85
    oss << "oid = " << id;
86

  
87
    where = oss.str();
88

  
89
    oss.str("");
90

  
91
    rc = (static_cast<HostPool *>(pool))->dump_monitoring(oss, where);
92

  
93
    if ( rc != 0 )
94
    {
95
        failure_response(INTERNAL,request_error("Internal Error",""), att);
96
        return;
97
    }
98

  
99
    success_response(oss.str(), att);
100

  
101
    return;
102
}
103

  
104
/* -------------------------------------------------------------------------- */
105
/* -------------------------------------------------------------------------- */
106

  
src/rm/RequestManagerPoolInfoFilter.cc
163 163

  
164 164
    return;
165 165
}
166

  
166 167
/* ------------------------------------------------------------------------- */
167 168
/* ------------------------------------------------------------------------- */
168 169

  
......
176 177
/* ------------------------------------------------------------------------- */
177 178
/* ------------------------------------------------------------------------- */
178 179

  
180
void HostPoolMonitoring::request_execute(
181
        xmlrpc_c::paramList const& paramList,
182
        RequestAttributes& att)
183
{
184
    ostringstream oss;
185
    string        where;
186
    int           rc;
187

  
188
    where_filter(att, ALL, -1, -1, "", "", where);
189

  
190
    rc = (static_cast<HostPool *>(pool))->dump_monitoring(oss, where);
191

  
192
    if ( rc != 0 )
193
    {
194
        failure_response(INTERNAL,request_error("Internal Error",""), att);
195
        return;
196
    }
197

  
198
    success_response(oss.str(), att);
199

  
200
    return;
201
}
202

  
203
/* ------------------------------------------------------------------------- */
204
/* ------------------------------------------------------------------------- */
205

  
179 206
void GroupPoolInfo::request_execute(
180 207
        xmlrpc_c::paramList const& paramList,
181 208
        RequestAttributes& att)

Also available in: Unified diff