diff --git a/Jamf Pro/Reporting/Database Maintenance and SQL Queries/Queries_MDM Commands.sql b/Jamf Pro/Reporting/Database Maintenance and SQL Queries/Queries_MDM Commands.sql new file mode 100644 index 0000000..dfff40f --- /dev/null +++ b/Jamf Pro/Reporting/Database Maintenance and SQL Queries/Queries_MDM Commands.sql @@ -0,0 +1,134 @@ +-- Queries on APNS/MDM Commands +-- Most of these should be working, but some may still be a work in progress. +-- These are formatted for readability, just fyi. + +-- ################################################## + +-- # Install application commands loop +SELECT device_id, device_object_id, profile_id, command, COUNT(*) +FROM mobile_device_management_commands +WHERE + command="InstallApplication" AND + date_completed_epoch>unix_timestamp(date_sub(NOW(), interval 24 hour))*1000 +GROUP BY device_id,device_object_id,profile_id,command +ORDER BY 5 DESC LIMIT 50; + +-- # Pending Commands +SELECT COUNT(*), command +FROM mobile_device_management_commands +WHERE apns_result_status="" +GROUP BY command +HAVING COUNT(*) > 99 +ORDER BY COUNT(*) DESC; + +-- # Completed Commands; verify that counts are dropping +SELECT device_id, command, device_object_id, COUNT(*) +FROM mobile_device_management_commands +WHERE date_completed_epoch>unix_timestamp(date_sub(NOW(), interval 24 hour))*1000 +GROUP BY device_id,command, device_object_id ORDER BY COUNT(*) DESC LIMIT 100; + + +-- ################################################## +-- Clean up records in the mobile_device_management_commands table + +-- Get total count +SELECT COUNT(*) FROM mobile_device_management_commands; + +-- Rename the original table +RENAME TABLE mobile_device_management_commands TO mobile_device_management_commands_original; + +-- Create new table like the old table +CREATE TABLE mobile_device_management_commands LIKE mobile_device_management_commands_original; + +-- Insert desired the records from the original table +INSERT INTO mobile_device_management_commands +SELECT * FROM mobile_device_management_commands_original +WHERE command NOT IN ( + "RemoveProfile", "ProfileList", "InstalledApplicationList", "CertificateList", + "DeviceInformation", "SecurityInfo", "UpdateInventory", "ContentCachingInformation", + "RemoveApplication", "UserList" +); + +-- Jamf Pro Product Issue: (Couldn't find the PI at the moment) +-- When a machine is re-enrolled, it gets a new push token and all of the remote commands +-- associated with the old push token are supposed to be cleared out. +-- However, the this doesn't happen which is what the PI is for and the below command will clear out. +-- ***PLEASE NOTE*** Workarounds that remove objects from the mobile_device_management_commands +-- table will leave orphan records in the mdm_command_source and mdm_command_group tables. +-- If you are running this workaround, the workaround for PI-009639 must be run promptly after. + +-- Get orphaned remote commands +SELECT COUNT(*) +FROM mobile_device_management_commands +WHERE + apns_result_status="" AND + device_object_id=12 AND + device_id NOT IN ( + SELECT computer_user_pushtoken_id + FROM computer_user_pushtokens + ); + +-- Clean up additional records +DELETE FROM mobile_device_management_commands +WHERE + apns_result_status="" AND + device_object_id=12 AND + device_id NOT IN ( + SELECT computer_user_pushtoken_id + FROM computer_user_pushtokens + ); + + +-- ################################################## +-- Jamf Pro Product Issue: PI-009639 +-- Clean up records in the computer_user_pushtokens table + +-- Rename the original mdm_command_source table +RENAME TABLE mdm_command_source TO mdm_command_source_original; + +-- Rename the original mdm_command_group table +RENAME TABLE mdm_command_group TO mdm_command_group_original; + +-- Creating new mdm_command_source table +CREATE TABLE mdm_command_source LIKE mdm_command_source_original; + +-- Creating new mdm_command_group table +CREATE TABLE mdm_command_group LIKE mdm_command_group_original; + +-- Inserting desired records into new table +INSERT INTO mdm_command_source ( + SELECT * FROM mdm_command_source_original + WHERE mdm_command_id IN ( + SELECT mobile_device_management_command_id FROM mobile_device_management_commands + ) +); + +-- Inserting desired records into new table +INSERT INTO mdm_command_group ( + SELECT * FROM mdm_command_group_original + WHERE id IN ( + SELECT id FROM mdm_command_source + ) +); + + +-- ################################################## +-- Clean up records in the computer_user_pushtokens table + +-- Get total count +SELECT COUNT(*) FROM computer_user_pushtokens; + +-- Get number of records to delete +SELECT COUNT(*) FROM computer_user_pushtokens +WHERE user_short_name LIKE "uid_%"; + +-- Rename the original table +RENAME TABLE computer_user_pushtokens TO computer_user_pushtokens_original; + +-- Create new table like the old table +CREATE TABLE computer_user_pushtokens LIKE computer_user_pushtokens_original; + +-- Insert the records from the original table +INSERT INTO computer_user_pushtokens +SELECT * FROM computer_user_pushtokens_original +WHERE user_short_name NOT LIKE "uid_%"; diff --git a/Jamf Pro/Reporting/Database Maintenance and SQL Queries/Query_Miscellaneous.sql b/Jamf Pro/Reporting/Database Maintenance and SQL Queries/Query_Miscellaneous.sql index 4faaf1e..d2efb83 100644 --- a/Jamf Pro/Reporting/Database Maintenance and SQL Queries/Query_Miscellaneous.sql +++ b/Jamf Pro/Reporting/Database Maintenance and SQL Queries/Query_Miscellaneous.sql @@ -46,3 +46,62 @@ WHERE printers.printer_id NOT IN ( -- ################################################## -- Queries to check configuration profiles would likely be useful + + + +-- ################################################## +-- Clean up orphaned records in the log_actions table + +-- Get total count +SELECT COUNT(*) FROM log_actions; + +-- Get number of orphaned records +SELECT COUNT(*) FROM log_actions +WHERE log_id NOT IN ( SELECT log_id FROM logs ); + +-- Rename the original table +RENAME TABLE log_actions TO log_actions_original; + +-- Create new table like the old table +CREATE TABLE log_actions LIKE log_actions_original; + +-- Select the non-orphaned records from the original table +INSERT INTO log_actions +SELECT * FROM log_actions_original +WHERE log_id IN ( SELECT log_id FROM logs ); + +-- Verify no orphaned records +SELECT COUNT(*) FROM log_actions +WHERE log_id NOT IN ( SELECT log_id FROM logs ); + + +-- ################################################## +-- Clean up orphaned records in the mobile_device_extension_attribute_values table + +-- Get total count +SELECT COUNT(*) FROM mobile_device_extension_attribute_values; + +-- Get number of orphaned records +SELECT COUNT(*) FROM mobile_device_extension_attribute_values +WHERE report_id NOT IN ( + SELECT report_id FROM reports WHERE mobile_device_id > 0 ); + +-- Rename the original table +RENAME TABLE mobile_device_extension_attribute_values TO mobile_device_extension_attribute_values_original; + +-- Create new table like the old table +CREATE TABLE mobile_device_extension_attribute_values LIKE mobile_device_extension_attribute_values_original; + +-- Select the non-orphaned records from the original table +INSERT INTO mobile_device_extension_attribute_values +SELECT * FROM mobile_device_extension_attribute_values_original +WHERE report_id IN ( + SELECT report_id FROM reports WHERE mobile_device_id > 0 ); + +-- Verify no orphaned records +SELECT COUNT(*) FROM mobile_device_extension_attribute_values +WHERE report_id NOT IN ( + SELECT report_id FROM reports WHERE mobile_device_id > 0 ); + +-- Get new total count +SELECT COUNT(*) FROM mobile_device_extension_attribute_values;