1
0
mirror of https://github.com/OpenNebula/one.git synced 2024-12-24 21:34:01 +03:00

Merge branch 'feature-1658'

Conflicts:
	src/scheduler/src/sched/Scheduler.cc
	src/scheduler/src/sched/SchedulerTemplate.cc
This commit is contained in:
Ruben S. Montero 2013-02-01 19:03:31 +01:00
commit 6ed2e75657
14 changed files with 500 additions and 81 deletions

View File

@ -118,6 +118,9 @@ if mysql=='yes':
else:
main_env.Append(mysql='no')
# log4cpp
main_env.Append(LIBS=['pthread','log4cpp'])
# xmlrpc
xmlrpc_dir=ARGUMENTS.get('xmlrpc', 'none')
if xmlrpc_dir!='none':

View File

@ -20,6 +20,10 @@
#include <string>
#include <fstream>
#include "PoolObjectSQL.h"
#include "log4cpp/Priority.hh"
using namespace std;
/**
@ -137,4 +141,77 @@ public:
const char * message);
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/**
* Send log messages to syslog
*/
class SysLog : public Log
{
public:
SysLog(const MessageType level,
const string& label);
virtual ~SysLog() {};
virtual void log(
const char * module,
const MessageType type,
const char * message);
static log4cpp::Priority::PriorityLevel get_priority_level(
const MessageType level);
protected:
/**
* Specialized constructor only for derived classes that uses an initialzed
* SysLog system.
*/
SysLog(const MessageType level):Log(level){};
/**
* This is the root category name used by any syslog resource
* in the process
*/
static const char * CATEGORY;
/**
* This is the daemon name+pid, used to label every message in the process
*/
static string LABEL;
};
/**
* Send log messages to syslog per resource. It requires a Root Syslog
* to be initialized before using a SysLogResource
*/
class SysLogResource : public SysLog
{
public:
SysLogResource(
int oid,
const PoolObjectSQL::ObjectType obj_type,
const MessageType clevel);
virtual ~SysLogResource(){};
void log(
const char * module,
const MessageType type,
const char * message);
protected:
/**
* This is the resource category name used by any syslog resource
* in the process
*/
static const char * CATEGORY;
/**
* The resource log label
*/
string obj_label;
};
#endif /* _LOG_H_ */

View File

