Skip to content

Commit 1854a0d

Browse files
committed
Firewall: Add Policy Usage
Add new chapiter about policy usage with summary of 30, 7, 1 days (and never) used policy
1 parent 80ba5f8 commit 1854a0d

File tree

1 file changed

+248
-0
lines changed

1 file changed

+248
-0
lines changed

Src/Private/Get-AbrFgtFirewall.ps1

Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,254 @@ function Get-AbrFgtFirewall {
590590
}
591591
}
592592

593+
$MonitorRules = Get-FGTMonitorFirewallPolicy
594+
if ($MonitorRules -and $InfoLevel.Firewall -ge 1) {
595+
596+
$monitor_result = @()
597+
$now = [int64](Get-Date -UFormat %s)
598+
$one_day = 86400 # 1 day en seconds
599+
$seven_days = 604800 # 7 day en seconds
600+
$thirty_days = 2592000 # 30 day en seconds
601+
602+
foreach ($monitor in $MonitorRules) {
603+
604+
#Skip the policy id 0 (Implicit Deny)
605+
if ($monitor.policyid -eq "0 ") {
606+
continue;
607+
}
608+
$last_policy_use = "Never"
609+
$now_minus_one_day = $now - $one_day
610+
$now_minus_seven_days = $now - $seven_days
611+
$now_minus_thirty_days = $now - $thirty_days
612+
if ($monitor.last_used -ge $now_minus_thirty_days) {
613+
$last_policy_use = "Last Month"
614+
}
615+
if ($monitor.last_used -ge $now_minus_seven_days) {
616+
$last_policy_use = "Last Week"
617+
}
618+
if ($monitor.last_used -ge $now_minus_one_day) {
619+
$last_policy_use = "Last Day"
620+
}
621+
622+
$rule = $policy | Where-Object { $_.policyid -eq $monitor.policyid }
623+
624+
#Using ISDB for Destination ?
625+
if ($rule.'internet-service' -eq "enable") {
626+
627+
$dst = $rule.'internet-service-name'.name -join ", "
628+
}
629+
else {
630+
$dst = $rule.dstaddr.name -join ", "
631+
}
632+
633+
$dst += " (To $($rule.dstintf.name -join ", "))"
634+
635+
#Using ISDB for Source ?
636+
if ($rule.'internet-service-src ' -eq "enable") {
637+
638+
$src = $rule.'internet-service-src-name'.name -join ", "
639+
}
640+
else {
641+
$src = $rule.srcaddr.name -join ", "
642+
}
643+
$src += " (From $($rule.srcintf.name -join ', '))"
644+
645+
$monitor_result += [pscustomobject]@{
646+
"Id" = $rule.policyid
647+
"Name" = $rule.name
648+
"Source" = $src
649+
"Destination" = $dst
650+
"Service" = $rule.service.name -join ", "
651+
"Action" = $rule.action
652+
"Hit" = $monitor.hit_count
653+
"Usage" = $last_policy_use
654+
}
655+
}
656+
657+
Section -Style Heading3 'Usage Policy Summary' {
658+
Paragraph "The following section provides an usage policy summary of firewall."
659+
BlankLine
660+
$usage_count = @($monitor_result).count
661+
662+
$never_status = @($monitor_result | Where-Object { $_.usage -eq 'Never' }).count
663+
$never_text = "$never_status"
664+
if ($usage_count) {
665+
$never_pourcentage = [math]::Round(($never_status / $usage_count * 100), 2)
666+
$never_text += " ($never_pourcentage%)"
667+
}
668+
669+
$thirty_status = @($monitor_result | Where-Object { $_.usage -eq 'Last Month' }).count
670+
$thirty_text = "$thirty_status"
671+
if ($usage_count) {
672+
$thirty_pourcentage = [math]::Round(($thirty_status / $usage_count * 100), 2)
673+
$thirty_text += " ($thirty_pourcentage%)"
674+
}
675+
676+
$seven_status = @($monitor_result | Where-Object { $_.usage -eq 'Last Week' }).count
677+
$seven_text = "$seven_status"
678+
if ($usage_count) {
679+
$seven_pourcentage = [math]::Round(($seven_status / $usage_count * 100), 2)
680+
$seven_text += " ($seven_pourcentage%)"
681+
}
682+
683+
$one_status = @($monitor_result | Where-Object { $_.usage -eq 'Last Day' }).count
684+
$one_text = "$one_status"
685+
if ($usage_count) {
686+
$one_pourcentage = [math]::Round(($one_status / $usage_count * 100), 2)
687+
$one_text += " ($one_pourcentage%)"
688+
}
689+
690+
$OutObj = [pscustomobject]@{
691+
"Never" = $never_text
692+
"Last 30 Day" = $thirty_text
693+
"Last 7 Day" = $seven_text
694+
"Last 1 day" = $one_text
695+
}
696+
697+
$TableParams = @{
698+
Name = "Usage Policy Summary"
699+
List = $true
700+
ColumnWidths = 50, 50
701+
}
702+
703+
if ($Report.ShowTableCaptions) {
704+
$TableParams['Caption'] = "- $($TableParams.Name)"
705+
}
706+
707+
$OutObj | Table @TableParams
708+
}
709+
710+
$usages = $monitor_result | Where-Object { $_.usage -eq 'Never' }
711+
712+
if ($usages) {
713+
Section -Style Heading3 'Usage Policy (Never)' {
714+
$OutObj = @()
715+
716+
717+
foreach ($usage in $usages) {
718+
$OutObj += [pscustomobject]@{
719+
"Id" = $usage.id
720+
"Name" = $usage.name
721+
"Source" = $usage.source
722+
"Destination" = $usage.destination
723+
"Service" = $usage.service
724+
"Action" = $usage.action
725+
"Hit" = $usage.hit
726+
}
727+
}
728+
729+
$TableParams = @{
730+
Name = "Usage Policy (Never)"
731+
List = $false
732+
ColumnWidths = 5, 10, 29, 29, 10, 8, 9
733+
}
734+
735+
if ($Report.ShowTableCaptions) {
736+
$TableParams['Caption'] = "- $($TableParams.Name)"
737+
}
738+
739+
$OutObj | Table @TableParams
740+
}
741+
}
742+
743+
$usages = $monitor_result | Where-Object { $_.usage -eq 'Last Month' }
744+
745+
if ($usages) {
746+
Section -Style Heading3 'Usage Policy (1 Month)' {
747+
$OutObj = @()
748+
749+
foreach ($usage in $usages) {
750+
$OutObj += [pscustomobject]@{
751+
"Id" = $usage.id
752+
"Name" = $usage.name
753+
"Source" = $usage.source
754+
"Destination" = $usage.destination
755+
"Service" = $usage.service
756+
"Action" = $usage.action
757+
"Hit" = $usage.hit
758+
}
759+
}
760+
761+
$TableParams = @{
762+
Name = "Usage Policy (1 Month)"
763+
List = $false
764+
ColumnWidths = 5, 10, 29, 29, 10, 8, 9
765+
}
766+
767+
if ($Report.ShowTableCaptions) {
768+
$TableParams['Caption'] = "- $($TableParams.Name)"
769+
}
770+
771+
$OutObj | Table @TableParams
772+
}
773+
}
774+
775+
$usages = $monitor_result | Where-Object { $_.usage -eq 'Last Week' }
776+
777+
if ($usages) {
778+
Section -Style Heading3 'Usage Policy (1 Week)' {
779+
$OutObj = @()
780+
781+
foreach ($usage in $usages) {
782+
$OutObj += [pscustomobject]@{
783+
"Id" = $usage.id
784+
"Name" = $usage.name
785+
"Source" = $usage.source
786+
"Destination" = $usage.destination
787+
"Service" = $usage.service
788+
"Action" = $usage.action
789+
"Hit" = $usage.hit
790+
}
791+
}
792+
793+
$TableParams = @{
794+
Name = "Usage Policy (1 Week)"
795+
List = $false
796+
ColumnWidths = 5, 10, 29, 29, 10, 8, 9
797+
}
798+
799+
if ($Report.ShowTableCaptions) {
800+
$TableParams['Caption'] = "- $($TableParams.Name)"
801+
}
802+
803+
$OutObj | Table @TableParams
804+
}
805+
}
806+
807+
$usages = $monitor_result | Where-Object { $_.usage -eq 'Last Day' }
808+
809+
if ($usages) {
810+
Section -Style Heading3 'Usage Policy (1 Day)' {
811+
$OutObj = @()
812+
813+
foreach ($usage in $usages) {
814+
$OutObj += [pscustomobject]@{
815+
"Id" = $usage.id
816+
"Name" = $usage.name
817+
"Source" = $usage.source
818+
"Destination" = $usage.destination
819+
"Service" = $usage.service
820+
"Action" = $usage.action
821+
"Hit" = $usage.hit
822+
}
823+
}
824+
825+
$TableParams = @{
826+
Name = "Usage Policy (1 day)"
827+
List = $false
828+
ColumnWidths = 5, 10, 29, 29, 10, 8, 9
829+
}
830+
831+
if ($Report.ShowTableCaptions) {
832+
$TableParams['Caption'] = "- $($TableParams.Name)"
833+
}
834+
835+
$OutObj | Table @TableParams
836+
}
837+
}
838+
839+
}
840+
593841
}
594842
}
595843

0 commit comments

Comments
 (0)