コード例 #1
0
async def write_remote_crontab_monitor(cfg, ssh):
    v_cmd = '{0}/db_monitor.sh {1} {2}'.format(cfg['msg']['script_path'],
                                               cfg['msg']['script_file'],
                                               cfg['msg']['task_tag'])

    v_cron = '''
               crontab -l > /tmp/config && sed -i "/{0}/d" /tmp/config && echo  -e "\n#{1} tag={2}\n{3} {4} &>/dev/null &" >> /tmp/config
              '''.format(cfg['msg']['task_tag'], cfg['msg']['comments'],
                         cfg['msg']['task_tag'], cfg['msg']['run_time'], v_cmd)

    v_cron_ = '''
                crontab -l > /tmp/config && sed -i "/{0}/d" /tmp/config && echo  -e "\n#{1} tag={2}\n#{3} {4} &>/dev/null &" >> /tmp/config
              '''.format(cfg['msg']['task_tag'], cfg['msg']['comments'],
                         cfg['msg']['task_tag'], cfg['msg']['run_time'], v_cmd)

    v_cron2 = '''sed -i '/^$/{N;/\\n$/D};' /tmp/config'''

    v_cron3 ='''mkdir -p {}/crontab && crontab -l >{}/crontab/crontab.{}'''.\
             format(cfg['msg']['script_path'],cfg['msg']['script_path'],get_time2())

    v_cron4 = '''crontab /tmp/config'''

    if cfg['msg']['status'] == '1':
        if not ssh.exec(v_cron)['status']:
            return {'code': -1, 'msg': 'failure!'}
    else:
        if not ssh.exec(v_cron_)['status']:
            return {'code': -1, 'msg': 'failure!'}

    if not ssh.exec(v_cron2)['status']:
        return {'code': -1, 'msg': 'failure!'}

    if not ssh.exec(v_cron3)['status']:
        return {'code': -1, 'msg': 'failure!'}

    if not ssh.exec(v_cron4)['status']:
        return {'code': -1, 'msg': 'failure!'}

    res = ssh.exec('crontab -l')
    if res['status']:
        return {'code': 200, 'msg': res['stdout']}
    else:
        return {'code': -1, 'msg': 'failure!'}
コード例 #2
0
async def write_remote_crontab_minio(v_tag):
    cfg = await get_minio_config(v_tag)
    if cfg['code']!=200:
       return cfg

    v_cmd   = '{0}/minio_sync.sh {1} {2}'.format(cfg['msg']['script_path'],cfg['msg']['script_file'], v_tag)

    v_cron0 = '''
                crontab -l > /tmp/config && sed -i "/{0}/d" /tmp/config && echo  -e "\n#{1} sync_tag={2}\n{3} {4} &>/dev/null & #sync_tag={5}" >> /tmp/config  && crontab /tmp/config
              '''.format("sync_tag="+v_tag,cfg['msg']['comments'],v_tag,cfg['msg']['run_time'],v_cmd,v_tag)

    v_cron_ = '''
                crontab -l > /tmp/config && sed -i "/{0}/d" /tmp/config >> /tmp/config && crontab /tmp/config
              '''.format(v_tag)

    v_cron1 = '''crontab -l > /tmp/config && sed -i '/^$/{N;/\\n$/D};' /tmp/config && crontab /tmp/config'''

    v_cron2 = '''mkdir -p {}/crontab && crontab -l >{}/crontab/crontab.{}
              '''.format(cfg['msg']['script_path'],cfg['msg']['script_path'],get_time2())

    if not exec_ssh_cmd(cfg, v_cron2)['status']:
       return {'code': -1, 'msg': 'failure!'}

    if cfg['msg']['status'] == '0':
        if not exec_ssh_cmd(cfg, v_cron_)['status']:
           return {'code': -1, 'msg': 'failure!'}

    if cfg['msg']['status'] == '1':
        if not exec_ssh_cmd(cfg, v_cron0)['status']:
           return {'code': -1, 'msg': 'failure!'}

        if exec_ssh_cmd(cfg, v_cron1)['status']:
           return {'code': -1, 'msg': 'failure!'}

    res = exec_ssh_cmd(cfg, 'crontab -l')
    if res['status']:
        return {'code': 200, 'msg': res['stdout']}
    else:
        return {'code': -1, 'msg': 'failure!'}
