Created
March 23, 2015 20:39
-
-
Save MurphyMc/792534236ae918cbe998 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Copyright 2015 James McCauley | |
| # | |
| # Licensed under the Apache License, Version 2.0 (the "License"); | |
| # you may not use this file except in compliance with the License. | |
| # You may obtain a copy of the License at: | |
| # | |
| # http://www.apache.org/licenses/LICENSE-2.0 | |
| # | |
| # Unless required by applicable law or agreed to in writing, software | |
| # distributed under the License is distributed on an "AS IS" BASIS, | |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| # See the License for the specific language governing permissions and | |
| # limitations under the License. | |
| """ | |
| A CPU-usage reporting service for the POX messenger | |
| This is basically meant to report server CPU usage for the cpu_loadbalancer. | |
| """ | |
| import time | |
| from pox.core import core | |
| from pox.lib.revent import EventMixin, Event | |
| from messenger import ChannelBot | |
| class CPUUtilizationEvent (Event): | |
| def __init__ (self, utilization): | |
| self.utilization = utilization | |
| class CPUUtilization (EventMixin): | |
| """ | |
| A very bad class to get the CPU utilization on Linux | |
| """ | |
| _eventMixin_events = set([CPUUtilizationEvent]) | |
| def __init__ (self): | |
| self._last = (None,None) | |
| self._cur = self._last | |
| self.utilization = 0 # Until first cycle | |
| self._sample() | |
| def _sample (self): | |
| f = open("/proc/stat", "r") | |
| t = time.time() | |
| l = f.readline().split()[1:] | |
| del l[3] # Delete idle | |
| l = sum([int(x) for x in l]) | |
| # Count CPUs so we can normalize | |
| cpus = 0 | |
| while True: | |
| if not f.readline().startswith("cpu"): break | |
| cpus += 1 | |
| f.close() | |
| self._last = self._cur | |
| self._cur = (float(t),int(l)) | |
| core.callDelayed(1, self._sample) | |
| if self._last[0] is None: return | |
| td = self._cur[0]-self._last[0] | |
| cd = self._cur[1]-self._last[1] | |
| self.utilization = cd/td/cpus | |
| self.raiseEventNoErrors(CPUUtilizationEvent, self.utilization) | |
| class UtilizationBot (ChannelBot): | |
| def _init (self, extra): | |
| core.CPUMonitor.cpu.addListeners(self) | |
| def _handle_CPUUtilizationEvent (self, event): | |
| self.send(utilization=event.utilization) | |
| class CPUMonitor (object): | |
| def __init__ (self): | |
| self.cpu = CPUUtilization() | |
| core.listen_to_dependencies(self, components=['MessengerNexus']) | |
| def _all_dependencies_met (self): | |
| self.channel = core.MessengerNexus.get_channel("CPU") | |
| self.bot = UtilizationBot(self.channel.name) | |
| def launch (): | |
| core.registerNew(CPUMonitor) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@MurphyMc please help me where can i use cpu_agents.py there was no docstring declare the wau to use it ????