Line data Source code
1 : /*
2 : * Copyright (c) 2017 Juniper Networks, Inc. All rights reserved.
3 : */
4 : #ifndef ANALYTICS_STRUCTURED_SYSLOG_CONFIG_H_
5 : #define ANALYTICS_STRUCTURED_SYSLOG_CONFIG_H_
6 :
7 :
8 : #include <map>
9 : #include <iostream>
10 : #include <string>
11 : #include <vector>
12 : #include <algorithm>
13 : #include <stdint.h>
14 : #include <cstdio>
15 : #include <boost/shared_ptr.hpp>
16 : #include <boost/regex.hpp>
17 : #include <boost/thread/mutex.hpp>
18 : #include <boost/algorithm/string.hpp>
19 : #include "config_client_collector.h"
20 : #include "parser_util.h"
21 :
22 : class Options;
23 :
24 :
25 : struct IPNetwork
26 : {
27 0 : IPNetwork(uint32_t lower, uint32_t upper, std::string net_name) {
28 0 : address_begin = lower;
29 0 : address_end = upper;
30 0 : id = net_name;
31 0 : }
32 :
33 0 : bool operator < (const IPNetwork& other) const{
34 0 : return address_begin < other.address_begin;
35 : }
36 :
37 0 : IPNetwork& operator = (const IPNetwork& other) {
38 0 : this->address_begin = other.address_begin;
39 0 : this->address_end = other.address_end;
40 0 : this->id = other.id;
41 0 : return *this;
42 : }
43 :
44 : uint32_t address_begin;
45 : uint32_t address_end;
46 : std::string id;
47 :
48 : };
49 :
50 : class HostnameRecord {
51 : public:
52 0 : explicit HostnameRecord(const std::string &name, const std::string &hostaddr,
53 : const std::string &tenant, const std::string &location,
54 : const std::string &device, const std::string &tags,
55 0 : const std::map< std::string, std::string > &linkmap):
56 0 : refreshed_(true), name_(name), hostaddr_(hostaddr), tenant_(tenant),
57 0 : location_(location), device_(device), tags_(tags), linkmap_(linkmap) {
58 0 : }
59 : bool operator==(const HostnameRecord &rhs) {
60 : return rhs.IsMe(name_) && rhs.hostaddr() == hostaddr_ &&
61 : rhs.tenant() == tenant_ && rhs.location() == location_ &&
62 : rhs.device() == device_ && rhs.tags() == tags_ &&
63 : rhs.linkmap().size() == linkmap_.size() &&
64 : std::equal(rhs.linkmap().begin(), rhs.linkmap().end(), linkmap_.begin());
65 : }
66 :
67 0 : const std::string name() { return name_; }
68 : const std::string hostaddr() const { return hostaddr_; }
69 0 : const std::string tenant() const { return tenant_; }
70 0 : const std::string location() const { return location_; }
71 0 : const std::string device() const { return device_; }
72 0 : const std::string tags() const { return tags_; }
73 0 : const std::map< std::string, std::string > linkmap() const { return linkmap_; }
74 :
75 : bool IsMe(const std::string &name) const { return name == name_; }
76 0 : void Refresh(const std::string &name, const std::string &hostaddr,
77 : const std::string &tenant, const std::string &location,
78 : const std::string &device, const std::string &tags,
79 : const std::map< std::string, std::string > &linkmap) {
80 0 : if (hostaddr_ != hostaddr)
81 0 : hostaddr_ = hostaddr;
82 0 : if (tenant_ != tenant)
83 0 : tenant_ = tenant;
84 0 : if (location_ != location)
85 0 : location_ = location;
86 0 : if (device_ != device)
87 0 : device_ = device;
88 0 : if (tags_ != tags)
89 0 : tags_ = tags;
90 0 : if (linkmap.size() != linkmap_.size()) {
91 0 : linkmap_ = linkmap;
92 0 : } else if (!(std::equal(linkmap.begin(), linkmap.end(), linkmap_.begin()))) {
93 0 : linkmap_ = linkmap;
94 : }
95 0 : refreshed_ = true;
96 0 : }
97 : bool GetandClearRefreshed() { bool r = refreshed_; refreshed_ = false; return r;}
98 : private:
99 : bool refreshed_;
100 : std::string name_;
101 : std::string hostaddr_;
102 : std::string tenant_;
103 : std::string location_;
104 : std::string device_;
105 : std::string tags_;
106 : std::map< std::string, std::string > linkmap_;
107 : };
108 :
109 : class TenantRecord {
110 : public:
111 0 : explicit TenantRecord(const std::string &name, const std::string &tenantaddr,
112 : const std::string &tenant, const std::string &tags,
113 : const std::map< std::string, std::string > &dscpmap_ipv4,
114 0 : const std::map< std::string, std::string > &dscpmap_ipv6):
115 0 : refreshed_(true), name_(name), tenantaddr_(tenantaddr), tenant_(tenant),
116 0 : tags_(tags), dscpmap_ipv4_(dscpmap_ipv4), dscpmap_ipv6_(dscpmap_ipv6) {
117 0 : }
118 : bool operator==(const TenantRecord &rhs) {
119 : return rhs.IsMe(name_) && rhs.tenantaddr() == tenantaddr_ &&
120 : rhs.tenant() == tenant_ && rhs.tags() == tags_ &&
121 : rhs.dscpmap_ipv4().size() == dscpmap_ipv4_.size() &&
122 : std::equal(rhs.dscpmap_ipv4().begin(), rhs.dscpmap_ipv4().end(), dscpmap_ipv4_.begin()) &&
123 : rhs.dscpmap_ipv6().size() == dscpmap_ipv6_.size() &&
124 : std::equal(rhs.dscpmap_ipv6().begin(), rhs.dscpmap_ipv6().end(), dscpmap_ipv6_.begin());
125 : }
126 :
127 0 : const std::string name() { return name_; }
128 0 : const std::string tenantaddr() const { return tenantaddr_; }
129 : const std::string tenant() const { return tenant_; }
130 0 : const std::string tags() const { return tags_; }
131 0 : const std::map< std::string, std::string > dscpmap_ipv4() const { return dscpmap_ipv4_; }
132 0 : const std::map< std::string, std::string > dscpmap_ipv6() const { return dscpmap_ipv6_; }
133 :
134 : bool IsMe(const std::string &name) const { return name == name_; }
135 0 : void Refresh(const std::string &name, const std::string &tenantaddr,
136 : const std::string &tenant, const std::string &tags,
137 : const std::map< std::string, std::string > &dscpmap_ipv4,
138 : const std::map< std::string, std::string > &dscpmap_ipv6) {
139 0 : if (tenantaddr_ != tenantaddr)
140 0 : tenantaddr_ = tenantaddr;
141 0 : if (tenant_ != tenant)
142 0 : tenant_ = tenant;
143 0 : if (tags_ != tags)
144 0 : tags_ = tags;
145 0 : if (dscpmap_ipv4.size() != dscpmap_ipv4_.size()) {
146 0 : dscpmap_ipv4_ = dscpmap_ipv4;
147 0 : } else if (!(std::equal(dscpmap_ipv4.begin(), dscpmap_ipv4.end(), dscpmap_ipv4_.begin()))) {
148 0 : dscpmap_ipv4_ = dscpmap_ipv4;
149 : }
150 0 : if (dscpmap_ipv6.size() != dscpmap_ipv6_.size()) {
151 0 : dscpmap_ipv6_ = dscpmap_ipv6;
152 0 : } else if (!(std::equal(dscpmap_ipv6.begin(), dscpmap_ipv6.end(), dscpmap_ipv6_.begin()))) {
153 0 : dscpmap_ipv6_ = dscpmap_ipv6;
154 : }
155 0 : refreshed_ = true;
156 0 : }
157 : bool GetandClearRefreshed() { bool r = refreshed_; refreshed_ = false; return r;}
158 : private:
159 : bool refreshed_;
160 : std::string name_;
161 : std::string tenantaddr_;
162 : std::string tenant_;
163 : std::string tags_;
164 : std::map< std::string, std::string > dscpmap_ipv4_;
165 : std::map< std::string, std::string > dscpmap_ipv6_;
166 : };
167 :
168 : class ApplicationRecord {
169 : public:
170 0 : explicit ApplicationRecord(const std::string &name, const std::string &app_category,
171 : const std::string &app_subcategory, const std::string &app_groups,
172 0 : const std::string &app_risk, const std::string &app_service_tags):
173 0 : refreshed_(true), name_(name), app_category_(app_category),
174 0 : app_subcategory_(app_subcategory), app_groups_(app_groups),
175 0 : app_risk_(app_risk), app_service_tags_(app_service_tags) {
176 0 : }
177 : bool operator==(const ApplicationRecord &rhs) {
178 : return rhs.IsMe(name_) && rhs.app_category() == app_category_ &&
179 : rhs.app_subcategory() == app_subcategory_ &&
180 : rhs.app_groups() == app_groups_ &&
181 : rhs.app_risk() == app_risk_ &&
182 : rhs.app_service_tags() == app_service_tags_;
183 : }
184 :
185 0 : const std::string name() { return name_; }
186 : const std::string app_category() const { return app_category_; }
187 : const std::string app_subcategory() const { return app_subcategory_; }
188 : const std::string app_groups() const { return app_groups_; }
189 : const std::string app_risk() const { return app_risk_; }
190 : const std::string app_service_tags() const { return app_service_tags_; }
191 :
192 : bool IsMe(const std::string &name) const { return name == name_; }
193 0 : void Refresh(const std::string &name, const std::string &app_category,
194 : const std::string &app_subcategory, const std::string &app_groups,
195 : const std::string &app_risk, const std::string &app_service_tags) {
196 0 : if (app_category_ != app_category)
197 0 : app_category_ = app_category;
198 0 : if (app_subcategory_ != app_subcategory)
199 0 : app_subcategory_ = app_subcategory;
200 0 : if (app_groups_ != app_groups)
201 0 : app_groups_ = app_groups;
202 0 : if (app_risk_ != app_risk)
203 0 : app_risk_ = app_risk;
204 0 : if (app_service_tags_ != app_service_tags)
205 0 : app_service_tags_ = app_service_tags;
206 :
207 0 : refreshed_ = true;
208 0 : }
209 : bool GetandClearRefreshed() { bool r = refreshed_; refreshed_ = false; return r;}
210 : private:
211 : bool refreshed_;
212 : std::string name_;
213 : std::string app_category_;
214 : std::string app_subcategory_;
215 : std::string app_groups_;
216 : std::string app_risk_;
217 : std::string app_service_tags_;
218 : };
219 :
220 : class TenantApplicationRecord {
221 : public:
222 0 : explicit TenantApplicationRecord(const std::string &name, const std::string &tenant_app_category,
223 : const std::string &tenant_app_subcategory, const std::string &tenant_app_groups,
224 0 : const std::string &tenant_app_risk, const std::string &tenant_app_service_tags):
225 0 : refreshed_(true), name_(name), tenant_app_category_(tenant_app_category),
226 0 : tenant_app_subcategory_(tenant_app_subcategory),
227 0 : tenant_app_groups_(tenant_app_groups), tenant_app_risk_(tenant_app_risk),
228 0 : tenant_app_service_tags_(tenant_app_service_tags) {
229 0 : }
230 : bool operator==(const TenantApplicationRecord &rhs) {
231 : return rhs.IsMe(name_) && rhs.tenant_app_category() == tenant_app_category_ &&
232 : rhs.tenant_app_subcategory() == tenant_app_subcategory_ &&
233 : rhs.tenant_app_groups() == tenant_app_groups_ &&
234 : rhs.tenant_app_risk() == tenant_app_risk_ &&
235 : rhs.tenant_app_service_tags() == tenant_app_service_tags_;
236 : }
237 :
238 0 : const std::string name() { return name_; }
239 : const std::string tenant_app_category() const { return tenant_app_category_; }
240 : const std::string tenant_app_subcategory() const { return tenant_app_subcategory_; }
241 : const std::string tenant_app_groups() const { return tenant_app_groups_; }
242 : const std::string tenant_app_risk() const { return tenant_app_risk_; }
243 : const std::string tenant_app_service_tags() const { return tenant_app_service_tags_; }
244 :
245 : bool IsMe(const std::string &name) const { return name == name_; }
246 0 : void Refresh(const std::string &name, const std::string &tenant_app_category,
247 : const std::string &tenant_app_subcategory, const std::string &tenant_app_groups,
248 : const std::string &tenant_app_risk, const std::string &tenant_app_service_tags) {
249 0 : if (tenant_app_category_ != tenant_app_category)
250 0 : tenant_app_category_ = tenant_app_category;
251 0 : if (tenant_app_subcategory_ != tenant_app_subcategory)
252 0 : tenant_app_subcategory_ = tenant_app_subcategory;
253 0 : if (tenant_app_groups_ != tenant_app_groups)
254 0 : tenant_app_groups_ = tenant_app_groups;
255 0 : if (tenant_app_risk_ != tenant_app_risk)
256 0 : tenant_app_risk_ = tenant_app_risk;
257 0 : if (tenant_app_service_tags_ != tenant_app_service_tags)
258 0 : tenant_app_service_tags_ = tenant_app_service_tags;
259 :
260 0 : refreshed_ = true;
261 0 : }
262 : bool GetandClearRefreshed() { bool r = refreshed_; refreshed_ = false; return r;}
263 : private:
264 : bool refreshed_;
265 : std::string name_;
266 : std::string tenant_app_category_;
267 : std::string tenant_app_subcategory_;
268 : std::string tenant_app_groups_;
269 : std::string tenant_app_risk_;
270 : std::string tenant_app_service_tags_;
271 : };
272 :
273 : class MessageConfig {
274 : public:
275 0 : explicit MessageConfig(const std::string &name, const std::vector< std::string > &tags,
276 : const std::vector< std::string > &ints, bool process_and_store, bool forward,
277 0 : bool process_before_forward, bool process_and_summarize, bool process_and_summarize_user):
278 0 : refreshed_(true), name_(name), tags_(tags), ints_(ints), process_and_store_(process_and_store),
279 0 : forward_(forward), process_before_forward_(process_before_forward), process_and_summarize_(process_and_summarize),
280 0 : process_and_summarize_user_(process_and_summarize_user) {
281 0 : }
282 : bool operator==(const MessageConfig &rhs) {
283 : return rhs.IsMe(name_) && rhs.tags().size() == tags_.size() &&
284 : std::equal(rhs.tags().begin(), rhs.tags().end(), tags_.begin()) &&
285 : rhs.ints().size() == ints_.size() &&
286 : std::equal(rhs.ints().begin(), rhs.ints().end(), ints_.begin()) &&
287 : rhs.forward() == forward_ &&
288 : rhs.process_and_store() == process_and_store_ &&
289 : rhs.process_before_forward() == process_before_forward_ &&
290 : rhs.process_and_summarize() == process_and_summarize_ &&
291 : rhs.process_and_summarize_user() == process_and_summarize_user_;
292 : }
293 :
294 0 : const std::string name() { return name_; }
295 0 : const std::vector< std::string > tags() const { return tags_; }
296 0 : const std::vector< std::string > ints() const { return ints_; }
297 0 : const bool process_and_store() const { return process_and_store_; }
298 0 : const bool process_and_summarize() const { return process_and_summarize_; }
299 0 : const bool process_and_summarize_user() const { return process_and_summarize_user_; }
300 0 : const bool forward() const { return forward_; }
301 0 : const bool process_before_forward() const { return process_before_forward_; }
302 : bool IsMe(const std::string &name) const { return name == name_; }
303 0 : void Refresh(const std::string &name, const std::vector< std::string > &tags,
304 : const std::vector< std::string > &ints, bool process_and_store,
305 : bool forward, bool process_before_forward, bool process_and_summarize,
306 : bool process_and_summarize_user) {
307 0 : if (tags.size() != tags_.size()) {
308 0 : tags_ = tags;
309 0 : } else if (!(std::equal(tags.begin(), tags.end(), tags_.begin()))) {
310 0 : tags_ = tags;
311 : }
312 0 : if (ints.size() != ints_.size()) {
313 0 : ints_ = ints;
314 0 : } else if (!(std::equal(ints.begin(), ints.end(), ints_.begin()))) {
315 0 : ints_ = ints;
316 : }
317 0 : if (process_and_store_ != process_and_store)
318 0 : process_and_store_ = process_and_store;
319 0 : if (process_and_summarize_ != process_and_summarize)
320 0 : process_and_summarize_ = process_and_summarize;
321 0 : if (process_and_summarize_user_ != process_and_summarize_user)
322 0 : process_and_summarize_user_ = process_and_summarize_user;
323 0 : if (forward_ != forward)
324 0 : forward_ = forward;
325 0 : if (process_before_forward_ != process_before_forward)
326 0 : process_before_forward_ = process_before_forward;
327 :
328 0 : refreshed_ = true;
329 0 : }
330 : bool GetandClearRefreshed() { bool r = refreshed_; refreshed_ = false; return r;}
331 : private:
332 : bool refreshed_;
333 : std::string name_;
334 : std::vector< std::string > tags_;
335 : std::vector< std::string > ints_;
336 : bool process_and_store_;
337 : bool forward_;
338 : bool process_before_forward_;
339 : bool process_and_summarize_;
340 : bool process_and_summarize_user_;
341 : };
342 :
343 : class SlaProfileRecord {
344 : public:
345 0 : explicit SlaProfileRecord(const std::string &name, const std::string &sla_params):
346 0 : refreshed_(true), name_(name), sla_params_(sla_params) {
347 0 : }
348 : bool operator==(const SlaProfileRecord &rhs) {
349 : return rhs.IsMe(name_) && rhs.sla_params() == sla_params_;
350 : }
351 :
352 0 : const std::string name() { return name_; }
353 0 : const std::string sla_params() const { return sla_params_; }
354 :
355 : bool IsMe(const std::string &name) const { return name == name_; }
356 0 : void Refresh(const std::string &name, const std::string &sla_params) {
357 0 : if (sla_params_ != sla_params)
358 0 : sla_params_ = sla_params;
359 :
360 0 : refreshed_ = true;
361 0 : }
362 : bool GetandClearRefreshed() { bool r = refreshed_; refreshed_ = false; return r;}
363 : private:
364 : bool refreshed_;
365 : std::string name_;
366 : std::string sla_params_;
367 : };
368 :
369 : typedef std::map<std::string, boost::shared_ptr<HostnameRecord> > Chr_t;
370 : typedef std::map<std::string, boost::shared_ptr<TenantRecord> > Ctr_t;
371 : typedef std::map<std::string, boost::shared_ptr<ApplicationRecord> > Car_t;
372 : typedef std::map<std::string, boost::shared_ptr<TenantApplicationRecord> > Ctar_t;
373 : typedef std::map<std::string, boost::shared_ptr<MessageConfig> > Cmc_t;
374 : typedef std::map<std::string, boost::shared_ptr<SlaProfileRecord> > Csr_t;
375 : typedef std::vector<IPNetwork> IPNetworks;
376 : typedef std::map<std::string, IPNetworks> IPNetworks_map;
377 : typedef std::map<std::string, std::map<std::string, uint64_t> > SyslogSessionConfig;
378 :
379 : class StructuredSyslogConfig {
380 : public:
381 : StructuredSyslogConfig(ConfigClientCollector *config_client, uint64_t structured_syslog_active_session_map_limit);
382 : ~StructuredSyslogConfig();
383 : uint32_t IPToUInt(std::string ip);
384 : int get_ip_version (const std::string ip);
385 : bool AddSyslogSessionCounter(const std::string session_unique_key,
386 : std::map<std::string, uint64_t> session_traffic_counters);
387 : int RemoveSyslogSessionCounter(const std::string &session_unique_key);
388 : bool FetchSyslogSessionCounters(const std::string &session_unique_key,
389 : std::map<std::string, uint64_t> &session_traffic_counters);
390 : std::vector<std::string> split_into_vector(std::string str, char delimiter) ;
391 : bool AddNetwork(const std::string& key, const std::string& network, const std::string& mask, const std::string& location);
392 : bool RefreshNetworksMap(const std::string location);
393 : std::string FindNetwork(std::string ip, std::string key, std::string src_location);
394 : void AddHostnameRecord(const std::string &name, const std::string &hostaddr,
395 : const std::string &tenant, const std::string &location,
396 : const std::string &device, const std::string &tags,
397 : const std::map< std::string, std::string > &linkmap);
398 : void AddTenantRecord(const std::string &name, const std::string &tenantaddr,
399 : const std::string &tenant, const std::string &tags,
400 : const std::map< std::string, std::string > &dscpmap_ipv4,
401 : const std::map< std::string, std::string > &dscpmap_ipv6);
402 : void AddApplicationRecord(const std::string &name, const std::string &app_category,
403 : const std::string &app_subcategory, const std::string &app_groups,
404 : const std::string &app_risk, const std::string &app_service_tags);
405 : void AddTenantApplicationRecord(const std::string &name, const std::string &tenant_app_category,
406 : const std::string &tenant_app_subcategory, const std::string &tenant_app_groups,
407 : const std::string &tenant_app_risk, const std::string &tenant_app_service_tags);
408 : void AddMessageConfig(const std::string &name, const std::vector< std::string > &tags,
409 : const std::vector< std::string > &ints, bool process_and_store,
410 : const std::string &forward, bool process_and_summarize, bool process_and_summarize_user);
411 : void AddSlaProfileRecord(const std::string &name, const std::string &sla_params);
412 : boost::shared_ptr<HostnameRecord> GetHostnameRecord(const std::string &name);
413 : boost::shared_ptr<TenantRecord> GetTenantRecord(const std::string &name);
414 : boost::shared_ptr<ApplicationRecord> GetApplicationRecord(const std::string &name);
415 : boost::shared_ptr<TenantApplicationRecord> GetTenantApplicationRecord(const std::string &name);
416 : boost::shared_ptr<MessageConfig> GetMessageConfig(const std::string &name);
417 : boost::shared_ptr<SlaProfileRecord> GetSlaProfileRecord(const std::string &name);
418 : void ReceiveConfig(const contrail_rapidjson::Document &jdoc, bool add_change);
419 : private:
420 : void HostnameRecordsHandler(const contrail_rapidjson::Document &jdoc,
421 : bool add_change);
422 : void TenantRecordsHandler(const contrail_rapidjson::Document &jdoc,
423 : bool add_change);
424 : void ApplicationRecordsHandler(const contrail_rapidjson::Document &jdoc,
425 : bool add_change);
426 : void MessageConfigsHandler(const contrail_rapidjson::Document &jdoc,
427 : bool add_change);
428 : void SlaProfileRecordsHandler(const contrail_rapidjson::Document &jdoc,
429 : bool add_change);
430 : Chr_t hostname_records_;
431 : Ctr_t tenant_records_;
432 : Car_t application_records_;
433 : Ctar_t tenant_application_records_;
434 : Cmc_t message_configs_;
435 : Csr_t sla_profile_records_;
436 :
437 : //networks_map_refresh_mutex should be used only to refresh networks map
438 : std::mutex networks_map_refresh_mutex;
439 : IPNetworks_map networks_map_;
440 : SyslogSessionConfig session_config_map_;
441 : uint64_t activeSessionConfigMapLIMIT;
442 : };
443 :
444 :
445 : #endif
|