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