Accessing metric data in check routine

have a look at the get_value_store() function. With this you can save values on one run of the check and access them on the next run to do your calculations. A second function that could help is get_rate. For both functions see the online Check plugin API reference inclued in your CMK site.