def InstallAgent(site, ws_protocol, id): # if id is None: return host = models.Host.objects.get(pk=id) ssh = utils.SSH(hostname=host.addr, port=host.port, username=host.username, password=host.password) if not ssh.exist_cmd('yum -y install net-tools'): return ssh.command('mkdir /usr/local/FreeEye') configfile = os.path.join(settings.BASE_DIR, 'Agent', 'FreeEye_Agent.conf') config = open(configfile).read() % locals() ssh.writefile(config, '/usr/local/FreeEye/FreeEye_Agent.conf') ssh.putfile(os.path.join(settings.BASE_DIR, 'Agent', 'FreeEye_Agent_py2.py'), '/usr/local/FreeEye/', on_exists='ov') ssh.putfile(os.path.join(settings.BASE_DIR, 'Agent', 'FreeEye_Agent'), '/etc/init.d/', on_exists='ov') ssh.command('chmod 755 /etc/init.d/FreeEye_Agent') if not ssh.exist_cmd('pip'): ssh.command('yum install -y python-setuptools python-devel') ssh.command('easy_install pip') if not ssh.exist_cmd('gcc'): ssh.command('yum install -y gcc') ssh.command('pip install websocket-client psutil') ssh.command('service FreeEye_Agent start') ssh.close() SyncConfig(id)
def ws_home(message): content = json.loads(message.content['text']) host = models.Host.objects.get(pk=content['hostid']) ssh = utils.SSH(hostname=host.addr, port=host.port, username=host.username, password=host.password) app = content['appName'] app = models.Application.objects.filter(appName=app).all()[0] if content['status']: print(app.startCommand) ssh.command(app.startCommand) else: print(app.stopCommand) ssh.command(app.stopCommand)
def SyncConfig(id): host = models.Host.objects.get(pk=id) ssh = utils.SSH(hostname=host.addr, port=host.port, username=host.username, password=host.password) cfgs = ssh._sftp.listdir('/etc/logrotate.d/') for cfg in cfgs: inst, create = models.LogCleanConfig.objects.get_or_create( configName=cfg, host_id=id) config = ssh.readfile('/etc/logrotate.d/' + cfg) cfgconfig = models.LogCleanConfig.fromCfg(config, cfg) cfgconfig.host_id = id cfgconfig.id = inst.id cfgconfig.sync = True cfgconfig.save()
def LogConfig(message): kw = message.content host_id = kw.get('host_id') host = models.Host.objects.get(pk=host_id) cfgs = models.LogCleanConfig.objects.filter(host_id=host_id).all() ssh = utils.SSH(hostname=host.addr, port=host.port, username=host.username, password=host.password) for cfg in cfgs: if cfg.isDel: ssh.command('rm -rf /etc/logrotate.d/' + cfg.configName) cfg.delete() else: ssh.writefile(cfg.toConfig(), '/etc/logrotate.d/' + cfg.configName) cfg.sync = True cfg.save()
def on_message(message, taskType, id): content = json.loads(message.content['text']) taskmodels = models.FileTask if taskType == 'file' else models.CommandTask taskprogressmodels = models.FileTaskProgress if taskType == 'file' else models.CommandTaskProgress task = taskmodels.objects.get(pk=id) taskprogress = taskprogressmodels.objects.filter(task=task) if content['action'] == 'execute': logger.info( message, '执行%s任务(%s)!' % ('文件' if taskType == 'file' else 'command', task.name)) taskprogress = taskprogress.filter(is_start=False) elif content['action'] == 'reexecute': logger.info( message, '重新%s执行任务(%s)!' % ('文件' if taskType == 'file' else 'command', task.name)) taskprogress = taskprogress.filter(is_start=True) elif content['action'] == 'singleexecute' or content[ 'action'] == 'singlereexecute': taskprogress = taskprogress.filter(id=content['id']) logger.info( message, '%s%s执行任务(%s)!' % ('重新' if 're' in content['action'] else '', '文件' if taskType == 'file' else 'command', task.name)) taskprogress = taskprogress.filter(is_start=True) for tp in taskprogress: message.reply_channel.send( {'text': json.dumps(dict(action='task', id=tp.id, status='开始'))}) tp.is_start = True tp.save() host = tp.taskhost logger.info( message, '%s开始%s%s执行任务(%s)!' % (host.name, '重新' if 're' in content['action'] else '', '文件' if taskType == 'file' else 'command', task.name)) try: ssh = utils.SSH(hostname=host.addr, port=host.port, username=host.username, password=host.password) logger.info( message, '%s完成%s%s执行任务(%s)!' % (host.name, '重新' if 're' in content['action'] else '', '文件' if taskType == 'file' else 'command', task.name)) except: logger.info( message, '%s%s%s执行任务(%s),出现错误!' % (host.name, '重新' if 're' in content['action'] else '', '文件' if taskType == 'file' else 'command', task.name)) tp.result = '错误' tp.save() continue if taskType == 'file': try: ssh.putfile( os.path.join(settings.BASE_DIR, 'taskfile', task.file), task.remote_file, task.on_exists) except: tp.result = '错误' message.reply_channel.send({ 'text': json.dumps( dict(action='task', id=tp.id, status='完成', result='错误')) }) tp.save() continue message.reply_channel.send({ 'text': json.dumps( dict(action='task', id=tp.id, status='完成', result='成功')) }) tp.is_finish = True tp.result = '成功' tp.save() elif taskType == 'command': tp.is_start = True tp.save() try: ret = ssh.command(task.cmdline) if type(ret) is bytes: ret = str(ret, 'utf-8') except: tp.result = '错误' message.reply_channel.send({ 'text': json.dumps( dict(action='task', id=tp.id, status='完成', result='错误')) }) tp.save() continue message.reply_channel.send({ 'text': json.dumps( dict(action='task', id=tp.id, status='完成', result=ret)) }) tp.is_finish = True tp.result = ret tp.save()