[icinga-checkins] icinga.org: icinga2/master: Change DB IDO 'categories' attribute to Array notation

git at icinga.org git at icinga.org
Thu Jun 23 16:01:24 CEST 2016


Module: icinga2
Branch: master
Commit: 091274e41797c89e62e68961673692da43f94122
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=091274e41797c89e62e68961673692da43f94122

Author: Michael Friedrich <michael.friedrich at netways.de>
Date:   Wed Jun 22 16:11:15 2016 +0200

Change DB IDO 'categories' attribute to Array notation

Example:
    categories = [ "DbCatProgramStatus", "DbCatState" ]

The old method with constants and OR is still supported but deprecated.

refs #11445
fixes #12024

---

 doc/6-object-types.md                   |   26 +++++++++++++++-------
 lib/db_ido/dbconnection.cpp             |    8 +++++++
 lib/db_ido/dbconnection.ti              |   28 +++++++++++++++++++----
 lib/db_ido/dbquery.cpp                  |   25 ++++++++++++++++++++-
 lib/db_ido/dbquery.hpp                  |   37 ++++++++++++++++++-------------
 lib/db_ido_mysql/idomysqlconnection.cpp |    2 +-
 lib/db_ido_pgsql/idopgsqlconnection.cpp |    2 +-
 7 files changed, 97 insertions(+), 31 deletions(-)

