Yes, it is legal. Under the "police power," local government has the power to ensure health and safety, such as bu inspecting buildings generally or rentals in particular. Government also has the power to levy fees to offset the cost of governmental services, like inspections, code enforcement, etc. This is well within a municipal government's legal authority.