adding rudimentary JSON support

This commit is contained in:
dataking 2016-11-18 15:13:09 -08:00
parent 6005466f60
commit 225330465e

View File

@ -10,17 +10,20 @@ use Getopt::Long qw( :config no_ignore_case bundling );
use Data::Dumper;
use Module::Load::Conditional qw( can_load check_install requires );
my ($help,$verbose,$excel,$output,$pdf);
my ($help,$verbose,$excel,$output,$pdf,$debug,$json,$quiet);
GetOptions(
'h|help' => \$help,
'v|verbose+' => \$verbose,
'E|excel' => \$excel,
'o|output=s' => \$output,
'p|pdf' => \$pdf,
'D|debug' => \$debug,
'j|json' => \$json,
'q|quiet' => \$quiet,
);
&usage if ($help);
&usage if (!$output);
&usage if ((!$output) and (!$json));
my %to_bool = ( 0 => 'false', 1 => 'true' );
my %vm_mode = ( 0 => 'false', 1 => 'guest', 2 => 'host' );
@ -32,6 +35,8 @@ my %systemd_uf_status_color = (
'masked' => 'goldenrod'
);
if ($json) { $quiet = 1; }
my ($basename, $path, $suffix, $htmldoc);
if ($excel) {
@ -59,18 +64,20 @@ if (( -e $lynis_report) and ( ! -z $lynis_report )) {
}
if (($audit_run) and ($audit_run >= 1)) {
print colored("Looks like the audit has been run.", "bold green");
print "\n";
print colored("Looks like the audit has been run.", "bold green") unless ($quiet);
print "\n" unless ($quiet);
} else {
print colored("Couldn't find one or more of the lynis output files. Try running the audit again. \n", "bold red");
warn colored("Couldn't find one or more of the lynis output files. Try running the audit again. \n", "bold red");
}
print colored("Outputting report to $output, in ", "bold green");
if ($excel) { print colored("Excel ", "bold green"); }
elsif ($pdf) { print colored("PDF ", "bold green)"); }
else { print colored("HTML ", "bold green"); }
print colored("format.", "bold green");
print "\n";
unless ($quiet) {
print colored("Outputting report to $output, in ", "bold green");
if ($excel) { print colored("Excel ", "bold green"); }
elsif ($pdf) { print colored("PDF ", "bold green)"); }
else { print colored("HTML ", "bold green"); }
print colored("format.", "bold green");
print "\n";
}
# the report is easy to process, and actually doesn't contain the "audit findings"....just the data.
# but it is not our job to draw conclusions here, just present the findings of the tool.
@ -100,6 +107,24 @@ while (my $line = <RPT>) {
}
close RPT or die colored("There was a problem closing the lynis report: $! \n", "bold red");
if ($debug) {
print Dumper(\%lynis_report_data);
exit 1;
}
if ($json) {
require JSON;
if ($output) {
# open the file and write to it
}
# it's moe likely JSON consumers would want to pipe the output to another process
# so print to STDOUT
my $json = JSON->new->allow_nonref;
my $json_text = $json->encode( \%lynis_report_data );
print $json_text;
exit 0;
}
@{$lynis_report_data{'automation_tool_running[]'}} = &dedup_array($lynis_report_data{'automation_tool_running[]'}) if (ref($lynis_report_data{'automation_tool_running[]'}) eq 'ARRAY');
@{$lynis_report_data{'boot_service[]'}} = &dedup_array($lynis_report_data{'boot_service[]'}) if (ref($lynis_report_data{'boot_service[]'}) eq "ARRAY");
@{$lynis_report_data{'cronjob[]'}} = &dedup_array($lynis_report_data{'cronjob[]'}) if (ref($lynis_report_data{'cronjob[]'}) eq 'ARRAY');
@ -2010,6 +2035,11 @@ if ($verbose) {
# subs
###############################################################################
sub usage {
if (!$output) {
print colored("You must specify an output file.\n", "bold yellow");
}
print <<END;
$0 -h|--help -v|--verbose -E|--excel -o|--output