コード例 #3
0
async def write_remote_crontab_inst(v_inst_id, v_flag):
    cfg = await get_db_inst_config(v_inst_id)
    if cfg['code'] != 200:
        return cfg

    v_cmd = '{0}/db_creator.sh status '.format(cfg['msg']['script_path'])

    v_cron = '''
               crontab -l > /tmp/config && sed -i "/{0}/d" /tmp/config && echo  -e "\n#{1} tag={2}\n{3} {4} &>/dev/null & #tag={5}" >> /tmp/config
              '''.format(v_inst_id, cfg['msg']['inst_name'], v_inst_id,
                         '*/1 * * * *', v_cmd, v_inst_id)

    v_cron_ = '''
                crontab -l > /tmp/config && sed -i "/{0}/d" /tmp/config >> /tmp/config
              '''.format(v_inst_id)

    v_cron2 = '''sed -i '/^$/{N;/\\n$/D};' /tmp/config'''

    v_cron3 = '''mkdir -p {}/crontab && crontab -l >{}/crontab/crontab.{}'''.\
             format(cfg['msg']['script_path'],cfg['msg']['script_path'],get_time2())

    v_cron4 = '''crontab /tmp/config'''

    if v_flag == 'destroy':
        exec_ssh_cmd(cfg, v_cron_)
        exec_ssh_cmd(cfg, v_cron4)

    if v_flag == 'create':
        exec_ssh_cmd(cfg, v_cron)
        exec_ssh_cmd(cfg, v_cron2)
        exec_ssh_cmd(cfg, v_cron3)
        exec_ssh_cmd(cfg, v_cron4)

    res = exec_ssh_cmd(cfg, 'crontab -l')
    if res['status']:
        return {'code': 200, 'msg': res['stdout']}
    else:
        return {'code': -1, 'msg': 'failure!'}
コード例 #4
0
async def write_remote_crontab_slow(cfg,ssh):
    v_cmd_c = '{0}/gather_slow.sh {1} cut {2}'.format(cfg['msg']['script_path'],cfg['msg']['script_file'],cfg['msg']['slow_id'])

    v_cmd_s = '{0}/gather_slow.sh {1} stats {2}'.format(cfg['msg']['script_path'],cfg['msg']['script_file'],cfg['msg']['slow_id'])

    v_cron0 = '''
                crontab -l > /tmp/config && sed -i "/{0}/d" /tmp/config && echo  -e "\n#{1} slow_id={2}\n{3} {4} &>/dev/null & #slow_id={5}" >> /tmp/config  && crontab /tmp/config
              '''.format("slow_id="+cfg['msg']['slow_id'],cfg['msg']['inst_name']+'日志切割任务',cfg['msg']['slow_id'],'0 0 * * *',v_cmd_c,cfg['msg']['slow_id'])

    v_cron1 = '''
                echo  -e "\n#{} slow_id={}\n{} {} &>/dev/null & #slow_id={}" >> /tmp/config  && crontab /tmp/config
              '''.format(cfg['msg']['inst_name']+'慢日志采集任务', cfg['msg']['slow_id'], cfg['msg']['run_time'], v_cmd_s, cfg['msg']['slow_id'])

    v_cron2 = '''
                crontab -l > /tmp/config && sed -i "/{}/d" /tmp/config && echo  -e "\n#{} slow_id={}\n{} {} &>/dev/null & #slow_id={}" >> /tmp/config  && crontab /tmp/config
              '''.format("slow_id="+cfg['msg']['slow_id'],cfg['msg']['inst_name'] + '慢日志采集任务', cfg['msg']['slow_id'], cfg['msg']['run_time'], v_cmd_s,cfg['msg']['slow_id'])

    v_cron_ = '''
                crontab -l > /tmp/config && sed -i "/{0}/d" /tmp/config >> /tmp/config && crontab /tmp/config
              '''.format(cfg['msg']['slow_id'])

    v_cron3 = '''crontab -l > /tmp/config && sed -i '/^$/{N;/\\n$/D};' /tmp/config && crontab /tmp/config'''

    v_cron4 = '''mkdir -p {}/crontab && crontab -l >{}/crontab/crontab.{}
              '''.format(cfg['msg']['script_path'],cfg['msg']['script_path'],get_time2())

    if cfg['msg']['status'] == '0':
       if ssh.exec(v_cron_)['status']:
          return {'code': -1, 'msg': 'failure!'}

       if ssh.exec(v_cron3)['status']:
          return {'code': -1, 'msg': 'failure!'}

    if cfg['msg']['status'] == '1':
       if cfg['msg']['db_type'] == '0' and cfg['msg']['inst_id'] != '':

          if not ssh.exec(v_cron0)['status']:
             return {'code': -1, 'msg': 'failure!'}

          if not ssh.exec(v_cron1)['status']:
             return {'code': -1, 'msg': 'failure!'}

          if not ssh.exec( v_cron3)['status']:
             return {'code': -1, 'msg': 'failure!'}

          if not ssh.exec(v_cron4)['status']:
             return {'code': -1, 'msg': 'failure!'}
       else:
          if not ssh.exec(v_cron2)['status']:
             return {'code': -1, 'msg': 'failure!'}

          if not ssh.exec(v_cron3)['status']:
             return {'code': -1, 'msg': 'failure!'}

          if not ssh.exec(v_cron4)['status']:
             return {'code': -1, 'msg': 'failure!'}

    res = ssh.exec('crontab -l')
    if res['status']:
        return {'code': 200, 'msg': res['stdout']}
    else:
        return {'code': -1, 'msg': 'failure!'}
