mirror of https://github.com/Icinga/icinga2.git
parent
043302cee4
commit
264fdf40d5
|
@ -2,6 +2,7 @@ add_subdirectory(checker)
|
|||
add_subdirectory(cluster)
|
||||
add_subdirectory(compat)
|
||||
add_subdirectory(db_ido_mysql)
|
||||
add_subdirectory(db_ido_pgsql)
|
||||
add_subdirectory(demo)
|
||||
add_subdirectory(livestatus)
|
||||
add_subdirectory(notification)
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
# Icinga 2
|
||||
# Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/)
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU General Public License
|
||||
# as published by the Free Software Foundation; either version 2
|
||||
# of the License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software Foundation
|
||||
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
find_package(PostgreSQL)
|
||||
|
||||
if(PostgreSQL_FOUND)
|
||||
mkclass_target(idopgsqlconnection.ti idopgsqlconnection.th)
|
||||
|
||||
mkembedconfig_target(db_ido_pgsql-type.conf db_ido_pgsql-type.cpp)
|
||||
|
||||
add_library(db_ido_pgsql SHARED idopgsqlconnection.cpp idopgsqlconnection.th db_ido_pgsql-type.cpp)
|
||||
|
||||
include_directories(${PostgreSQL_INCLUDE_DIR})
|
||||
target_link_libraries(db_ido_pgsql ${Boost_LIBRARIES} ${PostgreSQL_LIBRARIES} base config icinga db_ido)
|
||||
|
||||
set_target_properties (
|
||||
db_ido_pgsql PROPERTIES
|
||||
INSTALL_RPATH ${CMAKE_INSTALL_FULL_LIBDIR}/icinga2
|
||||
)
|
||||
|
||||
install(
|
||||
TARGETS db_ido_pgsql
|
||||
RUNTIME DESTINATION ${CMAKE_INSTALL_SBINDIR}
|
||||
LIBRARY DESTINATION ${CMAKE_INSTALL_FULL_LIBDIR}/icinga2
|
||||
)
|
||||
endif()
|
|
@ -0,0 +1,31 @@
|
|||
/******************************************************************************
|
||||
* Icinga 2 *
|
||||
* Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/) *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or *
|
||||
* modify it under the terms of the GNU General Public License *
|
||||
* as published by the Free Software Foundation; either version 2 *
|
||||
* of the License, or (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the Free Software Foundation *
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
||||
******************************************************************************/
|
||||
|
||||
type IdoPgsqlConnection inherits DbConnection {
|
||||
%attribute string "host",
|
||||
%attribute number "port",
|
||||
|
||||
%attribute string "user",
|
||||
%attribute string "password",
|
||||
|
||||
%attribute string "database",
|
||||
|
||||
%attribute string "instance_name",
|
||||
%attribute string "instance_description"
|
||||
}
|
|
@ -0,0 +1,601 @@
|
|||
/******************************************************************************
|
||||
* Icinga 2 *
|
||||
* Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/) *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or *
|
||||
* modify it under the terms of the GNU General Public License *
|
||||
* as published by the Free Software Foundation; either version 2 *
|
||||
* of the License, or (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the Free Software Foundation *
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
||||
******************************************************************************/
|
||||
|
||||
#include "base/logger_fwd.h"
|
||||
#include "base/objectlock.h"
|
||||
#include "base/convert.h"
|
||||
#include "base/utility.h"
|
||||
#include "base/application.h"
|
||||
#include "base/dynamictype.h"
|
||||
#include "db_ido/dbtype.h"
|
||||
#include "db_ido/dbvalue.h"
|
||||
#include "db_ido_pgsql/idopgsqlconnection.h"
|
||||
#include <boost/exception/diagnostic_information.hpp>
|
||||
#include <boost/tuple/tuple.hpp>
|
||||
#include <boost/smart_ptr/make_shared.hpp>
|
||||
#include <boost/foreach.hpp>
|
||||
|
||||
using namespace icinga;
|
||||
|
||||
REGISTER_TYPE(IdoPgsqlConnection);
|
||||
|
||||
#define SCHEMA_VERSION "1.10.0"
|
||||
|
||||
void IdoPgsqlConnection::Start(void)
|
||||
{
|
||||
DbConnection::Start();
|
||||
|
||||
m_Connection = NULL;
|
||||
|
||||
m_TxTimer = boost::make_shared<Timer>();
|
||||
m_TxTimer->SetInterval(5);
|
||||
m_TxTimer->OnTimerExpired.connect(boost::bind(&IdoPgsqlConnection::TxTimerHandler, this));
|
||||
m_TxTimer->Start();
|
||||
|
||||
m_ReconnectTimer = boost::make_shared<Timer>();
|
||||
m_ReconnectTimer->SetInterval(10);
|
||||
m_ReconnectTimer->OnTimerExpired.connect(boost::bind(&IdoPgsqlConnection::ReconnectTimerHandler, this));
|
||||
m_ReconnectTimer->Start();
|
||||
m_ReconnectTimer->Reschedule(0);
|
||||
|
||||
ASSERT(PQisthreadsafe());
|
||||
}
|
||||
|
||||
void IdoPgsqlConnection::Stop(void)
|
||||
{
|
||||
m_QueryQueue.Enqueue(boost::bind(&IdoPgsqlConnection::Disconnect, this));
|
||||
m_QueryQueue.Join();
|
||||
}
|
||||
|
||||
void IdoPgsqlConnection::AssertOnWorkQueue(void)
|
||||
{
|
||||
ASSERT(boost::this_thread::get_id() == m_QueryQueue.GetThreadId());
|
||||
}
|
||||
|
||||
void IdoPgsqlConnection::Disconnect(void)
|
||||
{
|
||||
AssertOnWorkQueue();
|
||||
|
||||
boost::mutex::scoped_lock lock(m_ConnectionMutex);
|
||||
|
||||
if (!m_Connection)
|
||||
return;
|
||||
|
||||
Query("COMMIT");
|
||||
PQfinish(m_Connection);
|
||||
|
||||
m_Connection = NULL;
|
||||
}
|
||||
|
||||
void IdoPgsqlConnection::TxTimerHandler(void)
|
||||
{
|
||||
m_QueryQueue.Enqueue(boost::bind(&IdoPgsqlConnection::NewTransaction, this));
|
||||
}
|
||||
|
||||
void IdoPgsqlConnection::NewTransaction(void)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_ConnectionMutex);
|
||||
|
||||
if (!m_Connection)
|
||||
return;
|
||||
|
||||
Query("COMMIT");
|
||||
Query("BEGIN");
|
||||
}
|
||||
|
||||
void IdoPgsqlConnection::ReconnectTimerHandler(void)
|
||||
{
|
||||
m_QueryQueue.Enqueue(boost::bind(&IdoPgsqlConnection::Reconnect, this));
|
||||
}
|
||||
|
||||
void IdoPgsqlConnection::Reconnect(void)
|
||||
{
|
||||
AssertOnWorkQueue();
|
||||
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_ConnectionMutex);
|
||||
|
||||
bool reconnect = false;
|
||||
|
||||
if (m_Connection) {
|
||||
/* Check if we're really still connected */
|
||||
try {
|
||||
Query("SELECT 1");
|
||||
return;
|
||||
} catch (const std::exception&) {
|
||||
PQfinish(m_Connection);
|
||||
m_Connection = NULL;
|
||||
reconnect = true;
|
||||
}
|
||||
}
|
||||
|
||||
String ihost, iport, iuser, ipasswd, idb;
|
||||
const char *host, *port, *user , *passwd, *db;
|
||||
|
||||
ihost = GetHost();
|
||||
iport = GetPort();
|
||||
iuser = GetUser();
|
||||
ipasswd = GetPassword();
|
||||
idb = GetDatabase();
|
||||
|
||||
host = (!ihost.IsEmpty()) ? ihost.CStr() : NULL;
|
||||
port = (!iport.IsEmpty()) ? iport.CStr() : NULL;
|
||||
user = (!iuser.IsEmpty()) ? iuser.CStr() : NULL;
|
||||
passwd = (!ipasswd.IsEmpty()) ? ipasswd.CStr() : NULL;
|
||||
db = (!idb.IsEmpty()) ? idb.CStr() : NULL;
|
||||
|
||||
m_Connection = PQsetdbLogin(host, port, NULL, NULL, db, user, passwd);
|
||||
|
||||
if (!m_Connection)
|
||||
return;
|
||||
|
||||
if (PQstatus(m_Connection) != CONNECTION_OK) {
|
||||
String message = PQerrorMessage(m_Connection);
|
||||
PQfinish(m_Connection);
|
||||
m_Connection = NULL;
|
||||
|
||||
BOOST_THROW_EXCEPTION(std::runtime_error(message));
|
||||
}
|
||||
|
||||
String dbVersionName = "idoutils";
|
||||
Array::Ptr version_rows = Query("SELECT version FROM " + GetTablePrefix() + "dbversion WHERE name='" + Escape(dbVersionName) + "'");
|
||||
|
||||
if (version_rows->GetLength() == 0)
|
||||
BOOST_THROW_EXCEPTION(std::runtime_error("Schema does not provide any valid version! Verify your schema installation."));
|
||||
|
||||
Dictionary::Ptr version_row = version_rows->Get(0);
|
||||
String version = version_row->Get("version");
|
||||
|
||||
if (Utility::CompareVersion(SCHEMA_VERSION, version) < 0) {
|
||||
BOOST_THROW_EXCEPTION(std::runtime_error("Schema version '" + version + "' does not match the required version '" +
|
||||
SCHEMA_VERSION + "'! Please check the upgrade documentation."));
|
||||
}
|
||||
|
||||
String instanceName = GetInstanceName();
|
||||
|
||||
Array::Ptr rows = Query("SELECT instance_id FROM " + GetTablePrefix() + "instances WHERE instance_name = '" + Escape(instanceName) + "'");
|
||||
|
||||
if (rows->GetLength() == 0) {
|
||||
Query("INSERT INTO " + GetTablePrefix() + "instances (instance_name, instance_description) VALUES ('" + Escape(instanceName) + "', '" + Escape(GetInstanceDescription()) + "')");
|
||||
m_InstanceID = GetSequenceValue(GetTablePrefix() + "instances", "instance_id");
|
||||
} else {
|
||||
Dictionary::Ptr row = rows->Get(0);
|
||||
m_InstanceID = DbReference(row->Get("instance_id"));
|
||||
}
|
||||
|
||||
std::ostringstream msgbuf;
|
||||
msgbuf << "pgSQL IDO instance id: " << static_cast<long>(m_InstanceID) << " (schema version: '" + version + "')";
|
||||
Log(LogInformation, "db_ido_pgsql", msgbuf.str());
|
||||
|
||||
/* record connection */
|
||||
Query("INSERT INTO " + GetTablePrefix() + "conninfo " +
|
||||
"(instance_id, connect_time, last_checkin_time, agent_name, agent_version, connect_type, data_start_time) VALUES ("
|
||||
+ Convert::ToString(static_cast<long>(m_InstanceID)) + ", NOW(), NOW(), 'icinga2 db_ido_pgsql', '" + Escape(Application::GetVersion())
|
||||
+ "', '" + (reconnect ? "RECONNECT" : "INITIAL") + "', NOW())");
|
||||
|
||||
/* clear config tables for the initial config dump */
|
||||
ClearConfigTables();
|
||||
|
||||
Query("UPDATE " + GetTablePrefix() + "objects SET is_active = 0");
|
||||
|
||||
std::ostringstream q1buf;
|
||||
q1buf << "SELECT object_id, objecttype_id, name1, name2 FROM " + GetTablePrefix() + "objects WHERE instance_id = " << static_cast<long>(m_InstanceID);
|
||||
rows = Query(q1buf.str());
|
||||
|
||||
ObjectLock olock(rows);
|
||||
BOOST_FOREACH(const Dictionary::Ptr& row, rows) {
|
||||
DbType::Ptr dbtype = DbType::GetByID(row->Get("objecttype_id"));
|
||||
|
||||
if (!dbtype)
|
||||
continue;
|
||||
|
||||
DbObject::Ptr dbobj = dbtype->GetOrCreateObjectByName(row->Get("name1"), row->Get("name2"));
|
||||
SetObjectID(dbobj, DbReference(row->Get("object_id")));
|
||||
}
|
||||
|
||||
Query("BEGIN");
|
||||
}
|
||||
|
||||
UpdateAllObjects();
|
||||
}
|
||||
|
||||
void IdoPgsqlConnection::ClearConfigTables(void)
|
||||
{
|
||||
/* TODO make hardcoded table names modular */
|
||||
ClearConfigTable("commands");
|
||||
ClearConfigTable("contact_addresses");
|
||||
ClearConfigTable("contact_notificationcommands");
|
||||
ClearConfigTable("contactgroup_members");
|
||||
ClearConfigTable("contactgroups");
|
||||
ClearConfigTable("contacts");
|
||||
ClearConfigTable("customvariables");
|
||||
ClearConfigTable("host_contactgroups");
|
||||
ClearConfigTable("host_contacts");
|
||||
ClearConfigTable("host_parenthosts");
|
||||
ClearConfigTable("hostdependencies");
|
||||
ClearConfigTable("hostgroup_members");
|
||||
ClearConfigTable("hostgroups");
|
||||
ClearConfigTable("hosts");
|
||||
ClearConfigTable("service_contactgroups");
|
||||
ClearConfigTable("service_contacts");
|
||||
ClearConfigTable("servicedependencies");
|
||||
ClearConfigTable("servicegroup_members");
|
||||
ClearConfigTable("servicegroups");
|
||||
ClearConfigTable("services");
|
||||
ClearConfigTable("timeperiod_timeranges");
|
||||
ClearConfigTable("timeperiods");
|
||||
}
|
||||
|
||||
void IdoPgsqlConnection::ClearConfigTable(const String& table)
|
||||
{
|
||||
Query("DELETE FROM " + GetTablePrefix() + table + " WHERE instance_id = " + Convert::ToString(static_cast<long>(m_InstanceID)));
|
||||
}
|
||||
|
||||
Array::Ptr IdoPgsqlConnection::Query(const String& query)
|
||||
{
|
||||
AssertOnWorkQueue();
|
||||
|
||||
Log(LogDebug, "db_ido_pgsql", "Query: " + query);
|
||||
|
||||
PGresult *result = PQexec(m_Connection, query.CStr());
|
||||
|
||||
if (!result)
|
||||
BOOST_THROW_EXCEPTION(std::runtime_error("unknown error during pgSQL query"));
|
||||
|
||||
if (PQresultStatus(result) == PGRES_COMMAND_OK)
|
||||
return Array::Ptr();
|
||||
|
||||
if (PQresultStatus(result) != PGRES_TUPLES_OK) {
|
||||
String message = PQresultErrorMessage(result);
|
||||
PQclear(result);
|
||||
|
||||
BOOST_THROW_EXCEPTION(std::runtime_error(message));
|
||||
}
|
||||
|
||||
Array::Ptr rows = boost::make_shared<Array>();
|
||||
|
||||
int rownum = 0;
|
||||
|
||||
for (;;) {
|
||||
Dictionary::Ptr row = FetchRow(result, rownum);
|
||||
|
||||
if (!row)
|
||||
break;
|
||||
|
||||
rows->Add(row);
|
||||
|
||||
rownum++;
|
||||
}
|
||||
|
||||
PQclear(result);
|
||||
|
||||
return rows;
|
||||
}
|
||||
|
||||
DbReference IdoPgsqlConnection::GetSequenceValue(const String& table, const String& column)
|
||||
{
|
||||
AssertOnWorkQueue();
|
||||
|
||||
Array::Ptr rows = Query("SELECT CURRVAL(pg_get_serial_sequence('" + Escape(table) + "', '" + Escape(column) + "')) AS id");
|
||||
|
||||
ASSERT(rows->GetLength() == 1);
|
||||
|
||||
Dictionary::Ptr row = rows->Get(0);
|
||||
|
||||
return DbReference(Convert::ToLong(row->Get("id")));
|
||||
}
|
||||
|
||||
String IdoPgsqlConnection::Escape(const String& s)
|
||||
{
|
||||
AssertOnWorkQueue();
|
||||
|
||||
ssize_t length = s.GetLength();
|
||||
char *to = new char[s.GetLength() * 2 + 1];
|
||||
|
||||
PQescapeStringConn(m_Connection, to, s.CStr(), length, NULL);
|
||||
|
||||
String result = String(to);
|
||||
|
||||
delete [] to;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Dictionary::Ptr IdoPgsqlConnection::FetchRow(PGresult *result, int row)
|
||||
{
|
||||
AssertOnWorkQueue();
|
||||
|
||||
if (row >= PQntuples(result))
|
||||
return Dictionary::Ptr();
|
||||
|
||||
int columns = PQnfields(result);
|
||||
|
||||
Dictionary::Ptr dict = boost::make_shared<Dictionary>();
|
||||
|
||||
for (int column = 0; column < columns; column++) {
|
||||
Value value;
|
||||
|
||||
if (!PQgetisnull(result, row, column))
|
||||
value = PQgetvalue(result, row, column);
|
||||
|
||||
dict->Set(PQfname(result, column), value);
|
||||
}
|
||||
|
||||
return dict;
|
||||
}
|
||||
|
||||
void IdoPgsqlConnection::ActivateObject(const DbObject::Ptr& dbobj)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_ConnectionMutex);
|
||||
InternalActivateObject(dbobj);
|
||||
}
|
||||
|
||||
void IdoPgsqlConnection::InternalActivateObject(const DbObject::Ptr& dbobj)
|
||||
{
|
||||
if (!m_Connection)
|
||||
return;
|
||||
|
||||
DbReference dbref = GetObjectID(dbobj);
|
||||
std::ostringstream qbuf;
|
||||
|
||||
if (!dbref.IsValid()) {
|
||||
qbuf << "INSERT INTO " + GetTablePrefix() + "objects (instance_id, objecttype_id, name1, name2, is_active) VALUES ("
|
||||
<< static_cast<long>(m_InstanceID) << ", " << dbobj->GetType()->GetTypeID() << ", "
|
||||
<< "'" << Escape(dbobj->GetName1()) << "', '" << Escape(dbobj->GetName2()) << "', 1)";
|
||||
Query(qbuf.str());
|
||||
SetObjectID(dbobj, GetSequenceValue(GetTablePrefix() + "objects", "object_id"));
|
||||
} else {
|
||||
qbuf << "UPDATE " + GetTablePrefix() + "objects SET is_active = 1 WHERE object_id = " << static_cast<long>(dbref);
|
||||
Query(qbuf.str());
|
||||
}
|
||||
}
|
||||
|
||||
void IdoPgsqlConnection::DeactivateObject(const DbObject::Ptr& dbobj)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_ConnectionMutex);
|
||||
|
||||
if (!m_Connection)
|
||||
return;
|
||||
|
||||
DbReference dbref = GetObjectID(dbobj);
|
||||
|
||||
if (!dbref.IsValid())
|
||||
return;
|
||||
|
||||
std::ostringstream qbuf;
|
||||
qbuf << "UPDATE " + GetTablePrefix() + "objects SET is_active = 0 WHERE object_id = " << static_cast<long>(dbref);
|
||||
Query(qbuf.str());
|
||||
|
||||
/* Note that we're _NOT_ clearing the db refs via SetReference/SetConfigUpdate/SetStatusUpdate
|
||||
* because the object is still in the database. */
|
||||
}
|
||||
|
||||
/* caller must hold m_ConnectionMutex */
|
||||
bool IdoPgsqlConnection::FieldToEscapedString(const String& key, const Value& value, Value *result)
|
||||
{
|
||||
if (key == "instance_id") {
|
||||
*result = static_cast<long>(m_InstanceID);
|
||||
return true;
|
||||
}
|
||||
if (key == "notification_id") {
|
||||
*result = static_cast<long>(m_LastNotificationID);
|
||||
return true;
|
||||
}
|
||||
|
||||
Value rawvalue = DbValue::ExtractValue(value);
|
||||
|
||||
if (rawvalue.IsObjectType<DynamicObject>()) {
|
||||
DbObject::Ptr dbobjcol = DbObject::GetOrCreateByObject(rawvalue);
|
||||
|
||||
if (!dbobjcol) {
|
||||
*result = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
DbReference dbrefcol;
|
||||
|
||||
if (DbValue::IsObjectInsertID(value)) {
|
||||
dbrefcol = GetInsertID(dbobjcol);
|
||||
|
||||
ASSERT(dbrefcol.IsValid());
|
||||
} else {
|
||||
dbrefcol = GetObjectID(dbobjcol);
|
||||
|
||||
if (!dbrefcol.IsValid()) {
|
||||
InternalActivateObject(dbobjcol);
|
||||
|
||||
dbrefcol = GetObjectID(dbobjcol);
|
||||
|
||||
if (!dbrefcol.IsValid())
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
*result = static_cast<long>(dbrefcol);
|
||||
} else if (DbValue::IsTimestamp(value)) {
|
||||
long ts = rawvalue;
|
||||
std::ostringstream msgbuf;
|
||||
msgbuf << "TO_TIMESTAMP(" << ts << ")";
|
||||
*result = Value(msgbuf.str());
|
||||
} else if (DbValue::IsTimestampNow(value)) {
|
||||
*result = "NOW()";
|
||||
} else {
|
||||
*result = "'" + Escape(rawvalue) + "'";
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void IdoPgsqlConnection::ExecuteQuery(const DbQuery& query)
|
||||
{
|
||||
m_QueryQueue.Enqueue(boost::bind(&IdoPgsqlConnection::InternalExecuteQuery, this, query));
|
||||
}
|
||||
|
||||
void IdoPgsqlConnection::InternalExecuteQuery(const DbQuery& query)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_ConnectionMutex);
|
||||
|
||||
ASSERT(query.Category != DbCatInvalid);
|
||||
|
||||
if ((query.Category & GetCategories()) == 0)
|
||||
return;
|
||||
|
||||
if (!m_Connection)
|
||||
return;
|
||||
|
||||
std::ostringstream qbuf, where;
|
||||
int type;
|
||||
|
||||
if (query.WhereCriteria) {
|
||||
where << " WHERE ";
|
||||
|
||||
ObjectLock olock(query.WhereCriteria);
|
||||
String key;
|
||||
Value value;
|
||||
bool first = true;
|
||||
|
||||
BOOST_FOREACH(boost::tie(key, value), query.WhereCriteria) {
|
||||
if (!FieldToEscapedString(key, value, &value))
|
||||
return;
|
||||
|
||||
if (!first)
|
||||
where << " AND ";
|
||||
|
||||
where << key << " = " << value;
|
||||
|
||||
if (first)
|
||||
first = false;
|
||||
}
|
||||
}
|
||||
|
||||
if ((query.Type & DbQueryInsert) && (query.Type & DbQueryUpdate)) {
|
||||
bool hasid = false;
|
||||
|
||||
ASSERT(query.Object);
|
||||
|
||||
if (query.ConfigUpdate)
|
||||
hasid = GetConfigUpdate(query.Object);
|
||||
else if (query.StatusUpdate)
|
||||
hasid = GetStatusUpdate(query.Object);
|
||||
else
|
||||
ASSERT(!"Invalid query flags.");
|
||||
|
||||
if (hasid)
|
||||
type = DbQueryUpdate;
|
||||
else {
|
||||
if (query.WhereCriteria)
|
||||
Query("DELETE FROM " + GetTablePrefix() + query.Table + where.str());
|
||||
|
||||
type = DbQueryInsert;
|
||||
}
|
||||
} else
|
||||
type = query.Type;
|
||||
|
||||
switch (type) {
|
||||
case DbQueryInsert:
|
||||
qbuf << "INSERT INTO " << GetTablePrefix() << query.Table;
|
||||
break;
|
||||
case DbQueryUpdate:
|
||||
qbuf << "UPDATE " << GetTablePrefix() << query.Table << " SET";
|
||||
break;
|
||||
case DbQueryDelete:
|
||||
qbuf << "DELETE FROM " << GetTablePrefix() << query.Table;
|
||||
break;
|
||||
default:
|
||||
ASSERT(!"Invalid query type.");
|
||||
}
|
||||
|
||||
if (type == DbQueryInsert || type == DbQueryUpdate) {
|
||||
String cols;
|
||||
String values;
|
||||
|
||||
ObjectLock olock(query.Fields);
|
||||
|
||||
String key;
|
||||
Value value;
|
||||
bool first = true;
|
||||
BOOST_FOREACH(boost::tie(key, value), query.Fields) {
|
||||
if (!FieldToEscapedString(key, value, &value))
|
||||
return;
|
||||
|
||||
if (type == DbQueryInsert) {
|
||||
if (!first) {
|
||||
cols += ", ";
|
||||
values += ", ";
|
||||
}
|
||||
|
||||
cols += key;
|
||||
values += Convert::ToString(value);
|
||||
} else {
|
||||
if (!first)
|
||||
qbuf << ", ";
|
||||
|
||||
qbuf << " " << key << " = " << value;
|
||||
}
|
||||
|
||||
if (first)
|
||||
first = false;
|
||||
}
|
||||
|
||||
if (type == DbQueryInsert)
|
||||
qbuf << " (" << cols << ") VALUES (" << values << ")";
|
||||
}
|
||||
|
||||
if (type != DbQueryInsert)
|
||||
qbuf << where.str();
|
||||
|
||||
Query(qbuf.str());
|
||||
|
||||
if (query.Object) {
|
||||
if (query.ConfigUpdate)
|
||||
SetConfigUpdate(query.Object, true);
|
||||
else if (query.StatusUpdate)
|
||||
SetStatusUpdate(query.Object, true);
|
||||
|
||||
if (type == DbQueryInsert && query.ConfigUpdate) {
|
||||
String idField = query.IdColumn;
|
||||
|
||||
if (idField.IsEmpty())
|
||||
idField = query.Table.SubStr(0, query.Table.GetLength() - 1) + "_id";
|
||||
|
||||
SetInsertID(query.Object, GetSequenceValue(GetTablePrefix() + query.Table, idField));
|
||||
}
|
||||
}
|
||||
if (type == DbQueryInsert && query.Table == "notifications") { // FIXME remove hardcoded table name
|
||||
m_LastNotificationID = GetSequenceValue(GetTablePrefix() + "notifications", "notification_id");
|
||||
Log(LogDebug, "db_ido", "saving contactnotification notification_id=" + Convert::ToString(static_cast<long>(m_LastNotificationID)));
|
||||
}
|
||||
}
|
||||
|
||||
void IdoPgsqlConnection::CleanUpExecuteQuery(const String& table, const String& time_column, double max_age)
|
||||
{
|
||||
m_QueryQueue.Enqueue(boost::bind(&IdoPgsqlConnection::InternalCleanUpExecuteQuery, this, table, time_column, max_age));
|
||||
}
|
||||
|
||||
void IdoPgsqlConnection::InternalCleanUpExecuteQuery(const String& table, const String& time_column, double max_age)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_ConnectionMutex);
|
||||
|
||||
if (!m_Connection)
|
||||
return;
|
||||
|
||||
Query("DELETE FROM " + GetTablePrefix() + table + " WHERE instance_id = " +
|
||||
Convert::ToString(static_cast<long>(m_InstanceID)) + " AND " + time_column +
|
||||
" < TO_TIMESTAMP(" + Convert::ToString(static_cast<long>(max_age)) + ")");
|
||||
}
|
|
@ -0,0 +1,91 @@
|
|||
/******************************************************************************
|
||||
* Icinga 2 *
|
||||
* Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/) *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or *
|
||||
* modify it under the terms of the GNU General Public License *
|
||||
* as published by the Free Software Foundation; either version 2 *
|
||||
* of the License, or (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the Free Software Foundation *
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef IDOPGSQLCONNECTION_H
|
||||
#define IDOPGSQLCONNECTION_H
|
||||
|
||||
#include "db_ido_pgsql/idopgsqlconnection.th"
|
||||
#include "base/array.h"
|
||||
#include "base/timer.h"
|
||||
#include "base/workqueue.h"
|
||||
#include <postgresql/libpq-fe.h>
|
||||
|
||||
namespace icinga
|
||||
{
|
||||
|
||||
/**
|
||||
* An IDO pgSQL database connection.
|
||||
*
|
||||
* @ingroup ido
|
||||
*/
|
||||
class IdoPgsqlConnection : public ReflectionObjectImpl<IdoPgsqlConnection>
|
||||
{
|
||||
public:
|
||||
DECLARE_PTR_TYPEDEFS(IdoPgsqlConnection);
|
||||
|
||||
//virtual void UpdateObject(const DbObject::Ptr& dbobj, DbUpdateType kind);
|
||||
|
||||
protected:
|
||||
virtual void Start(void);
|
||||
virtual void Stop(void);
|
||||
|
||||
virtual void ActivateObject(const DbObject::Ptr& dbobj);
|
||||
virtual void DeactivateObject(const DbObject::Ptr& dbobj);
|
||||
virtual void ExecuteQuery(const DbQuery& query);
|
||||
virtual void CleanUpExecuteQuery(const String& table, const String& time_key, double time_value);
|
||||
|
||||
private:
|
||||
DbReference m_InstanceID;
|
||||
DbReference m_LastNotificationID;
|
||||
|
||||
WorkQueue m_QueryQueue;
|
||||
|
||||
boost::mutex m_ConnectionMutex;
|
||||
PGconn *m_Connection;
|
||||
|
||||
Timer::Ptr m_ReconnectTimer;
|
||||
Timer::Ptr m_TxTimer;
|
||||
|
||||
Array::Ptr Query(const String& query);
|
||||
DbReference GetSequenceValue(const String& table, const String& column);
|
||||
String Escape(const String& s);
|
||||
Dictionary::Ptr FetchRow(PGresult *result, int row);
|
||||
|
||||
bool FieldToEscapedString(const String& key, const Value& value, Value *result);
|
||||
void InternalActivateObject(const DbObject::Ptr& dbobj);
|
||||
|
||||
void Disconnect(void);
|
||||
void NewTransaction(void);
|
||||
void Reconnect(void);
|
||||
|
||||
void AssertOnWorkQueue(void);
|
||||
|
||||
void TxTimerHandler(void);
|
||||
void ReconnectTimerHandler(void);
|
||||
|
||||
void InternalExecuteQuery(const DbQuery& query);
|
||||
void InternalCleanUpExecuteQuery(const String& table, const String& time_key, double time_value);
|
||||
|
||||
void ClearConfigTables(void);
|
||||
void ClearConfigTable(const String& table);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif /* IDOPGSQLCONNECTION_H */
|
|
@ -0,0 +1,29 @@
|
|||
#include "db_ido/dbconnection.h"
|
||||
|
||||
namespace icinga
|
||||
{
|
||||
|
||||
class IdoPgsqlConnection : DbConnection
|
||||
{
|
||||
[config] String host {
|
||||
default {{{ return "localhost"; }}}
|
||||
};
|
||||
[config] String port {
|
||||
default {{{ return "5432"; }}}
|
||||
};
|
||||
[config] String user {
|
||||
default {{{ return "icinga"; }}}
|
||||
};
|
||||
[config] String password {
|
||||
default {{{ return "icinga"; }}}
|
||||
};
|
||||
[config] String database {
|
||||
default {{{ return "icinga"; }}}
|
||||
};
|
||||
[config] String instance_name {
|
||||
default {{{ return "default"; }}}
|
||||
};
|
||||
[config] String instance_description;
|
||||
};
|
||||
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -504,7 +504,7 @@ Attributes:
|
|||
|
||||
### <a id="objecttype-idomysqlconnection"></a> IdoMySqlConnection
|
||||
|
||||
IDO DB schema compatible output into MySQL database.
|
||||
IDO database adapter for MySQL.
|
||||
|
||||
Example:
|
||||
|
||||
|
@ -584,6 +584,88 @@ Data Categories:
|
|||
|
||||
Multiple categories can be combined using the `|` operator.
|
||||
|
||||
### <a id="objecttype-idomysqlconnection"></a> IdoPgSqlConnection
|
||||
|
||||
IDO database adapter for PostgreSQL.
|
||||
|
||||
Example:
|
||||
|
||||
library "db_ido_pgsql"
|
||||
|
||||
object IdoMysqlConnection "pgsql-ido" {
|
||||
host = "127.0.0.1",
|
||||
port = 5432,
|
||||
user = "icinga",
|
||||
password = "icinga",
|
||||
database = "icinga",
|
||||
table_prefix = "icinga_",
|
||||
instance_name = "icinga2",
|
||||
instance_description = "icinga2 dev instance",
|
||||
|
||||
cleanup = {
|
||||
downtimehistory_age = 48h,
|
||||
logentries_age = 31d,
|
||||
},
|
||||
|
||||
categories = (DbCatConfig | DbCatState)
|
||||
}
|
||||
|
||||
Attributes:
|
||||
|
||||
Name |Description
|
||||
----------------|----------------
|
||||
host |**Optional.** PostgreSQL database host address. Defaults to "localhost".
|
||||
port |**Optional.** PostgreSQL database port. Defaults to "5432".
|
||||
user |**Optional.** PostgreSQL database user with read/write permission to the icinga database. Defaults to "icinga".
|
||||
password |**Optional.** PostgreSQL database user's password. Defaults to "icinga".
|
||||
database |**Optional.** PostgreSQL database name. Defaults to "icinga".
|
||||
table\_prefix |**Optional.** PostgreSQL database table prefix. Defaults to "icinga\_".
|
||||
instance\_name |**Optional.** Unique identifier for the local Icinga 2 instance. Defaults to "default".
|
||||
instance\_description|**Optional.** Description for the Icinga 2 instance.
|
||||
cleanup |**Optional.** Dictionary with items for historical table cleanup.
|
||||
categories |**Optional.** The types of information that should be written to the database.
|
||||
|
||||
Cleanup Items:
|
||||
|
||||
Name | Description
|
||||
----------------|----------------
|
||||
acknowledgements_age |**Optional.** Max age for acknowledgements table rows (entry_time). Defaults to 0 (never).
|
||||
commenthistory_age |**Optional.** Max age for commenthistory table rows (entry_time). Defaults to 0 (never).
|
||||
contactnotifications_age |**Optional.** Max age for contactnotifications table rows (start_time). Defaults to 0 (never).
|
||||
contactnotificationmethods_age |**Optional.** Max age for contactnotificationmethods table rows (start_time). Defaults to 0 (never).
|
||||
downtimehistory_age |**Optional.** Max age for downtimehistory table rows (entry_time). Defaults to 0 (never).
|
||||
eventhandlers_age |**Optional.** Max age for eventhandlers table rows (start_time). Defaults to 0 (never).
|
||||
externalcommands_age |**Optional.** Max age for externalcommands table rows (entry_time). Defaults to 0 (never).
|
||||
flappinghistory_age |**Optional.** Max age for flappinghistory table rows (event_time). Defaults to 0 (never).
|
||||
hostchecks_age |**Optional.** Max age for hostchecks table rows (start_time). Defaults to 0 (never).
|
||||
logentries_age |**Optional.** Max age for logentries table rows (logentry_time). Defaults to 0 (never).
|
||||
notifications_age |**Optional.** Max age for notifications table rows (start_time). Defaults to 0 (never).
|
||||
processevents_age |**Optional.** Max age for processevents table rows (event_time). Defaults to 0 (never).
|
||||
statehistory_age |**Optional.** Max age for statehistory table rows (state_time). Defaults to 0 (never).
|
||||
servicechecks_age |**Optional.** Max age for servicechecks table rows (start_time). Defaults to 0 (never).
|
||||
systemcommands_age |**Optional.** Max age for systemcommands table rows (start_time). Defaults to 0 (never).
|
||||
|
||||
Data Categories:
|
||||
|
||||
Name | Description
|
||||
---------------------|----------------
|
||||
DbCatConfig | Configuration data
|
||||
DbCatState | Current state data
|
||||
DbCatAcknowledgement | Acknowledgements
|
||||
DbCatComment | Comments
|
||||
DbCatDowntime | Downtimes
|
||||
DbCatEventHandler | Event handler data
|
||||
DbCatExternalCommand | External commands
|
||||
DbCatFlapping | Flap detection data
|
||||
DbCatCheck | Check results
|
||||
DbCatLog | Log messages
|
||||
DbCatNotification | Notifications
|
||||
DbCatProgramStatus | Program status data
|
||||
DbCatRetention | Retention data
|
||||
DbCatStateHistory | Historical state data
|
||||
|
||||
Multiple categories can be combined using the `|` operator.
|
||||
|
||||
### <a id="objecttype-livestatuslistener"></a> LiveStatusListener
|
||||
|
||||
Livestatus API interface available as TCP or UNIX socket.
|
||||
|
|
|
@ -80,6 +80,7 @@ void DbConnection::ProgramStatusHandler(void)
|
|||
|
||||
DbQuery query2;
|
||||
query2.Table = "programstatus";
|
||||
query2.IdColumn = "programstatus_id";
|
||||
query2.Type = DbQueryInsert;
|
||||
query2.Category = DbCatProgramStatus;
|
||||
|
||||
|
|
|
@ -61,6 +61,7 @@ struct I2_DB_IDO_API DbQuery
|
|||
int Type;
|
||||
DbQueryCategory Category;
|
||||
String Table;
|
||||
String IdColumn;
|
||||
Dictionary::Ptr Fields;
|
||||
Dictionary::Ptr WhereCriteria;
|
||||
boost::shared_ptr<DbObject> Object;
|
||||
|
|
Loading…
Reference in New Issue