class WebsiteUser(HttpLocust): file = os.path.dirname(os.path.realpath(__file__)) + '/config.cfg' config = common.cfg(file, 'locust') task_set = WebsiteTasks host = config.query('host') # ticketlist = common.exportTicket(num=int(config.query('ticketnum'))) postdata = postdata() min_waittime = int(config.query('min_waittime')) max_waittime = int(config.query('max_waittime'))
def __init__(self): rongcfg = common.cfg(filecfg, 'rongyun') app_key = rongcfg.query('app_key') app_secret = rongcfg.query('app_secret') self.rcloud = RongCloud(app_key, app_secret)
def server(): mem = {} stdin, stdout, stderr = ssh.exec_command('cat /proc/meminfo') lines = stdout.readlines() for line in lines: if len(line) < 2: continue name = line.split(':')[0] var = line.split(':')[1].split()[0] mem[name] = float(var) mem['MemUsed'] = mem['MemTotal'] - mem['MemFree'] - mem['Buffers'] - mem[ 'Cached'] serverlist = {} # 记录内存使用率 已使用 总内存和缓存大小 memory = {} memory['percent'] = str(round( mem['MemUsed'] / mem['MemTotal'] * 100)) + '%' memory['used'] = str(round(mem['MemUsed'] / (1024), 2)) + ' MB' memory['MemTotal'] = str(round(mem['MemTotal'] / (1024), 2)) + ' MB' memory['Buffers'] = str(round(mem['Buffers'] / (1024), 2)) + ' MB' # cpu负载 loadavg = {} stdin, stdout, stderr = ssh.exec_command('cat /proc/loadavg') con = stdout.read().split() loadavg['lavg_1'] = con[0] loadavg['lavg_5'] = con[1] loadavg['lavg_15'] = con[2] loadavg['nr'] = con[3] # 网卡速度 stdin, stdout, stderr = ssh.exec_command(' sar -n DEV 1 1') lines = stdout.readlines() Average = {} for line in lines: if 'Average' in line and 'IFACE' not in line: line1 = ' '.join(line.split()).split(" ") Average[line1[1]] = u'下载速度:' + str( line1[4]) + ' kb/s' + u' 上传速度:' + str(line1[5]) + ' kb/s' stdin, stdout, stderr = ssh.exec_command( "netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'") lines = stdout.readlines() netstat = {} netstat['ESTABLISHED'], netstat['CLOSE_WAIT'], netstat['TIME_WAIT'], netstat['SYN_RECV'], netstat['FIN_WAIT1'], \ netstat['FIN_WAIT2'] = 0, 0, 0, 0, 0, 0 for line in lines: line1 = line.split(" ") netstat[line1[0]] = line1[1] # 服务器连接数 try: db = common.cfg(filecfg, 'mysql') conn = pymysql.connect(host=db.query('host'), user=db.query('user'), passwd=db.query('passwd'), db=db.query('db'), charset=db.query('charset')) cursors = conn.cursor() cursors.execute('SHOW FULL PROCESSLIST') dbdata = cursors.fetchall() mysqlnum = len(dbdata) except Exception: mysqlnum = '数据库连接失败' try: reidinfo = pool.info() reidslsit = {} reidslsit['qps'] = reidinfo['instantaneous_ops_per_sec'] reidslsit['keyspace_misses'] = reidinfo['keyspace_misses'] reidslsit['used_memory'] = reidinfo['used_memory_human'] reidslsit['blocked_clients'] = reidinfo['blocked_clients'] reidslsit['connected_clients'] = reidinfo['connected_clients'] except Exception: reidslsit = {} reidslsit['qps'] = 'redis连接失败' serverlist['reidslsit'] = reidslsit serverlist['netstat'] = netstat serverlist['Average'] = Average serverlist['memory'] = memory serverlist['loadavg'] = loadavg serverlist['mysqlnum'] = mysqlnum return jsonify(serverlist)
def zhibo(): global timer peoplenum = [] roomlist = [] messagedata = request.form.get('data') rongc = rongcloud() try: locustcfg = common.cfg(filecfg, 'locust') url = locustcfg.query('HOST') + "/platform-rest/service.jws" home = common.liveHome data = requests.post(url=url, json=home).json() disport = data['b']['data']['liveList'] for live in disport: if live['state'] == 1: roomlist.append( [live['liveId'], live['title'], live['watchNumStr']]) rongkeys = pool.keys('l-imAccount-map*') ronglist = [] for rong in rongkeys: ronglist.append(rong.split(':')[1]) peoplenum = rongc.rongcheckOnline(ronglist) except Exception as E: flash(u'警告:请求错误{0 }'.format(E), 'danger') return render_template('zhibo.html', roomlist=roomlist, peoplenum=len(peoplenum)) if messagedata: messagedata = messagedata.encode('unicode-escape').decode( 'string_escape') messagedata = json.loads(messagedata) def killtimer(): global kill kill = False if not messagedata.get('liveID'): flash(u'警告:请选择直播间', 'warning') return render_template('zhibo.html', roomlist=roomlist, peoplenum=len(peoplenum)) if timer: flash(u'警告:正在执行任务请先结束当前任务', 'warning') else: timer = threading.Timer(0.1, rongc.rongpublishChatroom, [common.PTdata, messagedata['frequency']]) timer.start() timer1 = threading.Timer(int(messagedata['time']), killtimer) timer1.start() return render_template('zhibo.html', roomlist=roomlist, peoplenum=len(peoplenum)) kill = request.form.get('kill') if kill: if timer: print(timer, 'ceshi') timer.cancel() timer = None kill = True else: flash(u'警告:没有正在执行的任务', 'warning') # home['b']['type']='金融产业' # data=requests.post(url=url, json=home).json() # Finance=data['b']['data']['liveList'] # for i in Finance: # if i['state']==1: # roomlist.append([i['liveId'],i['title']]) return render_template('zhibo.html', roomlist=roomlist, peoplenum=len(peoplenum))
import paramiko global null null = '' app = Flask(__name__) app.debug = True app.config['SESSION_TYPE'] = 'filesystem' app.config['SECRET_KEY'] = os.urandom(24) manager = Manager(app) bootstrap = Bootstrap(app) moment = Moment(app) cf = ConfigParser.ConfigParser() filecfg = os.path.dirname(os.path.realpath(__file__)) + '/config.cfg' locustcfg = common.cfg(filecfg, 'locust') rediscfg = common.cfg(filecfg, 'redis') pool = redis.Redis(host=rediscfg.query('host'), port=rediscfg.query('port'), db=rediscfg.query('db'), password=rediscfg.query('password')) linuxcfg = common.cfg(filecfg, 'linux') ssh = paramiko.SSHClient() ssh.load_system_host_keys() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname=linuxcfg.query('hostname'), username=linuxcfg.query('username'), password=linuxcfg.query('password'), timeout=300, allow_agent=False,