{
const RuleKey& k = it.first;
OptTreeNode* otn = OtnLookup(sc->otn_map, k.gid, k.sid);
+ auto empty_ips_id = get_empty_ips_policy(sc)->policy_id;
if ( !otn )
ParseWarning(WARN_RULES, "Rule state specified for unknown rule %u:%u", k.gid, k.sid);
{
for ( unsigned i = 0; i < sc->policy_map->ips_policy_count(); i++ )
{
- if ( sc->policy_map->get_ips_policy(i) )
+ auto policy = sc->policy_map->get_ips_policy(i);
+ if ( policy and (policy->policy_id != empty_ips_id) )
apply(sc, otn, i, it.second);
}
}
void RuleStateMap::apply(
SnortConfig* sc, OptTreeNode* otn, unsigned ips_num, RuleState& s)
{
+ IpsPolicy* policy = nullptr;
RuleTreeNode* rtn = getRtnFromOtn(otn, ips_num);
if ( !rtn )
- rtn = getRtnFromOtn(otn, 0);
+ if ( ips_num and (rtn = getRtnFromOtn(otn, 0)) )
+ policy = sc->policy_map->get_ips_policy(ips_num);
if ( !rtn )
return;
+ if ( policy )
+ policy->rules_shared++;
+
rtn = dup_rtn(rtn);
update_rtn(rtn, s);
addRtnToOtn(sc, otn, rtn, ips_num);
};
static int rule_count = 0;
+static int prev_rule_count = 0;
static int skip_count = 0;
static int detect_rule_count = 0;
static int builtin_rule_count = 0;
static int head_count = 0; // rule headers
static int otn_count = 0; // rule bodies
static int dup_count = 0; // rule bodies
+static int prev_dup_count = 0;
static int rule_proto = 0;
static rule_count_t tcpCnt;
{ return rule_count; }
}
+int get_policy_loaded_rule_count()
+{
+ auto policy_rule_count = rule_count - prev_rule_count;
+ prev_rule_count = rule_count;
+ return policy_rule_count;
+}
+
+int get_policy_shared_rule_count()
+{
+ auto policy_rule_count = dup_count - prev_dup_count;
+ prev_dup_count = dup_count;
+ return policy_rule_count;
+}
+
void parse_rule_init()
{
rule_count = 0;
+ prev_rule_count = 0;
skip_count = 0;
detect_rule_count = 0;
builtin_rule_count = 0;
head_count = 0;
otn_count = 0;
dup_count = 0;
+ prev_dup_count = 0;
rule_proto = 0;
memset(&ipCnt, 0, sizeof(ipCnt));
}
};
+struct PolicyRuleStats
+{
+ const char* file;
+ int loaded;
+ int shared;
+ int enabled;
+};
+
//-------------------------------------------------------------------------
// private / implementation methods
//-------------------------------------------------------------------------
parse_stream(std::cin, sc);
pop_parse_location();
}
+
+ p->rules_loaded = get_policy_loaded_rule_count();
+ p->rules_shared = get_policy_shared_rule_count();
}
set_ips_policy(sc, 0);
void ShowPolicyStats(const SnortConfig* sc)
{
std::unordered_map<PolicyId, int> stats;
- std::multimap<PolicyId, std::tuple<const char*, int>> sorted_stats;
+ std::multimap<PolicyId, PolicyRuleStats> sorted_stats;
if ( !sc->otn_map )
return;
}
}
- for (const auto& s : stats)
+ for (unsigned i = 0; i < sc->policy_map->ips_policy_count(); i++)
{
- auto shell = sc->policy_map->get_shell_by_policy(s.first);
+ auto policy = sc->policy_map->get_ips_policy(i);
+ if ( !policy )
+ continue;
+
+ auto shell = sc->policy_map->get_shell_by_policy(i);
if ( !shell )
continue;
if ( !file or !file[0] )
continue;
- auto policy = sc->policy_map->get_ips_policy(s.first);
- auto id = policy ? policy->user_policy_id : 0;
+ auto id = policy->user_policy_id;
+ auto rules_loaded = policy->rules_loaded;
+ auto rules_shared = policy->rules_shared;
+
+ auto res = stats.find(i);
+ auto rules_enabled = (res == stats.end()) ? 0 : res->second;
- sorted_stats.emplace(id, std::make_tuple(file, s.second));
+ if ( rules_loaded or rules_shared or rules_enabled )
+ sorted_stats.emplace(id, PolicyRuleStats{file, rules_loaded,
+ rules_shared, rules_enabled});
}
if ( !sorted_stats.size() )
return;
- LogLabel("ips policies");
- LogMessage("%16s%16s%8s\n", "id", "rules enabled", "file");
+ LogLabel("ips policies rule stats");
+ LogMessage("%16s%8s%8s%8s%8s\n", "id", "loaded", "shared", "enabled", "file");
for (const auto& s : sorted_stats)
- {
- auto file = std::get<0>(s.second);
- auto rules_count = std::get<1>(s.second);
- LogMessage("%16u%16d%4s%s\n", s.first, rules_count, " ", file);
- }
+ LogMessage("%16u%8d%8d%8d%4s%s\n", s.first, s.second.loaded, s.second.shared,
+ s.second.enabled, " ", s.second.file);
}
/****************************************************************************