日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python远程监控服务器多个日志_python压测+paramiko远程监下载日志+js测试报告

發(fā)布時間:2023/12/4 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python远程监控服务器多个日志_python压测+paramiko远程监下载日志+js测试报告 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

關于壓測客戶端

netty nio壓測端

package com.nio.test;

import io.netty.bootstrap.Bootstrap;

import io.netty.buffer.ByteBuf;

import io.netty.buffer.Unpooled;

import io.netty.channel.ChannelFuture;

import io.netty.channel.ChannelInitializer;

import io.netty.channel.ChannelOption;

import io.netty.channel.EventLoopGroup;

import io.netty.channel.nio.NioEventLoopGroup;

import io.netty.channel.socket.SocketChannel;

import io.netty.channel.socket.nio.NioSocketChannel;

import io.netty.handler.codec.http.DefaultFullHttpRequest;

import io.netty.handler.codec.http.FullHttpRequest;

import io.netty.handler.codec.http.HttpHeaders;

import io.netty.handler.codec.http.HttpMethod;

import io.netty.handler.codec.http.HttpRequestEncoder;

import io.netty.handler.codec.http.HttpResponseDecoder;

import io.netty.handler.codec.http.HttpVersion;

import java.net.URI;

import java.nio.charset.StandardCharsets;

public class HttpClient {

public void connect(String host, int port) throws Exception {

EventLoopGroup workerGroup = new NioEventLoopGroup();

try {

Bootstrap b = new Bootstrap();

b.group(workerGroup);

b.channel(NioSocketChannel.class);

b.option(ChannelOption.SO_KEEPALIVE, true);

b.handler(new ChannelInitializer() {

@Override

public void initChannel(SocketChannel ch) throws Exception {

// 客戶端接收到的是httpResponse響應,所以要使用HttpResponseDecoder進行解碼

ch.pipeline().addLast(new HttpResponseDecoder());

// 客戶端發(fā)送的是httprequest,所以要使用HttpRequestEncoder進行編碼

ch.pipeline().addLast(new HttpRequestEncoder());

ch.pipeline().addLast(new HttpClientInboundHandler());

}

});

ChannelFuture f = b.connect(host, port).sync();

URI uri = new URI("http://gc.ditu.aliyun.com:80/geocoding?a=深圳市");

String msg = "Are you ok?";

DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET,

uri.toASCIIString(), Unpooled.wrappedBuffer(msg.getBytes("UTF-8")));

// 構建http請求

request.headers().set(HttpHeaders.Names.HOST, host);

request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);

request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, request.content().readableBytes());

// 發(fā)送http請求

f.channel().write(request);

f.channel().flush();

f.channel().closeFuture().sync();

// }

} finally {

workerGroup.shutdownGracefully();

}

}

public void connect_post(String host, int port) throws Exception {

EventLoopGroup workerGroup = new NioEventLoopGroup();

try {

Bootstrap b = new Bootstrap();

b.group(workerGroup);

b.channel(NioSocketChannel.class);

b.option(ChannelOption.SO_KEEPALIVE, true);

b.handler(new ChannelInitializer() {

@Override

public void initChannel(SocketChannel ch) throws Exception {

// 客戶端接收到的是httpResponse響應,所以要使用HttpResponseDecoder進行解碼

ch.pipeline().addLast(new HttpResponseDecoder());

// 客戶端發(fā)送的是httprequest,所以要使用HttpRequestEncoder進行編碼

ch.pipeline().addLast(new HttpRequestEncoder());

ch.pipeline().addLast(new HttpClientInboundHandler());

}

});

ChannelFuture f = b.connect(host, port).sync();

URI uri = new URI("http://gc.ditu.aliyun.com:80/geocoding?a=深圳市");

FullHttpRequest request = new DefaultFullHttpRequest(

HttpVersion.HTTP_1_1, HttpMethod.POST, uri.getRawPath());

// 構建http請求

request.headers().set(HttpHeaders.Names.HOST, host);

request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE); // or HttpHeaders.Values.CLOSE

request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);

request.headers().add(HttpHeaders.Names.CONTENT_TYPE, "application/json");

ByteBuf bbuf = Unpooled.copiedBuffer("{\"jsonrpc\":\"2.0\",\"method\":\"calc.add\",\"params\":[1,2],\"id\":1}", StandardCharsets.UTF_8);

request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, bbuf.readableBytes());

// 發(fā)送http請求

f.channel().write(request);

f.channel().flush();

f.channel().closeFuture().sync();

// }

} finally {

workerGroup.shutdownGracefully();

}

}

public static void main(String[] args) throws Exception {

HttpClient client = new HttpClient();

//請自行修改成服務端的IP

for(int k=0; k<1;k++){

client.connect("http://gc.ditu.aliyun.com/", 80);

System.out.println(k);

}

}

}

監(jiān)聽代碼

package com.nio.test;

import io.netty.buffer.ByteBuf;

import io.netty.channel.ChannelHandlerContext;

import io.netty.channel.ChannelInboundHandlerAdapter;

import io.netty.handler.codec.http.HttpContent;

import io.netty.handler.codec.http.HttpHeaders;

import io.netty.handler.codec.http.HttpResponse;

public class HttpClientInboundHandler extends ChannelInboundHandlerAdapter {

@Override

public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

System.out.print("success!~!~");

if (msg instanceof HttpResponse)

{

HttpResponse response = (HttpResponse) msg;

// System.out.println("CONTENT_TYPE:" + response.headers().get(HttpHeaders.Names.CONTENT_TYPE));

System.out.println("HTTP_CODE:" + response.getStatus());

}

if(msg instanceof HttpContent)

{

HttpContent content = (HttpContent)msg;

ByteBuf buf = content.content();

System.out.println(buf.toString(io.netty.util.CharsetUtil.UTF_8));

buf.release();

}

ctx.close();

}

}

python tornado異步框架壓測

import tornado.ioloop

from tornado.httpclient import AsyncHTTPClient

def handle_request(response):

if response.error:

print ("Error:", response.error)

else:

print(response.body)

param = {"msg":"111"}

param["img_msg"] = open("t.jpg",'r',encoding='utf-8')

url = 'http://gc.ditu.aliyun.com/geocoding?a=蘇州市'

i = 1

print(param)

req = tornado.httpclient.HTTPRequest(url, 'POST', body=str(param))

http_client = AsyncHTTPClient()

while i<10000:

i += 1

http_client.fetch(req, handle_request)

tornado.ioloop.IOLoop.instance().start()

python 協(xié)程壓測端

#-*- coding: utf-8 -*-

import urllib.request

# url = "http://r.qzone.qq.com/cgi-bin/user/cgi_personal_card?uin=284772894"

url = "http://gc.ditu.aliyun.com/geocoding?a="+urllib.request.quote("蘇州市")

HEADER = {'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8','User-Agent':'Mozilla/5.0 (Windows NT 6.1; rv:29.0) Gecko/20100101 Firefox/29.0'}

# 自定義請求的方法,get post

def f(url):

import urllib

import urllib.request

data=urllib.request.urlopen(url).read()

z_data=data.decode('UTF-8')

print(z_data)

# 我自己封裝了gevent 的方法,重載了run

from gevent import Greenlet

class MyGreenlet(Greenlet):

def __init__(self, func):

Greenlet.__init__(self)

self.func = func

def _run(self):

# gevent.sleep(self.n)

self.func

count = 3

green_let = []

for i in range(0, count):

green_let.append(MyGreenlet(f(url)))

for j in range(0, count):

green_let[j].start()

for k in range(0, count):

green_let[k].join()

** 當然也可以用多線程,apache ab等,如果是大量并發(fā)可以使用netty nio壓測+虛擬機的方式(其實原理差不多,協(xié)程,tornado等)**

遠程監(jiān)控

把此shell腳本上傳到服務器

#!/bin/bash

