1 /******************************************************************************
3 * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "base/logger_fwd.h"
21 #include "base/objectlock.h"
22 #include "base/convert.h"
23 #include "base/utility.h"
24 #include "base/application.h"
25 #include "base/dynamictype.h"
26 #include "base/exception.h"
27 #include "base/statsfunction.h"
28 #include "db_ido/dbtype.h"
29 #include "db_ido/dbvalue.h"
30 #include "db_ido_mysql/idomysqlconnection.h"
31 #include <boost/tuple/tuple.hpp>
32 #include <boost/foreach.hpp>
34 using namespace icinga;
36 #define SCHEMA_VERSION "1.11.0"
38 REGISTER_TYPE(IdoMysqlConnection);
39 REGISTER_STATSFUNCTION(IdoMysqlConnectionStats, &IdoMysqlConnection::StatsFunc);
41 Value IdoMysqlConnection::StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata)
43 Dictionary::Ptr nodes = make_shared<Dictionary>();
45 BOOST_FOREACH(const IdoMysqlConnection::Ptr& idomysqlconnection, DynamicType::GetObjects<IdoMysqlConnection>()) {
46 size_t items = idomysqlconnection->m_QueryQueue.GetLength();
48 Dictionary::Ptr stats = make_shared<Dictionary>();
49 stats->Set("version", SCHEMA_VERSION);
50 stats->Set("instance_name", idomysqlconnection->GetInstanceName());
51 stats->Set("query_queue_items", items);
53 nodes->Set(idomysqlconnection->GetName(), stats);
55 perfdata->Set("idomysqlconnection_" + idomysqlconnection->GetName() + "_query_queue_items", Convert::ToDouble(items));
58 status->Set("idomysqlconnection", nodes);
63 void IdoMysqlConnection::Start(void)
65 DbConnection::Start();
69 m_QueryQueue.SetExceptionCallback(boost::bind(&IdoMysqlConnection::ExceptionHandler, this, _1));
71 m_TxTimer = make_shared<Timer>();
72 m_TxTimer->SetInterval(5);
73 m_TxTimer->OnTimerExpired.connect(boost::bind(&IdoMysqlConnection::TxTimerHandler, this));
76 m_ReconnectTimer = make_shared<Timer>();
77 m_ReconnectTimer->SetInterval(10);
78 m_ReconnectTimer->OnTimerExpired.connect(boost::bind(&IdoMysqlConnection::ReconnectTimerHandler, this));
79 m_ReconnectTimer->Start();
80 m_ReconnectTimer->Reschedule(0);
82 ASSERT(mysql_thread_safe());
85 void IdoMysqlConnection::Stop(void)
87 m_QueryQueue.Enqueue(boost::bind(&IdoMysqlConnection::Disconnect, this));
91 void IdoMysqlConnection::ExceptionHandler(boost::exception_ptr exp)
93 Log(LogCritical, "db_ido_mysql", "Exception during database operation: " + DiagnosticInformation(exp));
95 boost::mutex::scoped_lock lock(m_ConnectionMutex);
98 mysql_close(&m_Connection);
104 void IdoMysqlConnection::AssertOnWorkQueue(void)
106 ASSERT(boost::this_thread::get_id() == m_QueryQueue.GetThreadId());
109 void IdoMysqlConnection::Disconnect(void)
113 boost::mutex::scoped_lock lock(m_ConnectionMutex);
119 mysql_close(&m_Connection);
124 void IdoMysqlConnection::TxTimerHandler(void)
126 m_QueryQueue.Enqueue(boost::bind(&IdoMysqlConnection::NewTransaction, this), true);
129 void IdoMysqlConnection::NewTransaction(void)
131 boost::mutex::scoped_lock lock(m_ConnectionMutex);
140 void IdoMysqlConnection::ReconnectTimerHandler(void)
142 m_QueryQueue.Enqueue(boost::bind(&IdoMysqlConnection::Reconnect, this));
145 void IdoMysqlConnection::Reconnect(void)
149 CONTEXT("Reconnecting to MySQL IDO database '" + GetName() + "'");
151 std::vector<DbObject::Ptr> active_dbobjs;
154 boost::mutex::scoped_lock lock(m_ConnectionMutex);
156 bool reconnect = false;
159 /* Check if we're really still connected */
160 if (mysql_ping(&m_Connection) == 0)
163 mysql_close(&m_Connection);
170 String ihost, iuser, ipasswd, idb;
171 const char *host, *user , *passwd, *db;
176 ipasswd = GetPassword();
179 host = (!ihost.IsEmpty()) ? ihost.CStr() : NULL;
181 user = (!iuser.IsEmpty()) ? iuser.CStr() : NULL;
182 passwd = (!ipasswd.IsEmpty()) ? ipasswd.CStr() : NULL;
183 db = (!idb.IsEmpty()) ? idb.CStr() : NULL;
185 if (!mysql_init(&m_Connection))
186 BOOST_THROW_EXCEPTION(std::bad_alloc());
188 if (!mysql_real_connect(&m_Connection, host, user, passwd, db, port, NULL, CLIENT_FOUND_ROWS))
189 BOOST_THROW_EXCEPTION(std::runtime_error(mysql_error(&m_Connection)));
193 String dbVersionName = "idoutils";
194 IdoMysqlResult result = Query("SELECT version FROM " + GetTablePrefix() + "dbversion WHERE name='" + Escape(dbVersionName) + "'");
196 Dictionary::Ptr version_row = FetchRow(result);
199 BOOST_THROW_EXCEPTION(std::runtime_error("Schema does not provide any valid version! Verify your schema installation."));
203 String version = version_row->Get("version");
205 if (Utility::CompareVersion(SCHEMA_VERSION, version) < 0) {
206 BOOST_THROW_EXCEPTION(std::runtime_error("Schema version '" + version + "' does not match the required version '" +
207 SCHEMA_VERSION + "'! Please check the upgrade documentation."));
210 String instanceName = GetInstanceName();
212 result = Query("SELECT instance_id FROM " + GetTablePrefix() + "instances WHERE instance_name = '" + Escape(instanceName) + "'");
214 Dictionary::Ptr row = FetchRow(result);
217 Query("INSERT INTO " + GetTablePrefix() + "instances (instance_name, instance_description) VALUES ('" + Escape(instanceName) + "', '" + Escape(GetInstanceDescription()) + "')");
218 m_InstanceID = GetLastInsertID();
222 m_InstanceID = DbReference(row->Get("instance_id"));
225 std::ostringstream msgbuf;
226 msgbuf << "MySQL IDO instance id: " << static_cast<long>(m_InstanceID) << " (schema version: '" + version + "')";
227 Log(LogInformation, "db_ido_mysql", msgbuf.str());
229 /* set session time zone to utc */
230 Query("SET SESSION TIME_ZONE='+00:00'");
232 /* record connection */
233 Query("INSERT INTO " + GetTablePrefix() + "conninfo " +
234 "(instance_id, connect_time, last_checkin_time, agent_name, agent_version, connect_type, data_start_time) VALUES ("
235 + Convert::ToString(static_cast<long>(m_InstanceID)) + ", NOW(), NOW(), 'icinga2 db_ido_mysql', '" + Escape(Application::GetVersion())
236 + "', '" + (reconnect ? "RECONNECT" : "INITIAL") + "', NOW())");
238 /* clear config tables for the initial config dump */
241 std::ostringstream q1buf;
242 q1buf << "SELECT object_id, objecttype_id, name1, name2, is_active FROM " + GetTablePrefix() + "objects WHERE instance_id = " << static_cast<long>(m_InstanceID);
243 result = Query(q1buf.str());
245 while ((row = FetchRow(result))) {
246 DbType::Ptr dbtype = DbType::GetByID(row->Get("objecttype_id"));
251 DbObject::Ptr dbobj = dbtype->GetOrCreateObjectByName(row->Get("name1"), row->Get("name2"));
252 SetObjectID(dbobj, DbReference(row->Get("object_id")));
253 SetObjectActive(dbobj, row->Get("is_active"));
255 if (GetObjectActive(dbobj))
256 active_dbobjs.push_back(dbobj);
264 /* deactivate all deleted configuration objects */
265 BOOST_FOREACH(const DbObject::Ptr& dbobj, active_dbobjs) {
266 if (dbobj->GetObject() == NULL) {
267 Log(LogDebug, "db_ido", "Deactivate deleted object name1: '" + Convert::ToString(dbobj->GetName1() +
268 "' name2: '" + Convert::ToString(dbobj->GetName2() + "'.")));
269 DeactivateObject(dbobj);
274 void IdoMysqlConnection::ClearConfigTable(const String& table)
276 Query("DELETE FROM " + GetTablePrefix() + table + " WHERE instance_id = " + Convert::ToString(static_cast<long>(m_InstanceID)));
279 IdoMysqlResult IdoMysqlConnection::Query(const String& query)
283 Log(LogDebug, "db_ido_mysql", "Query: " + query);
285 if (mysql_query(&m_Connection, query.CStr()) != 0)
286 BOOST_THROW_EXCEPTION(
288 << errinfo_message(mysql_error(&m_Connection))
289 << errinfo_database_query(query)
292 m_AffectedRows = mysql_affected_rows(&m_Connection);
294 MYSQL_RES *result = mysql_use_result(&m_Connection);
297 if (mysql_field_count(&m_Connection) > 0)
298 BOOST_THROW_EXCEPTION(
300 << errinfo_message(mysql_error(&m_Connection))
301 << errinfo_database_query(query)
304 return IdoMysqlResult();
307 return IdoMysqlResult(result, std::ptr_fun(mysql_free_result));
310 DbReference IdoMysqlConnection::GetLastInsertID(void)
314 return DbReference(mysql_insert_id(&m_Connection));
317 int IdoMysqlConnection::GetAffectedRows(void)
321 return m_AffectedRows;
324 String IdoMysqlConnection::Escape(const String& s)
328 size_t length = s.GetLength();
329 char *to = new char[s.GetLength() * 2 + 1];
331 mysql_real_escape_string(&m_Connection, to, s.CStr(), length);
333 String result = String(to);
340 Dictionary::Ptr IdoMysqlConnection::FetchRow(const IdoMysqlResult& result)
346 unsigned long *lengths, i;
348 row = mysql_fetch_row(result.get());
351 return Dictionary::Ptr();
353 lengths = mysql_fetch_lengths(result.get());
356 return Dictionary::Ptr();
358 Dictionary::Ptr dict = make_shared<Dictionary>();
360 mysql_field_seek(result.get(), 0);
361 for (field = mysql_fetch_field(result.get()), i = 0; field; field = mysql_fetch_field(result.get()), i++)
362 dict->Set(field->name, String(row[i], row[i] + lengths[i]));
367 void IdoMysqlConnection::DiscardRows(const IdoMysqlResult& result)
371 while ((row = FetchRow(result)))
372 ; /* empty loop body */
375 void IdoMysqlConnection::ActivateObject(const DbObject::Ptr& dbobj)
377 boost::mutex::scoped_lock lock(m_ConnectionMutex);
378 InternalActivateObject(dbobj);
381 void IdoMysqlConnection::InternalActivateObject(const DbObject::Ptr& dbobj)
386 DbReference dbref = GetObjectID(dbobj);
387 std::ostringstream qbuf;
389 if (!dbref.IsValid()) {
390 qbuf << "INSERT INTO " + GetTablePrefix() + "objects (instance_id, objecttype_id, name1, name2, is_active) VALUES ("
391 << static_cast<long>(m_InstanceID) << ", " << dbobj->GetType()->GetTypeID() << ", "
392 << "'" << Escape(dbobj->GetName1()) << "', '" << Escape(dbobj->GetName2()) << "', 1)";
394 SetObjectID(dbobj, GetLastInsertID());
396 qbuf << "UPDATE " + GetTablePrefix() + "objects SET is_active = 1 WHERE object_id = " << static_cast<long>(dbref);
401 void IdoMysqlConnection::DeactivateObject(const DbObject::Ptr& dbobj)
403 boost::mutex::scoped_lock lock(m_ConnectionMutex);
408 DbReference dbref = GetObjectID(dbobj);
410 if (!dbref.IsValid())
413 std::ostringstream qbuf;
414 qbuf << "UPDATE " + GetTablePrefix() + "objects SET is_active = 0 WHERE object_id = " << static_cast<long>(dbref);
417 /* Note that we're _NOT_ clearing the db refs via SetReference/SetConfigUpdate/SetStatusUpdate
418 * because the object is still in the database. */
421 /* caller must hold m_ConnectionMutex */
422 bool IdoMysqlConnection::FieldToEscapedString(const String& key, const Value& value, Value *result)
424 if (key == "instance_id") {
425 *result = static_cast<long>(m_InstanceID);
428 if (key == "notification_id") {
429 *result = static_cast<long>(GetNotificationInsertID(value));
433 Value rawvalue = DbValue::ExtractValue(value);
435 if (rawvalue.IsObjectType<DynamicObject>()) {
436 DbObject::Ptr dbobjcol = DbObject::GetOrCreateByObject(rawvalue);
443 DbReference dbrefcol;
445 if (DbValue::IsObjectInsertID(value)) {
446 dbrefcol = GetInsertID(dbobjcol);
448 ASSERT(dbrefcol.IsValid());
450 dbrefcol = GetObjectID(dbobjcol);
452 if (!dbrefcol.IsValid()) {
453 InternalActivateObject(dbobjcol);
455 dbrefcol = GetObjectID(dbobjcol);
457 if (!dbrefcol.IsValid())
462 *result = static_cast<long>(dbrefcol);
463 } else if (DbValue::IsTimestamp(value)) {
465 std::ostringstream msgbuf;
466 msgbuf << "FROM_UNIXTIME(" << ts << ")";
467 *result = Value(msgbuf.str());
468 } else if (DbValue::IsTimestampNow(value)) {
471 *result = "'" + Escape(rawvalue) + "'";
477 void IdoMysqlConnection::ExecuteQuery(const DbQuery& query)
479 ASSERT(query.Category != DbCatInvalid);
481 m_QueryQueue.Enqueue(boost::bind(&IdoMysqlConnection::InternalExecuteQuery, this, query, (DbQueryType *)NULL), true);
484 void IdoMysqlConnection::InternalExecuteQuery(const DbQuery& query, DbQueryType *typeOverride)
486 boost::mutex::scoped_lock lock(m_ConnectionMutex);
488 if ((query.Category & GetCategories()) == 0)
494 std::ostringstream qbuf, where;
497 if (query.WhereCriteria) {
500 ObjectLock olock(query.WhereCriteria);
504 BOOST_FOREACH(const Dictionary::Pair& kv, query.WhereCriteria) {
505 if (!FieldToEscapedString(kv.first, kv.second, &value))
511 where << kv.first << " = " << value;
518 type = typeOverride ? *typeOverride : query.Type;
522 if ((type & DbQueryInsert) && (type & DbQueryUpdate)) {
525 ASSERT(query.Object);
527 if (query.ConfigUpdate)
528 hasid = GetConfigUpdate(query.Object);
529 else if (query.StatusUpdate)
530 hasid = GetStatusUpdate(query.Object);
532 ASSERT(!"Invalid query flags.");
537 type = DbQueryUpdate;
542 qbuf << "INSERT INTO " << GetTablePrefix() << query.Table;
545 qbuf << "UPDATE " << GetTablePrefix() << query.Table << " SET";
548 qbuf << "DELETE FROM " << GetTablePrefix() << query.Table;
551 ASSERT(!"Invalid query type.");
554 if (type == DbQueryInsert || type == DbQueryUpdate) {
555 std::ostringstream colbuf, valbuf;
557 ObjectLock olock(query.Fields);
560 BOOST_FOREACH(const Dictionary::Pair& kv, query.Fields) {
563 if (!FieldToEscapedString(kv.first, kv.second, &value))
566 if (type == DbQueryInsert) {
578 qbuf << " " << kv.first << " = " << value;
585 if (type == DbQueryInsert)
586 qbuf << " (" << colbuf.str() << ") VALUES (" << valbuf.str() << ")";
589 if (type != DbQueryInsert)
594 if (upsert && GetAffectedRows() == 0) {
597 DbQueryType to = DbQueryInsert;
598 InternalExecuteQuery(query, &to);
604 if (query.ConfigUpdate)
605 SetConfigUpdate(query.Object, true);
606 else if (query.StatusUpdate)
607 SetStatusUpdate(query.Object, true);
609 if (type == DbQueryInsert && query.ConfigUpdate)
610 SetInsertID(query.Object, GetLastInsertID());
613 if (type == DbQueryInsert && query.Table == "notifications" && query.NotificationObject) { // FIXME remove hardcoded table name
614 SetNotificationInsertID(query.NotificationObject, GetLastInsertID());
615 Log(LogDebug, "db_ido", "saving contactnotification notification_id=" + Convert::ToString(static_cast<long>(GetLastInsertID())));
619 void IdoMysqlConnection::CleanUpExecuteQuery(const String& table, const String& time_column, double max_age)
621 m_QueryQueue.Enqueue(boost::bind(&IdoMysqlConnection::InternalCleanUpExecuteQuery, this, table, time_column, max_age), true);
624 void IdoMysqlConnection::InternalCleanUpExecuteQuery(const String& table, const String& time_column, double max_age)
626 boost::mutex::scoped_lock lock(m_ConnectionMutex);
631 Query("DELETE FROM " + GetTablePrefix() + table + " WHERE instance_id = " +
632 Convert::ToString(static_cast<long>(m_InstanceID)) + " AND " + time_column +
633 " < FROM_UNIXTIME(" + Convert::ToString(static_cast<long>(max_age)) + ")");
636 void IdoMysqlConnection::FillIDCache(const DbType::Ptr& type)
638 String query = "SELECT " + type->GetIDColumn() + " AS object_id, " + type->GetTable() + "_id FROM " + GetTablePrefix() + type->GetTable() + "s";
639 IdoMysqlResult result = Query(query);
643 while ((row = FetchRow(result))) {
644 SetInsertID(type, DbReference(row->Get("object_id")), DbReference(row->Get(type->GetTable() + "_id")));