All modern HDDs have built in monitoring (SMART). There’s a seemingly limitless amount of software out there which can read this information. But one needs to interpreted it correctly, as the numbers are not always quite as they seem. This varies significantly between manufacturers. It’s not unknown for HDD monitoring software to report a drive is faulty, when it is in fact just misinterpreting the data.
Crystal Disk Info is a very good (free) utility for reading the SMART data.
Very few optical drives record their usage. The only ones known to are the old, long discontinued, ‘real’ Plextor drives (can be read using QPXTool or PXtool) and some of the later Lite-On drives (using Smartburn). Note: Smartburn reports a meaningless number for drives which don’t record their usage.
The methodology for recording the data is known only to the people who designed them.