| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  | from django.core.management.base import BaseCommand | 
					
						
							|  |  |  | import peeringdb_server.models as pdbm | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MODELS = [ | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |     pdbm.Organization, | 
					
						
							|  |  |  |     pdbm.Network, | 
					
						
							|  |  |  |     pdbm.InternetExchange, | 
					
						
							|  |  |  |     pdbm.InternetExchangeFacility, | 
					
						
							|  |  |  |     pdbm.Facility, | 
					
						
							|  |  |  |     pdbm.NetworkContact, | 
					
						
							|  |  |  |     pdbm.NetworkFacility, | 
					
						
							|  |  |  |     pdbm.IXLan, | 
					
						
							|  |  |  |     pdbm.IXLanPrefix, | 
					
						
							|  |  |  |     pdbm.NetworkIXLan, | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  | ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | STATUS_TYPES = ["ok", "pending", "deleted"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Command(BaseCommand): | 
					
						
							|  |  |  |     help = "Check data status/health" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def log(self, id, msg): | 
					
						
							| 
									
										
										
										
											2020-01-08 13:29:58 -06:00
										 |  |  |         print("%s: %s" % (id, msg)) | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def print_line(self): | 
					
						
							| 
									
										
										
										
											2020-01-08 13:29:58 -06:00
										 |  |  |         print("".join(["-" for i in range(0, 80)])) | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def handle(self, *args, **options): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # STATUS: handleref status breakdown | 
					
						
							|  |  |  |         self.print_line() | 
					
						
							|  |  |  |         self.log("status", "handleref status breakdown") | 
					
						
							|  |  |  |         self.print_line() | 
					
						
							|  |  |  |         for model in MODELS: | 
					
						
							|  |  |  |             counts = {} | 
					
						
							|  |  |  |             for c in STATUS_TYPES: | 
					
						
							|  |  |  |                 counts[c] = model.objects.filter(status=c).count() | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |             counts["invalid"] = model.objects.exclude(status__in=STATUS_TYPES).count() | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |             self.log( | 
					
						
							|  |  |  |                 model.handleref.tag, | 
					
						
							| 
									
										
										
										
											2020-01-08 13:29:58 -06:00
										 |  |  |                 " ".join(["%s(%d)" % (k, v) for k, v in list(counts.items())]), | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |             ) | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # VERSION: print the id of the instances with the highest | 
					
						
							|  |  |  |         # version for each model - this allows to spot possible import issues | 
					
						
							|  |  |  |         self.print_line() | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |         self.log("version", "5 highest version numbers for each handleref type") | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  |         self.print_line() | 
					
						
							|  |  |  |         for model in MODELS: | 
					
						
							|  |  |  |             inst = model.objects.order_by("-version")[:5] | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |             self.log( | 
					
						
							|  |  |  |                 model.handleref.tag, | 
					
						
							|  |  |  |                 ",".join(["%d v=%d" % (o.id, o.version) for o in inst]), | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # Find orphaned elements | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |         ixlan = ( | 
					
						
							|  |  |  |             pdbm.IXLan.objects.filter(status="ok", ix__status="deleted") | 
					
						
							|  |  |  |             .select_related("ix") | 
					
						
							|  |  |  |             .count() | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  |         if ixlan > 0: | 
					
						
							| 
									
										
										
										
											2020-01-08 13:29:58 -06:00
										 |  |  |             print("%d orphaned ixlans (ix status='deleted')" % ixlan) | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |         ixfac = ( | 
					
						
							|  |  |  |             pdbm.InternetExchangeFacility.objects.filter( | 
					
						
							|  |  |  |                 status="ok", ix__status="deleted" | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |             .select_related("ix") | 
					
						
							|  |  |  |             .count() | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  |         if ixfac > 0: | 
					
						
							| 
									
										
										
										
											2020-01-08 13:29:58 -06:00
										 |  |  |             print("%d orphaned ixfacs (ix status='deleted')" % ixfac) | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |         ixfac = ( | 
					
						
							|  |  |  |             pdbm.InternetExchangeFacility.objects.filter( | 
					
						
							|  |  |  |                 status="ok", facility__status="deleted" | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |             .select_related("facility") | 
					
						
							|  |  |  |             .count() | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  |         if ixfac > 0: | 
					
						
							| 
									
										
										
										
											2020-01-08 13:29:58 -06:00
										 |  |  |             print("%d orphaned ixfacs (fac status='deleted')" % ixfac) | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |         netfac = ( | 
					
						
							|  |  |  |             pdbm.NetworkFacility.objects.filter(status="ok", network__status="deleted") | 
					
						
							|  |  |  |             .select_related("network") | 
					
						
							|  |  |  |             .count() | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  |         if netfac > 0: | 
					
						
							| 
									
										
										
										
											2020-01-08 13:29:58 -06:00
										 |  |  |             print("%d orphaned netfacs (net status='deleted')" % netfac) | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |         netfac = ( | 
					
						
							|  |  |  |             pdbm.NetworkFacility.objects.filter(status="ok", facility__status="deleted") | 
					
						
							|  |  |  |             .select_related("facility") | 
					
						
							|  |  |  |             .count() | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  |         if netfac > 0: | 
					
						
							| 
									
										
										
										
											2020-01-08 13:29:58 -06:00
										 |  |  |             print("%d orphaned netfacs (fac status='deleted')" % netfac) | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |         poc = ( | 
					
						
							|  |  |  |             pdbm.NetworkContact.objects.filter(status="ok", network__status="deleted") | 
					
						
							|  |  |  |             .select_related("network") | 
					
						
							|  |  |  |             .count() | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  |         if poc > 0: | 
					
						
							| 
									
										
										
										
											2020-01-08 13:29:58 -06:00
										 |  |  |             print("%d orphaned poc (net status='deleted')" % poc) | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |         netixlan = ( | 
					
						
							|  |  |  |             pdbm.NetworkIXLan.objects.filter(status="ok", network__status="deleted") | 
					
						
							|  |  |  |             .select_related("network") | 
					
						
							|  |  |  |             .count() | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  |         if netixlan > 0: | 
					
						
							| 
									
										
										
										
											2020-01-08 13:29:58 -06:00
										 |  |  |             print("%d orphaned netixlans (net status='deleted')" % netixlan) | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |         netixlan = ( | 
					
						
							|  |  |  |             pdbm.NetworkIXLan.objects.filter(status="ok", ixlan__status="deleted") | 
					
						
							|  |  |  |             .select_related("ixlan") | 
					
						
							|  |  |  |             .count() | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  |         if netixlan > 0: | 
					
						
							| 
									
										
										
										
											2020-01-08 13:29:58 -06:00
										 |  |  |             print("%d orphaned netixlans (ixlan status='deleted')" % netixlan) | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |         ixpfx = ( | 
					
						
							|  |  |  |             pdbm.IXLanPrefix.objects.filter(status="ok", ixlan__status="deleted") | 
					
						
							|  |  |  |             .select_related("ixlan") | 
					
						
							|  |  |  |             .count() | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  |         if ixpfx: | 
					
						
							| 
									
										
										
										
											2020-01-08 13:29:58 -06:00
										 |  |  |             print("%d orphaned ixpfxs (ixlan status='deleted')" % ixpfx) | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         for model in [pdbm.Network, pdbm.InternetExchange, pdbm.Facility]: | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |             count = ( | 
					
						
							|  |  |  |                 model.objects.filter(status="ok", org__status="deleted") | 
					
						
							|  |  |  |                 .select_related("org") | 
					
						
							|  |  |  |                 .count() | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2018-11-08 19:45:21 +00:00
										 |  |  |             if count > 0: | 
					
						
							| 
									
										
										
										
											2020-01-08 13:29:58 -06:00
										 |  |  |                 print( | 
					
						
							|  |  |  |                     "%d orphaned %ss (org status='deleted')" | 
					
						
							|  |  |  |                     % (count, model.handleref.tag,) | 
					
						
							| 
									
										
										
										
											2019-12-05 16:57:52 +00:00
										 |  |  |                 ) |