diff --git a/include/Host.h b/include/Host.h index 5507cbbb4b..5d9992d5a3 100644 --- a/include/Host.h +++ b/include/Host.h @@ -23,32 +23,20 @@ using namespace std; -extern "C" int host_select_cb (void * _host, - int num, - char ** values, - char ** names); - -extern "C" int host_dump_cb (void * _oss, - int num, - char ** values, - char ** names); -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - /** - * The Host class. It represents a Host... + * The Host class. */ class Host : public PoolObjectSQL { public: - + // ------------------------------------------------------------------------ // Host States // ------------------------------------------------------------------------ enum HostState { - INIT = 0, /**< Initial state for enabled hosts. */ + INIT = 0, /**< Initial state for enabled hosts. */ MONITORING = 1, /**< The host is being monitored. */ MONITORED = 2, /**< The host has been successfully monitored. */ ERROR = 3, /**< An error ocurrer while monitoring the host. */ @@ -63,14 +51,14 @@ public: /** * Function to print the Host object into a string in plain text * @param str the resulting string - * @return a reference to the generated string + * @return a reference to the generated string */ string& to_str(string& str) const; /** * Function to print the Host object into a string in XML format * @param xml the resulting XML string - * @return a reference to the generated string + * @return a reference to the generated string */ string& to_xml(string& xml) const; @@ -91,7 +79,7 @@ public: { return state != DISABLED; } - + /** * Updates the Host's last_monitored time stamp. * @param success if the monitored action was successfully performed @@ -99,7 +87,7 @@ public: void touch(bool success) { last_monitored = time(0); - + if ( state != DISABLED) //Don't change the state is host is disabled { if (success == true) @@ -112,25 +100,25 @@ public: } } }; - + /** - * Disables the current host, it will not be monitored nor used by the + * Disables the current host, it will not be monitored nor used by the * scheduler - */ + */ void disable() { state = DISABLED; }; /** - * Enables the current host, it will be monitored and could be used by + * Enables the current host, it will be monitored and could be used by * the scheduler - */ + */ void enable() { state = INIT; }; - + /** * Returns host host_name * @return host_name Host's hostname @@ -139,7 +127,7 @@ public: { return hostname; }; - + /** Update host counters and update the whole host on the DB * @param parse_str string with values to be parsed * @return 0 on success @@ -147,7 +135,7 @@ public: int update_info(string &parse_str); /** - * + * */ HostState get_state() const { @@ -155,39 +143,39 @@ public: }; /** - * + * */ const string& get_vmm_mad() const { return vmm_mad_name; }; - + /** - * + * */ const string& get_tm_mad() const { return tm_mad_name; }; - + /** - * + * */ const string& get_im_mad() const { return im_mad_name; }; - + /** - * + * */ void set_state(HostState state) { this->state = state; }; - + /** - * + * */ time_t get_last_monitored() const { @@ -196,7 +184,7 @@ public: // ------------------------------------------------------------------------ // Template - // ------------------------------------------------------------------------ + // ------------------------------------------------------------------------ /** * Gets the values of a template attribute @@ -205,7 +193,7 @@ public: * @return the number of values */ int get_template_attribute( - string& name, + string& name, vector& values) const { return host_template.get(name,values); @@ -224,61 +212,61 @@ public: string str=name; return host_template.get(str,values); }; - + /** * Gets a string based host attribute * @param name of the attribute - * @param value of the attribute (a string), will be "" if not defined + * @param value of the attribute (a string), will be "" if not defined */ void get_template_attribute( - const char * name, + const char * name, string& value) const { string str=name; - host_template.get(str,value); - } - + host_template.get(str,value); + } + /** * Gets a string based host attribute * @param name of the attribute - * @param value of the attribute (an int), will be 0 if not defined + * @param value of the attribute (an int), will be 0 if not defined */ void get_template_attribute( - const char * name, + const char * name, int& value) const { string str=name; - host_template.get(str,value); + host_template.get(str,value); } // --------------------------------------------------------- // Lex & bison parser for requirements and rank expressions // --------------------------------------------------------- - + /** * Evaluates a requirement expression on the given host. * @param requirements string * @param result true if the host matches the requirements - * @param errmsg string describing the error, must be freed by the + * @param errmsg string describing the error, must be freed by the * calling function - * @return 0 on success + * @return 0 on success */ int match(const string& requirements, bool& result, char **errmsg); - + /** * Evaluates a rank expression on the given host. * @param rank string * @param result of the rank evaluation - * @param errmsg string describing the error, must be freed by the + * @param errmsg string describing the error, must be freed by the * calling function - * @return 0 on success - */ + * @return 0 on success + */ int rank(const string& rank, int& result, char **errmsg); // ------------------------------------------------------------------------ // Share functions - // ------------------------------------------------------------------------ - + // ------------------------------------------------------------------------ + /** * * @@ -349,7 +337,7 @@ public: } /** - * Adds a new VM to the given share by icrementing the cpu,mem and disk + * Adds a new VM to the given share by icrementing the cpu,mem and disk * counters * @param cpu needed by the VM (percentage) * @param mem needed by the VM (in Kb) @@ -360,9 +348,9 @@ public: { host_share.add(cpu,mem,disk); }; - + /** - * Deletes a new VM from the given share by decrementing the cpu,mem and + * Deletes a new VM from the given share by decrementing the cpu,mem and * disk counters * @param cpu useded by the VM (percentage) * @param mem used by the VM (in Kb) @@ -370,8 +358,8 @@ public: * @return 0 on success */ void del_capacity(int cpu, int mem, int disk) - { - host_share.del(cpu,mem,disk); + { + host_share.del(cpu,mem,disk); }; /** @@ -385,22 +373,15 @@ public: { return host_share.test(cpu,mem,disk); } - + private: + // ------------------------------------------------------------------------- // Friends // ------------------------------------------------------------------------- - friend class HostPool; - - friend int host_select_cb (void * _host, - int num, - char ** values, - char ** names); - friend int host_dump_cb (void * _oss, - int num, - char ** values, - char ** names); + friend class HostPool; + // ------------------------------------------------------------------------- // Host Description // ------------------------------------------------------------------------- @@ -411,32 +392,32 @@ private: * The state of the Host */ HostState state; - + /** * Name of the IM driver used to monitor this host - */ + */ string im_mad_name; - + /** * Name of the VM driver used to execute VMs in this host - */ + */ string vmm_mad_name; - + /** * Name of the TM driver used to transfer file to and from this host - */ + */ string tm_mad_name; - + /** * If Host State = MONITORED last time it got fully monitored or 1 Jan 1970 * Host State = MONITORING last time it got a signal to be monitored - */ + */ time_t last_monitored; // ------------------------------------------------------------------------- // Host Attributes // ------------------------------------------------------------------------- - + /** * The Host template, holds the Host attributes. */ @@ -450,51 +431,41 @@ private: // ------------------------------------------------------------------------- // Lex & bison // ------------------------------------------------------------------------- - + /** * Mutex to perform just one flex-bison parsing at a time */ - static pthread_mutex_t lex_mutex; + static pthread_mutex_t lex_mutex; // ************************************************************************* // DataBase implementation (Private) // ************************************************************************* /** - * Function to unmarshall a Host object - * @param num the number of columns read from the DB - * @param names the column names - * @param vaues the column values - * @return 0 on success - */ - int unmarshall(int num, char **names, char ** values); - - /** - * Function to unmarshall a Host object in to an output stream in XML - * @param oss the output stream + * Callback function to unmarshall a Host object (Host::select) * @param num the number of columns read from the DB * @param names the column names * @param vaues the column values * @return 0 on success */ - static int unmarshall(ostringstream& oss, - int num, - char ** names, - char ** values); + int select_cb(void *nil, int num, char **values, char **names); + /** * Bootstraps the database table(s) associated to the Host */ - static void bootstrap(SqliteDB * db) - { - db->exec(Host::db_bootstrap); - - db->exec(HostShare::db_bootstrap); - - db->exec(HostTemplate::db_bootstrap); + static void bootstrap(SqlDB * db) + { + ostringstream oss_host(Host::db_bootstrap); + ostringstream oss_share(HostShare::db_bootstrap); + ostringstream oss_templ(HostTemplate::db_bootstrap); + + db->exec(oss_host); + db->exec(oss_share); + db->exec(oss_templ); }; protected: - + // ************************************************************************* // Constructor // ************************************************************************* @@ -502,23 +473,23 @@ protected: Host(int id=-1, string _hostname="", string _im_mad_name="", - string _vmm_mad_name="", + string _vmm_mad_name="", string _tm_mad_name=""); virtual ~Host(); - + // ************************************************************************* // DataBase implementation // ************************************************************************* - + enum ColNames { - OID = 0, - HOST_NAME = 1, - STATE = 2, - IM_MAD = 3, - VM_MAD = 4, - TM_MAD = 5, + OID = 0, + HOST_NAME = 1, + STATE = 2, + IM_MAD = 3, + VM_MAD = 4, + TM_MAD = 5, LAST_MON_TIME = 6, LIMIT = 7 }; @@ -526,7 +497,7 @@ protected: static const char * db_names; static const char * db_bootstrap; - + static const char * table; /** @@ -534,38 +505,39 @@ protected: * @param db pointer to the db * @return 0 on success */ - virtual int select(SqliteDB *db); + virtual int select(SqlDB *db); /** * Writes the Host and its associated HostShares in the database. * @param db pointer to the db * @return 0 on success */ - virtual int insert(SqliteDB *db); + virtual int insert(SqlDB *db); /** * Writes/updates the Hosts data fields in the database. * @param db pointer to the db * @return 0 on success */ - virtual int update(SqliteDB *db); - + virtual int update(SqlDB *db); + /** * Drops host from the database * @param db pointer to the db * @return 0 on success */ - virtual int drop(SqliteDB *db); + virtual int drop(SqlDB *db); /** - * Dumps the contect of a set of Host objects in the given stream - * using XML format - * @param db pointer to the db + * Callabck function to output a Host object in to an stream in XML format + * (Host::dump) * @param oss the output stream - * @param where string to filter the VirtualMachine objects + * @param num the number of columns read from the DB + * @param names the column names + * @param vaues the column values * @return 0 on success */ - static int dump(SqliteDB * db, ostringstream& oss, const string& where); + static int dump(ostringstream& oss, int num, char **values, char **names); }; #endif /*HOST_H_*/ diff --git a/include/HostPool.h b/include/HostPool.h index 0bd48f03a6..c270730f68 100644 --- a/include/HostPool.h +++ b/include/HostPool.h @@ -30,13 +30,13 @@ using namespace std; /** - * The Host Pool class. ... + * The Host Pool class. */ class HostPool : public PoolSQL { public: - HostPool(SqliteDB * db):PoolSQL(db,Host::table){}; + HostPool(SqlDB * db):PoolSQL(db,Host::table){}; ~HostPool(){}; @@ -48,8 +48,8 @@ public: int allocate ( int * oid, string hostname, - string im_mad_name, - string vmm_mad_name, + string im_mad_name, + string vmm_mad_name, string tm_mad_name); /** @@ -65,8 +65,8 @@ public: { return static_cast(PoolSQL::get(oid,lock)); }; - - /** Update a particular Host + + /** Update a particular Host * @param host pointer to Host * @return 0 on success */ @@ -74,8 +74,8 @@ public: { return host->update(db); }; - - + + /** Drops a host from the DB, the host mutex MUST BE locked * @param host pointer to Host */ @@ -87,11 +87,11 @@ public: /** * Bootstraps the database table(s) associated to the Host pool */ - static void bootstrap(SqliteDB *_db) + static void bootstrap(SqlDB *_db) { Host::bootstrap(_db); }; - + /** * Get the 10 least monitored hosts * @param discovered hosts, map to store the retrieved hosts hids and @@ -109,36 +109,37 @@ public: */ void add_capacity(int oid,int cpu, int mem, int disk) { - Host * host = get(oid, true); - - if ( host != 0 ) - { - host->add_capacity(cpu, mem, disk); - - update(host); - - host->unlock(); - } + Host * host = get(oid, true); + + if ( host != 0 ) + { + host->add_capacity(cpu, mem, disk); + + update(host); + + host->unlock(); + } }; + /** * De-Allocates a given capacity to the host * @param oid the id of the host to allocate the capacity * @param cpu amount of CPU * @param mem amount of main memory * @param disk amount of disk - */ + */ void del_capacity(int oid,int cpu, int mem, int disk) { - Host * host = get(oid, true); - - if ( host != 0 ) - { - host->del_capacity(cpu, mem, disk); - - update(host); - - host->unlock(); - } + Host * host = get(oid, true); + + if ( host != 0 ) + { + host->del_capacity(cpu, mem, disk); + + update(host); + + host->unlock(); + } }; /** @@ -149,19 +150,8 @@ public: * * @return 0 on success */ - int dump(ostringstream& oss, const string& where) - { - int rc; + int dump(ostringstream& oss, const string& where); - oss << ""; - - rc = Host::dump(db,oss,where); - - oss << ""; - - return rc; - } - private: /** * Factory method to produce Host objects @@ -172,6 +162,25 @@ private: return new Host; }; + /** + * Callback function to get the IDs of the hosts to be monitored + * (Host::discover) + * @param num the number of columns read from the DB + * @param names the column names + * @param vaues the column values + * @return 0 on success + */ + int discover_cb(void * _map, int num, char **values, char **names); + + /** + * Callback function to get output the host pool in XML format + * (Host::dump) + * @param num the number of columns read from the DB + * @param names the column names + * @param vaues the column values + * @return 0 on success + */ + int dump_cb(void * _oss, int num, char **values, char **names); }; -#endif /*HOST_POOL_H_*/ +#endif /*HOST_POOL_H_*/ \ No newline at end of file diff --git a/include/HostShare.h b/include/HostShare.h index 55c455b99c..dfc91341ff 100644 --- a/include/HostShare.h +++ b/include/HostShare.h @@ -17,24 +17,19 @@ #ifndef HOST_SHARE_H_ #define HOST_SHARE_H_ -#include "SqliteDB.h" +#include "SqlDB.h" #include "ObjectSQL.h" #include using namespace std; -extern "C" int host_share_select_cb (void * _hs, - int num, - char ** values, - char ** names); - /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ /** * The HostShare class. It represents a logical partition of a host... */ -class HostShare : public ObjectSQL +class HostShare : public ObjectSQL { public: @@ -57,7 +52,7 @@ public: cpu_usage += cpu; mem_usage += mem; disk_usage += disk; - + running_vms++; } @@ -72,17 +67,17 @@ public: cpu_usage -= cpu; mem_usage -= mem; disk_usage -= disk; - - running_vms--; + + running_vms--; } - + /** - * Check if this share can host a VM. + * Check if this share can host a VM. * @param cpu requested by the VM * @param mem requested by the VM * @param disk requested by the VM - * - * @return true if the share can host the VM or it is the only one + * + * @return true if the share can host the VM or it is the only one * configured */ bool test(int cpu, int mem, int disk) const @@ -90,8 +85,8 @@ public: return (((max_cpu - cpu_usage ) >= cpu) && ((max_mem - mem_usage ) >= mem) && ((max_disk - disk_usage) >= disk)); - } - + } + /** * Function to write a HostShare to an output stream */ @@ -112,15 +107,15 @@ public: * @return a reference to the generated string */ string& to_xml(string& xml) const; - + private: int hsid; /**< HostShare identifier */ - + int disk_usage; /**< Disk allocated to VMs (in Mb). */ int mem_usage; /**< Memory allocated to VMs (in Mb) */ int cpu_usage; /**< CPU allocated to VMs (in percentage) */ - + int max_disk; /**< Total disk capacity (in Mb) */ int max_mem; /**< Total memory capacity (in Mb) */ int max_cpu; /**< Total cpu capacity (in percentage) */ @@ -134,19 +129,14 @@ private: int used_cpu; /**< Used cpu from the IM monitor */ int running_vms; /**< Number of running VMs in this Host */ - + // ---------------------------------------- // Friends // ---------------------------------------- friend class Host; - - friend int host_share_select_cb ( - void * _hostshare, - int num, - char ** values, - char ** names); - + friend class HostPool; + // ---------------------------------------- // DataBase implementation variables // ---------------------------------------- @@ -171,7 +161,7 @@ private: }; static const char * table; - + static const char * db_names; static const char * db_bootstrap; @@ -179,43 +169,43 @@ private: // ---------------------------------------- // Database methods // ---------------------------------------- - + /** * Reads the HostShare (identified with its HSID) from the database. * @param db pointer to the db * @return 0 on success */ - int select(SqliteDB * db); + int select(SqlDB * db); /** * Writes the HostShare in the database. * @param db pointer to the db * @return 0 on success */ - int insert(SqliteDB * db); + int insert(SqlDB * db); /** * Writes/updates the HostShare data fields in the database. * @param db pointer to the db * @return 0 on success */ - int update(SqliteDB * db); - + int update(SqlDB * db); + /** * Drops hostshare from the database * @param db pointer to the db * @return 0 on success */ - int drop(SqliteDB * db); - + int drop(SqlDB * db); + /** - * Function to unmarshall a HostShare object + * Callback function to unmarshall a HostShare object (HostShare::select) * @param num the number of columns read from the DB * @para names the column names * @para vaues the column values * @return 0 on success */ - int unmarshall(int num, char **names, char ** values); + int select_cb(void * nil, int num, char **values, char **names); /** * Function to unmarshall a HostShare object in to an output stream in XML @@ -225,10 +215,8 @@ private: * @param vaues the column values * @return 0 on success */ - static int unmarshall(ostringstream& oss, - int num, - char ** names, - char ** values); + static int dump(ostringstream& oss, int num, char **values, char **names); + }; diff --git a/src/host/Host.cc b/src/host/Host.cc index a4bc25a75c..e68451d5ec 100644 --- a/src/host/Host.cc +++ b/src/host/Host.cc @@ -61,7 +61,7 @@ const char * Host::db_bootstrap = "CREATE TABLE host_pool (" /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int Host::unmarshall(int num, char **names, char ** values) +int Host::select_cb(void * nil, int num, char **values, char ** names) { if ((!values[OID]) || (!values[HOST_NAME]) || @@ -78,11 +78,11 @@ int Host::unmarshall(int num, char **names, char ** values) oid = atoi(values[OID]); hostname = values[HOST_NAME]; state = static_cast(atoi(values[STATE])); - + im_mad_name = values[IM_MAD]; vmm_mad_name = values[VM_MAD]; tm_mad_name = values[TM_MAD]; - + last_monitored = static_cast(atoi(values[LAST_MON_TIME])); host_template.id = oid; @@ -91,41 +91,22 @@ int Host::unmarshall(int num, char **names, char ** values) return 0; } -/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -extern "C" int host_select_cb ( - void * _host, - int num, - char ** values, - char ** names) -{ - Host * host; - - host = static_cast(_host); - - if (host == 0) - { - return -1; - } - - return host->unmarshall(num,names,values); -}; - -/* -------------------------------------------------------------------------- */ - -int Host::select(SqliteDB *db) +int Host::select(SqlDB *db) { ostringstream oss; int rc; int boid; - + + set_callback(static_cast(&Host::select_cb)); + oss << "SELECT * FROM " << table << " WHERE oid = " << oid; boid = oid; oid = -1; - rc = db->exec(oss, host_select_cb, (void *) this); + rc = db->exec(oss, this); if ((rc != 0) || (oid != boid )) { @@ -156,7 +137,7 @@ int Host::select(SqliteDB *db) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int Host::insert(SqliteDB *db) +int Host::insert(SqlDB *db) { int rc; map::iterator iter; @@ -174,7 +155,7 @@ int Host::insert(SqliteDB *db) { host_share.hsid = oid; } - + //Insert the Host and its template rc = update(db); @@ -189,17 +170,17 @@ int Host::insert(SqliteDB *db) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int Host::update(SqliteDB *db) +int Host::update(SqlDB *db) { ostringstream oss; - + int rc; char * sql_hostname; char * sql_im_mad_name; char * sql_tm_mad_name; char * sql_vmm_mad_name; - + // Update the Template rc = host_template.update(db); @@ -219,35 +200,35 @@ int Host::update(SqliteDB *db) } // Update the Host - - sql_hostname = sqlite3_mprintf("%q",hostname.c_str()); + + sql_hostname = db->escape_str(hostname.c_str()); if ( sql_hostname == 0 ) { goto error_hostname; } - - sql_im_mad_name = sqlite3_mprintf("%q",im_mad_name.c_str()); + + sql_im_mad_name = db->escape_str(im_mad_name.c_str()); if ( sql_im_mad_name == 0 ) { goto error_im; } - - sql_tm_mad_name = sqlite3_mprintf("%q",tm_mad_name.c_str()); + + sql_tm_mad_name = db->escape_str(tm_mad_name.c_str()); if ( sql_tm_mad_name == 0 ) { goto error_tm; } - sql_vmm_mad_name = sqlite3_mprintf("%q",vmm_mad_name.c_str()); + sql_vmm_mad_name = db->escape_str(vmm_mad_name.c_str()); if ( sql_vmm_mad_name == 0 ) { goto error_vmm; } - + // Construct the SQL statement to Insert or Replace (effectively, update) oss << "INSERT OR REPLACE INTO " << table << " "<< db_names <<" VALUES (" @@ -261,19 +242,19 @@ int Host::update(SqliteDB *db) rc = db->exec(oss); - sqlite3_free(sql_hostname); - sqlite3_free(sql_im_mad_name); - sqlite3_free(sql_tm_mad_name); - sqlite3_free(sql_vmm_mad_name); + db->free_str(sql_hostname); + db->free_str(sql_im_mad_name); + db->free_str(sql_tm_mad_name); + db->free_str(sql_vmm_mad_name); return rc; - + error_vmm: - sqlite3_free(sql_tm_mad_name); + db->free_str(sql_tm_mad_name); error_tm: - sqlite3_free(sql_im_mad_name); + db->free_str(sql_im_mad_name); error_im: - sqlite3_free(sql_hostname); + db->free_str(sql_hostname); error_hostname: return -1; } @@ -281,10 +262,7 @@ error_hostname: /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int Host::unmarshall(ostringstream& oss, - int num, - char ** names, - char ** values) +int Host::dump(ostringstream& oss, int num, char **values, char **names) { if ((!values[OID]) || (!values[HOST_NAME]) || @@ -301,65 +279,24 @@ int Host::unmarshall(ostringstream& oss, oss << "" << "" << values[OID] <<"" << - "" << values[HOST_NAME] <<"" << - "" << values[STATE] <<"" << - "" << values[IM_MAD] <<"" << - "" << values[VM_MAD] <<"" << - "" << values[TM_MAD] <<"" << - ""<< values[LAST_MON_TIME]<<""; - - HostShare::unmarshall(oss,num - LIMIT, names + LIMIT, values + LIMIT); - + "" << values[HOST_NAME] <<"" << + "" << values[STATE] <<"" << + "" << values[IM_MAD] <<"" << + "" << values[VM_MAD] <<"" << + "" << values[TM_MAD] <<"" << + ""<< values[LAST_MON_TIME]<<""; + + HostShare::dump(oss,num - LIMIT, values + LIMIT, names + LIMIT); + oss << ""; return 0; - -} - -/* -------------------------------------------------------------------------- */ - -extern "C" int host_dump_cb ( - void * _oss, - int num, - char ** values, - char ** names) -{ - ostringstream * oss; - - oss = static_cast(_oss); - - if (oss == 0) - { - return -1; - } - - return Host::unmarshall(*oss,num,names,values); -}; - -/* -------------------------------------------------------------------------- */ - -int Host::dump(SqliteDB * db, ostringstream& oss, const string& where) -{ - int rc; - ostringstream cmd; - - cmd << "SELECT * FROM " << Host::table << "," << HostShare::table - << " ON " << Host::table << ".oid = " << HostShare::table << ".hid"; - - if ( !where.empty() ) - { - cmd << " WHERE " << where; - } - - rc = db->exec(cmd,host_dump_cb,(void *) &oss); - - return rc; } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int Host::drop(SqliteDB * db) +int Host::drop(SqlDB * db) { ostringstream oss; int rc; @@ -389,15 +326,15 @@ int Host::update_info(string &parse_str) int rc; rc = host_template.parse(parse_str, &error_msg); - + if ( rc != 0 ) { //Nebula::log("ONE", Log::ERROR, error_msg); - + free(error_msg); return -1; } - + get_template_attribute("TOTALCPU",host_share.max_cpu); get_template_attribute("TOTALMEMORY",host_share.max_mem); @@ -417,9 +354,9 @@ int Host::update_info(string &parse_str) ostream& operator<<(ostream& os, Host& host) { string host_str; - + os << host.to_xml(host_str); - + return os; }; @@ -432,18 +369,18 @@ string& Host::to_xml(string& xml) const string template_xml; string share_xml; ostringstream oss; - - oss << - "" - "" << oid << "" << - "" << hostname << "" << - "" << state << "" << - "" << im_mad_name << "" << - "" << vmm_mad_name << "" << - "" << tm_mad_name << "" << - "" << last_monitored << "" << - host_share.to_xml(share_xml) << - host_template.to_xml(template_xml) << + + oss << + "" + "" << oid << "" << + "" << hostname << "" << + "" << state << "" << + "" << im_mad_name << "" << + "" << vmm_mad_name << "" << + "" << tm_mad_name << "" << + "" << last_monitored << "" << + host_share.to_xml(share_xml) << + host_template.to_xml(template_xml) << ""; xml = oss.str(); @@ -458,10 +395,10 @@ string& Host::to_str(string& str) const { string template_str; string share_str; - + ostringstream os; - os << + os << "ID = " << oid << endl << "NAME = " << hostname << endl << "STATE = " << state << endl << @@ -473,7 +410,7 @@ string& Host::to_str(string& str) const "HOST SHARES" << endl << host_share.to_str(share_str) < * discovered_hosts; + string im_mad(values[1]); + int hid; + + discovered_hosts = static_cast *>(_map); + + if ( (num<=0) || (values[0] == 0) ) { - map * discovered_hosts; - string im_mad(values[1]); - int hid; - - discovered_hosts = static_cast *>(_discovered_hosts); + return -1; + } - if ( (discovered_hosts == 0) || (num<=0) || (values[0] == 0) ) - { - return -1; - } - - hid = atoi(values[0]); - im_mad = values[1]; + hid = atoi(values[0]); + im_mad = values[1]; - discovered_hosts->insert(make_pair(hid,im_mad)); + discovered_hosts->insert(make_pair(hid,im_mad)); - return 0; - }; + return 0; } /* -------------------------------------------------------------------------- */ @@ -86,15 +79,54 @@ int HostPool::discover(map * discovered_hosts) lock(); - sql << "SELECT oid, im_mad FROM " + set_callback(static_cast(&HostPool::discover_cb), + static_cast(discovered_hosts)); + + sql << "SELECT oid, im_mad FROM " << Host::table << " ORDER BY last_mon_time LIMIT 10"; - rc = db->exec(sql,discover_cb,(void *) discovered_hosts); - + rc = db->exec(sql,this); + unlock(); - + return rc; } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ + +int HostPool::dump_cb(void * _oss, int num, char **values, char **names) +{ + ostringstream * oss; + + oss = static_cast(_oss); + + return Host::dump(*oss, num, values, names); +} + +/* -------------------------------------------------------------------------- */ + +int HostPool::dump(ostringstream& oss, const string& where) +{ + int rc; + ostringstream cmd; + + oss << ""; + + set_callback(static_cast(&HostPool::dump_cb), + static_cast(&oss)); + + cmd << "SELECT * FROM " << Host::table << "," << HostShare::table + << " ON " << Host::table << ".oid = " << HostShare::table << ".hid"; + + if ( !where.empty() ) + { + cmd << " WHERE " << where; + } + + rc = db->exec(cmd, this); + + oss << ""; + + return rc; +} \ No newline at end of file diff --git a/src/host/HostShare.cc b/src/host/HostShare.cc index d40285d4a5..34821e6b57 100644 --- a/src/host/HostShare.cc +++ b/src/host/HostShare.cc @@ -18,7 +18,7 @@ #include #include -#include +#include #include "HostShare.h" @@ -73,7 +73,7 @@ const char * HostShare::db_bootstrap = "CREATE TABLE host_shares (" /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int HostShare::unmarshall(int num, char **names, char ** values) +int HostShare::select_cb(void * nil, int num, char **values, char **names) { if ((!values[HID]) || (!values[DISK_USAGE]) || @@ -99,7 +99,7 @@ int HostShare::unmarshall(int num, char **names, char ** values) disk_usage = atoi(values[DISK_USAGE]); mem_usage = atoi(values[MEM_USAGE]); cpu_usage = atoi(values[CPU_USAGE]); - + max_disk = atoi(values[MAX_DISK]); max_mem = atoi(values[MAX_MEMORY]); max_cpu = atoi(values[MAX_CPU]); @@ -113,93 +113,75 @@ int HostShare::unmarshall(int num, char **names, char ** values) used_cpu = atoi(values[USED_CPU]); running_vms = atoi(values[RUNNING_VMS]); - + return 0; } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int HostShare::unmarshall(ostringstream& oss, - int num, - char ** names, - char ** values) +int HostShare::dump(ostringstream& oss, + int num, + char ** values, + char ** names) { - if ((!values[HID]) || + if ((!values[HID]) || (!values[DISK_USAGE]) || - (!values[MEM_USAGE]) || - (!values[CPU_USAGE]) || - (!values[MAX_DISK]) || - (!values[MAX_MEMORY]) || - (!values[MAX_CPU]) || - (!values[FREE_DISK]) || - (!values[FREE_MEMORY]) || - (!values[FREE_CPU]) || - (!values[USED_DISK]) || - (!values[USED_MEMORY]) || - (!values[USED_CPU]) || - (!values[RUNNING_VMS]) || - (num != LIMIT)) - { - return -1; - } - - oss << - "" << - "" << values[HID] << "" << - ""<< values[DISK_USAGE] << ""<< - "" << values[MEM_USAGE] << "" << - "" << values[CPU_USAGE] << "" << - "" << values[MAX_DISK] << "" << - "" << values[MAX_MEMORY] << "" << - "" << values[MAX_CPU] << "" << - "" << values[FREE_DISK] << "" << - "" << values[FREE_MEMORY] << "" << - "" << values[FREE_CPU] << "" << - "" << values[USED_DISK] << "" << - "" << values[USED_MEMORY] << "" << - "" << values[USED_CPU] << "" << - ""<"<< - ""; - - return 0; + (!values[MEM_USAGE]) || + (!values[CPU_USAGE]) || + (!values[MAX_DISK]) || + (!values[MAX_MEMORY]) || + (!values[MAX_CPU]) || + (!values[FREE_DISK]) || + (!values[FREE_MEMORY]) || + (!values[FREE_CPU]) || + (!values[USED_DISK]) || + (!values[USED_MEMORY]) || + (!values[USED_CPU]) || + (!values[RUNNING_VMS]) || + (num != LIMIT)) + { + return -1; + } + + oss << + "" << + "" << values[HID] << "" << + ""<< values[DISK_USAGE] << ""<< + "" << values[MEM_USAGE] << "" << + "" << values[CPU_USAGE] << "" << + "" << values[MAX_DISK] << "" << + "" << values[MAX_MEMORY] << "" << + "" << values[MAX_CPU] << "" << + "" << values[FREE_DISK] << "" << + "" << values[FREE_MEMORY] << "" << + "" << values[FREE_CPU] << "" << + "" << values[USED_DISK] << "" << + "" << values[USED_MEMORY] << "" << + "" << values[USED_CPU] << "" << + ""<"<< + ""; + + return 0; } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -extern "C" int host_share_select_cb ( - void * _hs, - int num, - char ** values, - char ** names) -{ - HostShare * hs; - - hs = static_cast(_hs); - - if (hs == 0) - { - return -1; - } - - return hs->unmarshall(num,names,values); -}; - -/* -------------------------------------------------------------------------- */ - -int HostShare::select(SqliteDB * db) +int HostShare::select(SqlDB * db) { ostringstream oss; int rc; int bhsid; + set_callback(static_cast(&HostShare::select_cb)); + oss << "SELECT * FROM " << table << " WHERE hid = " << hsid; bhsid = hsid; hsid = -1; - rc = db->exec(oss,host_share_select_cb,(void *) this); + rc = db->exec(oss,this); if (hsid != bhsid ) { @@ -212,7 +194,7 @@ int HostShare::select(SqliteDB * db) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int HostShare::insert(SqliteDB * db) +int HostShare::insert(SqlDB * db) { int rc; @@ -229,7 +211,7 @@ int HostShare::insert(SqliteDB * db) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int HostShare::update(SqliteDB * db) +int HostShare::update(SqlDB * db) { ostringstream oss; int rc; @@ -250,7 +232,7 @@ int HostShare::update(SqliteDB * db) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int HostShare::drop(SqliteDB * db) +int HostShare::drop(SqlDB * db) { ostringstream oss; @@ -268,7 +250,7 @@ ostream& operator<<(ostream& os, HostShare& hs) string str; os << hs.to_xml(str); - + return os; }; @@ -281,19 +263,19 @@ string& HostShare::to_xml(string& xml) const ostringstream oss; oss << "" - << "" << hsid << "" + << "" << hsid << "" << "" << disk_usage << "" << "" << mem_usage << "" - << "" << cpu_usage << "" + << "" << cpu_usage << "" << "" << max_disk << "" << "" << max_mem << "" - << "" << max_cpu << "" + << "" << max_cpu << "" << "" << free_disk << "" << "" << free_mem << "" - << "" << free_cpu << "" + << "" << free_cpu << "" << "" << used_disk << "" << "" << used_mem << "" - << "" << used_cpu << "" + << "" << used_cpu << "" << ""<" << ""; @@ -310,10 +292,10 @@ string& HostShare::to_str(string& str) const string template_xml; ostringstream oss; - oss<< "\tHID = " << hsid + oss<< "\tHID = " << hsid << "\tCPU_USAGE = " << cpu_usage << endl << "\tMEMORY_USAGE = " << mem_usage << endl - << "\tDISK_USAGE = " << disk_usage<< endl + << "\tDISK_USAGE = " << disk_usage<< endl << "\tMAX_CPU = " << max_cpu << endl << "\tMAX_MEMORY = " << max_mem << endl << "\tMAX_DISK = " << max_disk<< endl