diff --git a/doc/6-object-types.md b/doc/6-object-types.md
index b4bd027..e18fe06 100644
--- a/doc/6-object-types.md
+++ b/doc/6-object-types.md
@@ -722,7 +722,7 @@ Configuration Attributes:
   enable_ha       |**Optional.** Enable the high availability functionality. Only valid in a [cluster setup](13-distributed-monitoring-ha.md#high-availability-db-ido). Defaults to "true".
   failover_timeout | **Optional.** Set the failover timeout in a [HA cluster](13-distributed-monitoring-ha.md#high-availability-db-ido). Must not be lower than 60s. Defaults to "60s".
   cleanup         |**Optional.** Dictionary with items for historical table cleanup.
-  categories      |**Optional.** The types of information that should be written to the database.
+  categories      |**Optional.** Array of information types that should be written to the database.
 
 Cleanup Items:
 
@@ -763,9 +763,14 @@ Data Categories:
   DbCatRetention       | Retention data         | Icinga Web 2
   DbCatStateHistory    | Historical state data  | Icinga Web 2
 
-Multiple categories can be combined using the `|` operator. In addition to
-the category flags listed above the `DbCatEverything` flag may be used as
-a shortcut for listing all flags.
+In addition to the category flags listed above the `DbCatEverything`
+flag may be used as a shortcut for listing all flags.
+
+> **Note**
+>
+> The previous way of defining the `categories` attribute e.g.
+> `DbCatProgramStatus | DbCatState` was deprecated in 2.5 and will
+> be removed in future versions.
 
 External interfaces like Icinga Web 2 require everything except `DbCatCheck`
 which is the default value if `categories` is not set.
@@ -806,7 +811,7 @@ Configuration Attributes:
   enable_ha       |**Optional.** Enable the high availability functionality. Only valid in a [cluster setup](13-distributed-monitoring-ha.md#high-availability-db-ido). Defaults to "true".
   failover_timeout | **Optional.** Set the failover timeout in a [HA cluster](13-distributed-monitoring-ha.md#high-availability-db-ido). Must not be lower than 60s. Defaults to "60s".
   cleanup         |**Optional.** Dictionary with items for historical table cleanup.
-  categories      |**Optional.** The types of information that should be written to the database.
+  categories      |**Optional.** Array of information types that should be written to the database.
 
 Cleanup Items:
 
@@ -847,9 +852,14 @@ Data Categories:
   DbCatRetention       | Retention data         | Icinga Web 2
   DbCatStateHistory    | Historical state data  | Icinga Web 2
 
-Multiple categories can be combined using the `|` operator. In addition to
-the category flags listed above the `DbCatEverything` flag may be used as
-a shortcut for listing all flags.
+In addition to the category flags listed above the `DbCatEverything`
+flag may be used as a shortcut for listing all flags.
+
+> **Note**
+>
+> The previous way of defining the `categories` attribute e.g.
+> `DbCatProgramStatus | DbCatState` was deprecated in 2.5 and will
+> be removed in future versions.
 
 External interfaces like Icinga Web 2 require everything except `DbCatCheck`
 which is the default value if `categories` is not set.
diff --git a/lib/db_ido/dbconnection.cpp b/lib/db_ido/dbconnection.cpp
index c7995c8..495ffde 100644
--- a/lib/db_ido/dbconnection.cpp
+++ b/lib/db_ido/dbconnection.cpp
@@ -46,6 +46,14 @@ void DbConnection::OnConfigLoaded(void)
 {
 	ConfigObject::OnConfigLoaded();
 
+	Value categories = GetCategories();
+
+	//TODO: Remove 'cat1 | cat2' notation in 2.6
+	if (categories.IsNumber())
+		SetCategoryFilter(categories);
+	else
+		SetCategoryFilter(FilterArrayToInt(categories, DbQuery::GetCategoryFilterMap(), DbCatEverything));
+
 	if (!GetEnableHa()) {
 		Log(LogDebug, "DbConnection")
 		    << "HA functionality disabled. Won't pause IDO connection: " << GetName();
diff --git a/lib/db_ido/dbconnection.ti b/lib/db_ido/dbconnection.ti
index 6491ae4..e1b6373 100644
--- a/lib/db_ido/dbconnection.ti
+++ b/lib/db_ido/dbconnection.ti
@@ -35,13 +35,27 @@ abstract class DbConnection : ConfigObject
 		default {{{ return new Dictionary(); }}}
 	};
 
-	[config] int categories {
+	[config] Value categories {
 		default {{{
-			return DbCatConfig | DbCatState | DbCatAcknowledgement |
-			    DbCatComment | DbCatDowntime | DbCatEventHandler | DbCatExternalCommand | DbCatFlapping |
-			    DbCatLog | DbCatNotification | DbCatProgramStatus | DbCatRetention | DbCatStateHistory;
+			Array::Ptr cat = new Array();
+			cat->Add("DbCatConfig");
+			cat->Add("DbCatState");
+			cat->Add("DbCatAcknowledgement");
+			cat->Add("DbCatComment");
+			cat->Add("DbCatDowntime");
+			cat->Add("DbCatEventHandler");
+			cat->Add("DbCatExternalCommand");
+			cat->Add("DbCatFlapping");
+			cat->Add("DbCatLog");
+			cat->Add("DbCatNotification");
+			cat->Add("DbCatProgramStatus");
+			cat->Add("DbCatRetention");
+			cat->Add("DbCatStateHistory");
+
+			return cat;
 		}}}
 	};
+	int categories_filter_real (CategoryFilter);
 
 	[config] bool enable_ha {
 		default {{{ return true; }}}
@@ -77,6 +91,12 @@ validator DbConnection {
 		Number servicechecks_age;
 		Number systemcommands_age;
 	};
+
+	Number categories;
+	Array categories {
+		String "*";
+		Number "*";
+	};
 };
 
 }
diff --git a/lib/db_ido/dbquery.cpp b/lib/db_ido/dbquery.cpp
index 01a54a9..e11cb5a 100644
--- a/lib/db_ido/dbquery.cpp
+++ b/lib/db_ido/dbquery.cpp
@@ -25,6 +25,8 @@ using namespace icinga;
 
 INITIALIZE_ONCE(&DbQuery::StaticInitialize);
 
+std::map<String, int> DbQuery::m_CategoryFilterMap;
+
 void DbQuery::StaticInitialize(void)
 {
 	ScriptGlobal::Set("DbCatConfig", DbCatConfig);
@@ -42,5 +44,26 @@ void DbQuery::StaticInitialize(void)
 	ScriptGlobal::Set("DbCatRetention", DbCatRetention);
 	ScriptGlobal::Set("DbCatStateHistory", DbCatStateHistory);
 
-	ScriptGlobal::Set("DbCatEverything", ~(unsigned int)0);
+	ScriptGlobal::Set("DbCatEverything", DbCatEverything);
+
+	m_CategoryFilterMap["DbCatConfig"] = DbCatConfig;
+	m_CategoryFilterMap["DbCatState"] = DbCatState;
+	m_CategoryFilterMap["DbCatAcknowledgement"] = DbCatAcknowledgement;
+	m_CategoryFilterMap["DbCatComment"] = DbCatComment;
+	m_CategoryFilterMap["DbCatDowntime"] = DbCatDowntime;
+	m_CategoryFilterMap["DbCatEventHandler"] = DbCatEventHandler;
+	m_CategoryFilterMap["DbCatExternalCommand"] = DbCatExternalCommand;
+	m_CategoryFilterMap["DbCatFlapping"] = DbCatFlapping;
+	m_CategoryFilterMap["DbCatCheck"] = DbCatCheck;
+	m_CategoryFilterMap["DbCatLog"] = DbCatLog;
+	m_CategoryFilterMap["DbCatNotification"] = DbCatNotification;
+	m_CategoryFilterMap["DbCatProgramStatus"] = DbCatProgramStatus;
+	m_CategoryFilterMap["DbCatRetention"] = DbCatRetention;
+	m_CategoryFilterMap["DbCatStateHistory"] = DbCatStateHistory;
+	m_CategoryFilterMap["DbCatEverything"] = DbCatEverything;
+}
+
+const std::map<String, int>& DbQuery::GetCategoryFilterMap(void)
+{
+	return m_CategoryFilterMap;
 }
diff --git a/lib/db_ido/dbquery.hpp b/lib/db_ido/dbquery.hpp
index 3161d1c..a1fd503 100644
--- a/lib/db_ido/dbquery.hpp
+++ b/lib/db_ido/dbquery.hpp
@@ -39,23 +39,23 @@ enum DbQueryType
 
 enum DbQueryCategory
 {
-	DbCatInvalid = -1,
+	DbCatInvalid = 0, //-1 is required for DbCatEverything
+	DbCatEverything = ~0,
 
-	DbCatConfig = (1 << 0),
-	DbCatState = (1 << 1),
-
-	DbCatAcknowledgement = (1 << 2),
-	DbCatComment = (1 << 3),
-	DbCatDowntime = (1 << 4),
-	DbCatEventHandler = (1 << 5),
-	DbCatExternalCommand = (1 << 6),
-	DbCatFlapping = (1 << 7),
-	DbCatCheck = (1 << 8),
-	DbCatLog = (1 << 9),
-	DbCatNotification = (1 << 10),
-	DbCatProgramStatus = (1 << 11),
-	DbCatRetention = (1 << 12),
-	DbCatStateHistory = (1 << 13)
+	DbCatConfig = 1,
+	DbCatState = 2,
+	DbCatAcknowledgement = 4,
+	DbCatComment = 8,
+	DbCatDowntime = 16,
+	DbCatEventHandler = 32,
+	DbCatExternalCommand = 64,
+	DbCatFlapping = 128,
+	DbCatCheck = 256,
+	DbCatLog = 512,
+	DbCatNotification = 1024,
+	DbCatProgramStatus = 2048,
+	DbCatRetention = 4096,
+	DbCatStateHistory = 8192
 };
 
 class DbObject;
@@ -79,6 +79,11 @@ struct I2_DB_IDO_API DbQuery
 	DbQuery(void)
 		: Type(0), Category(DbCatInvalid), ConfigUpdate(false), StatusUpdate(false), Priority(PriorityLow)
 	{ }
+
+	static const std::map<String, int>& GetCategoryFilterMap(void);
+
+private:
+	static std::map<String, int> m_CategoryFilterMap;
 };
 
 }
diff --git a/lib/db_ido_mysql/idomysqlconnection.cpp b/lib/db_ido_mysql/idomysqlconnection.cpp
index 4e11f6f..40f720b 100644
--- a/lib/db_ido_mysql/idomysqlconnection.cpp
+++ b/lib/db_ido_mysql/idomysqlconnection.cpp
@@ -872,7 +872,7 @@ void IdoMysqlConnection::InternalExecuteQuery(const DbQuery& query, DbQueryType
 		return;
 	}
 
-	if ((query.Category & GetCategories()) == 0)
+	if (GetCategoryFilter() != DbCatEverything && (query.Category & GetCategoryFilter()) == 0)
 		return;
 
 	if (query.Object && query.Object->GetObject()->GetExtension("agent_check").ToBool())
diff --git a/lib/db_ido_pgsql/idopgsqlconnection.cpp b/lib/db_ido_pgsql/idopgsqlconnection.cpp
index e9e745d..94976c2 100644
--- a/lib/db_ido_pgsql/idopgsqlconnection.cpp
+++ b/lib/db_ido_pgsql/idopgsqlconnection.cpp
@@ -734,7 +734,7 @@ void IdoPgsqlConnection::InternalExecuteQuery(const DbQuery& query, DbQueryType
 		return;
 	}
 
-	if ((query.Category & GetCategories()) == 0)
+	if (GetCategoryFilter() != DbCatEverything && (query.Category & GetCategoryFilter()) == 0)
 		return;
 
 	if (query.Object && query.Object->GetObject()->GetExtension("agent_check").ToBool())



More information about the icinga-checkins mailing list