165 lines
No EOL
14 KiB
HTML
165 lines
No EOL
14 KiB
HTML
<!DOCTYPE html>
|
||
<html>
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="tool" content="leanote-desktop-app">
|
||
<title>python监控远程服务器</title>
|
||
<style>
|
||
|
||
*{font-family:"lucida grande","lucida sans unicode",lucida,helvetica,"Hiragino Sans GB","Microsoft YaHei","WenQuanYi Micro Hei",sans-serif;}
|
||
|
||
body {
|
||
margin: 0;
|
||
}
|
||
|
||
/*公用文字样式*/
|
||
h1{font-size:30px}h2{font-size:24px}h3{font-size:18px}h4{font-size:14px}
|
||
.note-container{
|
||
width:850px;
|
||
margin:auto;
|
||
padding: 10px 20px;
|
||
box-shadow: 1px 1px 10px #eee;
|
||
}
|
||
#title {
|
||
margin: 0;
|
||
}
|
||
table {
|
||
margin-bottom: 16px;
|
||
border-collapse: collapse;
|
||
}
|
||
table th, table td {
|
||
padding: 6px 13px;
|
||
border: 1px solid #ddd;
|
||
}
|
||
table th {
|
||
font-weight: bold;
|
||
}
|
||
|
||
table tr {
|
||
background-color: none;
|
||
border-top: 1px solid #ccc;
|
||
}
|
||
table tr:nth-child(2n) {
|
||
background-color: rgb(247, 247, 249);
|
||
}
|
||
.mce-item-table, .mce-item-table td, .mce-item-table th, .mce-item-table caption {
|
||
border: 1px solid #ddd;
|
||
border-collapse: collapse;
|
||
padding: 6px 13px;
|
||
}
|
||
blockquote {
|
||
border-left-width:10px;
|
||
background-color:rgba(128,128,128,0.05);
|
||
border-top-right-radius:5px;
|
||
border-bottom-right-radius:5px;
|
||
padding:15px 20px;
|
||
border-left:5px solid rgba(128,128,128,0.075);
|
||
}
|
||
blockquote p {
|
||
margin-bottom:1.1em;
|
||
font-size:1em;
|
||
line-height:1.45
|
||
}
|
||
blockquote ul:last-child,blockquote ol:last-child {
|
||
margin-bottom:0
|
||
}
|
||
pre {
|
||
padding: 18px;
|
||
background-color: #f7f7f9;
|
||
border: 1px solid #e1e1e8;
|
||
border-radius: 3px;
|
||
display: block;
|
||
}
|
||
code {
|
||
padding: 2px 4px;
|
||
font-size: 90%;
|
||
color: #c7254e;
|
||
white-space: nowrap;
|
||
background-color: #f9f2f4;
|
||
border-radius: 4px;
|
||
}
|
||
.footnote {
|
||
vertical-align: top;
|
||
position: relative;
|
||
top: -0.5em;
|
||
font-size: .8em;
|
||
}
|
||
|
||
hr {
|
||
margin:2em 0
|
||
}
|
||
img {
|
||
max-width:100%
|
||
}
|
||
pre {
|
||
word-break:break-word
|
||
}
|
||
p,pre,pre.prettyprint,blockquote {
|
||
margin:0 0 1.1em
|
||
}
|
||
hr {
|
||
margin:2em 0
|
||
}
|
||
img {
|
||
max-width:100%
|
||
}
|
||
.sequence-diagram,.flow-chart {
|
||
text-align:center;
|
||
margin-bottom:1.1em
|
||
}
|
||
.sequence-diagram text,.flow-chart text {
|
||
font-size:15px !important;
|
||
font-family:"Source Sans Pro",sans-serif !important
|
||
}
|
||
.sequence-diagram [fill="#ffffff"],.flow-chart [fill="#ffffff"] {
|
||
fill:#f6f6f6
|
||
}
|
||
.sequence-diagram [stroke="#000000"],.flow-chart [stroke="#000000"] {
|
||
stroke:#3f3f3f
|
||
}
|
||
.sequence-diagram text[stroke="#000000"],.flow-chart text[stroke="#000000"] {
|
||
stroke:none
|
||
}
|
||
.sequence-diagram [fill="#000"],.flow-chart [fill="#000"],.sequence-diagram [fill="#000000"],.flow-chart [fill="#000000"],.sequence-diagram [fill="black"],.flow-chart [fill="black"] {
|
||
fill:#3f3f3f
|
||
}
|
||
ul,ol {
|
||
margin-bottom:1.1em
|
||
}
|
||
ul ul,ol ul,ul ol,ol ol {
|
||
margin-bottom:1.1em
|
||
}
|
||
kbd {
|
||
padding:.1em .6em;
|
||
border:1px solid rgba(63,63,63,0.25);
|
||
-webkit-box-shadow:0 1px 0 rgba(63,63,63,0.25);
|
||
box-shadow:0 1px 0 rgba(63,63,63,0.25);
|
||
font-size:.7em;
|
||
font-family:sans-serif;
|
||
background-color:#fff;
|
||
color:#333;
|
||
border-radius:3px;
|
||
display:inline-block;
|
||
margin:0 .1em;
|
||
white-space:nowrap
|
||
}
|
||
.toc ul {
|
||
list-style-type:none;
|
||
margin-bottom:15px
|
||
}
|
||
</style>
|
||
<!-- 该css供自定义样式 -->
|
||
<link href="../leanote-html.css" rel="stylesheet">
|
||
</head>
|
||
|
||
<body>
|
||
|
||
<div class="note-container">
|
||
<h1 class="title" id="leanote-title">python监控远程服务器</h1>
|
||
<div class="content-html" id="leanote-content"><p>#!/usr/bin/env python<br># -*- coding: gbk -*-<br>#2010-3-25<br><br>#程序针对ip.txt中的ip,ssh上去执行命令,取回执行结果,写入本地文件<br>#存在问题:执行一个命令需要ssh一次。执行命令中的大括号不知如何处理。<br>#另外有些正则表达式也可以使用cut之类的命令来替换<br>#gtalk: xurongzhong#gmail.com<br><br>import pexpect<br>import sys<br>import re<br>import os<br>import time<br><br><br>#设定字符编码为GBK<br>reload(sys)<br>sys.setdefaultencoding('gbk')<br> <br>def ssh_command(ip, command):<br> """<br> 登录到一台机器执行执行的命令,取回返回结果<br> """<br> ssh_newkey = 'Are you sure you want to continue connecting'<br> # 为 ssh 命令生成一个 spawn 类的子程序对象.<br> child = pexpect.spawn('ssh -q -p 36000 soso_plt@'+ip + " " + command)<br> i = child.expect([pexpect.TIMEOUT, ssh_newkey, 'password: '])<br> # 如果登录超时,打印出错信息,并退出.<br> if i == 0: # Timeout<br> print 'ERROR!'<br> print 'SSH could not login. Here is what SSH said:'<br> print child.before, child.after<br> return None<br> # 如果 ssh 没有 public key,接受它.<br> if i == 1: # SSH does not have the public key. Just accept it.<br> child.sendline ('yes')<br> child.expect ('password: ')<br> i = child.expect([pexpect.TIMEOUT, 'password: '])<br> if i == 0: # Timeout<br> print 'ERROR!'<br> print 'SSH could not login. Here is what SSH said:'<br> print child.before, child.after<br> return None<br> # 输入密码.<br> child.sendline("!plat@soso")<br> return child<br><br>def process_info(Pr_name):<br> #获取drm_processes 的进程号<br> cmd = r"ps aux | grep " + Pr_name + r" | grep -v grep"<br> child = ssh_command(ip,cmd)<br> child.expect(pexpect.EOF) <br> processes = child.before <br> processes_num = re.findall('soso_plt.*?(\d+) ', processes)<br> f_out.write("\n\n" + Pr_name + " 的进程信息:")<br> if not processes_num:<br> f_out.write("\nThere is NO " + Pr_name + " running" )<br> else:<br> for pid in processes_num:<br> f_out.write("\n****"+ pid)<br> #获取进程的CPU和内存使用率<br> cmd = 'top -p' + pid + ' -b -n1 | tail -n2 | head -n1'<br> child = ssh_command(ip,cmd)<br> child.expect(pexpect.EOF) <br> info = child.before <br> info = info.split()<br> cpu = info[8]<br> mem= info[9]<br> <br> #获取句柄数<br> cmd = 'ls -l /proc/' + pid + '/fd |wc -l '<br> child = ssh_command(ip,cmd)<br> child.expect(pexpect.EOF) <br> handle = child.before.strip() <br> <br> f_out.write("\n********CPU占用率:"+ cpu + "****内存占用率:" + mem + "****句柄:" + handle)<br> <br>def cpu_info():<br> #获取CPU数目<br> child = ssh_command (ip , "cat /proc/cpuinfo " )<br> child.expect(pexpect.EOF)<br> cpuinfo = child.before<br> cpu_num = re.findall('processor.*?(\d+)', cpuinfo)[-1]<br> cpu_num = str(int(cpu_num) + 1 )<br> f_out.write("\nCPU number:" + cpu_num )<br><br> <br> <br>def mem_info():<br> #获取内存信息<br> child = ssh_command(ip,"cat /proc/meminfo ")<br> child.expect(pexpect.EOF)<br> meminfo = child.before<br> mem_values = re.findall('(\d+)\ kB', meminfo)<br> <br> MemTotal = mem_values[0]<br> MemFree = mem_values[1]<br> Buffers = mem_values[2]<br> Cached = mem_values[3]<br> SwapCached = mem_values[4]<br> Active = mem_values[5]<br> Inactive = mem_values[6]<br> HighTotal = mem_values[7]<br> HighFree = mem_values[8]<br> LowTotal = mem_values[9]<br> LowFree = mem_values[10]<br> SwapTotal = mem_values[11]<br> SwapFree = mem_values[12]<br> Dirty = mem_values[13]<br> Writeback= mem_values[14]<br> AnonPages = mem_values[15]<br> Mapped = mem_values[16]<br> Slab = mem_values[17]<br> CommitLimit = mem_values[18]<br> Committed_AS = mem_values[19]<br> PageTables = mem_values[20]<br> VmallocTotal = mem_values[21] <br> VmallocUsed = mem_values[22] <br> VmallocChunk = mem_values[23] <br> Hugepagesize = mem_values[-1] <br> <br> #计算内存使用比率<br> Free_Mem = int(MemFree) + int(Buffers) + int(Cached) <br> Used_Mem = int(MemTotal) - Free_Mem<br> Rate_Mem = Used_Mem/float(MemTotal)<br> <br> f_out.write("\nMEM number:" + MemTotal )<br> f_out.write("\nMEM Free:" + str(Free_Mem) )<br> f_out.write("\nMEM Used:" + str(Used_Mem) )<br> f_out.write("\nMEM Usage:" + str(Rate_Mem) )<br> <br> #计算交换内存使用比率<br> Rate_Swap = 1 - int(SwapFree)/float(SwapTotal) <br> f_out.write("\nSwapTotal:" + SwapTotal ) <br> f_out.write("\nSwapFree:" + SwapFree )<br> f_out.write("\nSwap Usage:" + str(Rate_Swap) ) <br> <br>def vmstat_info():<br> #获取CPU数目<br> child = ssh_command (ip , "vmstat 1 2 | tail -n 1" )<br> child.expect(pexpect.EOF)<br> vmstat_info = child.before.strip().split()<br> processes_waiting = vmstat_info[0]<br> processes_sleep = vmstat_info[1]<br> io_bi = vmstat_info[8]<br> io_bo = vmstat_info[9]<br> System_in = vmstat_info[10]<br> System_cs = vmstat_info[11]<br> cpu_us = vmstat_info[12]<br> cpu_sy = vmstat_info[13]<br> cpu_id = vmstat_info[14]<br> cpu_wa = vmstat_info[15]<br> cpu_st = vmstat_info[16]<br><br> f_out.write("\nprocesses_waiting:" + processes_waiting )<br> f_out.write("\nprocesses_sleep:" + processes_sleep ) <br> f_out.write("\nio_bi:" + io_bi )<br> f_out.write("\nio_bo:" + io_bo ) <br> f_out.write("\nSystem_in:" + System_in )<br> f_out.write("\nSystem_cs :" + System_cs ) <br> f_out.write("\ncpu_us:" + cpu_us )<br> f_out.write("\ncpu_sy:" + cpu_sy ) <br> f_out.write("\ncpu_id:" + cpu_id )<br> f_out.write("\ncpu_wa:" + cpu_wa ) <br> f_out.write("\ncpu_st:" + cpu_st ) <br> <br>#打开日志文件<br>Pr_name = sys.argv[1]<br>time_now = time.strftime("%Y-%m-%d_%H-%M-%S-",time.localtime())<br>filename = time_now + Pr_name + ".log"<br>f_out = open(filename,'w') <br>#对每个ip的信息进行监控<br>for ip in open("ip.txt"):<br> ip = ip.strip()<br> f_out.write("\n" + "*"*80+"\n\n"+ip)<br> <br> cpu_info()<br> mem_info()<br> vmstat_info()<br> <br> #获取top中的cpu空闲率<br> child = ssh_command(ip,"top -b -n 1")<br> child.expect(pexpect.EOF) <br> top = child.before <br> idle_cpu = re.search(',\ (\d+.*?)%id', top).group(1)<br> f_out.write("\nCPU IDLE:" + idle_cpu )<br> <br> process_info(Pr_name)<br> </p></div>
|
||
</div>
|
||
|
||
<!-- 该js供其它处理 -->
|
||
<script src="../leanote-html.js"></script>
|
||
</body>
|
||
</html> |