Skip to content

Commit 7c5698b

Browse files
Merge pull request #1657 from caberos/issue1654
add firewall monitoring command
2 parents 393e70c + d0ea4cc commit 7c5698b

File tree

7 files changed

+96
-4
lines changed

7 files changed

+96
-4
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""Get monitoring for a firewall device."""
2+
# :license: MIT, see LICENSE for more details.
3+
import datetime
4+
5+
import click
6+
7+
import SoftLayer
8+
from SoftLayer.CLI import environment
9+
from SoftLayer.CLI import formatting
10+
11+
12+
@click.command(cls=SoftLayer.CLI.command.SLCommand, )
13+
@click.argument('identifier')
14+
@environment.pass_env
15+
def cli(env, identifier):
16+
"""Gets bandwidth details for a firewall from the past 30 days."""
17+
18+
mgr = SoftLayer.FirewallManager(env.client)
19+
20+
_firewall = mgr.get_instance(identifier)
21+
22+
table = formatting.KeyValueTable(['name', 'value'])
23+
table.align['name'] = 'r'
24+
table.align['value'] = 'l'
25+
26+
end = datetime.date.today()
27+
start = end.replace(day=1)
28+
last_month = start - datetime.timedelta(days=30)
29+
30+
monitoring = mgr.get_summary(_firewall['metricTrackingObject']['id'],
31+
last_month.strftime('%Y-%m-%d'), end.strftime('%Y-%m-%d'))
32+
public_out = 0
33+
public_in = 0
34+
for monitor in monitoring:
35+
36+
if monitor['type'] == 'publicOut_net_octet':
37+
public_out += monitor['counter']
38+
39+
if monitor['type'] == 'publicIn_net_octet':
40+
public_in += monitor['counter']
41+
42+
table.add_row(['Id', _firewall.get('id')])
43+
table.add_row(['Name', _firewall['networkGateway']['name']])
44+
table.add_row(['Stard Date', last_month.strftime('%Y-%m-%d')])
45+
table.add_row(['End Date', end.strftime('%Y-%m-%d')])
46+
table.add_row(['Out', formatting.b_to_gb(public_out)])
47+
table.add_row(['In', formatting.b_to_gb(public_in)])
48+
table.add_row(['Total', formatting.b_to_gb(public_out + public_in)])
49+
table.add_row(['Status', _firewall['networkGateway']['status']['name']])
50+
51+
env.fout(table)

SoftLayer/CLI/routes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@
180180
('firewall:detail', 'SoftLayer.CLI.firewall.detail:cli'),
181181
('firewall:edit', 'SoftLayer.CLI.firewall.edit:cli'),
182182
('firewall:list', 'SoftLayer.CLI.firewall.list:cli'),
183+
('firewall:monitoring', 'SoftLayer.CLI.firewall.monitoring:cli'),
183184

184185
('globalip', 'SoftLayer.CLI.globalip'),
185186
('globalip:assign', 'SoftLayer.CLI.globalip.assign:cli'),

SoftLayer/fixtures/SoftLayer_Network_Vlan_Firewall.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@
174174
"modifyDate": "2022-05-17T14:00:42-06:00",
175175
"primarySubnetId": 2623338,
176176
"vlanNumber": 1255
177+
},
178+
"status": {
179+
"description": "Gateway is ready to accept commands and actions",
180+
"id": 1,
181+
"keyName": "ACTIVE",
182+
"name": "Active"
177183
}
178184
},
179185
"rules": [
@@ -212,7 +218,16 @@
212218
'action': 'permit',
213219
'sourceIpAddress': '0.0.0.0'
214220
}
215-
]
221+
],
222+
"metricTrackingObject": {
223+
"id": 147258369,
224+
"resourceTableId": 17438,
225+
"startDate": "2022-05-17T14:01:48-06:00",
226+
"type": {
227+
"keyname": "FIREWALL_CONTEXT",
228+
"name": "Firewall Module Context"
229+
}
230+
}
216231

217232
}
218233

SoftLayer/managers/firewall.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,9 @@ def get_instance(self, firewall_id, mask=None):
297297
:param integer firewall_id: the instance ID of the standard firewall
298298
"""
299299
if not mask:
300-
mask = 'mask[firewallType,networkGateway[insideVlans,members,privateIpAddress,publicIpAddress,' \
301-
'publicIpv6Address,privateVlan,publicVlan],datacenter,managementCredentials,networkVlan]'
300+
mask = 'mask[firewallType,datacenter,managementCredentials,networkVlan,' \
301+
'metricTrackingObject[data,type],networkGateway[insideVlans,members,privateIpAddress,' \
302+
'publicIpAddress,publicIpv6Address,privateVlan,publicVlan,status]]'
302303

303304
svc = self.client['Network_Vlan_Firewall']
304305

@@ -319,3 +320,20 @@ def get_firewalls_gatewalls(self):
319320
_filter = {"networkGateways": {"networkFirewall": {"operation": "not null"}}}
320321

321322
return self.account.getNetworkGateways(mask=mask, filter=_filter)
323+
324+
def get_summary(self, identifier, start_date, end_date):
325+
"""Returns the metric data for the date range provided
326+
327+
:param integer metric_tracking_id
328+
"""
329+
body = [{
330+
"keyName": "PUBLICIN",
331+
"summaryType": "sum"
332+
333+
}, {
334+
"keyName": "PUBLICOUT",
335+
"summaryType": "sum"
336+
}]
337+
338+
return self.client['Metric_Tracking_Object'].getSummaryData(start_date,
339+
end_date, body, 86400, id=identifier)

docs/cli/firewall.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ Firewall Management
2222
.. click:: SoftLayer.CLI.firewall.list:cli
2323
:prog: firewall list
2424
:show-nested:
25+
26+
.. click:: SoftLayer.CLI.firewall.monitoring:cli
27+
:prog: firewall monitoring
28+
:show-nested:

tests/CLI/modules/firewall_tests.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,7 @@ def test_edit(self, confirm_mock):
154154
confirm_mock.return_value = True
155155
result = self.run_command(['firewall', 'edit', 'vlan:1234'])
156156
print(result.output)
157+
158+
def test_monitoring(self):
159+
result = self.run_command(['firewall', 'monitoring', '123456'])
160+
print(result.output)

tests/managers/firewall_tests.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ def test_get_standard_fwl_rules(self):
4646
identifier=1234)
4747

4848
def test_get_dedicated_fwl_rules(self):
49-
5049
rules = self.firewall.get_dedicated_fwl_rules(1234)
5150

5251
self.assertEqual(rules,

0 commit comments

Comments
 (0)