UniSet  2.8.0
BackendOpenTSDB.h
1 /*
2  * Copyright (c) 2015 Pavel Vainerman.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as
6  * published by the Free Software Foundation, version 2.1.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Lesser Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 // -----------------------------------------------------------------------------
17 #ifndef _BackendOpenTSDB_H_
18 #define _BackendOpenTSDB_H_
19 // -----------------------------------------------------------------------------
20 #include <deque>
21 #include <memory>
22 #include <unordered_map>
23 #include <chrono>
24 #include "UObject_SK.h"
25 #include "SMInterface.h"
26 #include "SharedMemory.h"
27 #include "extensions/Extensions.h"
28 #include "UTCPStream.h"
29 // --------------------------------------------------------------------------
30 namespace uniset
31 {
32  // -----------------------------------------------------------------------------
102  public UObject_SK
103  {
104  public:
105  BackendOpenTSDB( uniset::ObjectId objId, xmlNode* cnode, uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = nullptr,
106  const std::string& prefix = "opentsdb" );
107  virtual ~BackendOpenTSDB();
108 
110  static std::shared_ptr<BackendOpenTSDB> init_opendtsdb( int argc, const char* const* argv,
111  uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = nullptr,
112  const std::string& prefix = "opentsdb" );
113 
115  static void help_print( int argc, const char* const* argv );
116 
117  inline std::shared_ptr<LogAgregator> getLogAggregator()
118  {
119  return loga;
120  }
121  inline std::shared_ptr<DebugStream> log()
122  {
123  return mylog;
124  }
125 
126  enum Timers
127  {
128  tmFlushBuffer,
129  tmReconnect,
130  tmLastNumberOfTimer
131  };
132 
133  protected:
134  BackendOpenTSDB();
135 
136  // переопределяем callback, чтобы оптимизировать
137  // обработку большого количества сообщений
138  // и убрать не нужную в данном процессе обработку (включая sleep_msec)
139  virtual void callback() noexcept override;
140 
141  virtual void askSensors( UniversalIO::UIOCommand cmd ) override;
142  virtual void sensorInfo( const uniset::SensorMessage* sm ) override;
143  virtual void timerInfo( const uniset::TimerMessage* tm ) override;
144  virtual void sysCommand( const uniset::SystemMessage* sm ) override;
145  virtual std::string getMonitInfo() const override;
146 
147  void init( xmlNode* cnode );
148  bool flushBuffer();
149  bool reconnect();
150 
151  std::shared_ptr<SMInterface> shm;
152 
153  struct ParamInfo
154  {
155  const std::string name;
156  const std::string tags;
157 
158  ParamInfo( const std::string& _name, const std::string& _tags ):
159  name(_name), tags(_tags) {}
160  };
161 
162  std::string tsdbPrefix;
163  std::string tsdbTags; // теги в виде строки TAG=VAL TAG2=VAL2 ...
164  std::unordered_map<uniset::ObjectId, ParamInfo> tsdbParams;
165 
166  timeout_t bufSyncTime = { 5000 };
167  size_t bufSize = { 500 };
168  size_t bufMaxSize = { 5000 }; // drop messages
169  bool timerIsOn = { false };
170  timeout_t reconnectTime = { 5000 };
171  std::string lastError;
172 
173  // буфер mutex-ом можно не защищать
174  // т.к. к нему идёт обращение только из основного потока обработки
175  // (sensorInfo, timerInfo)
176  std::deque<std::string> buf;
177 
178  // работа с OpenTSDB
179  std::shared_ptr<UTCPStream> tcp;
180  std::string host = { "localhost" };
181  int port = { 4242 };
182 
183  private:
184 
185  std::string prefix;
186  };
187  // --------------------------------------------------------------------------
188 } // end of namespace uniset
189 // -----------------------------------------------------------------------------
190 #endif // _BackendOpenTSDB_H_
191 // -----------------------------------------------------------------------------
virtual std::string getMonitInfo() const override
Definition: BackendOpenTSDB.cc:431
Definition: CommonEventLoop.h:14
Definition: BackendOpenTSDB.h:101
Definition: MessageType.h:170
Definition: MessageType.h:126
virtual void callback() noexcept override
Definition: BackendOpenTSDB.cc:216
Definition: BackendOpenTSDB.h:153
static std::shared_ptr< BackendOpenTSDB > init_opendtsdb(int argc, const char *const *argv, uniset::ObjectId shmID, const std::shared_ptr< SharedMemory > &ic=nullptr, const std::string &prefix="opentsdb")
Definition: BackendOpenTSDB.cc:179
Definition: MessageType.h:213
static void help_print(int argc, const char *const *argv)
Definition: BackendOpenTSDB.cc:144
Definition: UObject_SK.h:28
long ObjectId
Definition: UniSetTypes_i.idl:30