Skip to content

Commit dbd2ed0

Browse files
authored
Added support for logging SM clients
1 parent d7f8e7d commit dbd2ed0

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

offline_logging/config.yaml

+10-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ endpoints:
5050
# requires getNetworkClients
5151
# This can be very slow in large environments, since every client needs to be fetched individually
5252
# and log entries on long-running networks can be huge, even hitting the MongoDB 16MB/document limit
53-
enabled: true
53+
enabled: false
5454
# Splits large traffic history arrays to multiple pages to be able to write them into the database.
5555
# Lower this value if you are getting "document too large" errors, or increase to have fewer documents
5656
# per client
@@ -66,4 +66,12 @@ endpoints:
6666
getOrganizationAdmins:
6767
enabled: true
6868
collection: organizationAdmins
69-
mode: update
69+
mode: update
70+
getNetworkSmDevices:
71+
enabled: true
72+
collection: networkSmDevices
73+
mode: update
74+
# Set "filter_by_device_tag_enabled" to true to only log devices with a specific device tag
75+
# Set the device tag to be matched in "target_device_tag"
76+
filter_by_device_tag_enabled: false
77+
target_device_tag: logging

offline_logging/offline_logging.py

+34-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def merakiRequest(p_apiKey, p_httpVerb, p_endpoint, p_additionalHeaders=None, p_
6565

6666
query = ""
6767
if not p_queryItems is None:
68-
query = "?" + urlencode(p_queryItems)
68+
query = "?" + urlencode(p_queryItems, True)
6969
url = API_BASE_URL + p_endpoint + query
7070

7171
verb = p_httpVerb.upper()
@@ -192,6 +192,17 @@ def getNetworkMerakiAuthUsers(p_apiKey, p_networkId):
192192
success, errors, headers, response = merakiRequest(p_apiKey, "GET", endpoint, p_verbose=FLAG_REQUEST_VERBOSE)
193193
return success, errors, headers, response
194194

195+
196+
def getNetworkSmDevices(p_apiKey, p_networkId):
197+
endpoint = "/networks/%s/sm/devices" % p_networkId
198+
query = {"fields[]": ['ip', 'systemType', 'lastConnected', 'location', 'lastUser',
199+
'ownerEmail', 'ownerUsername', 'imei', 'simCarrierNetwork']}
200+
201+
success, errors, headers, response = merakiRequest(p_apiKey, "GET", endpoint, p_queryItems=query,
202+
p_verbose=FLAG_REQUEST_VERBOSE)
203+
204+
return success, errors, headers, response
205+
195206

196207
def getOrganizationAdmins(p_apiKey, p_organizationId):
197208
endpoint = "/organizations/%s/admins" % p_organizationId
@@ -462,8 +473,29 @@ def perform_scan(config):
462473
log_to_database(db, document, config['endpoints']['getNetworkMerakiAuthUsers']['collection'],
463474
config['endpoints']['getNetworkMerakiAuthUsers']['mode'],
464475
keyValuePair={'id': user['id'], 'networkId': network['id']})
476+
if 'getNetworkSmDevices' in config['endpoints'] and config['endpoints']['getNetworkSmDevices']['enabled']:
477+
if 'systemsManager' in network['productTypes']:
478+
success, errors, headers, sm_devices = getNetworkSmDevices(api_key, network['id'])
479+
if not sm_devices is None:
480+
tag_disabled = not config['endpoints']['getNetworkSmDevices']['filter_by_device_tag_enabled']
481+
tag_filter = config['endpoints']['getNetworkSmDevices']['target_device_tag']
482+
scan_time = datetime.datetime.now()
483+
for device in sm_devices:
484+
if tag_disabled or tag_filter in device['tags']:
485+
document = {
486+
'scanTime': scan_time,
487+
'scanIntervalMinutes': config['scan_interval_minutes'],
488+
'networkId': network['id'],
489+
'networkName': network['name']
490+
}
491+
for key in device:
492+
document[key] = device[key]
493+
494+
log_to_database(db, document,
495+
config['endpoints']['getNetworkSmDevices']['collection'],
496+
config['endpoints']['getNetworkSmDevices']['mode'],
497+
keyValuePair={'id': device['id']})
465498

466-
467499
print(str(datetime.datetime.now()) + " -- Scan complete")
468500

469501

0 commit comments

Comments
 (0)