@ -172,24 +172,61 @@ public:
// --------------------------------------------------------------
/**
* Returns the value of DEBUG_LEVEL in oned.conf file
* Returns the value of LOG->DEBUG_LEVEL in oned.conf file
* @return the debug level, to instantiate Log'ers
*/
Log::MessageType get_debug_level() const
{
Log::MessageType clevel = Log::ERROR;
int log_level_int;
Log::MessageType clevel = Log::ERROR;
vector<const Attribute *> logs;
int rc;
int log_level_int;
nebula_configuration->get("DEBUG_LEVEL", log_level_int);
rc = nebula_configuration->get("LOG", logs);
if (0 <= log_level_int && log_level_int <= 3 )
if ( rc != 0 )
{
clevel = static_cast<Log::MessageType>(log_level_int);
string value;
const VectorAttribute * log = static_cast<const VectorAttribute *>
(logs[0]);
value = log->vector_value("DEBUG_LEVEL");
log_level_int = atoi(value.c_str());
if (0 <= log_level_int && log_level_int <= 3 )
{
clevel = static_cast<Log::MessageType>(log_level_int);
}
}
return clevel;
}
/**
* Returns the value of LOG->SYSTEM in oned.conf file
* @return the logging system CERR, FILE_TS or SYSLOG
*/
NebulaLog::LogType get_log_system() const
{
vector<const Attribute *> logs;
int rc;
NebulaLog::LogType log_system = NebulaLog::UNDEFINED;
rc = nebula_configuration->get("LOG", logs);
if ( rc != 0 )
{
string value;
const VectorAttribute * log = static_cast<const VectorAttribute *>
(logs[0]);
value = log->vector_value("SYSTEM");
log_system = NebulaLog::str_to_type(value);
}
return log_system;
};
/**
* Returns the value of ONE_LOCATION env variable. When this variable is
* not defined the nebula location is "/".

View File

@ -18,7 +18,9 @@
#define _NEBULA_LOG_H_
#include "Log.h"
#include <sstream>
#include <syslog.h>
using namespace std;
@ -31,7 +33,9 @@ public:
enum LogType {
FILE = 0,
FILE_TS = 1,
CERR = 2
CERR = 2,
SYSLOG = 3,
UNDEFINED = 4
};
// ---------------------------------------------------------------
@ -41,23 +45,47 @@ public:
static void init_log_system(
LogType ltype,
Log::MessageType clevel,
const char * filename = 0,
ios_base::openmode mode = ios_base::trunc)
const char * filename,
ios_base::openmode mode,
const string& daemon)
{
switch(ltype)
{
case FILE:
NebulaLog::logger = new FileLog(filename,clevel,mode);
break;
NebulaLog::logger = new FileLog(filename,clevel,mode);
break;
case FILE_TS:
NebulaLog::logger = new FileLogTS(filename,clevel,mode);
break;
NebulaLog::logger = new FileLogTS(filename,clevel,mode);
break;
case SYSLOG:
NebulaLog::logger = new SysLog(clevel, daemon);
break;
default:
NebulaLog::logger = new CerrLog(clevel);
break;
NebulaLog::logger = new CerrLog(clevel);
break;
}
};
static LogType str_to_type(string& type)
{
transform(type.begin(), type.end(), type.begin(), (int(*)(int))toupper);
if (type == "FILE")
{
return FILE_TS;
}
else if (type == "SYSLOG")
{
return SYSLOG;
}
else if (type == "STDERR")
{
return CERR;
}
return UNDEFINED;
}
static void finalize_log_system()
{
delete logger;

View File

@ -1049,8 +1049,9 @@ private:
* $ONE_LOCATION/var/$VID/vm.log
* or, in case that OpenNebula is installed in root
* /var/log/one/$VM_ID.log
* For the syslog... TODO
*/
FileLog * _log;
Log * _log;
/**
* User template to store custom metadata. This template can be updated

View File

@ -37,11 +37,21 @@
# VNC_BASE_PORT: VNC ports for VMs can be automatically set to VNC_BASE_PORT +
# VMID
#
# DEBUG_LEVEL: 0 = ERROR, 1 = WARNING, 2 = INFO, 3 = DEBUG
# LOG: Configuration for the logging system
# system: defines the logging system:
# file to log in the oned.log file
# syslog to use the syslog facilities
# debug_level: 0 = ERROR, 1 = WARNING, 2 = INFO, 3 = DEBUG
#
# VM_SUBMIT_ON_HOLD: Forces VMs to be created on hold state instead of pending.
# Values: YES or NO.
#*******************************************************************************
#
LOG = [
system = "file",
debug_level = 3
]
#MANAGER_TIMER = 30
@ -69,8 +79,6 @@ DB = [ backend = "sqlite" ]
VNC_BASE_PORT = 5900
DEBUG_LEVEL = 3
#VM_SUBMIT_ON_HOLD = "NO"
#*******************************************************************************

View File

@ -22,6 +22,19 @@
#include <sstream>
#include <iostream>
#include <sys/types.h>
#include <unistd.h>
#include <syslog.h>
#include "log4cpp/Category.hh"
#include "log4cpp/CategoryStream.hh"
#include "log4cpp/Appender.hh"
#include "log4cpp/SyslogAppender.hh"
#include "log4cpp/Layout.hh"
#include "log4cpp/PatternLayout.hh"
#include "log4cpp/Priority.hh"
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
@ -141,3 +154,162 @@ void CerrLog::log(
cerr.flush();
}
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
const char * SysLog::CATEGORY = "ROOT";
string SysLog::LABEL;
/* -------------------------------------------------------------------------- */
SysLog::SysLog(const MessageType level,
const string& label):Log(level)
{
static bool initialized = false;
if (!initialized) //Initialize just once for all SysLog instances
{
ostringstream oss;
log4cpp::Appender *appender;
oss << label << "[" << getpid() << "]";
LABEL = oss.str();
appender = new log4cpp::SyslogAppender(CATEGORY, LABEL, LOG_DAEMON);
appender->setLayout(new log4cpp::PatternLayout());
log4cpp::Category& root = log4cpp::Category::getRoot();
root.setPriority(SysLog::get_priority_level(level));
root.addAppender(appender);
initialized = true;
}
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void SysLog::log(
const char * module,
const MessageType type,
const char * message)
{
log4cpp::Category& root = log4cpp::Category::getRoot();
log4cpp::Priority::PriorityLevel level = get_priority_level(type);
istringstream smessage;
string line;
smessage.str(message);
while ( getline(smessage, line) )
{
root << level << "[" << module << "]"
<< "[" << error_names[type] << "]: "
<< line;
}
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
log4cpp::Priority::PriorityLevel SysLog::get_priority_level(
const MessageType level)
{
log4cpp::Priority::PriorityLevel priority_level;
switch (level)
{
case Log::ERROR:
priority_level = log4cpp::Priority::ERROR;
break;
case Log::WARNING:
priority_level = log4cpp::Priority::WARN;
break;
case Log::INFO:
priority_level = log4cpp::Priority::INFO;
break;
case Log::DEBUG:
case Log::DDEBUG:
case Log::DDDEBUG:
priority_level = log4cpp::Priority::DEBUG;
break;
default:
priority_level = log4cpp::Priority::NOTSET;
break;
}
return priority_level;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
const char * SysLogResource::CATEGORY = "RESOURCE";
/* -------------------------------------------------------------------------- */
SysLogResource::SysLogResource(
int oid,
const PoolObjectSQL::ObjectType obj_type,
const MessageType clevel):SysLog(clevel)
{
static bool initialized = false;
ostringstream oss_label;
string obj_type_str;
if (!initialized)
{
log4cpp::Appender *appender;
appender = new log4cpp::SyslogAppender(CATEGORY,
SysLog::LABEL,
LOG_DAEMON);
appender->setLayout(new log4cpp::PatternLayout());
log4cpp::Category& res = log4cpp::Category::getInstance(CATEGORY);
res.addAppender(appender);
res.setPriority(SysLog::get_priority_level(clevel));
initialized = true;
}
obj_type_str = PoolObjectSQL::type_to_str(obj_type);
oss_label << "[" << obj_type_str << " " << oid << "]";
obj_label = oss_label.str();
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void SysLogResource::log(
const char * module,
const MessageType type,
const char * message)
{
log4cpp::Category& res = log4cpp::Category::getInstance(CATEGORY);
log4cpp::Priority::PriorityLevel level = get_priority_level(type);
istringstream smessage;
string line;
smessage.str(message);
while ( getline(smessage, line) )
{
res << level << obj_label
<< "[" << module << "]"
<< "[" << error_names[type] << "]: "
<< line;
}
}

View File

@ -317,18 +317,28 @@ void Nebula::start()
try
{
string log_fname;
Log::MessageType clevel;
Log::MessageType clevel;
NebulaLog::LogType log_system;
log_fname = log_location + "oned.log";
clevel = get_debug_level();
log_system = get_log_system();
clevel = get_debug_level();
// Initializing ONE Daemon log system
if ( log_system != NebulaLog::UNDEFINED )
{
string log_fname;
log_fname = log_location + "oned.log";
NebulaLog::init_log_system(NebulaLog::FILE_TS,
clevel,
log_fname.c_str(),
ios_base::trunc);
NebulaLog::init_log_system(log_system,
clevel,
log_fname.c_str(),
ios_base::trunc,
"oned");
}
else
{
throw runtime_error("Unknown LOG_SYSTEM.");
}
os << "Starting " << version() << endl;
os << "----------------------------------------\n";

View File

@ -78,6 +78,7 @@ void OpenNebulaTemplate::set_conf_default()
SingleAttribute * attribute;
VectorAttribute * vattribute;
string value;
map<string,string> vvalue;
// MANAGER_TIMER
value = "15";
@ -144,7 +145,6 @@ void OpenNebulaTemplate::set_conf_default()
conf_default.insert(make_pair(attribute->name(),attribute));
//DB CONFIGURATION
map<string,string> vvalue;
vvalue.insert(make_pair("BACKEND","sqlite"));
vattribute = new VectorAttribute("DB",vvalue);
@ -174,6 +174,13 @@ void OpenNebulaTemplate::set_conf_default()
attribute = new SingleAttribute("VM_SUBMIT_ON_HOLD",value);
conf_default.insert(make_pair(attribute->name(),attribute));
// LOG CONFIGURATION
vvalue.clear();
vvalue.insert(make_pair("SYSTEM","file"));
vvalue.insert(make_pair("DEBUG_LEVEL","3"));
vattribute = new VectorAttribute("LOG",vvalue);
conf_default.insert(make_pair(vattribute->name(),vattribute));
/*
#*******************************************************************************
# Physical Networks configuration

View File

@ -34,6 +34,12 @@
# 3 = Custom.
# - rank: Custom arithmetic exprission to rank suitable hosts based in their
# attributes
#
# LOG: Configuration for the logging system
# - system: defines the logging system:
# file to log in the sched.log file
# syslog to use the syslog facilities
# - debug_level: 0 = ERROR, 1 = WARNING, 2 = INFO, 3 = DEBU
#*******************************************************************************
ONED_PORT = 2633
@ -49,10 +55,15 @@ LIVE_RESCHEDS = 0
HYPERVISOR_MEM = 0.1
DEFAULT_SCHED = [
policy = 1
policy = 1
]
#DEFAULT_SCHED = [
# policy = 3,
# policy = 3,
# rank = "- (RUNNING_VMS * 50 + FREE_CPU)"
#]
LOG = [
system = "file",
debug_level = 3
]

View File

@ -140,7 +140,6 @@ private:
friend void * scheduler_action_loop(void *arg);
// ---------------------------------------------------------------
// Scheduling Policies
// ---------------------------------------------------------------

View File

@ -77,46 +77,28 @@ void Scheduler::start()
pthread_attr_t pattr;
// -----------------------------------------------------------
// Log system & Configuration File
// -----------------------------------------------------------
try
{
string log_file;
const char * nl = getenv("ONE_LOCATION");
if (nl == 0) //OpenNebula installed under root directory
{
log_file = "/var/log/one/sched.log";
etc_path = "/etc/one/";
}
else
{
oss << nl << "/var/sched.log";
log_file = oss.str();
oss.str("");
oss << nl << "/etc/";
etc_path = oss.str();
}
NebulaLog::init_log_system(NebulaLog::FILE,
Log::DEBUG,
log_file.c_str());
NebulaLog::log("SCHED", Log::INFO, "Init Scheduler Log system");
}
catch(runtime_error &)
{
throw;
}
// -----------------------------------------------------------
// Configuration File
// -----------------------------------------------------------
string log_file;
const char * nl = getenv("ONE_LOCATION");
if (nl == 0) //OpenNebula installed under root directory
{
log_file = "/var/log/one/sched.log";
etc_path = "/etc/one/";
}
else
{
oss << nl << "/var/sched.log";
log_file = oss.str();
oss.str("");
oss << nl << "/etc/";
etc_path = oss.str();
}
SchedulerTemplate conf(etc_path);
@ -143,6 +125,60 @@ void Scheduler::start()
conf.get("HYPERVISOR_MEM", hypervisor_mem);
// -----------------------------------------------------------
// Log system & Configuration File
// -----------------------------------------------------------
try
{
vector<const Attribute *> logs;
int rc;
NebulaLog::LogType log_system = NebulaLog::UNDEFINED;
Log::MessageType clevel = Log::ERROR;;
rc = conf.get("LOG", logs);
if ( rc != 0 )
{
string value;
int ilevel;
const VectorAttribute * log = static_cast<const VectorAttribute *>
(logs[0]);
value = log->vector_value("SYSTEM");
log_system = NebulaLog::str_to_type(value);
value = log->vector_value("DEBUG_LEVEL");
ilevel = atoi(value.c_str());
if (0 <= ilevel && ilevel <= 3 )
{
clevel = static_cast<Log::MessageType>(ilevel);
}
}
// Start the log system
if ( log_system != NebulaLog::UNDEFINED )
{
NebulaLog::init_log_system(log_system,
clevel,
log_file.c_str(),
ios_base::trunc,
"mm_sched");
}
else
{
throw runtime_error("Unknown LOG_SYSTEM.");
}
NebulaLog::log("SCHED", Log::INFO, "Init Scheduler Log system");
}
catch(runtime_error &)
{
throw;
}
oss.str("");
oss << "Starting Scheduler Daemon" << endl;
@ -167,7 +203,6 @@ void Scheduler::start()
throw;
}
xmlInitParser();
// -----------------------------------------------------------

View File

@ -29,6 +29,7 @@ void SchedulerTemplate::set_conf_default()
SingleAttribute * attribute;
VectorAttribute * vattribute;
string value;
map<string,string> vvalue;
/*
#*******************************************************************************
@ -42,6 +43,7 @@ void SchedulerTemplate::set_conf_default()
# DEFAULT_SCHED
# LIVE_RESCHEDS
# HYPERVISOR_MEM
# LOG
#-------------------------------------------------------------------------------
*/
// ONED_PORT
@ -81,7 +83,7 @@ void SchedulerTemplate::set_conf_default()
conf_default.insert(make_pair(attribute->name(),attribute));
//DEFAULT_SCHED
map<string,string> vvalue;
vvalue.clear();
vvalue.insert(make_pair("POLICY","1"));
vattribute = new VectorAttribute("DEFAULT_SCHED",vvalue);
@ -92,6 +94,14 @@ void SchedulerTemplate::set_conf_default()
attribute = new SingleAttribute("HYPERVISOR_MEM",value);
conf_default.insert(make_pair(attribute->name(),attribute));
//LOG CONFIGURATION
vvalue.clear();
vvalue.insert(make_pair("SYSTEM","file"));
vvalue.insert(make_pair("DEBUG_LEVEL","3"));
vattribute = new VectorAttribute("LOG",vvalue);
conf_default.insert(make_pair(vattribute->name(),vattribute));
}
/* -------------------------------------------------------------------------- */

View File

@ -191,10 +191,31 @@ int VirtualMachine::select(SqlDB * db)
//--------------------------------------------------------------------------
try
{
Log::MessageType clevel;
Log::MessageType clevel;
NebulaLog::LogType log_system;
clevel = nd.get_debug_level();
_log = new FileLog(nd.get_vm_log_filename(oid), clevel);
log_system = nd.get_log_system();
clevel = nd.get_debug_level();
switch(log_system)
{
case NebulaLog::FILE_TS:
case NebulaLog::FILE:
_log = new FileLog(nd.get_vm_log_filename(oid), clevel);
break;
case NebulaLog::SYSLOG:
_log = new SysLogResource(oid, obj_type, clevel);
break;
case NebulaLog::CERR:
_log = new CerrLog(clevel);
break;
default:
throw runtime_error("Unknown log system.");
break;
}
}
catch(exception &e)
{