Skip to content

Commit

Permalink
fortios-firewall-stats: Allow the check to run even some FortiOS user…
Browse files Browse the repository at this point in the history
…s use only IPv4 or IPv6
  • Loading branch information
markuslf committed Oct 13, 2023
1 parent 746391b commit 1b0d5cb
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Monitoring Plugins:
* cpu-usage: On Windows, exclude "System Idle Process" from the Top3 list
* disk-smart: Skip unsupported disks (fix #672)
* fail2ban: Improve output, add unit-test
* fortios-firewall-stats: Allow the check to run even some FortiOS users use only IPv4 or IPv6 ([PR #719](https://github.com/Linuxfabrik/monitoring-plugins/issues/716), thanks to [Pierrot la menace](https://github.com/Pierrot-la-menace))
* grafana-version: Add Grafana v9.5
* infomaniak-events: Add filter for service categories
* infomaniak-swiss-backup-devices: Improve column ordering in output
Expand Down
66 changes: 45 additions & 21 deletions check-plugins/fortios-firewall-stats/fortios-firewall-stats
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ import urllib.parse # pylint: disable=C0413
import lib.base # pylint: disable=C0413
import lib.human # pylint: disable=C0413
import lib.url # pylint: disable=C0413
from lib.globals import (STATE_CRIT, STATE_OK, # pylint: disable=C0413
STATE_UNKNOWN, STATE_WARN)
from lib.globals import (STATE_OK, STATE_UNKNOWN) # pylint: disable=C0413

__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
__version__ = '2023051201'
__version__ = '2023101301'

DESCRIPTION = """Summarizes traffic statistics for all IPv4 and IPv6 firewall policies from Forti
Appliances like FortiGate running FortiOS via FortiOS REST API. The authentication
Expand Down Expand Up @@ -105,32 +104,57 @@ def main():
except SystemExit:
sys.exit(STATE_UNKNOWN)

# try to fetch data
# fetch data
url = 'https://{}/api/v2/monitor/firewall/policy/select/?access_token={}'.format(
args.HOSTNAME, urllib.parse.quote(args.PASSWORD))
success4, result4 = lib.url.fetch_json(url, insecure=args.INSECURE, no_proxy=args.NO_PROXY, timeout=args.TIMEOUT)
args.HOSTNAME,
urllib.parse.quote(args.PASSWORD),
)
success4, result4 = lib.url.fetch_json(
url,
insecure=args.INSECURE,
no_proxy=args.NO_PROXY,
timeout=args.TIMEOUT,
)

url = 'https://{}/api/v2/monitor/firewall/policy6/select/?access_token={}'.format(
args.HOSTNAME, urllib.parse.quote(args.PASSWORD))
success6, result6 = lib.url.fetch_json(url, insecure=args.INSECURE, no_proxy=args.NO_PROXY, timeout=args.TIMEOUT)
args.HOSTNAME,
urllib.parse.quote(args.PASSWORD),
)
success6, result6 = lib.url.fetch_json(
url,
insecure=args.INSECURE,
no_proxy=args.NO_PROXY,
timeout=args.TIMEOUT,
)

# if both requests fail then exit
if success4 == False and success6 == False:
print("Both requests fail :\n"+result4+"\n"+result6+"\n")
sys.exit(STATE_UNKNOWN)

# create an empty dict if IPv4 request fails
if success4 == False:
result4 = {'results': []}

# create an empty dict if IPv6 request fails
if success6 == False:
result6 = {'results': []}
if success4 is False and success6 is False:
lib.base.oao(
'Both requests for IPv4 and IPv6 policies failed.\n* IPv4: "{}"\n* IPv6: "{}"'.format(
result4,
result6,
),
STATE_UNKNOWN,
)

# create an empty dict if IPv4 request fails or is empty
if not success4:
result4 = {
'results': [],
}

# create an empty dict if IPv6 request fails or is empty
if not success6:
result6 = {
'results': [],
}

# count and compute the total
policy_count = len(result4['results']) + len(result6['results'])
total = lib.base.sum_dict(lib.base.sum_lod(
result4['results']), lib.base.sum_lod(result6['results']))
total = lib.base.sum_dict(
lib.base.sum_lod(result4['results']),
lib.base.sum_lod(result6['results']),
)
# {
# u'active_sessions': 2
# u'asic_bytes': 0
Expand Down

0 comments on commit 1b0d5cb

Please sign in to comment.