コード例 #5
0
ファイル: syncer.py プロジェクト: mafeicnnui/dbapi
def write_remote_crontab_sync(cfg, ssh):
    v_cmd = '{0}/db_sync.sh {1} {2}'.format(cfg['msg']['script_path'],
                                            cfg['msg']['script_file'],
                                            cfg['msg']['sync_tag'])

    v_cmd_ = '{0}/db_agent.sh '.format(cfg['msg']['script_path'])

    v_cls = '{0}/db_sync.sh {1} {2}'.format(cfg['msg']['script_path'],
                                            'mysql2clickhouse_clear.py',
                                            cfg['msg']['sync_tag'])

    v_cls2 = '{0}/db_sync.sh {1} {2}'.format(cfg['msg']['script_path'],
                                             'mysql2mysql_real_clear.py',
                                             cfg['msg']['sync_tag'])

    v_cron = '''crontab -l > /tmp/config && sed -i "/{0}/d" /tmp/config && echo  -e "\n#{1} tag={2}\n{3} {4} &>/dev/null &" >> /tmp/config'''.format(
        cfg['msg']['sync_tag'], cfg['msg']['comments'], cfg['msg']['sync_tag'],
        cfg['msg']['run_time'], v_cmd)

    v_cron_ = '''crontab -l > /tmp/config && sed -i "/{0}/d" /tmp/config && echo  -e "\n#{1} tag={2}\n#{3} {4} &>/dev/null &" >> /tmp/config'''.format(
        cfg['msg']['sync_tag'], cfg['msg']['comments'], cfg['msg']['sync_tag'],
        cfg['msg']['run_time'], v_cmd)

    v_agent = '''sed -i "/{0}/d" /tmp/config && echo  -e "\n#{1} tag={2}\n{3} {4} &>/dev/null &" >> /tmp/config'''.format(
        'db_agent', '数据库代理服务', 'db_agent.py', '*/1 * * * *', v_cmd_)

    v_agent_ = '''sed -i "/{0}/d" /tmp/config && echo  -e "\n#{1} tag={2}\n#{3} {4} &>/dev/null &" >> /tmp/config'''.format(
        'db_agent', '数据库代理服务', 'db_agent.py', '*/1 * * * *', v_cmd_)

    v_clear = '''echo  -e "\n#{0} tag={1}\n{2} {3} &>/dev/null &" >> /tmp/config'''.format(
        '实时日志清理[mysql->clickhouse]', cfg['msg']['sync_tag'], '*/30 * * * *',
        v_cls)

    v_clear_ = '''echo  -e "\n#{0} tag={1}\n#{2} {3} &>/dev/null &" >> /tmp/config'''.format(
        '实时日志清理[mysql->clickhouse]', cfg['msg']['sync_tag'], '*/30 * * * *',
        v_cls)

    v_clear2 = '''echo  -e "\n#{0} tag={1}\n{2} {3} &>/dev/null &" >> /tmp/config'''.format(
        '实时日志清理[mysql->mysql]', cfg['msg']['sync_tag'], '*/30 * * * *', v_cls2)

    v_clear2_ = '''echo -e "\n#{0} tag={1}\n#{2} {3} &>/dev/null &" >> /tmp/config'''.format(
        '实时日志清理[mysql->mysql]', cfg['msg']['sync_tag'], '*/30 * * * *', v_cls2)

    v_cron2 = '''sed -i '/^$/{N;/\\n$/D};' /tmp/config'''

    v_cron3 = '''mkdir -p {}/crontab && crontab -l >{}/crontab/crontab.{}'''.format(
        cfg['msg']['script_path'], cfg['msg']['script_path'], get_time2())

    v_cron4 = '''crontab /tmp/config'''

    if cfg['msg']['status'] == '1':
        if not ssh.exec(v_cron)['status']:
            return {'code': -1, 'msg': 'failure!'}

        if not ssh.exec(v_agent)['status']:
            return {'code': -1, 'msg': 'failure!'}

        if cfg['msg']['sync_tag'].count('logger') > 0:
            if cfg['msg']['sync_type'] == '8':
                if not ssh.exec(v_clear)['status']:
                    return {'code': -1, 'msg': 'failure!'}
            if cfg['msg']['sync_type'] == '2':
                if not ssh.exec(v_clear2)['status']:
                    return {'code': -1, 'msg': 'failure!'}

    else:
        if not ssh.exec(v_cron_)['status']:
            return {'code': -1, 'msg': 'failure!'}

        if not ssh.exec(v_agent_)['status']:
            return {'code': -1, 'msg': 'failure!'}

        if cfg['msg']['sync_tag'].count('logger') > 0:
            if cfg['msg']['sync_type'] == '8':
                if not ssh.exec(v_clear_)['status']:
                    return {'code': -1, 'msg': 'failure!'}
            if cfg['msg']['sync_type'] == '2':
                if not ssh.exec(v_clear2_)['status']:
                    return {'code': -1, 'msg': 'failure!'}

    if not ssh.exec(v_cron2)['status']:
        return {'code': -1, 'msg': 'failure!'}

    if not ssh.exec(v_cron3)['status']:
        return {'code': -1, 'msg': 'failure!'}

    if not ssh.exec(v_cron4)['status']:
        return {'code': -1, 'msg': 'failure!'}

    res = ssh.exec('crontab -l')
    print("res['stdout']=", res['stdout'])

    if res['status']:
        return {'code': 200, 'msg': res['stdout']}
    else:
        return {'code': -1, 'msg': 'failure!'}