For memory stuff look at
vmstat threshold script. The CPU stuff Id use SAR. Look at AIX (its not for linux or windows, but conceptually it does the trick) NMON, perhaps something using rrdtool and mrtng. Theres plenty of tools out there, why reinvent the wheel.
Good luck
Ted
These are just ideas, don't crucify them.