UniSet  2.7.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  // -----------------------------------------------------------------------------
100  public UObject_SK
101  {
102  public:
103  BackendOpenTSDB( uniset::ObjectId objId, xmlNode* cnode, uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = nullptr,
104  const std::string& prefix = "opentsdb" );
105  virtual ~BackendOpenTSDB();
106 
108  static std::shared_ptr<BackendOpenTSDB> init_opendtsdb( int argc, const char* const* argv,
109  uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = nullptr,
110  const std::string& prefix = "opentsdb" );
111 
113  static void help_print( int argc, const char* const* argv );
114 
115  inline std::shared_ptr<LogAgregator> getLogAggregator()
116  {
117  return loga;
118  }
119  inline std::shared_ptr<DebugStream> log()
120  {
121  return mylog;
122  }
123 
124  enum Timers
125  {
126  tmFlushBuffer,
127  tmReconnect,
128  tmLastNumberOfTimer
129  };
130 
131  protected:
132  BackendOpenTSDB();
133 
134  virtual void askSensors( UniversalIO::UIOCommand cmd ) override;
135  virtual void sensorInfo( const uniset::SensorMessage* sm ) override;
136  virtual void timerInfo( const uniset::TimerMessage* tm ) override;
137  virtual void sysCommand( const uniset::SystemMessage* sm ) override;
138  virtual std::string getMonitInfo() const override;
139 
140  void init( xmlNode* cnode );
141  bool flushBuffer();
142  bool reconnect();
143 
144  std::shared_ptr<SMInterface> shm;
145 
146  struct ParamInfo
147  {
148  const std::string name;
149  const std::string tags;
150 
151  ParamInfo( const std::string& _name, const std::string& _tags ):
152  name(_name), tags(_tags) {}
153  };
154 
155  std::string tsdbPrefix;
156  std::string tsdbTags; // теги в виде строки TAG=VAL TAG2=VAL2 ...
157  std::unordered_map<uniset::ObjectId, ParamInfo> tsdbParams;
158 
159  timeout_t bufSyncTime = { 5000 };
160  size_t bufSize = { 100 };
161  bool timerIsOn = { false };
162  timeout_t reconnectTime = { 5000 };
163  std::string lastError;
164 
165  // буфер mutex-ом можно не защищать
166  // т.к. к нему идёт обращение только из основного потока обработки
167  // (sensorInfo, timerInfo)
168  std::deque<std::string> buf;
169 
170  // работа с OpenTSDB
171  std::shared_ptr<UTCPStream> tcp;
172  std::string host = { "localhost" };
173  int port = { 4242 };
174 
175  private:
176 
177  std::string prefix;
178  };
179  // --------------------------------------------------------------------------
180 } // end of namespace uniset
181 // -----------------------------------------------------------------------------
182 #endif // _BackendOpenTSDB_H_
183 // -----------------------------------------------------------------------------
Definition: CallbackTimer.h:29
Definition: BackendOpenTSDB.h:99
Definition: MessageType.h:168
Definition: MessageType.h:124
Definition: BackendOpenTSDB.h:146
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:173
Definition: MessageType.h:211
static void help_print(int argc, const char *const *argv)
Definition: BackendOpenTSDB.cc:139
Definition: UObject_SK.h:28
long ObjectId
Definition: UniSetTypes_i.idl:30