Skip to content

Commit a2faa3a

Browse files
committed
Make error handling more robust in example sensor_exports.py
1 parent f7a9f7b commit a2faa3a

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

examples/response/sensor_export.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,41 @@
66
from cbapi.example_helpers import build_cli_parser, get_cb_response_object
77
import logging
88
import csv
9+
import traceback
910

1011
log = logging.getLogger(__name__)
1112

1213

1314
def export_sensors(cb, export_file_name, export_fields, query):
14-
print ("Starting CbR Sensor Export")
15+
print("Starting CbR Sensor Export")
1516
if query:
16-
sensors = list(cb.select(Sensor).where(query))
17+
sensors = list(cb.select(Sensor).where(query).all())
1718
else:
1819
sensors = list(cb.select(Sensor))
19-
with open(export_file_name, "w",encoding="utf8") as csv_file:
20+
with open(export_file_name, "w", encoding="utf8") as csv_file:
2021
csv_writer = csv.writer(csv_file, delimiter=',', lineterminator='\n')
2122
csv_writer.writerow(export_fields)
2223
for sensor in sensors:
23-
row = [getattr(sensor,field) for field in export_fields]
24-
csv_writer.writerow(row)
25-
print("Export finished, exported {0} sensors to {1}".format(len(sensors),export_file_name))
24+
try:
25+
row = [getattr(sensor, field) for field in export_fields]
26+
csv_writer.writerow(row)
27+
except Exception as e:
28+
print("Exception {1} caused sensor export to fail for {0}".format(sensor.hostname, str(e)))
29+
traceback.format_exc(0)
30+
print("Export finished, exported {0} sensors to {1}".format(len(sensors), export_file_name))
2631

2732

2833
def main():
2934
parser = build_cli_parser(description="Export CbR Sensors from your environment as CSV")
30-
parser.add_argument("--output","-o", dest="exportfile", help="The file to export to", required=True)
31-
parser.add_argument("--fields","-f", dest="exportfields", help="The fields to export", default = "id,hostname,group_id,network_interfaces,os_environment_display_string,build_version_string,network_isolation_enabled,last_checkin_time", required=False)
32-
parser.add_argument("--query","-q",dest="query",help="optional query to filter exported sensors",required=False)
35+
parser.add_argument("--output", "-o", dest="exportfile", help="The file to export to", required=True)
36+
parser.add_argument("--fields", "-f", dest="exportfields", help="The fields to export",
37+
default="id,hostname,group_id,network_interfaces,os_environment_display_string,build_version_string,network_isolation_enabled,last_checkin_time",
38+
required=False)
39+
parser.add_argument("--query", "-q", dest="query", help="optional query to filter exported sensors", required=False)
3340
args = parser.parse_args()
3441
cb = get_cb_response_object(args)
3542
export_fields = args.exportfields.split(",")
36-
return export_sensors(cb, export_file_name=args.exportfile, export_fields=export_fields,query=args.query)
43+
return export_sensors(cb, export_file_name=args.exportfile, export_fields=export_fields, query=args.query)
3744

3845

3946
if __name__ == "__main__":

0 commit comments

Comments
 (0)