-
Notifications
You must be signed in to change notification settings - Fork 696
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
46 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .sub import command_sub | ||
from .stats import command_stats | ||
from .most_used_rules import command_most_used_rules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import json | ||
|
||
from ssg.build_profile import XCCDFBenchmark | ||
|
||
|
||
def _count_rules_per_profile(profile, rules): | ||
for rule in profile.get("rules", []): | ||
if rule in rules: | ||
rules[rule] += 1 | ||
else: | ||
rules[rule] = 1 | ||
|
||
|
||
def _count_rules_per_benchmark(benchmark, rules): | ||
benchmark = XCCDFBenchmark(benchmark) | ||
for profile in benchmark.get_all_profile_stats(): | ||
_count_rules_per_profile(profile, rules) | ||
|
||
|
||
def command_most_used_rules(args): | ||
rules = {} | ||
for benchmark in args.benchmarks: | ||
_count_rules_per_benchmark(benchmark, rules) | ||
|
||
sorted_rules = { | ||
k: v for k, v in sorted(rules.items(), key=lambda x: x[1], reverse=True) | ||
} | ||
|
||
f_string = "{}: {}" | ||
|
||
if args.format == "json": | ||
print(json.dumps(sorted_rules, indent=4)) | ||
return | ||
elif args.format == "csv": | ||
print("rule_id,count_of_profiles") | ||
f_string = "{},{}" | ||
|
||
for rule_id, rule_count in sorted_rules.items(): | ||
print(f_string.format(rule_id, rule_count)) |