-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathsensor_export.py
More file actions
executable file
·48 lines (40 loc) · 1.99 KB
/
sensor_export.py
File metadata and controls
executable file
·48 lines (40 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python
#
import sys
from cbapi.response.models import Sensor
from cbapi.example_helpers import build_cli_parser, get_cb_response_object
import logging
import csv
import traceback
log = logging.getLogger(__name__)
def export_sensors(cb, export_file_name, export_fields, query):
print("Starting CbR Sensor Export")
if query:
sensors = list(cb.select(Sensor).where(query).all())
else:
sensors = list(cb.select(Sensor))
with open(export_file_name, "w", encoding="utf8") as csv_file:
csv_writer = csv.writer(csv_file, delimiter=',', lineterminator='\n')
csv_writer.writerow(export_fields)
for sensor in sensors:
try:
row = [getattr(sensor, field) for field in export_fields]
csv_writer.writerow(row)
except Exception as e:
print("Exception {1} caused sensor export to fail for {0}".format(sensor.hostname, str(e)))
traceback.format_exc(0)
print("Export finished, exported {0} sensors to {1}".format(len(sensors), export_file_name))
def main():
parser = build_cli_parser(description="Export CbR Sensors from your environment as CSV")
parser.add_argument("--output", "-o", dest="exportfile", help="The file to export to", required=True)
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)
parser.add_argument("--query", "-q", dest="query", help="optional query to filter exported sensors", required=False)
args = parser.parse_args()
cb = get_cb_response_object(args)
export_fields = args.exportfields.split(",")
return export_sensors(cb, export_file_name=args.exportfile, export_fields=export_fields, query=args.query)
if __name__ == "__main__":
sys.exit(main())