First integrals of generalized Ermakov systems via the Hamiltonian formulation