Line data Source code
1 : /*
2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3 : * Copyright Contributors to the OpenSDN project
4 : */
5 :
6 : #include "viz_message.h"
7 :
8 : #include <base/logging.h>
9 : #include <base/util.h>
10 : #include <sandesh/sandesh_message_builder.h>
11 : #include <analytics/collector_uve_types.h>
12 :
13 0 : RuleMsg::RuleMsg(const VizMsg* vmsgp) :
14 0 : hdr(vmsgp->msg->GetHeader()),
15 0 : messagetype(vmsgp->msg->GetMessageType()),
16 0 : message_node_(static_cast<const SandeshXMLMessage *>(
17 0 : vmsgp->msg)->GetMessageNode()) {
18 0 : }
19 :
20 0 : RuleMsg::~RuleMsg() {
21 0 : }
22 :
23 0 : int RuleMsg::field_value_recur(const std::string& field_id, std::string& type, std::string& value, pugi::xml_node doc) const {
24 : size_t dotpos;
25 0 : if ((dotpos = field_id.find_first_of('.')) != std::string::npos) {
26 0 : std::string parent = field_id.substr(0, dotpos);
27 0 : std::string children = field_id.substr(dotpos+1);
28 :
29 0 : RuleMsgPredicate p1(parent);
30 :
31 0 : pugi::xml_node node = doc.find_node(p1);
32 0 : if (node.type() == pugi::node_null) {
33 0 : return -1;
34 : }
35 :
36 : const char *ret;
37 0 : ret = node.attribute("type").value();
38 0 : if (strncmp("struct", ret, 6))
39 0 : return -1;
40 :
41 0 : return (field_value_recur(children, type, value, node));
42 0 : }
43 :
44 0 : RuleMsgPredicate p1(field_id);
45 0 : pugi::xml_node node = doc.find_node(p1);
46 0 : if (node.type() == pugi::node_null) {
47 0 : return -1;
48 : }
49 0 : value = node.child_value();;
50 0 : type = node.attribute("type").value();
51 :
52 0 : return 0;
53 0 : }
54 :
55 0 : int RuleMsg::field_value(const std::string& field_id, std::string& type, std::string& value) const {
56 0 : return field_value_recur(field_id, type, value, message_node_);
57 : }
58 :
59 0 : void VizMsgStats::Update(const VizMsg *vmsg) {
60 0 : const SandeshHeader &header(vmsg->msg->GetHeader());
61 0 : messages++;
62 0 : bytes += vmsg->msg->GetSize();
63 0 : last_msg_timestamp = header.get_Timestamp();
64 0 : }
65 :
66 : template <typename T, typename K>
67 0 : void GetStats(T *stats, const VizMsgStats *vmstats, const K &key) {
68 0 : stats->set_messages(vmstats->messages);
69 0 : stats->set_bytes(vmstats->bytes);
70 0 : stats->set_last_msg_timestamp(vmstats->last_msg_timestamp);
71 0 : }
72 :
73 : template <>
74 0 : void GetStats<>(SandeshMessageInfo *sminfo,
75 : const VizMsgStats *vmstats, const VizMsgStatistics::TypeLevelKey &key) {
76 0 : sminfo->set_type(key.first);
77 0 : sminfo->set_level(key.second);
78 0 : sminfo->set_messages(vmstats->messages);
79 0 : sminfo->set_bytes(vmstats->bytes);
80 0 : }
81 :
82 : template <typename K, typename T>
83 0 : void VizMsgStats::Get(const K &key, T *stats) const {
84 0 : GetStats<T>(stats, this, key);
85 0 : }
86 :
87 : template <typename Table, typename Key>
88 0 : static void UpdateStats(Table &t, Key &k, const VizMsg *vmsg) {
89 0 : typename Table::iterator it = t.find(k);
90 0 : if (it == t.end()) {
91 0 : it = (t.insert(k, new VizMsgStats)).first;
92 : }
93 0 : VizMsgStats *vmstats = it->second;
94 0 : vmstats->Update(vmsg);
95 0 : }
96 :
97 0 : void VizMsgStatistics::Update(const VizMsg *vmsg) {
98 : // TypeMap
99 0 : std::string messagetype(vmsg->msg->GetMessageType());
100 0 : UpdateStats<TypeMap, std::string>(type_map, messagetype, vmsg);
101 : // LogLevelMap for only system log and syslog
102 0 : const SandeshHeader &header(vmsg->msg->GetHeader());
103 : SandeshLevel::type level(static_cast<SandeshLevel::type>(
104 0 : header.get_Level()));
105 0 : std::string level_str(Sandesh::LevelToString(level));
106 0 : const SandeshType::type &stype(header.get_Type());
107 0 : if (stype == SandeshType::SYSTEM ||
108 0 : stype == SandeshType::SYSLOG) {
109 0 : UpdateStats<LevelMap, std::string>(level_map, level_str,
110 : vmsg);
111 : }
112 : // TypeLevelMap
113 0 : TypeLevelKey tlkey(messagetype, level_str);
114 0 : UpdateStats<TypeLevelMap, TypeLevelKey>(type_level_map, tlkey, vmsg);
115 0 : }
116 :
117 : // TypeMap
118 0 : void VizMsgStatistics::Get(std::vector<SandeshStats> *ssv) const {
119 0 : for (TypeMap::const_iterator it = type_map.begin();
120 0 : it != type_map.end(); it++) {
121 0 : SandeshStats sstats;
122 0 : sstats.set_message_type(it->first);
123 0 : const VizMsgStats *vmstats(it->second);
124 0 : vmstats->Get(it->first, &sstats);
125 0 : ssv->push_back(sstats);
126 0 : }
127 0 : }
128 :
129 : // LogLevelMap
130 0 : void VizMsgStatistics::Get(std::vector<SandeshLogLevelStats> *lsv) const {
131 0 : for (LevelMap::const_iterator it = level_map.begin();
132 0 : it != level_map.end(); it++) {
133 0 : SandeshLogLevelStats lstats;
134 0 : lstats.set_level(it->first);
135 0 : const VizMsgStats *vmstats(it->second);
136 0 : vmstats->Get(it->first, &lstats);
137 0 : lsv->push_back(lstats);
138 0 : }
139 0 : }
140 :
141 : // TypeLevelMap
142 0 : void VizMsgStatistics::Get(std::vector<SandeshMessageInfo> *smv) {
143 0 : for (TypeLevelMap::const_iterator it = type_level_map.begin();
144 0 : it != type_level_map.end(); it++) {
145 0 : const VizMsgStats *vmstats(it->second);
146 0 : SandeshMessageInfo smi;
147 0 : vmstats->Get(it->first, &smi);
148 0 : smv->push_back(smi);
149 0 : }
150 0 : type_level_map.clear();
151 0 : }
|