Statistics
| Branch: | Tag: | Revision:

one / include / Host.h @ ea876146

History | View | Annotate | Download (16.9 KB)

1
/* ------------------------------------------------------------------------ */
2
/* Copyright 2002-2015, OpenNebula Project (OpenNebula.org), C12G Labs      */
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
#include "Clusterable.h"
24
#include "ObjectCollection.h"
25
#include "NebulaLog.h"
26
#include "NebulaUtil.h"
27

    
28
using namespace std;
29

    
30
/**
31
 *  The Host class.
32
 */
33
class Host : public PoolObjectSQL, public Clusterable
34
{
35
public:
36

    
37
    // ----------------------------------------------------------------------
38
    // Host States
39
    // ----------------------------------------------------------------------
40

    
41
    enum HostState
42
    {
43
        INIT                 = 0, /**< Initial state for enabled hosts. */
44
        MONITORING_MONITORED = 1, /**< Monitoring the host (from monitored). */
45
        MONITORED            = 2, /**< The host has been successfully monitored. */
46
        ERROR                = 3, /**< An error ocurrer while monitoring the host. */
47
        DISABLED             = 4, /**< The host is disabled won't be monitored. */
48
        MONITORING_ERROR     = 5, /**< Monitoring the host (from error). */
49
        MONITORING_INIT      = 6, /**< Monitoring the host (from init). */
50
        MONITORING_DISABLED  = 7  /**< Monitoring the host (from disabled). */
51
    };
52

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

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

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

    
77
    /**
78
     *  Check if the host is being monitored
79
     *    @return true if the host is enabled
80
     */
81
     bool isMonitoring() const
82
     {
83
        return ((state == MONITORING_ERROR) ||
84
                (state == MONITORING_MONITORED)||
85
                (state == MONITORING_INIT)||
86
                (state == MONITORING_DISABLED));
87
     }
88

    
89
     /**
90
      *  Checks if the host is a remote public cloud
91
      *    @return true if the host is a remote public cloud
92
      */
93
     bool is_public_cloud() const;
94

    
95
    /**
96
     *   Disables the current host, it will not be monitored nor used by the
97
     *   scheduler
98
     */
99
    void disable();
100

    
101
    /**
102
     *   Enables the current host, it will be monitored and could be used by
103
     *   the scheduler
104
     */
105
    void enable()
106
    {
107
        state = INIT;
108
    };
109

    
110
    /**
111
     *  Sets the host in error
112
     */
113
     void set_error()
114
     {
115
        state = ERROR;
116
     }
117

    
118
     /**
119
      *  Updates the Host's last_monitored time stamp.
120
      *    @param success if the monitored action was successfully performed
121
      */
122
    void touch(bool success)
123
    {
124
        last_monitored = time(0);
125

    
126
        switch (state)
127
        {
128
            case DISABLED:
129
            case MONITORING_DISABLED:
130
                state = DISABLED;
131
            break;
132

    
133
            case INIT:
134
            case ERROR:
135
            case MONITORED:
136
            case MONITORING_ERROR:
137
            case MONITORING_INIT:
138
            case MONITORING_MONITORED:
139
                if (success == true)
140
                {
141
                    state = MONITORED;
142
                }
143
                else
144
                {
145
                    state = ERROR;
146
                }
147
            break;
148
            default:
149
            break;
150
        }
151
    };
152

    
153
    /**
154
     * Update host after a successful monitor. It modifies counters, state
155
     * and template attributes
156
     *    @param parse_str string with values to be parsed
157
     *    @param with_vm_info if monitoring contains VM information
158
     *    @param lost set of VMs that should be in the host and were not found
159
     *    @param found VMs running in the host (as expected) and info.
160
     *    @param reserved_cpu from cluster defaults
161
     *    @param reserved_mem from cluster defaults
162
     *    @return 0 on success
163
     **/
164
    int update_info(Template        &tmpl,
165
                    bool            &with_vm_info,
166
                    set<int>        &lost,
167
                    map<int,string> &found,
168
                    set<int>        &found_twice,
169
                    const set<int>  &non_shared_ds,
170
                    long long       reserved_cpu,
171
                    long long       reserved_mem);
172
    /**
173
     * Extracts the DS attributes from the given template
174
     * @param parse_str string with values to be parsed
175
     * @param ds map of DS monitoring information
176
     * @param template object parsed from parse_str
177
     *
178
     * @return 0 on success
179
     */
180
    int extract_ds_info(
181
            string          &parse_str,
182
            Template        &tmpl,
183
            map<int, const VectorAttribute*> &ds);
184

    
185
    /**
186
     * Update host after a failed monitor. It state
187
     * and template attributes
188
     *    @param message from the driver
189
     *    @param vm_ids running on the host
190
     */
191
    void error_info(const string& message, set<int> &vm_ids);
192

    
193
    /**
194
     * Inserts the last monitoring, and deletes old monitoring entries.
195
     *
196
     * @param db pointer to the db
197
     * @return 0 on success
198
     */
199
    int update_monitoring(SqlDB * db);
200

    
201
    /**
202
     * Retrieves host state
203
     *    @return HostState code number
204
     */
205
    HostState get_state() const
206
    {
207
        return state;
208
    };
209

    
210
    /**
211
     * Retrieves VMM mad name
212
     *    @return string vmm mad name
213
     */
214
    const string& get_vmm_mad() const
215
    {
216
        return vmm_mad_name;
217
    };
218

    
219
    /**
220
     * Retrieves VNM mad name
221
     *    @return string vnm mad name
222
     */
223
    const string& get_vnm_mad() const
224
    {
225
        return vnm_mad_name;
226
    };
227

    
228
    /**
229
     * Retrieves IM mad name
230
     *    @return string im mad name
231
     */
232
    const string& get_im_mad() const
233
    {
234
        return im_mad_name;
235
    };
236

    
237
    /**
238
     * Sets the corresponding monitoring state based on the actual host state
239
     */
240
    void set_monitoring_state()
241
    {
242
        last_monitored = time(0); //Needed to expire this monitor action
243

    
244
        switch (state)
245
        {
246
            case ERROR:
247
                state = MONITORING_ERROR;
248
            break;
249

    
250
            case MONITORED:
251
                state = MONITORING_MONITORED;
252
            break;
253

    
254
            case INIT:
255
                state = MONITORING_INIT;
256
            break;
257

    
258
            case DISABLED:
259
                state = MONITORING_DISABLED;
260
            break;
261

    
262
            default:
263
            break;
264
        }
265
    };
266

    
267
    /**
268
     * Retrieves last time the host was monitored
269
     *    @return time_t last monitored time
270
     */
271
    time_t get_last_monitored() const
272
    {
273
        return last_monitored;
274
    };
275

    
276
    /**
277
     *  Get the reserved capacity for this host. Parameters will be only updated
278
     *  if values are defined in the host. Reserved capacity will be subtracted
279
     *  from the Host total capacity.
280
     *    @param cpu reserved cpu (in percentage)
281
     *    @param mem reserved mem (in KB)
282
     */
283
    void get_reserved_capacity(long long &cpu, long long& mem)
284
    {
285
        long long tcpu;
286
        long long tmem;
287

    
288
        if (get_template_attribute("RESERVED_CPU", tcpu))
289
        {
290
            cpu = tcpu;
291
        }
292
        else
293
        {
294
            replace_template_attribute("RESERVED_CPU", "");
295
        }
296

    
297
        if (get_template_attribute("RESERVED_MEM", tmem))
298
        {
299
            mem = tmem;
300
        }
301
        else
302
        {
303
            replace_template_attribute("RESERVED_MEM", "");
304
        }
305
    }
306

    
307
    // -------------------------------------------------------------------------
308
    // Share functions. Returns the value associated with each host share
309
    // metric
310
    // -------------------------------------------------------------------------
311
    long long get_share_running_vms()
312
    {
313
        return host_share.running_vms;
314
    }
315

    
316
    long long get_share_disk_usage()
317
    {
318
        return host_share.disk_usage;
319
    }
320

    
321
    long long get_share_mem_usage()
322
    {
323
        return host_share.mem_usage;
324
    }
325

    
326
    long long get_share_cpu_usage()
327
    {
328
        return host_share.cpu_usage;
329
    }
330

    
331
    long long get_share_max_disk()
332
    {
333
        return host_share.max_disk;
334
    }
335

    
336
    long long get_share_max_mem()
337
    {
338
        return host_share.max_mem;
339
    }
340

    
341
    long long get_share_max_cpu()
342
    {
343
        return host_share.max_cpu;
344
    }
345

    
346
    long long get_share_free_disk()
347
    {
348
        return host_share.free_disk;
349
    }
350

    
351
    long long get_share_free_mem()
352
    {
353
        return host_share.free_mem;
354
    }
355

    
356
    long long get_share_free_cpu()
357
    {
358
        return host_share.free_cpu;
359
    }
360

    
361
    long long get_share_used_disk()
362
    {
363
        return host_share.used_disk;
364
    }
365

    
366
    long long get_share_used_mem()
367
    {
368
        return host_share.used_mem;
369
    }
370

    
371
    long long get_share_used_cpu()
372
    {
373
        return host_share.used_cpu;
374
    }
375

    
376
    /**
377
     *  Adds a new VM to the given share by incrementing the cpu, mem and disk
378
     *  counters
379
     *    @param vm_id id of the vm to add to the host
380
     *    @param cpu needed by the VM (percentage)
381
     *    @param mem needed by the VM (in KB)
382
     *    @param disk needed by the VM
383
     *    @return 0 on success
384
     */
385
    void add_capacity(int vm_id, long long cpu, long long mem, long long disk)
386
    {
387
        if ( vm_collection.add_collection_id(vm_id) == 0 )
388
        {
389
            host_share.add(cpu,mem,disk);
390
        }
391
        else
392
        {
393
            ostringstream oss;
394
            oss << "Trying to add VM " << vm_id
395
                << ", that it is already associated to host " << oid << ".";
396

    
397
            NebulaLog::log("ONE", Log::ERROR, oss);
398
        }
399
    };
400

    
401
    /**
402
     *  Deletes a new VM from the given share by decrementing the cpu,mem and
403
     *  disk counters
404
     *    @param vm_id id of the vm to delete from the host
405
     *    @param cpu used by the VM (percentage)
406
     *    @param mem used by the VM (in KB)
407
     *    @param disk used by the VM
408
     *    @return 0 on success
409
     */
410
    void del_capacity(int vm_id, long long cpu, long long mem, long long disk)
411
    {
412
        if ( vm_collection.del_collection_id(vm_id) == 0 )
413
        {
414
            host_share.del(cpu,mem,disk);
415
        }
416
        else
417
        {
418
            ostringstream oss;
419
            oss << "Trying to remove VM " << vm_id
420
                << ", that it is not associated to host " << oid << ".";
421

    
422
            NebulaLog::log("ONE", Log::ERROR, oss);
423
        }
424
    };
425

    
426
    /**
427
     *  Updates the capacity used in a host when a VM is resized
428
     *  counters
429
     *    @param cpu increment of cpu requested by the VM
430
     *    @param mem increment of memory requested by the VM
431
     *    @param disk not used
432
     *    @return 0 on success
433
     */
434
    void update_capacity(int cpu, int mem, int disk)
435
    {
436
        host_share.update(cpu,mem,disk);
437
    };
438

    
439
    /**
440
     *  Tests whether a new VM can be hosted by the host or not
441
     *    @param cpu needed by the VM (percentage)
442
     *    @param mem needed by the VM (in Kb)
443
     *    @param disk needed by the VM
444
     *    @return true if the share can host the VM
445
     */
446
    bool test_capacity(long long cpu, long long mem, long long disk)
447
    {
448
        return host_share.test(cpu, mem, disk);
449
    }
450

    
451
    /**
452
     *  Returns a copy of the VM IDs set
453
     */
454
    set<int> get_vm_ids()
455
    {
456
        return vm_collection.get_collection_copy();
457
    }
458

    
459
    /**
460
     *  Factory method for host templates
461
     */
462
    Template * get_new_template() const
463
    {
464
        return new HostTemplate;
465
    }
466

    
467

    
468
    /**
469
     *  Executed after an update operation to process the new template
470
     *    - encrypt VCENTER_PASSWORD attribute.
471
     */
472
    int post_update_template(string& error);
473

    
474
private:
475

    
476
    // -------------------------------------------------------------------------
477
    // Friends
478
    // -------------------------------------------------------------------------
479

    
480
    friend class HostPool;
481

    
482
    // -------------------------------------------------------------------------
483
    // Host Description
484
    // -------------------------------------------------------------------------
485
    /**
486
     *  The state of the Host
487
     */
488
    HostState   state;
489

    
490
        /**
491
     *  Name of the IM driver used to monitor this host
492
     */
493
        string      im_mad_name;
494

    
495
        /**
496
     *  Name of the VM driver used to execute VMs in this host
497
     */
498
        string      vmm_mad_name;
499

    
500
    /**
501
     *  Name of the VN driver used to manage networking in this host
502
     */
503
    string      vnm_mad_name;
504

    
505
        /**
506
     *  If Host State = MONITORED last time it got fully monitored or 1 Jan 1970
507
     *     Host State = MONITORING* last time it got a signal to be monitored
508
     */
509
    time_t      last_monitored;
510

    
511
    // -------------------------------------------------------------------------
512
    //  Host Attributes
513
    // -------------------------------------------------------------------------
514
    /**
515
     *  The Share represents the logical capacity associated with the host
516
     */
517
    HostShare       host_share;
518

    
519
    /**
520
     * Tmp set of lost VM IDs. Used to give lost VMs one grace cycle, in case
521
     * they reappear.
522
     */
523
    set<int>        tmp_lost_vms;
524

    
525
    /**
526
     * Tmp set of zombie VM IDs. Used to give zombie VMs one grace cycle, in
527
     * case they are cleaned.
528
     */
529
    set<int>        tmp_zombie_vms;
530

    
531
    /**
532
     * Tmp set of found VM IDs. Used to give recovered poweroff VMs one
533
     * grace cycle, in case they reappear in outdated poll info.
534
     */
535
    set<int>        tmp_found_vms;
536

    
537
    // -------------------------------------------------------------------------
538
    //  VM Collection
539
    // -------------------------------------------------------------------------
540
    /**
541
     *  Stores a collection with the VMs running in the host
542
     */
543
    ObjectCollection vm_collection;
544

    
545

    
546
    // *************************************************************************
547
    // Constructor
548
    // *************************************************************************
549

    
550
    Host(int           id,
551
         const string& hostname,
552
         const string& im_mad_name,
553
         const string& vmm_mad_name,
554
         const string& vnm_mad_name,
555
         int           cluster_id,
556
         const string& cluster_name);
557

    
558
    virtual ~Host();
559

    
560
    // *************************************************************************
561
    // DataBase implementation (Private)
562
    // *************************************************************************
563

    
564
    static const char * db_names;
565

    
566
    static const char * db_bootstrap;
567

    
568
    static const char * table;
569

    
570
    static const char * monit_db_names;
571

    
572
    static const char * monit_db_bootstrap;
573

    
574
    static const char * monit_table;
575

    
576
    /**
577
     *  Execute an INSERT or REPLACE Sql query.
578
     *    @param db The SQL DB
579
     *    @param replace Execute an INSERT or a REPLACE
580
     *    @param error_str Returns the error reason, if any
581
     *    @return 0 one success
582
     */
583
    int insert_replace(SqlDB *db, bool replace, string& error_str);
584

    
585
    /**
586
     *  Bootstraps the database table(s) associated to the Host
587
     *    @return 0 on success
588
     */
589
    static int bootstrap(SqlDB * db)
590
    {
591
        int rc;
592

    
593
        ostringstream oss_host(Host::db_bootstrap);
594
        ostringstream oss_monit(Host::monit_db_bootstrap);
595

    
596
        rc =  db->exec(oss_host);
597
        rc += db->exec(oss_monit);
598

    
599
        return rc;
600
    };
601

    
602
    /**
603
     *  Writes the Host and its associated HostShares in the database.
604
     *    @param db pointer to the db
605
     *    @return 0 on success
606
     */
607
    int insert(SqlDB *db, string& error_str)
608
    {
609
        return insert_replace(db, false, error_str);
610
    };
611

    
612
    /**
613
     *  Writes/updates the Hosts data fields in the database.
614
     *    @param db pointer to the db
615
     *    @return 0 on success
616
     */
617
    int update(SqlDB *db)
618
    {
619
        string error_str;
620
        return insert_replace(db, true, error_str);
621
    };
622
};
623

    
624
#endif /*HOST_H_*/