Statistics
| Branch: | Tag: | Revision:

one / include / Host.h @ 6329d8b3

History | View | Annotate | Download (9.97 KB)

1
/* ------------------------------------------------------------------------ */
2
/* Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org)           */
3
/*                                                                          */
4
/* Licensed under the Apache License, Version 2.0 (the "License"); you may  */
5
/* not use this file except in compliance with the License. You may obtain  */
6
/* a copy of the License at                                                 */
7
/*                                                                          */
8
/* http://www.apache.org/licenses/LICENSE-2.0                               */
9
/*                                                                          */
10
/* Unless required by applicable law or agreed to in writing, software      */
11
/* distributed under the License is distributed on an "AS IS" BASIS,        */
12
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
13
/* See the License for the specific language governing permissions and      */
14
/* limitations under the License.                                           */
15
/* -------------------------------------------------------------------------*/
16

    
17
#ifndef HOST_H_
18
#define HOST_H_
19

    
20
#include "PoolSQL.h"
21
#include "HostTemplate.h"
22
#include "HostShare.h"
23

    
24
using namespace std;
25

    
26
/**
27
 *  The Host class.
28
 */
29
class Host : public PoolObjectSQL
30
{
31
public:
32

    
33
    // ----------------------------------------------------------------------
34
    // Host States
35
    // ----------------------------------------------------------------------
36

    
37
    enum HostState
38
    {
39
        INIT       = 0, /**< Initial state for enabled hosts. */
40
        MONITORING = 1, /**< The host is being monitored. */
41
        MONITORED  = 2, /**< The host has been successfully monitored. */
42
        ERROR      = 3, /**< An error ocurrer while monitoring the host. */
43
        DISABLED   = 4  /**< The host is disabled won't be monitored. */
44
    };
45

    
46
    /**
47
     *  Function to write a Host on an output stream
48
     */
49
     friend ostream& operator<<(ostream& os, Host& h);
50

    
51
    /**
52
     * Function to print the Host object into a string in XML format
53
     *  @param xml the resulting XML string
54
     *  @return a reference to the generated string
55
     */
56
    string& to_xml(string& xml) const;
57

    
58
    /**
59
     *  Rebuilds the object from an xml formatted string
60
     *    @param xml_str The xml-formatted string
61
     *
62
     *    @return 0 on success, -1 otherwise
63
     */
64
    int from_xml(const string &xml_str);
65

    
66
    /**
67
     *  Check if the host is enabled
68
     *    @return true if the host is enabled
69
     */
70
     bool isEnabled() const
71
     {
72
        return state != DISABLED;
73
     }
74

    
75
    /**
76
     *  Updates the Host's last_monitored time stamp.
77
     *    @param success if the monitored action was successfully performed
78
     */
79
    void touch(bool success)
80
    {
81
        last_monitored = time(0);
82

    
83
        if ( state != DISABLED) //Don't change the state is host is disabled
84
        {
85
            if (success == true)
86
            {
87
                state = MONITORED;
88
            }
89
            else
90
            {
91
                state = ERROR;
92
            }
93
        }
94
    };
95

    
96
    /**
97
     *   Disables the current host, it will not be monitored nor used by the
98
     *   scheduler
99
     */
100
    void disable()
101
    {
102
        state = DISABLED;
103
    };
104

    
105
    /**
106
     *   Enables the current host, it will be monitored and could be used by
107
     *   the scheduler
108
     */
109
    void enable()
110
    {
111
        state = INIT;
112
    };
113

    
114
    /** Update host counters and update the whole host on the DB
115
     *    @param parse_str string with values to be parsed
116
     *    @return 0 on success
117
     **/
118
    int update_info(string &parse_str);
119

    
120
    /**
121
     * Retrives host state
122
     *    @return HostState code number
123
     */
124
    HostState get_state() const
125
    {
126
        return state;
127
    };
128

    
129
    /**
130
     * Retrives VMM mad name
131
     *    @return string vmm mad name
132
     */
133
    const string& get_vmm_mad() const
134
    {
135
        return vmm_mad_name;
136
    };
137

    
138
    /**
139
     * Retrives TM mad name
140
     *    @return string tm mad name
141
     */
142
    const string& get_tm_mad() const
143
    {
144
        return tm_mad_name;
145
    };
146

    
147
    /**
148
     * Retrives IM mad name
149
     *    @return string im mad name
150
     */
151
    const string& get_im_mad() const
152
    {
153
        return im_mad_name;
154
    };
155

    
156
    /**
157
     * Sets host state
158
     *    @param HostState state that applies to this host
159
     */
160
    void set_state(HostState state)
161
    {
162
        this->state = state;
163
    };
164

    
165
    /**
166
     * Retrives last time the host was monitored
167
     *    @return time_t last monitored time
168
     */
169
    time_t get_last_monitored() const
170
    {
171
        return last_monitored;
172
    };
173

    
174
    /**
175
     *  Sets the cluster for this host
176
     *    @param cluster_id Cluster's oid
177
     *    @return 0 on success
178
     */
179
    int set_cluster(int cluster_id)
180
    {
181
        gid = cluster_id;
182
        return 0;
183
    };
184

    
185
    // ------------------------------------------------------------------------
186
    // Share functions
187
    // ------------------------------------------------------------------------
188

    
189
    /**
190
     *
191
     *
192
     */
193
    int get_share_running_vms()
194
    {
195
        return host_share.running_vms;
196
    }
197

    
198
    int get_share_disk_usage()
199
    {
200
        return host_share.disk_usage;
201
    }
202

    
203
    int get_share_mem_usage()
204
    {
205
        return host_share.mem_usage;
206
    }
207

    
208
    int get_share_cpu_usage()
209
    {
210
        return host_share.cpu_usage;
211
    }
212

    
213
    int get_share_max_disk()
214
    {
215
        return host_share.max_disk;
216
    }
217

    
218
    int get_share_max_mem()
219
    {
220
        return host_share.max_mem;
221
    }
222

    
223
    int get_share_max_cpu()
224
    {
225
        return host_share.max_cpu;
226
    }
227

    
228
    int get_share_free_disk()
229
    {
230
        return host_share.free_disk;
231
    }
232

    
233
    int get_share_free_mem()
234
    {
235
        return host_share.free_mem;
236
    }
237

    
238
    int get_share_free_cpu()
239
    {
240
        return host_share.free_cpu;
241
    }
242

    
243
    int get_share_used_disk()
244
    {
245
        return host_share.used_disk;
246
    }
247

    
248
    int get_share_used_mem()
249
    {
250
        return host_share.used_mem;
251
    }
252

    
253
    int get_share_used_cpu()
254
    {
255
        return host_share.used_cpu;
256
    }
257

    
258
    /**
259
     *  Adds a new VM to the given share by icrementing the cpu,mem and disk
260
     *  counters
261
     *    @param cpu needed by the VM (percentage)
262
     *    @param mem needed by the VM (in Kb)
263
     *    @param disk needed by the VM
264
     *    @return 0 on success
265
     */
266
    void add_capacity(int cpu, int mem, int disk)
267
    {
268
        host_share.add(cpu,mem,disk);
269
    };
270

    
271
    /**
272
     *  Deletes a new VM from the given share by decrementing the cpu,mem and
273
     *  disk counters
274
     *    @param cpu useded by the VM (percentage)
275
     *    @param mem used by the VM (in Kb)
276
     *    @param disk used by the VM
277
     *    @return 0 on success
278
     */
279
    void del_capacity(int cpu, int mem, int disk)
280
    {
281
        host_share.del(cpu,mem,disk);
282
    };
283

    
284
    /**
285
     *  Tests whether a new VM can be hosted by the host or not
286
     *    @param cpu needed by the VM (percentage)
287
     *    @param mem needed by the VM (in Kb)
288
     *    @param disk needed by the VM
289
     *    @return true if the share can host the VM
290
     */
291
    bool test_capacity(int cpu, int mem, int disk)
292
    {
293
        return host_share.test(cpu,mem,disk);
294
    }
295

    
296
private:
297

    
298
    // -------------------------------------------------------------------------
299
    // Friends
300
    // -------------------------------------------------------------------------
301

    
302
    friend class HostPool;
303

    
304
    // -------------------------------------------------------------------------
305
    // Host Description
306
    // -------------------------------------------------------------------------
307
    /**
308
     *  The state of the Host
309
     */
310
    HostState   state;
311

    
312
        /**
313
     *  Name of the IM driver used to monitor this host
314
     */
315
        string      im_mad_name;
316

    
317
        /**
318
     *  Name of the VM driver used to execute VMs in this host
319
     */
320
        string      vmm_mad_name;
321

    
322
        /**
323
     *  Name of the TM driver used to transfer file to and from this host
324
     */
325
        string      tm_mad_name;
326

    
327
        /**
328
     *  If Host State= MONITORED  last time it got fully monitored or 1 Jan 1970
329
     *     Host State = MONITORING last time it got a signal to be monitored
330
     */
331
    time_t      last_monitored;
332

    
333
    // -------------------------------------------------------------------------
334
    //  Host Attributes
335
    // -------------------------------------------------------------------------
336
    /**
337
     *  The Share represents the logical capacity associated with the host
338
     */
339
    HostShare       host_share;
340

    
341
    // *************************************************************************
342
    // Constructor
343
    // *************************************************************************
344

    
345
    Host(int           id=-1,
346
         int           cluster_id=-1,
347
         const string& hostname="",
348
         const string& im_mad_name="",
349
         const string& vmm_mad_name="",
350
         const string& tm_mad_name="");
351

    
352
    virtual ~Host();
353

    
354
    // *************************************************************************
355
    // DataBase implementation (Private)
356
    // *************************************************************************
357

    
358
    static const char * db_names;
359

    
360
    static const char * db_bootstrap;
361

    
362
    static const char * table;
363

    
364
    /**
365
     *  Execute an INSERT or REPLACE Sql query.
366
     *    @param db The SQL DB
367
     *    @param replace Execute an INSERT or a REPLACE
368
     *    @return 0 one success
369
    */
370
    int insert_replace(SqlDB *db, bool replace);
371

    
372
    /**
373
     *  Bootstraps the database table(s) associated to the Host
374
     */
375
    static void bootstrap(SqlDB * db)
376
    {
377
        ostringstream oss_host(Host::db_bootstrap);
378

    
379
        db->exec(oss_host);
380
    };
381

    
382
    /**
383
     *  Writes the Host and its associated HostShares in the database.
384
     *    @param db pointer to the db
385
     *    @return 0 on success
386
     */
387
    int insert(SqlDB *db, string& error_str);
388

    
389
    /**
390
     *  Writes/updates the Hosts data fields in the database.
391
     *    @param db pointer to the db
392
     *    @return 0 on success
393
     */
394
    int update(SqlDB *db);
395
};
396

    
397
#endif /*HOST_H_*/