2016-03-17 16:32:37 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright (C) 2016 GNS3 Technologies Inc.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
import json
|
2020-04-19 18:06:47 +00:00
|
|
|
import psutil
|
2016-03-17 16:32:37 +00:00
|
|
|
|
2020-04-19 18:42:46 +00:00
|
|
|
from gns3server.utils.cpu_percent import CpuPercent
|
|
|
|
|
2020-08-06 04:07:27 +00:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2016-03-17 16:32:37 +00:00
|
|
|
|
|
|
|
class NotificationQueue(asyncio.Queue):
|
|
|
|
"""
|
|
|
|
Queue returned by the notification manager.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self._first = True
|
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def get(self, timeout):
|
2016-03-17 16:32:37 +00:00
|
|
|
"""
|
2018-01-15 07:42:01 +00:00
|
|
|
When timeout is expire we send a ping notification with server information
|
2016-03-17 16:32:37 +00:00
|
|
|
"""
|
|
|
|
|
2018-01-15 07:42:01 +00:00
|
|
|
# At first get we return a ping so the client immediately receives data
|
2016-03-17 16:32:37 +00:00
|
|
|
if self._first:
|
|
|
|
self._first = False
|
2020-04-19 18:06:47 +00:00
|
|
|
return ("ping", self._getPing(), {})
|
2016-03-17 16:32:37 +00:00
|
|
|
|
|
|
|
try:
|
2018-10-15 10:05:49 +00:00
|
|
|
(action, msg, kwargs) = await asyncio.wait_for(super().get(), timeout)
|
2019-11-11 04:44:31 +00:00
|
|
|
except asyncio.TimeoutError:
|
2020-04-19 18:06:47 +00:00
|
|
|
return ("ping", self._getPing(), {})
|
2016-03-17 16:32:37 +00:00
|
|
|
return (action, msg, kwargs)
|
|
|
|
|
2020-04-19 18:06:47 +00:00
|
|
|
def _getPing(self):
|
|
|
|
"""
|
|
|
|
Return the content of the ping notification
|
|
|
|
"""
|
2020-08-06 04:07:27 +00:00
|
|
|
msg = {"cpu_usage_percent": 0,
|
|
|
|
"memory_usage_percent": 0}
|
2020-04-19 18:06:47 +00:00
|
|
|
# Non blocking call in order to get cpu usage. First call will return 0
|
2020-08-06 04:07:27 +00:00
|
|
|
try:
|
|
|
|
msg["cpu_usage_percent"] = CpuPercent.get(interval=None)
|
|
|
|
msg["memory_usage_percent"] = psutil.virtual_memory().percent
|
|
|
|
except OSError as e:
|
|
|
|
log.warning("Could not get CPU and memory usage from psutil: {}".format(e))
|
2020-04-19 18:06:47 +00:00
|
|
|
return msg
|
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def get_json(self, timeout):
|
2016-03-17 16:32:37 +00:00
|
|
|
"""
|
|
|
|
Get a message as a JSON
|
|
|
|
"""
|
2018-10-15 10:05:49 +00:00
|
|
|
(action, msg, kwargs) = await self.get(timeout)
|
2016-03-17 16:32:37 +00:00
|
|
|
if hasattr(msg, "__json__"):
|
|
|
|
msg = {"action": action, "event": msg.__json__()}
|
|
|
|
else:
|
|
|
|
msg = {"action": action, "event": msg}
|
|
|
|
msg.update(kwargs)
|
|
|
|
return json.dumps(msg, sort_keys=True)
|