forked from grokability/jamf2snipe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jamf2snipe.test
859 lines (798 loc) · 50.2 KB
/
jamf2snipe.test
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
#!/usr/bin/env python3
# jamf2snipe - Inventory Import
#
# ABOUT:
# This program is designed to import inventory information from a
# JAMFPro into snipe-it using api calls. For more information
# about both of these products, please visit their respecitive
# websites:
# https://jamf.com
# https://snipeitapp.com
#
# LICENSE:
# MIT
#
# CONFIGURATION:
# These settings are commonly found in the settings.conf file.
#
# This setting sets the Snipe Asset status when creating a new asset. By default it's set to 4 (Pending).
# defaultStatus = 4
#
# You can associate snipe hardware keys in the [api-mapping] section, to to a JAMF keys so it associates
# the jamf values into snipe. The default example associates information that exists by default in both
# Snipe and JAMF. The Key value is the exact name of the snipe key name.
# Value1 is the "Subset" (JAMF's wording not mine) name, and the Value2 is the JAMF key name.
# Note that MAC Address are a custom value in SNIPE by default and you can use it as an example.
#
# [api-mapping]
# name = general name
# _snipeit_mac_address_1 = general mac_address
# _snipeit_custom_name_1234567890 = subset jamf_key
#
# A list of valid subsets are:
validsubset = [
"general",
"location",
"purchasing",
"peripherals",
"hardware",
"certificates",
"software",
"extension_attributes",
"groups_accounts",
"iphones",
"configuration_profiles"
]
# Import all the things
import json
import requests
import time
import configparser
import argparse
import logging
# Set us up for using runtime arguments by defining them.
runtimeargs = argparse.ArgumentParser()
runtimeargs.add_argument("-v", "--verbose", help="Sets the logging level to INFO and gives you a better idea of what the script is doing.", action="store_true")
runtimeargs.add_argument("--auto_incrementing", help="You can use this if you have auto-incrementing enabled in your snipe instance to utilize that instead of adding the Jamf ID for the asset tag.", action="store_true")
runtimeargs.add_argument("--dryrun", help="This checks your config and tries to contact both the JAMFPro and Snipe-it instances, but exits before updating or syncing any assets.", action="store_true")
runtimeargs.add_argument("-d", "--debug", help="Sets logging to include additional DEBUG messages.", action="store_true")
runtimeargs.add_argument("--do_not_update_jamf", help="Does not update Jamf with the asset tags stored in Snipe.", action="store_false")
runtimeargs.add_argument('--do_not_verify_ssl', help="Skips SSL verification for all requests. Helpful when you use self-signed certificate.", action="store_false")
runtimeargs.add_argument("-r", "--ratelimited", help="Puts a half second delay between Snipe IT API calls to adhere to the standard 120/minute rate limit", action="store_true")
runtimeargs.add_argument("-f", "--force", help="Updates the Snipe asset with information from Jamf every time, despite what the timestamps indicate.", action="store_true")
user_opts = runtimeargs.add_mutually_exclusive_group()
user_opts.add_argument("-u", "--users", help="Checks out the item to the current user in Jamf if it's not already deployed", action="store_true")
user_opts.add_argument("-ui", "--users_inverse", help="Checks out the item to the current user in Jamf if it's already deployed", action="store_true")
user_opts.add_argument("-uf", "--users_force", help="Checks out the item to the user specified in Jamf no matter what", action="store_true")
runtimeargs.add_argument("-uns", "--users_no_search", help="Doesn't search for any users if the specified fields in Jamf and Snipe don't match. (case insensitive)", action="store_true")
type_opts = runtimeargs.add_mutually_exclusive_group()
type_opts.add_argument("-m", "--mobiles", help="Runs against the Jamf mobiles endpoint only.", action="store_true")
type_opts.add_argument("-c", "--computers", help="Runs against the Jamf computers endpoint only.", action="store_true")
user_args = runtimeargs.parse_args()
# Notify users they're going to get a wall of text in verbose mode.
if user_args.verbose:
logging.basicConfig(level=logging.INFO)
elif user_args.debug:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.WARNING)
# Notify users if we're doing a dry run.
if user_args.dryrun:
print("Dryrun: Starting jamf2snipe with a dry run where no assets will be updated.")
# Find a valid settings.conf file.
logging.info("Searching for a valid settings.conf file.")
config = configparser.ConfigParser()
logging.debug("Checking for a settings.conf in /opt/jamf2snipe ...")
config.read("/opt/jamf2snipe/settings.conf")
if 'snipe-it' not in set(config):
logging.debug("No valid config found in: /opt Checking for a settings.conf in /etc/jamf2snipe ...")
config.read('/etc/jamf2snipe/settings.conf')
if 'snipe-it' not in set(config):
logging.debug("No valid config found in /etc Checking for a settings.conf in current directory ...")
config.read("settings.conf")
if 'snipe-it' not in set(config):
logging.debug("No valid config found in current folder.")
logging.error("No valid settings.conf was found. We'll need to quit while you figure out where the settings are at. You can check the README for valid locations.")
raise SystemExit("Error: No valid settings.conf - Exiting.")
logging.info("Great, we found a settings file. Let's get started by parsing all fo the settings.")
# Set some Variables from the settings.conf:
# This is the address, cname, or FQDN for your JamfPro instance.
jamfpro_base = config['jamf']['url']
logging.info("The configured JAMFPro base url is: {}".format(jamfpro_base))
jamf_api_user = config['jamf']['username']
logging.info("The configured JAMFPro username we'll be connecting with is: {}".format(jamf_api_user))
jamf_api_password = config['jamf']['password']
logging.debug("The configured password to access the API is: {}".format(jamf_api_password))
# This is the address, cname, or FQDN for your snipe-it instance.
snipe_base = config['snipe-it']['url']
logging.info("The configured Snipe-IT base url is: {}".format(snipe_base))
apiKey = config['snipe-it']['apiKey']
logging.debug("The API key you provided for Snipe is: {}".format(apiKey))
defaultStatus = config['snipe-it']['defaultStatus']
logging.info("The default status we'll be setting updated computer to is: {} (I sure hope this is a number or something is probably wrong)".format(defaultStatus))
apple_manufacturer_id = config['snipe-it']['manufacturer_id']
logging.info("The configured JAMFPro base url is: {} (Pretty sure this needs to be a number too)".format(apple_manufacturer_id))
# Headers for the API call.
logging.info("Creating the headers we'll need for API calls")
jamfheaders = {'Accept': 'application/json'}
snipeheaders = {'Authorization': 'Bearer {}'.format(apiKey),'Accept': 'application/json','Content-Type':'application/json'}
logging.debug('Request headers for JamfPro will be: {}\nRequest headers for Snipe will be: {}'.format(jamfheaders, snipeheaders))
# Check the config file for correct headers
# Do some tests to see if the user has updated their settings.conf file
SETTINGS_CORRECT = True
if 'api-mapping' in config:
logging.error("Looks like you're using the old method for api-mapping. Please use computers-api-mapping and mobile_devices-api-mapping.")
SETTINGS_CORRECT = False
if not 'user-mapping' in config and (user_args.users or user_args.users_force or user_args.users_inverse):
logging.error("""You've chosen to check out assets to users in some capacity using a cmdline switch, but not specified how you want to
search Snipe IT for the users from Jamf. Make sure you have a 'user-mapping' section in your settings.conf file.""")
SETTINGS_CORRECT = False
if not SETTINGS_CORRECT:
raise SystemExit
# Check the config file for valid jamf subsets. This is based off the JAMF API and if it's not right we can't map fields over to SNIPE properly.
logging.debug("Checking the settings.conf file for valid JAMF subsets of the JAMF API so mapping can occur properly.")
for key in config['computers-api-mapping']:
jamfsplit = config['computers-api-mapping'][key].split()
if jamfsplit[0] in validsubset:
logging.info('Found subset {}: Acceptable'.format(jamfsplit[0]))
continue
else:
logging.error("Found invalid subset: {} in the settings.conf file.\nThis is not in the acceptable list of subsets. Check your settings.conf\n Valid subsets are: {}".format(jamfsplit[0], ', '.join(validsubset)))
raise SystemExit("Invalid Subset found in settings.conf")
### Setup Some Functions ###
snipe_api_count = 0
first_snipe_call = None
# This function is run every time a request is made, handles rate limiting for Snipe IT.
def request_handler(r, *args, **kwargs):
global snipe_api_count
global first_snipe_call
if (snipe_base in r.url) and user_args.ratelimited:
if '"messages":429' in r.text:
logging.warn("Despite respecting the rate limit of Snipe, we've still been limited. Trying again after sleeping for 2 seconds.")
time.sleep(2)
re_req = r.request
s = requests.Session()
return s.send(re_req)
if snipe_api_count == 0:
first_snipe_call = time.time()
time.sleep(0.5)
snipe_api_count += 1
time_elapsed = (time.time() - first_snipe_call)
snipe_api_rate = snipe_api_count / time_elapsed
if snipe_api_rate > 1.95:
sleep_time = 0.5 + (snipe_api_rate - 1.95)
logging.debug('Going over snipe rate limit of 120/minute ({}/minute), sleeping for {}'.format(snipe_api_rate,sleep_time))
time.sleep(sleep_time)
logging.debug("Made {} requests to Snipe IT in {} seconds, with a request being sent every {} seconds".format(snipe_api_count, time_elapsed, snipe_api_rate))
if '"messages":429' in r.text:
logging.error(r.content)
raise SystemExit("We've been rate limited. Use option -r to respect the built in Snipe IT API rate limit of 120/minute.")
return r
# Function to make the API call for all JAMF devices
def get_jamf_computers():
api_url = '{0}/JSSResource/computers'.format(jamfpro_base)
logging.debug('Calling for JAMF computers against: {}\n The username, passwords, and headers for this GET requestcan be found near the beginning of the output.'.format(api_url))
response = requests.get(api_url, auth=(jamf_api_user, jamf_api_password), headers=jamfheaders, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
if response.status_code == 200:
logging.debug("Got back a valid 200 response code.")
return response.json()
elif b'policies.ratelimit.QuotaViolation' in response.content:
logging.info('JAMFPro responded with error code: {} - Policy Ratelimit Quota Violation - when we tried to get a list of computers Waiting a bit to retry the lookup.'.format(response))
logging.warning('JAMFPro Ratelimit exceeded: pausing ')
time.sleep(75)
logging.info("Finished waiting. Retrying lookup...")
newresponse = get_jamf_computers()
return newresponse
else:
logging.warning('Received an invalid status code when trying to retreive JAMF Device list:{} - {}'.format(response.status_code, response.content))
logging.debug("Returning a null value for the function.")
return None
# Function to make the API call for JAMF devices in group
def get_jamf_computers_by_group(jamf_id):
api_url = '{0}/JSSResource/computergroups/id/{1}'.format(jamfpro_base, jamf_id)
logging.debug('Calling for JAMF computers against: {}\n The username, passwords, and headers for this GET requestcan be found near the beginning of the output.'.format(api_url))
response = requests.get(api_url, auth=(jamf_api_user, jamf_api_password), headers=jamfheaders, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
if response.status_code == 200:
logging.debug("Got back a valid 200 response code.")
jsonresponse = response.json()
logging.debug("Returning: {}".format(jsonresponse['computer_group']))
return jsonresponse['computer_group']
elif b'policies.ratelimit.QuotaViolation' in response.content:
logging.info('JAMFPro responded with error code: {} - Policy Ratelimit Quota Violation - when we tried to get a list of computers Waiting a bit to retry the lookup.'.format(response))
logging.warning('JAMFPro Ratelimit exceeded: pausing ')
time.sleep(75)
logging.info("Finished waiting. Retrying lookup...")
newresponse = get_jamf_computers_by_group(jamf_id)
return newresponse
else:
logging.warning('Received an invalid status code when trying to retreive JAMF Device list:{} - {}'.format(response.status_code, response.content))
logging.debug("Returning a null value for the function.")
return None
# Function to make the API call for all JAMF mobile devices
def get_jamf_mobiles():
api_url = '{0}/JSSResource/mobiledevices'.format(jamfpro_base)
logging.debug('Calling for JAMF mobiles against: {}\n The username, passwords, and headers for this GET requestcan be found near the beginning of the output.'.format(api_url))
response = requests.get(api_url, auth=(jamf_api_user, jamf_api_password), headers=jamfheaders, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
if response.status_code == 200:
logging.debug("Got back a valid 200 response code.")
return response.json()
elif b'policies.ratelimit.QuotaViolation' in response.content:
logging.info('JAMFPro responded with error code: {} - Policy Ratelimit Quota Violation - when we tried to get a list of mobiles Waiting a bit to retry the lookup.'.format(response))
logging.warning('JAMFPro Ratelimit exceeded: pausing ')
time.sleep(75)
logging.info("Finished waiting. Retrying lookup...")
newresponse = get_jamf_mobiles()
return newresponse
else:
logging.warning('Received an invalid status code when trying to retreive JAMF Device list:{} - {}'.format(response.status_code, response.content))
logging.debug("Returning a null value for the function.")
return None
# Function to make the API call for all JAMF mobile devices in group
def get_jamf_mobiles_by_group(jamf_id):
api_url = '{0}/JSSResource/mobiledevicegroups/id/{1}'.format(jamfpro_base, jamf_id)
logging.debug('Calling for JAMF mobiles against: {}\n The username, passwords, and headers for this GET requestcan be found near the beginning of the output.'.format(api_url))
response = requests.get(api_url, auth=(jamf_api_user, jamf_api_password), headers=jamfheaders, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
if response.status_code == 200:
logging.debug("Got back a valid 200 response code.")
jsonresponse = response.json()
logging.debug("Returning: {}".format(jsonresponse['mobile_device_group']))
return jsonresponse['mobile_device_group']
elif b'policies.ratelimit.QuotaViolation' in response.content:
logging.info('JAMFPro responded with error code: {} - Policy Ratelimit Quota Violation - when we tried to get a list of mobiles Waiting a bit to retry the lookup.'.format(response))
logging.warning('JAMFPro Ratelimit exceeded: pausing ')
time.sleep(75)
logging.info("Finished waiting. Retrying lookup...")
newresponse = get_jamf_mobiles_by_group(jamf_id)
return newresponse
else:
logging.warning('Received an invalid status code when trying to retreive JAMF Device list:{} - {}'.format(response.status_code, response.content))
logging.debug("Returning a null value for the function.")
return None
# Function to lookup a JAMF asset by id.
def search_jamf_asset(jamf_id):
api_url = "{}/JSSResource/computers/id/{}".format(jamfpro_base, jamf_id)
response = requests.get(api_url, auth=(jamf_api_user, jamf_api_password), headers=jamfheaders, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
if response.status_code == 200:
logging.debug("Got back a valid 200 response code.")
jsonresponse = response.json()
logging.debug("Returning: {}".format(jsonresponse['computer']))
return jsonresponse['computer']
elif b'policies.ratelimit.QuotaViolation' in response.content:
logging.info('JAMFPro responded with error code: {} - Policy Ratelimit Quota Violation - when we tried to look up id: {} Waiting a bit to retry the lookup.'.format(response, jamf_id))
logging.warning('JAMFPro Ratelimit exceeded: pausing ')
time.sleep(75)
logging.info("Finished waiting. Retrying lookup...")
newresponse = search_jamf_asset(jamf_id)
return newresponse
else:
logging.warning('JAMFPro responded with error code:{} when we tried to look up id: {}'.format(response, jamf_id))
logging.debug("Returning a null value for the function.")
return None
# Function to lookup a JAMF mobile asset by id.
def search_jamf_mobile(jamf_id):
api_url = "{}/JSSResource/mobiledevices/id/{}".format(jamfpro_base, jamf_id)
response = requests.get(api_url, auth=(jamf_api_user, jamf_api_password), headers=jamfheaders, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
if response.status_code == 200:
logging.debug("Got back a valid 200 response code.")
jsonresponse = response.json()
logging.debug("Returning: {}".format(jsonresponse['mobile_device']))
return jsonresponse['mobile_device']
elif b'policies.ratelimit.QuotaViolation' in response.content:
logging.info('JAMFPro responded with error code: {} - Policy Ratelimit Quota Violation - when we tried to look up id: {} Waiting a bit to retry the lookup.'.format(response, jamf_id))
logging.warning('JAMFPro Ratelimit exceeded: pausing ')
time.sleep(75)
logging.info("Finished waiting. Retyring lookup...")
newresponse = search_jamf_asset(jamf_id)
return newresponse
else:
logging.warning('JAMFPro responded with error code:{} when we tried to look up id: {}'.format(response, jamf_id))
logging.debug("Returning a null value for the function.")
return None
# Function to update the asset tag of computers in JAMF with an number passed from Snipe.
def update_jamf_asset_tag(jamf_id, asset_tag):
api_url = "{}/JSSResource/computers/id/{}".format(jamfpro_base, jamf_id)
payload = """<?xml version="1.0" encoding="UTF-8"?><computer><general><id>{}</id><asset_tag>{}</asset_tag></general></computer>""".format(jamf_id, asset_tag)
logging.debug('Making Get request against: {}\nPayload for the PUT request is: {}\nThe username, password, and headers can be found near the beginning of the output.'.format(api_url, payload))
# response = requests.put(api_url, auth=(jamf_api_user, jamf_api_password), data=payload, headers=jamfheaders, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
# if response.status_code == 201:
# logging.debug("Got a 201 response. Returning: True")
# return True
# elif b'policies.ratelimit.QuotaViolation' in response.content:
# logging.info('JAMFPro responded with error code: {} - Policy Ratelimit Quota Violation - when we tried to look up id: {} Waiting a bit to retry the lookup.'.format(response, jamf_id))
# logging.warning('JAMFPro Ratelimit exceeded: pausing ')
# time.sleep(75)
# logging.info("Finished waiting. Retrying update...")
# newresponse = update_jamf_asset_tag(jamf_id, asset_tag)
# return newresponse
# if response.status_code == 200:
# logging.debug("Got a 200 response code. Returning the response: {}".format(response))
# return response.json()
# else:
# logging.warning('Got back an error response code:{} - {}'.format(response.status_code, response.content))
# return None
# Function to update the asset tag of mobile devices in JAMF with an number passed from Snipe.
def update_jamf_mobiledevice_asset_tag(jamf_id, asset_tag):
api_url = "{}/JSSResource/mobiledevices/id/{}".format(jamfpro_base, jamf_id)
payload = """<?xml version="1.0" encoding="UTF-8"?><mobile_device><general><id>{}</id><asset_tag>{}</asset_tag></general></mobile_device>""".format(jamf_id, asset_tag)
logging.debug('Making Get request against: {}\nPayload for the PUT request is: {}\nThe username, password, and headers can be found near the beginning of the output.'.format(api_url, payload))
# response = requests.put(api_url, auth=(jamf_api_user, jamf_api_password), data=payload, headers=jamfheaders, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
# if response.status_code == 201:
# logging.debug("Got a 201 response. Returning: True")
# return True
# elif b'policies.ratelimit.QuotaViolation' in response.content:
# logging.info('JAMFPro responded with error code: {} - Policy Ratelimit Quota Violation - when we tried to look up id: {} Waiting a bit to retry the lookup.'.format(response, jamf_id))
# logging.warning('JAMFPro Ratelimit exceeded: pausing ')
# time.sleep(75)
# logging.info("Finished waiting. Retrying update...")
# newresponse = update_jamf_mobiledevice_asset_tag(jamf_id, asset_tag)
# return newresponse
# if response.status_code == 200:
# logging.debug("Got a 200 response code. Returning the response: {}".format(response))
# return response.json()
# else:
# logging.warning('Got back an error response code:{} - {}'.format(response.status_code, response.content))
# return None
# Function to lookup a snipe asset by serial number.
def search_snipe_asset(serial):
api_url = '{}/api/v1/hardware/byserial/{}'.format(snipe_base, serial)
response = requests.get(api_url, headers=snipeheaders, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
if response.status_code == 200:
jsonresponse = response.json()
# Check to make sure there's actually a result
if jsonresponse['total'] == 1:
return jsonresponse
elif jsonresponse['total'] == 0:
logging.info("No assets match {}".format(serial))
return "NoMatch"
else:
logging.warning('FOUND {} matching assets while searching for: {}'.format(jsonresponse['total'], serial))
return "MultiMatch"
else:
logging.warning('Snipe-IT responded with error code:{} when we tried to look up: {}'.format(response.text, serial))
logging.debug('{} - {}'.format(response.status_code, response.content))
return "ERROR"
# Function to get all the asset models
def get_snipe_models():
api_url = '{}/api/v1/models'.format(snipe_base)
logging.debug('Calling against: {}'.format(api_url))
response = requests.get(api_url, headers=snipeheaders, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
if response.status_code == 200:
jsonresponse = response.json()
logging.info("Got a valid response that should have {} models.".format(jsonresponse['total']))
if jsonresponse['total'] <= len(jsonresponse['rows']) :
return jsonresponse
else:
logging.info("We didn't get enough results so we need to get them again.")
api_url = '{}/api/v1/models?limit={}'.format(snipe_base, jsonresponse['total'])
newresponse = requests.get(api_url, headers=snipeheaders, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
if response.status_code == 200:
newjsonresponse = newresponse.json()
if newjsonresponse['total'] == len(newjsonresponse['rows']) :
return newjsonresponse
else:
logging.error("We couldn't seem to get all of the model numbers")
raise SystemExit("Unable to get all model objects from Snipe-IT instanace")
else:
logging.error('When we tried to retreive a list of models, Snipe-IT responded with error status code:{} - {}'.format(response.status_code, response.content))
raise SystemExit("Snipe models API endpoint failed.")
else:
logging.error('When we tried to retreive a list of models, Snipe-IT responded with error status code:{} - {}'.format(response.status_code, response.content))
raise SystemExit("Snipe models API endpoint failed.")
# Recursive function returns all users in a Snipe Instance, 100 at a time.
def get_snipe_users(previous=[]):
user_id_url = '{}/api/v1/users'.format(snipe_base)
payload = {
'limit': 100,
'offset': len(previous)
}
logging.debug('The payload for the snipe users GET is {}'.format(payload))
response = requests.get(user_id_url, headers=snipeheaders, params=payload, hooks={'response': request_handler})
response_json = response.json()
current = response_json['rows']
if len(previous) != 0:
current = previous + current
if response_json['total'] > len(current):
logging.debug('We have more than 100 users, get the next page - total: {} current: {}'.format(response_json['total'], len(current)))
return get_snipe_users(current)
else:
return current
# Function to search snipe for a user
def get_snipe_user_id(username):
if username == '':
return "NotFound"
username = username.lower()
for user in snipe_users:
for value in user.values():
if str(value).lower() == username:
id = user['id']
return id
if user_args.users_no_search:
logging.debug("No matches in snipe_users for {}, not querying the API for the next closest match since we've been told not to".format(username))
return "NotFound"
logging.debug('No matches in snipe_users for {}, querying the API for the next closest match'.format(username))
user_id_url = '{}/api/v1/users'.format(snipe_base)
payload = {
'search':username,
'limit':1,
'sort':'username',
'order':'asc'
}
logging.debug('The payload for the snipe user search is: {}'.format(payload))
response = requests.get(user_id_url, headers=snipeheaders, params=payload, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
try:
return response.json()['rows'][0]['id']
except:
return "NotFound"
# Function that creates a new Snipe Model - not an asset - with a JSON payload
def create_snipe_model(payload):
api_url = '{}/api/v1/models'.format(snipe_base)
logging.debug('Calling to create new snipe model type against: {}\nThe payload for the POST request is:{}\nThe request headers can be found near the start of the output.'.format(api_url, payload))
# response = requests.post(api_url, headers=snipeheaders, json=payload, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
# if response.status_code == 200:
# jsonresponse = response.json()
# modelnumbers[jsonresponse['payload']['model_number']] = jsonresponse['payload']['id']
# return True
# else:
# logging.warning('Error code: {} while trying to create a new model.'.format(response.status_code))
# return False
# Function to create a new asset by passing array
def create_snipe_asset(payload):
api_url = '{}/api/v1/hardware'.format(snipe_base)
logging.debug('Calling to create a new asset against: {}\nThe payload for the POST request is:{}\nThe request headers can be found near the start of the output.'.format(api_url, payload))
# response = requests.post(api_url, headers=snipeheaders, json=payload, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
# logging.debug(response.text)
# if response.status_code == 200:
# logging.debug("Got back status code: 200 - {}".format(response.content))
# jsonresponse = response.json()
# if jsonresponse['status'] == "error":
# logging.error('Asset creation failed for asset {} with error {}'.format(payload['name'],jsonresponse['messages']))
# return 'ERROR', response
# return 'AssetCreated', response
# else:
# logging.error('Asset creation failed for asset {} with error {}'.format(payload['name'],response.text))
# return 'ERROR', response
# Function that updates a snipe asset with a JSON payload
def update_snipe_asset(snipe_id, payload):
api_url = '{}/api/v1/hardware/{}'.format(snipe_base, snipe_id)
logging.debug('The payload for the snipe update is: {}'.format(payload))
# response = requests.patch(api_url, headers=snipeheaders, json=payload, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
# Verify that the payload updated properly.
# goodupdate = True
# if response.status_code == 200:
# logging.debug("Got back status code: 200 - Checking the payload updated properly: If you error here it's because you configure the API mapping right.")
# jsonresponse = response.json()
# Check if there's an Error and Log it, or parse the payload.
# if jsonresponse['status'] == "error":
# logging.error('Unable to update ID: {}. Error "{}"'.format(snipe_id, jsonresponse['messages']))
# goodupdate = False
# else:
# for key in payload:
# if key == 'purchase_date':
# payload[key] = payload[key] + " 00:00:00"
# if payload[key] == '':
# payload[key] = None
# if jsonresponse['payload'][key] != payload[key]:
# logging.warning('Unable to update ID: {}. We failed to update the {} field with "{}"'.format(snipe_id, key, payload[key]))
# goodupdate = False
# else:
# logging.info("Sucessfully updated {} with: {}".format(key, payload[key]))
# return goodupdate
# else:
# logging.error('Whoops. Got an error status code while updating ID {}: {} - {}'.format(snipe_id, response.status_code, response.content))
# return False
# Function that checks in an asset in snipe
def checkin_snipe_asset(asset_id):
api_url = '{}/api/v1/hardware/{}/checkin'.format(snipe_base, asset_id)
payload = {
'note':'checked in by script from Jamf'
}
logging.debug('The payload for the snipe checkin is: {}'.format(payload))
# response = requests.post(api_url, headers=snipeheaders, json=payload, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
# logging.debug('The response from Snipe IT is: {}'.format(response.json()))
# if response.status_code == 200:
# logging.debug("Got back status code: 200 - {}".format(response.content))
# return "CheckedOut"
# else:
# return response
# Function that checks out an asset in snipe
def checkout_snipe_asset(user, asset_id, checked_out_user=None):
logging.debug('Asset {} is being checked out to {}'.format(user, asset_id))
user_id = get_snipe_user_id(user)
if user_id == 'NotFound':
logging.info("User {} not found".format(user))
return "NotFound"
if checked_out_user == None:
logging.info("Not checked out, checking out to {}".format(user))
elif checked_out_user == "NewAsset":
logging.info("First time this asset will be checked out, checking out to {}".format(user))
elif checked_out_user['id'] == user_id:
logging.info(str(asset_id) + " already checked out to user " + user)
return 'CheckedOut'
else:
logging.info("Checking in {} to check it out to {}".format(asset_id,user))
checkin_snipe_asset(asset_id)
api_url = '{}/api/v1/hardware/{}/checkout'.format(snipe_base, asset_id)
logging.info("Checking out {} to check it out to {}".format(asset_id,user))
payload = {
'checkout_to_type':'user',
'assigned_user':user_id,
'note':'Assignment made automatically, via script from Jamf.'
}
logging.debug('The payload for the snipe checkin is: {}'.format(payload))
# response = requests.post(api_url, headers=snipeheaders, json=payload, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
# logging.debug('The response from Snipe IT is: {}'.format(response.json()))
# if response.status_code == 200:
# logging.debug("Got back status code: 200 - {}".format(response.content))
# return "CheckedOut"
# else:
# logging.error('Asset checkout failed for asset {} with error {}'.format(asset_id,response.text))
# return response
### Run Testing ###
# Report if we're verifying SSL or not.
logging.info("SSL Verification is set to: {}".format(user_args.do_not_verify_ssl))
# Do some tests to see if the hosts are up.
logging.info("Running tests to see if hosts are up.")
try:
SNIPE_UP = True if requests.get(snipe_base, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler}).status_code == 200 else False
except Exception as e:
logging.exception(e)
SNIPE_UP = False
try:
JAMF_UP = True if requests.get(jamfpro_base, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler}).status_code in (200, 401) else False
except Exception as e:
logging.exception(e)
JAMF_UP = False
if not SNIPE_UP:
logging.error('Snipe-IT looks like it is down from here. \nPlease check your config in the settings.conf file, or your instance.')
else:
logging.info('We were able to get a good response from your Snipe-IT instance.')
if not JAMF_UP:
logging.error('JAMFPro looks down from here. \nPlease check the your config in the settings.conf file, or your hosted JAMFPro instance.')
else:
logging.info('We were able to get a good response from your JAMFPro instance.')
# Exit if you can't contact SNIPE
if ( JAMF_UP == False ) or ( SNIPE_UP == False ):
raise SystemExit("Error: Host could not be contacted.")
# Test that we can actually connect with the API keys.
##TODO Write some more tests here. ha!
logging.info("Finished running our tests.")
### Get Started ###
# Get a list of known models from Snipe
logging.info("Getting a list of computer models that snipe knows about.")
snipemodels = get_snipe_models()
logging.debug("Parsing the {} model results for models with model numbers.".format(len(snipemodels['rows'])))
modelnumbers = {}
for model in snipemodels['rows']:
if model['model_number'] == "":
logging.debug("The model, {}, did not have a model number. Skipping.".format(model['name']))
continue
modelnumbers[model['model_number']] = model['id']
logging.info("Our list of models has {} entries.".format(len(modelnumbers)))
logging.debug("Here's the list of the {} models and their id's that we were able to collect:\n{}".format(len(modelnumbers), modelnumbers))
# Get the IDS of all active assets.
if 'computer_group_id' in config['jamf'] and config['jamf']['computer_group_id']:
logging.info("Getting list of computers from JAMF by computer group id.")
jamf_computer_list = get_jamf_computers_by_group(config['jamf']['computer_group_id'])
else:
jamf_computer_list = get_jamf_computers()
if 'mobile_group_id' in config['jamf'] and config['jamf']['mobile_group_id']:
logging.info("Getting list of mobiles from JAMF by mobile group id.")
jamf_mobile_list = get_jamf_mobiles_by_group(config['jamf']['mobile_group_id'])
else:
jamf_mobile_list = get_jamf_mobiles()
jamf_types = {
'computers': jamf_computer_list,
'mobile_devices': jamf_mobile_list
}
# Get a list of users from Snipe if the user has specified
# they're syncing users
if user_args.users or user_args.users_force or user_args.users_inverse:
snipe_users = get_snipe_users()
TotalNumber = 0
if user_args.computers:
TotalNumber = len(jamf_types['computers']['computers'])
elif user_args.mobiles:
TotalNumber = len(jamf_types['mobile_devices']['mobile_devices'])
else:
for jamf_type in jamf_types:
TotalNumber += len(jamf_types[jamf_type][jamf_type])
# Make sure we have a good list.
if jamf_computer_list != None:
logging.info('Received a list of JAMF assets that had {} entries.'.format(TotalNumber))
else:
logging.error("We were not able to retreive a list of assets from your JAMF instance. It's likely that your settings, or credentials are incorrect. Check your settings.conf and verify you can make API calls outside of this system with the credentials found in your settings.conf")
raise SystemExit("Unable to get JAMF Computers.")
# After this point we start editing data, so quit if this is a dryrun
if user_args.dryrun:
raise SystemExit("Dryrun: Complete.")
# From this point on, we're editing data.
logging.info('Starting to Update Inventory')
CurrentNumber = 0
for jamf_type in jamf_types:
if user_args.computers:
if jamf_type != 'computers':
continue
if user_args.mobiles:
if jamf_type != 'mobile_devices':
continue
for jamf_asset in jamf_types[jamf_type][jamf_type]:
CurrentNumber += 1
logging.info("Processing entry {} out of {} - JAMFID: {} - NAME: {}".format(CurrentNumber, TotalNumber, jamf_asset['id'], jamf_asset['name']))
# Search through the list by ID for all asset information\
if jamf_type == 'computers':
jamf = search_jamf_asset(jamf_asset['id'])
elif jamf_type == 'mobile_devices':
jamf = search_jamf_mobile(jamf_asset['id'])
if jamf == None:
continue
# Check that the model number exists in snipe, if not create it.
if jamf_type == 'computers':
if jamf['hardware']['model_identifier'] not in modelnumbers:
logging.info("Could not find a model ID in snipe for: {}".format(jamf['hardware']['model_identifier']))
newmodel = {"category_id":config['snipe-it']['computer_model_category_id'],"manufacturer_id":apple_manufacturer_id,"name": jamf['hardware']['model'],"model_number":jamf['hardware']['model_identifier']}
if 'computer_custom_fieldset_id' in config['snipe-it']:
fieldset_split = config['snipe-it']['computer_custom_fieldset_id']
newmodel['fieldset_id'] = fieldset_split
create_snipe_model(newmodel)
elif jamf_type == 'mobile_devices':
if jamf['general']['model_identifier'] not in modelnumbers:
logging.info("Could not find a model ID in snipe for: {}".format(jamf['general']['model_identifier']))
newmodel = {"category_id":config['snipe-it']['mobile_model_category_id'],"manufacturer_id":apple_manufacturer_id,"name": jamf['general']['model'],"model_number":jamf['general']['model_identifier']}
if 'mobile_custom_fieldset_id' in config['snipe-it']:
fieldset_split = config['snipe-it']['mobile_custom_fieldset_id']
newmodel['fieldset_id'] = fieldset_split
create_snipe_model(newmodel)
# Pass the SN from JAMF to search for a match in Snipe
snipe = search_snipe_asset(jamf['general']['serial_number'])
# Create a new asset if there's no match:
if snipe == 'NoMatch':
logging.info("Creating a new asset in snipe for JAMF ID {} - {}".format(jamf['general']['id'], jamf['general']['name']))
# This section checks to see if the asset tag was already put into JAMF, if not it creates one with with Jamf's ID.
if jamf['general']['asset_tag'] == '':
jamf_asset_tag = None
logging.debug('No asset tag found in Jamf, checking settings.conf for alternative specified field.')
if 'asset_tag' in config['snipe-it']:
tag_split = config['snipe-it']['asset_tag'].split()
try:
jamf_asset_tag = jamf['{}'.format(tag_split[0])]['{}'.format(tag_split[1])]
except:
raise SystemError('No such attribute {} in the jamf payload. Please check your settings.conf file'.format(tag_split))
if jamf_asset_tag == None or jamf_asset_tag == '':
logging.debug('No custom configuration found in settings.conf for asset tag name upon asset creation.')
if jamf_type == 'mobile_devices':
jamf_asset_tag = 'jamfid-m-{}'.format(jamf['general']['id'])
elif jamf_type == 'computers':
jamf_asset_tag = 'jamfid-{}'.format(jamf['general']['id'])
else:
jamf_asset_tag = jamf['general']['asset_tag']
logging.info("Asset tag found in Jamf, setting it to: {}".format(jamf_asset_tag))
# Create the payload
if jamf_type == 'mobile_devices':
logging.debug("Payload is being made for a mobile device")
newasset = {'asset_tag': jamf_asset_tag, 'model_id': modelnumbers['{}'.format(jamf['general']['model_identifier'])], 'name': jamf['general']['name'], 'status_id': defaultStatus,'serial': jamf['general']['serial_number']}
elif jamf_type == 'computers':
logging.debug("Payload is being made for a computer")
newasset = {'asset_tag': jamf_asset_tag,'model_id': modelnumbers['{}'.format(jamf['hardware']['model_identifier'])], 'name': jamf['general']['name'], 'status_id': defaultStatus,'serial': jamf['general']['serial_number']}
if jamf['general']['serial_number'] == 'Not Available':
logging.warning("The serial number is not available in JAMF. This is normal for DEP enrolled devices that have not yet checked in for the first time. Since there's no serial number yet, we'll skip it for now.")
continue
else:
for snipekey in config['{}-api-mapping'.format(jamf_type)]:
jamfsplit = config['{}-api-mapping'.format(jamf_type)][snipekey].split()
try:
for i, item in enumerate(jamfsplit):
try:
item = int(item)
except ValueError:
logging.debug('{} is not an integer'.format(item))
if i == 0:
jamf_value = jamf[item]
else:
if jamfsplit[0] == 'extension_attributes':
for attribute in jamf_value:
if attribute['id'] == item:
jamf_value = attribute['value']
else:
jamf_value = jamf_value[item]
newasset[snipekey] = jamf_value
except KeyError:
continue
# Reset the payload without the asset_tag if auto_incrementing flag is set.
if user_args.auto_incrementing:
newasset.pop('asset_tag', None)
new_snipe_asset = create_snipe_asset(newasset)
if new_snipe_asset[0] != "AssetCreated":
continue
if user_args.users or user_args.users_force or user_args.users_inverse:
jamfsplit = config['user-mapping']['jamf_api_field'].split()
if jamfsplit[1] not in jamf[jamfsplit[0]]:
logging.info("Couldn't find {} for this device in {}, not checking it out.".format(jamfsplit[1], jamfsplit[0]))
continue
logging.info('Checking out new item {} to user {}'.format(jamf['general']['name'], jamf['{}'.format(jamfsplit[0])]['{}'.format(jamfsplit[1])]))
checkout_snipe_asset(jamf['{}'.format(jamfsplit[0])]['{}'.format(jamfsplit[1])],new_snipe_asset[1].json()['payload']['id'], "NewAsset")
# Log an error if there's an issue, or more than once match.
elif snipe == 'MultiMatch':
logging.warning("WARN: You need to resolve multiple assets with the same serial number in your inventory. If you can't find them in your inventory, you might need to purge your deleted records. You can find that in the Snipe Admin settings. Skipping serial number {} for now.".format(jamf['general']['serial_number']))
elif snipe == 'ERROR':
logging.error("We got an error when looking up serial number {} in snipe, which shouldn't happen at this point. Check your snipe instance and setup. Skipping for now.".format(jamf['general']['serial_number']))
else:
# Only update if JAMF has more recent info.
snipe_id = snipe['rows'][0]['id']
snipe_time = snipe['rows'][0]['updated_at']['datetime']
if jamf_type == 'computers':
jamf_time = jamf['general']['report_date']
elif jamf_type == 'mobile_devices':
jamf_time = jamf['general']['last_inventory_update']
# Check to see that the JAMF record is newer than the previous Snipe update, or if it is a new record in Snipe
if ( jamf_time > snipe_time ) or ( user_args.force ):
if user_args.force:
logging.debug("Forced the Update regardless of the timestamps below.")
logging.debug("Updating the Snipe asset because JAMF has a more recent timestamp: {} > {} or the Snipe Record is new".format(jamf_time, snipe_time))
updates = {}
for snipekey in config['{}-api-mapping'.format(jamf_type)]:
try:
jamfsplit = config['{}-api-mapping'.format(jamf_type)][snipekey].split()
for i, item in enumerate(jamfsplit):
try:
item = int(item)
except ValueError:
logging.debug('{} is not an integer'.format(item))
if i == 0:
jamf_value = jamf[item]
else:
if jamfsplit[0] == 'extension_attributes':
for attribute in jamf_value:
if attribute['id'] == item:
jamf_value = attribute['value']
else:
jamf_value = jamf_value[item]
payload = {snipekey: jamf_value}
latestvalue = jamf_value
except KeyError:
logging.debug("Skipping the payload, because the JAMF key we're mapping to doesn't exist")
continue
# Need to check that we're not needlessly updating the asset.
# If it's a custom value it'll fail the first section and send it to except section that will parse custom sections.
try:
if snipe['rows'][0][snipekey] != latestvalue:
updates.update(payload)
else:
logging.debug("Skipping the payload, because it already exits.")
except:
logging.debug("The snipekey lookup failed, which means it's a custom field. Parsing those to see if it needs to be updated or not.")
needsupdate = False
for CustomField in snipe['rows'][0]['custom_fields']:
if snipe['rows'][0]['custom_fields'][CustomField]['field'] == snipekey :
if snipe['rows'][0]['custom_fields'][CustomField]['value'] != str(latestvalue):
logging.debug("Found the field, and the value needs to be updated from {} to {}".format(snipe['rows'][0]['custom_fields'][CustomField]['value'], latestvalue))
needsupdate = True
if needsupdate == True:
updates.update(payload)
else:
logging.debug("Skipping the payload, because it already exists, or the Snipe key we're mapping to doesn't.")
if updates:
update_snipe_asset(snipe_id, updates)
if ((user_args.users or user_args.users_inverse) and (snipe['rows'][0]['assigned_to'] == None) == user_args.users) or user_args.users_force:
if snipe['rows'][0]['status_label']['status_meta'] in ('deployable', 'deployed'):
jamfsplit = config['user-mapping']['jamf_api_field'].split()
if jamfsplit[1] not in jamf[jamfsplit[0]]:
logging.info("Couldn't find {} for this device in {}, not checking it out.".format(jamfsplit[1], jamfsplit[0]))
continue
checkout_snipe_asset(jamf['{}'.format(jamfsplit[0])]['{}'.format(jamfsplit[1])], snipe_id, snipe['rows'][0]['assigned_to'])
else:
logging.info("Can't checkout {} since the status isn't set to deployable".format(jamf['general']['name']))
else:
logging.info("Snipe Record is newer than the JAMF record. Nothing to sync. If this wrong, then force an inventory update in JAMF")
logging.debug("Not updating the Snipe asset because Snipe has a more recent timestamp: {} < {}".format(jamf_time, snipe_time))
# Update/Sync the Snipe Asset Tag Number back to JAMF
# The user arg below is set to false if it's called, so this would fail if the user called it.
if (jamf['general']['asset_tag'] != snipe['rows'][0]['asset_tag']) and user_args.do_not_update_jamf :
logging.info("JAMF doesn't have the same asset tag as SNIPE so we'll update it because it should be authoritative.")
if snipe['rows'][0]['asset_tag'][0]:
if jamf_type == 'computers':
update_jamf_asset_tag("{}".format(jamf['general']['id']), '{}'.format(snipe['rows'][0]['asset_tag']))
logging.info("Device is a computer, updating computer record")
elif jamf_type == 'mobile_devices':
update_jamf_mobiledevice_asset_tag("{}".format(jamf['general']['id']), '{}'.format(snipe['rows'][0]['asset_tag']))
logging.info("Device is a mobile device, updating the mobile device record")
logging.debug('Total amount of API calls made: {}'.format(snipe_api_count))