ECStatsCollector.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #ifndef EC_STATS_COLLECTOR_H
  18. #define EC_STATS_COLLECTOR_H
  19. #include <kopano/zcdefs.h>
  20. #include <string>
  21. #include <map>
  22. #include <mutex>
  23. namespace KC {
  24. enum SCName {
  25. /* server stats */
  26. SCN_SERVER_STARTTIME, SCN_SERVER_LAST_CACHECLEARED, SCN_SERVER_LAST_CONFIGRELOAD,
  27. SCN_SERVER_CONNECTIONS, SCN_MAX_SOCKET_NUMBER, SCN_REDIRECT_COUNT, SCN_SOAP_REQUESTS, SCN_RESPONSE_TIME, SCN_PROCESSING_TIME,
  28. /* search folder stats */
  29. SCN_SEARCHFOLDER_COUNT, SCN_SEARCHFOLDER_THREADS, SCN_SEARCHFOLDER_UPDATE_RETRY, SCN_SEARCHFOLDER_UPDATE_FAIL,
  30. /* database stats */
  31. SCN_DATABASE_CONNECTS, SCN_DATABASE_SELECTS, SCN_DATABASE_INSERTS, SCN_DATABASE_UPDATES, SCN_DATABASE_DELETES,
  32. SCN_DATABASE_FAILED_CONNECTS, SCN_DATABASE_FAILED_SELECTS, SCN_DATABASE_FAILED_INSERTS, SCN_DATABASE_FAILED_UPDATES, SCN_DATABASE_FAILED_DELETES, SCN_DATABASE_LAST_FAILED,
  33. SCN_DATABASE_MWOPS, SCN_DATABASE_MROPS, SCN_DATABASE_DEFERRED_FETCHES, SCN_DATABASE_MERGES, SCN_DATABASE_MERGED_RECORDS, SCN_DATABASE_ROW_READS, SCN_DATABASE_COUNTER_RESYNCS,
  34. /* logon stats */
  35. SCN_LOGIN_PASSWORD, SCN_LOGIN_SSL, SCN_LOGIN_SSO, SCN_LOGIN_SOCKET, SCN_LOGIN_DENIED,
  36. /* system session stats */
  37. SCN_SESSIONS_CREATED, SCN_SESSIONS_DELETED, SCN_SESSIONS_TIMEOUT, SCN_SESSIONS_INTERNAL_CREATED, SCN_SESSIONS_INTERNAL_DELETED,
  38. /* system session group stats */
  39. SCN_SESSIONGROUPS_CREATED, SCN_SESSIONGROUPS_DELETED,
  40. /* LDAP stats */
  41. SCN_LDAP_CONNECTS, SCN_LDAP_RECONNECTS, SCN_LDAP_CONNECT_FAILED, SCN_LDAP_CONNECT_TIME, SCN_LDAP_CONNECT_TIME_MAX,
  42. SCN_LDAP_AUTH_LOGINS, SCN_LDAP_AUTH_DENIED, SCN_LDAP_AUTH_TIME, SCN_LDAP_AUTH_TIME_MAX, SCN_LDAP_AUTH_TIME_AVG,
  43. SCN_LDAP_SEARCH, SCN_LDAP_SEARCH_FAILED, SCN_LDAP_SEARCH_TIME, SCN_LDAP_SEARCH_TIME_MAX,
  44. /* indexer stats */
  45. SCN_INDEXER_SEARCH_ERRORS, SCN_INDEXER_SEARCH_MAX, SCN_INDEXER_SEARCH_AVG, SCN_INDEXED_SEARCHES, SCN_DATABASE_SEARCHES
  46. };
  47. union SCData {
  48. float f;
  49. LONGLONG ll;
  50. time_t ts;
  51. };
  52. enum SCType { SCDT_FLOAT, SCDT_LONGLONG, SCDT_TIMESTAMP };
  53. struct ECStat {
  54. SCData data;
  55. LONGLONG avginc;
  56. SCType type;
  57. const char *name;
  58. const char *description;
  59. std::mutex lock;
  60. #if defined(__GNUG__) && __GNUG__ == 4 && \
  61. defined(__GNUC_MINOR__) && __GNUC_MINOR__ <= 7
  62. /*
  63. * g++-4.7 does not have std::map emplace support yet, so it just
  64. * attempts to use copy constructors when inserting an element.
  65. * However, a default copy ctor is not available for ECStat because
  66. * std::mutex is uncopyable. So we provide one.
  67. */
  68. ECStat(const ECStat &o) :
  69. data(o.data), avginc(o.avginc), type(o.type), name(o.name),
  70. description(o.description), lock()
  71. {}
  72. /* and if we define one ctor, we have to name all of them: */
  73. ECStat(void) = default;
  74. #endif
  75. };
  76. typedef std::map<SCName, ECStat> SCMap;
  77. struct ECStrings {
  78. std::string description;
  79. std::string value;
  80. };
  81. class _kc_export ECStatsCollector _kc_final {
  82. public:
  83. _kc_hidden ECStatsCollector(void);
  84. _kc_hidden void Increment(SCName name, float inc);
  85. void Increment(SCName name, int inc = 1);
  86. void Increment(SCName name, LONGLONG inc);
  87. _kc_hidden void Set(SCName name, float set);
  88. _kc_hidden void Set(SCName name, LONGLONG set);
  89. void SetTime(SCName name, time_t set);
  90. void Max(SCName name, LONGLONG max);
  91. _kc_hidden void Avg(SCName name, float add);
  92. void Avg(SCName name, LONGLONG add);
  93. /* strings are separate, used by ECSerial */
  94. _kc_hidden std::string GetValue(const SCMap::const_iterator::value_type &);
  95. _kc_hidden std::string GetValue(const SCName &name);
  96. _kc_hidden void ForEachStat(void (*cb)(const std::string &, const std::string &, const std::string &, void *), void *obj);
  97. _kc_hidden void ForEachString(void (*cb)(const std::string &, const std::string &, const std::string &, void *), void *obj);
  98. _kc_hidden void Reset(void);
  99. _kc_hidden void Reset(SCName name);
  100. private:
  101. _kc_hidden void AddStat(SCName index, SCType type, const char *name, const char *desc);
  102. SCMap m_StatData;
  103. std::mutex m_StringsLock;
  104. std::map<std::string, ECStrings> m_StatStrings;
  105. };
  106. /* actual variable is in ECServerEntryPoint.cpp */
  107. extern ECStatsCollector* g_lpStatsCollector;
  108. } /* namespace */
  109. #endif