Commit f57ae6e7 authored by ibuler's avatar ibuler

[Update] 修改目录

parent bcd0b053
...@@ -10,6 +10,8 @@ import json ...@@ -10,6 +10,8 @@ import json
import signal import signal
import copy import copy
import psutil
from .conf import config from .conf import config
from .sshd import SSHServer from .sshd import SSHServer
from .httpd import HttpServer from .httpd import HttpServer
...@@ -83,10 +85,26 @@ class Coco: ...@@ -83,10 +85,26 @@ class Coco:
self.monitor_sessions() self.monitor_sessions()
self.monitor_sessions_replay() self.monitor_sessions_replay()
@ignore_error # @ignore_error
def heartbeat(self): def heartbeat(self):
_sessions = [s.to_json() for s in Session.sessions.values() if s] sessions = list(Session.sessions.keys())
tasks = app_service.terminal_heartbeat(_sessions) p = psutil.Process(os.getpid())
cpu_used = p.cpu_percent(interval=1.0)
memory_used = int(p.memory_info().rss / 1024 / 1024)
connections = len(p.connections())
threads = p.num_threads()
session_online = len(sessions)
data = {
"cpu_used": cpu_used,
"memory_used": memory_used,
"connections": connections,
"threads": threads,
"boot_time": p.create_time(),
"session_online": session_online,
"sessions": sessions,
}
tasks = app_service.terminal_heartbeat(data)
if tasks: if tasks:
self.handle_task(tasks) self.handle_task(tasks)
if tasks is False: if tasks is False:
...@@ -107,7 +125,7 @@ class Coco: ...@@ -107,7 +125,7 @@ class Coco:
while not self.stop_evt.is_set(): while not self.stop_evt.is_set():
try: try:
self.heartbeat() self.heartbeat()
except Exception as e: except IndexError as e:
logger.error("Unexpected error occur: {}".format(e)) logger.error("Unexpected error occur: {}".format(e))
time.sleep(config["HEARTBEAT_INTERVAL"]) time.sleep(config["HEARTBEAT_INTERVAL"])
thread = threading.Thread(target=func) thread = threading.Thread(target=func)
......
...@@ -300,11 +300,19 @@ class Config(dict): ...@@ -300,11 +300,19 @@ class Config(dict):
def __getattr__(self, item): def __getattr__(self, item):
return self.__getitem__(item) return self.__getitem__(item)
def __setattr__(self, key, value):
return self.__setitem__(key, value)
def __repr__(self): def __repr__(self):
return '<%s %s>' % (self.__class__.__name__, dict.__repr__(self)) return '<%s %s>' % (self.__class__.__name__, dict.__repr__(self))
access_key_path = os.path.abspath(os.path.join(root_path, 'keys', '.access_key')) access_key_path = os.path.abspath(
os.path.join(root_path, 'data', 'keys', '.access_key')
)
host_key_path = os.path.abspath(
os.path.join(root_path, 'data', 'keys', 'host_rsa_key')
)
defaults = { defaults = {
'NAME': socket.gethostname(), 'NAME': socket.gethostname(),
'CORE_HOST': 'http://127.0.0.1:8080', 'CORE_HOST': 'http://127.0.0.1:8080',
...@@ -316,17 +324,17 @@ defaults = { ...@@ -316,17 +324,17 @@ defaults = {
'HTTPD_PORT': 5000, 'HTTPD_PORT': 5000,
'COCO_ACCESS_KEY': '', 'COCO_ACCESS_KEY': '',
'ACCESS_KEY_FILE': access_key_path, 'ACCESS_KEY_FILE': access_key_path,
'HOST_KEY_FILE': host_key_path,
'SECRET_KEY': 'SDK29K03%MM0ksf&#2', 'SECRET_KEY': 'SDK29K03%MM0ksf&#2',
'LOG_LEVEL': 'INFO', 'LOG_LEVEL': 'INFO',
'LOG_DIR': os.path.join(root_path, 'logs'), 'LOG_DIR': os.path.join(root_path, 'data', 'logs'),
'SESSION_DIR': os.path.join(root_path, 'sessions'),
'ASSET_LIST_SORT_BY': 'hostname', # hostname, ip 'ASSET_LIST_SORT_BY': 'hostname', # hostname, ip
'PASSWORD_AUTH': True, 'PASSWORD_AUTH': True,
'PUBLIC_KEY_AUTH': True, 'PUBLIC_KEY_AUTH': True,
'SSH_TIMEOUT': 10, 'SSH_TIMEOUT': 10,
'ALLOW_SSH_USER': [], 'ALLOW_SSH_USER': [],
'BLOCK_SSH_USER': [], 'BLOCK_SSH_USER': [],
'HEARTBEAT_INTERVAL': 5, 'HEARTBEAT_INTERVAL': 20,
'MAX_CONNECTIONS': 500, # Not use now 'MAX_CONNECTIONS': 500, # Not use now
'ADMINS': '', 'ADMINS': '',
'COMMAND_STORAGE': {'TYPE': 'server'}, # server 'COMMAND_STORAGE': {'TYPE': 'server'}, # server
...@@ -380,3 +388,13 @@ def load_user_config(): ...@@ -380,3 +388,13 @@ def load_user_config():
config = load_user_config() config = load_user_config()
old_host_key_path = os.path.join(root_path, 'keys', 'host_rsa_key')
old_access_key_path = os.path.join(root_path, 'keys', '.access_key')
if os.path.isfile(old_host_key_path) and not os.path.isfile(config.HOST_KEY_FILE):
config.HOST_KEY_FILE = old_host_key_path
if os.path.isfile(old_access_key_path) and not os.path.isfile(config.ACCESS_KEY_FILE):
config.ACCESS_KEY_FILE = old_access_key_path
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# #
import abc
import threading import threading
import time import time
import os import os
...@@ -13,7 +12,7 @@ from copy import deepcopy ...@@ -13,7 +12,7 @@ from copy import deepcopy
import jms_storage import jms_storage
from .conf import config from .conf import config
from .utils import get_logger, Singleton from .utils import get_logger
from .struct import MemoryQueue from .struct import MemoryQueue
from .service import app_service from .service import app_service
......
...@@ -6,7 +6,6 @@ from .conf import config ...@@ -6,7 +6,6 @@ from .conf import config
inited = False inited = False
app_service = AppService(config) app_service = AppService(config)
if not inited: if not inited:
......
...@@ -29,7 +29,7 @@ class SSHServer: ...@@ -29,7 +29,7 @@ class SSHServer:
@property @property
def host_key(self): def host_key(self):
host_key_path = os.path.join(config['ROOT_PATH'], 'keys', 'host_rsa_key') host_key_path = config['HOST_KEY_FILE']
if not os.path.isfile(host_key_path): if not os.path.isfile(host_key_path):
if config.HOST_KEY: if config.HOST_KEY:
with open(host_key_path, 'w') as f: with open(host_key_path, 'w') as f:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment