Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #ifndef _GENERATOR_H_ 6 : #define _GENERATOR_H_ 7 : 8 : #include <atomic> 9 : #include <mutex> 10 : #include <boost/shared_ptr.hpp> 11 : #include <string> 12 : #include <boost/tuple/tuple.hpp> 13 : #include "viz_message.h" 14 : #include "base/queue_task.h" 15 : 16 : #include <sandesh/sandesh_state_machine.h> 17 : #include "collector_uve_types.h" 18 : #include "syslog_collector.h" 19 : 20 : class DbHandler; 21 : class Sandesh; 22 : class VizSession; 23 : class Collector; 24 : class SandeshStateMachineStats; 25 : 26 : int GetDeferTimeMSec(uint64_t event_time_usec, 27 : uint64_t last_expiry_time_usec, uint64_t last_defer_time_usec); 28 : 29 : class Generator { 30 : public: 31 0 : virtual ~Generator() {} 32 : 33 : virtual const std::string ToString() const = 0; 34 : virtual bool ProcessRules(const VizMsg *vmsg, bool rsc) = 0; 35 : virtual DbHandler * GetDbHandler() const = 0; // db_handler_; 36 : 37 : bool ReceiveSandeshMsg(const VizMsg *vmsg, bool rsc); 38 : void SendSandeshMessageStatistics(); 39 : void GetStatistics(std::vector<SandeshStats> *ssv) const; 40 : void GetStatistics(std::vector<SandeshLogLevelStats> *lsv) const; 41 : 42 : private: 43 : void UpdateStatistics(const VizMsg *vmsg); 44 : 45 : VizMsgStatistics statistics_; 46 : mutable std::mutex smutex_; 47 : }; 48 : 49 : class SandeshGenerator : public Generator { 50 : public: 51 : const static int kInitialSmDeferTimeMSec = 300; 52 : const static int kMaxSmDeferTimeMSec = 30000; 53 : 54 : typedef boost::tuple<std::string /* Source */, std::string /* Module */, 55 : std::string /* Instance id */, std::string /* Node type */> GeneratorId; 56 : 57 : SandeshGenerator(Collector * const collector, VizSession *session, 58 : SandeshStateMachine *state_machine, 59 : const std::string &source, const std::string &module, 60 : const std::string &instance_id, const std::string &node_type, 61 : DbHandlerPtr global_db_handler); 62 : ~SandeshGenerator(); 63 : 64 : void ReceiveSandeshCtrlMsg(uint32_t connects); 65 : void DisconnectSession(VizSession *vsession); 66 : void ConnectSession(VizSession *vsession, SandeshStateMachine *state_machine); 67 : 68 : bool GetSandeshStateMachineQueueCount(uint64_t &queue_count) const; 69 : bool GetSandeshStateMachineDropLevel(std::string &drop_level) const; 70 : bool GetSandeshStateMachineStats(SandeshStateMachineStats &sm_stats, 71 : SandeshGeneratorBasicStats &sm_msg_stats) const; 72 : bool IsStateMachineDeferTimerRunning() const; 73 : int GetStateMachineDeferTimeMSec() const; 74 : 75 0 : const std::string &instance_id() const { return instance_id_; } 76 0 : const std::string &node_type() const { return node_type_; } 77 0 : VizSession * session() const { 78 0 : std::scoped_lock lock(mutex_); 79 0 : return viz_session_; 80 0 : } 81 0 : const std::string &module() const { return module_; } 82 0 : const std::string &source() const { return source_; } 83 0 : virtual const std::string ToString() const { return name_; } 84 0 : SandeshStateMachine * get_state_machine(void) { 85 0 : return state_machine_; 86 : } 87 : const std::string State() const; 88 : 89 : void GetGeneratorInfo(ModuleServerState &genlist) const; 90 : void SetDbQueueWaterMarkInfo(Sandesh::QueueWaterMarkInfo &wm); 91 : void ResetDbQueueWaterMarkInfo(); 92 : void SetSmQueueWaterMarkInfo(Sandesh::QueueWaterMarkInfo &wm); 93 : void ResetSmQueueWaterMarkInfo(); 94 0 : virtual DbHandler * GetDbHandler() const { return db_handler_.get(); } 95 : 96 : private: 97 : virtual bool ProcessRules(const VizMsg *vmsg, bool rsc); 98 : void set_session(VizSession *session); 99 : 100 0 : void set_state_machine(SandeshStateMachine *state_machine) { 101 0 : state_machine_ = state_machine; 102 : // Update state machine 103 0 : state_machine_->SetGeneratorKey(name_); 104 0 : } 105 : void HandleSeqRedisReply(const std::map<std::string,int32_t> &typeMap); 106 : void HandleDelRedisReply(bool res); 107 : void TimerErrorHandler(std::string name, std::string error); 108 : 109 : void ProcessRulesCb(GenDb::DbOpResult::type dresult); 110 : bool StateMachineDeferTimerExpired(); 111 : void CreateStateMachineDeferTimer(); 112 : void DeleteStateMachineDeferTimer(); 113 : void StartStateMachineDeferTimer(int time_msec); 114 : void StopStateMachineDeferTimer(); 115 : bool IsStateMachineDeferTimerRunningUnlocked() const; 116 : int GetStateMachineDeferMSec() const; 117 : 118 : Collector * const collector_; 119 : SandeshStateMachine *state_machine_; 120 : VizSession *viz_session_; 121 : GeneratorInfoAttr gen_attr_; 122 : 123 : const std::string instance_id_; 124 : const std::string node_type_; 125 : const std::string source_; 126 : const std::string module_; 127 : const std::string name_; 128 : int instance_; 129 : 130 : std::atomic<bool> disconnected_; 131 : DbHandlerPtr db_handler_; 132 : GenDb::GenDbIf::DbAddColumnCb process_rules_cb_; 133 : Timer *sm_defer_timer_; 134 : uint64_t sm_defer_timer_expiry_time_usec_; 135 : int sm_defer_time_msec_; 136 : mutable std::mutex mutex_; 137 : }; 138 : 139 : class SyslogGenerator : public Generator { 140 : public: 141 : SyslogGenerator(SyslogListeners *const listeners, 142 : const std::string &source, const std::string &module); 143 : const std::string &module() const { return module_; } 144 : const std::string &source() const { return source_; } 145 0 : virtual const std::string ToString() const { return name_; } 146 0 : virtual DbHandler * GetDbHandler() const { return db_handler_.get(); } 147 : 148 : private: 149 : virtual bool ProcessRules(const VizMsg *vmsg, bool rsc); 150 : 151 : SyslogListeners * const syslog_; 152 : const std::string source_; 153 : const std::string module_; 154 : const std::string name_; 155 : DbHandlerPtr db_handler_; 156 : }; 157 : 158 : #endif