if (( $# != 1))

then

echo "please input sum times:"

exit 1

fi

sar -u 1 $1 | sed '1,3d'|sed '$d' > sar_cpu_1.txt

sar -r 1 $1 |sed '1,3d'|sed '$d' > sar_men_1.txt

sar -b 1 $1 |sed '1,3d'|sed '$d' > sar_io_1.txt

遠程上傳shell腳本,執(zhí)行,下載日志

import paramiko

import paramiko

server_ip = '192.168.1.1'

server_user = 'root'

server_passwd = ''

server_port = 22

ssh = paramiko.SSHClient()

def ssh_connect():

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh.load_system_host_keys()

ssh.connect(server_ip, server_port,server_user, server_passwd)

return ssh

def client_connect():

client = paramiko.Transport((server_ip, server_port))

client.connect(username = server_user, password = server_passwd)

return client

def ssh_disconnect(client):

client.close()

def exec_cmd(command, ssh):

'''

windows客戶端遠程執(zhí)行l(wèi)inux服務器上命令

'''

stdin, stdout, stderr = ssh.exec_command(command)

err = stderr.readline()

out = stdout.readline()

print(stdout.read())

def win_to_linux(localpath, remotepath,client):

'''

windows向linux服務器上傳文件.

localpath 為本地文件的絕對路徑。如:D: est.py

remotepath 為服務器端存放上傳文件的絕對路徑,而不是一個目錄。如:/tmp/my_file.txt

'''

sftp = paramiko.SFTPClient.from_transport(client)

sftp.put(localpath,remotepath)

client.close()

def linux_to_win(localpath, remotepath,client):

'''

從linux服務器下載文件到本地

localpath 為本地文件的絕對路徑。如:D: est.py

remotepath 為服務器端存放上傳文件的絕對路徑,而不是一個目錄。如:/tmp/my_file.txt

'''

sftp = paramiko.SFTPClient.from_transport(client)

sftp.get(remotepath, localpath)

client.close()

class AllowAllKeys(paramiko.MissingHostKeyPolicy):

def missing_host_key(self, client, hostname, key):

return

def muit_exec_cmd(ssh,cmd):

'''

ssh ssh連接

cmd 多命名

'''

ssh.set_missing_host_key_policy(AllowAllKeys())

channel = ssh.invoke_shell()

stdin = channel.makefile('wb')

stdout = channel.makefile('rb')

stdin.write(cmd)

print(stdout.read())

stdout.close()

stdin.close()

cl = client_connect()

sh = ssh_connect()

win_to_linux("get_men_cpu.sh","/data/get_men_cpu.sh",cl)

exec_cmd("/data/get_men_cpu.sh 100",sh)

# 要和服務器那邊的監(jiān)控時間結束同步,可以設置下sleep時間

linux_to_win("d:/cpu.txt","/data/sar_cpu_1.txt",cl)

linux_to_win("d:/men.txt","/data/sar_men_1.txt",cl)

linux_to_win("d:/io.txt","/data/sar_io_1.txt",cl)

cl.close()

sh.close()

js分析日志文件.cpu,內(nèi)存,io都是基于這樣分析

用的highcharts插件,自己去學習吧

var idle= [];

var iowait = [];

var categories = [];

var temp;

$(function () {

$.get( "txt/sar_cpu.txt", function( data ) {

var resourceContent = data.toString(); // can be a global variable too...

var rc = resourceContent.split("\n");

for(var i=0; i

temp = getNotNullArr(rc[i].split(" "));

console.log(temp);

idle.push(parseFloat(temp[temp.length-1]));

categories.push(temp[0]);

iowait.push(parseFloat(temp[6]));

}

set_h_title("cpu使用情況");

set_h_sub_title("test.com");

set_h_xaxis_setp(1);

set_h_tooltip("%");

set_h_yaxis_title("cpu使用情況");

set_h_xaxis_categories(categories);

set_h_series( [{name: 'idle',data:idle}, {name: 'iowait', data: iowait}]);

highchartsinit("#container",get_h_title(),get_h_sub_title(),get_h_xaxis_setp(),get_h_xaxis_categories(),get_h_yaxis_title(),get_h_tooltip(),get_h_series());

});

});

Paste_Image.png

Paste_Image.png

Paste_Image.png

更新2015-7-15

監(jiān)控各種中間件,數(shù)據(jù)庫占用信息

top -u oracle>ora.txt

top -u mysql>ora.txt

更多參考

總結

以上是生活随笔為你收集整理的python远程监控服务器多个日志_python压测+paramiko远程监下载日志+js测试报告的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。