www.cnblogs.com/ruiyqinrui

开源、架构、Linux C/C++/python AI BI 运维开发自动化运维。 春风桃李花 秋雨梧桐叶。“力尽不知热 但惜夏日长”。夏不惜,秋不获。@ruiY--秦瑞

python爬虫,C编程,嵌入式开发.hadoop大数据,桉树,onenebula云计算架构.linux运维及驱动开发.

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  2912 随笔 :: 9 文章 :: 51 评论 :: 185万 阅读
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/python3
"""
监控采集多台主机的load io数据,被监控主机在hosts列表按要求添加
tab_header:
datetime ip loadaverage_5 loadaverage_10 loadaverage_15 mem_all mem_use mem_idle mem_use% disk_kb_read/s disk_kb_write/s network_rxkb/s network_txkb/s
"""
 
 
import paramiko
import time
import os
 
dt = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
#print(dt)
 
d = time.strftime("%Y-%m-%d",time.localtime())
t = time.strftime("%H:%M:%S",time.localtime())
dtshow = d + "_"+ t
#print(dtshow)
 
#tab head handle
logName = "rui.qin.o.logs"
tab_header = "datetime ip loadaverage_5 loadaverage_10 loadaverage_15 mem_all mem_use mem_idle mem_use% disk_kb_read/s disk_kb_write/s network_rxkb/s network_txkb/s\n"
 
if not os.path.exists(logName):
    with open(logName,"w") as log_w:
        log_w.write(tab_header)
else:
    pass
 
 
#logName = "/opt/rui.qin.o/NetTools.logs"
 
#remote exec ping hosts,remote exec ping aim host use username/passwd or passwdless
hosts=[
#'hostname="10.112.17.53",port=22,username="jenkins",password=""',
#'hostname="10.110.93.11",port=22,username="jenkins",password=""',
'hostname="10.110.176.192",port=22,username="root",password="n"',
#'hostname="10.110.73.74",port=22,username="rui.qin",password=""',
#'hostname="10.110.92.125",port=22,username="jenkins",password=""',
#'hostname="10.110.92.123",port=22,username="jenkins",password=""',
]
 
#for ips in pingAimIps:
#    tabstr = tabstr + " " + ips
#print(tabstr)
 
#time.sleep(10)
#if not os.path.exists(logName):
#    cmd = "touch " + logName
#    print(cmd)
#    #os.system(touch logName)
 
#time.sleep(10)
#if tabstr not in open('/opt/rui.qin.o/NetTools.logs').read():
#    openfs = open("/opt/rui.qin.o/NetTools.logs","a+")
#    openfs.seek(0,0)
#    openfs.write(tabstr)
    #print("True")
#else:
    #print("False")
#    pass
 
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
 
#counts=10
#showcontent01 = ""
#showcontent03 = ""
for i in hosts:
    #showcontent02 = ""
    varHeader = "当前监测主机:"
    varHost = varHeader + str(i.split(','))
    #print(varHost)
    #print(f"{varHeader}" +  i.split(','))
    #HN='"' + i.split(',')[0].split('=')[1].replace('"',"") + '"'
    HN=i.split(',')[0].split('=')[1].replace('"','')
    #print(HN)
    #print("{} {}".format(varHeader,HN))
    #print(dtshow + " " + HN)
    showcontent01 = dtshow + " "  + HN
    #print(showcontent01)
    Port=int(i.split(',')[1].split('=')[1])
    #print(Port)
    userName=i.split(',')[2].split('=')[1].replace('"','')
    #print(userName)
    passWord=i.split(',')[3].split('=')[1].replace('"','')
    #print(passWord)
    #
    ssh.connect(hostname=HN,port=Port,username=userName,password=passWord)
    #++stdin,stdout,stderr = ssh.exec_command("du -sh /home/jenkins/rui.qin.o/")
    #++result = stdout.read()
    #++print(bytes.decode(result))
    #获取主机挂载点list
    #print(os.system("df -h |awk ' NR>1 {if ($1==$NF){printf $1}else{print $6}}'"))
    #stdin,stdout,stderr = ssh.exec_command("df -h | awk 'NR>1 {if ($1==$NF){print $1}else {print($5,$6)}}'")
    #stdin,stdout,stderr = ssh.exec_command("df -h | awk 'NR>1 {if ($1==$NF){print $1}else {print($6)}}'")
    #stdin,stdout,stderr = ssh.exec_command("ping -c %s 100.64.43.12 |grep rtt |awk '{print $4}' |awk -F'/' '{print $2}'")
    #stdin,stdout,stderr = ssh.exec_command("sudo apt-get install sysstat && sudo apt-get install iotop")
    #软件组件安装
    #print(HN)
    stdin_init,stdout_init,stderr_init = ssh.exec_command("sudo apt-get -y install sysstat iotop")
    time.sleep(1)
 
 
    '''
    stdin,stdout,stderr = ssh.exec_command("iostat -d -k 1 1")
    res_0661 = bytes.decode(stdout.read())
    print(res_0661)
    time.sleep(1)
 
    stdin,stdout,stderr = ssh.exec_command("iostat -c 1 1")
    res_0662 = bytes.decode(stdout.read())
    print(res_0662)
    time.sleep(1)
    stdin,stdout,stderr = ssh.exec_command("sudo iotop -b -d 1 -n 1 -o -u jenkins -qqq")
    res_0663 = bytes.decode(stdout.read())
    print(res_0663)
time.sleep(1)
 
 
    stdin,stdout,stderr = ssh.exec_command("iostat -d -x -k 1")
    res_0664 = bytes.decode(stdout.read())
    print(res_0664)
    time.sleep(1)
    '''
 
    stdin,stdout,stderr = ssh.exec_command("/usr/bin/w  | grep average | awk '{print $10,$11,$12}'")
    cpu_la = bytes.decode(stdout.read())
    cpu_la_res = cpu_la.split(',')
    #print(cpu_la_res)
    cpu_la_5 = cpu_la_res[0]
    cpu_la_10 = cpu_la_res[1]
    cpu_la_15 = cpu_la_res[2]
    #print(cpu_la_5 + cpu_la_10 + cpu_la_15)
    #print(stderr.read())
    #res_0665 = bytes.decode(stdout.read())
    #print(HN + "\t" + res_0665)
    #time.sleep(19)
 
 
    """
    tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
    """
    #stdin,stdout,stderr = ssh.exec_command("iostat -d -m | sed -n '3P' && iostat -d -m | grep sda")
    stdin,stdout,stderr = ssh.exec_command("iostat -d -k | grep nvme0n1 | awk '{print $3,$4}'")
    #print(bytes.decode(stdout.read()))
    disk_io_res = bytes.decode(stdout.read()).lstrip()
    #print(disk_io_res)
    #time.sleep(19)
    #disk_io_read_kb_s = disk_io_res.split("\t")[1]
    #print(len(disk_io_read_kb_s))
    #disk_io_write_kb_s = disk_io_res.split(" ")[1]
    #print(disk_io_res_read_kb_s + disk_io_write_kb_s)
    #time.sleep(1)
 
 
    """
    stdin,stdout,stderr = ssh.exec_command("iostat -d -m")
    res_0666 = bytes.decode(stdout.read())
    print(res_0666)
    time.sleep(1)
    """
 
    """
    rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s   %ifutil
    """
    #stdin,stdout,stderr = ssh.exec_command("sar -n DEV 1 2 | grep 'Average' | sed -n '1P' && sar -n DEV 1 2 | grep ens4f0| grep Average || sar -n DEV 1 2 | grep ens160|grep Average ")
    #stdin,stdout,stderr = ssh.exec_command("sar -n DEV 1 2 | grep ens4f0| grep Average | awk '{print $5,$6}' || sar -n DEV 1 2 | grep ens160|grep Average | awk '{print $5,$6}'")
    stdin,stdout,stderr = ssh.exec_command("sar -n DEV 1 2 | grep enx000ec670d28f| grep 平均时间 | awk '{print $5,$6}'&& sar -n DEV 1 2 | grep ens160|grep Average | awk '{print $5,$6}'")
    network_io_res = bytes.decode(stdout.read()).lstrip().rstrip().strip()
    #print(network_io_res)
    time.sleep(1)
 
    #memory
    stdin_m,stdout_m,stderr_m = ssh.exec_command("free -m | grep '内存'| awk '{print $2,$3,$4,$3/$2}'")
mem = bytes.decode(stdout_m.read())
    time.sleep(1)
    #print(mem)
 
 
    results = dtshow + "\t" + HN + "\t" + cpu_la_5 + '\t' + cpu_la_10  + '\t' + cpu_la_15.replace('\n','').replace('\r','') + '\t' + mem.replace('\n','') + '\t' + disk_io_res.replace('\n','').replace('\n','') + '\t' + network_io_res.replace("\r","").replace("\n","") + '\n'
    print(results)
    time.sleep(0.5)
 
    if os.path.exists(logName):
        with open(logName,"a+") as log_w:
            log_w.write(results)
 
    """
    stdin,stdout,stderr = ssh.exec_command("sar -n DEV 1 2 | grep 'Average' | sed -n '1P' && sar -n DEV 1 2 | grep ens160| grep Average")
    res_0777 = bytes.decode(stdout.read())
    print(res_0777)
    time.sleep(1)
    """
 
"""
    #valueX[] = ""
    for ii in range(len(pingAimIps)):
        #print(ii)
        #valueX[ii] = ""
        #cmd="ping -c {} {}|grep rtt |awk '{print \$4}' |awk -F'/' '{print \$2}'".format(counts,ii)
        #print(cmd)
        #cmd="ping -c 10 %s |grep rtt |awk '{print $4}' |awk -F'/' '{print $2}'"%(ii)
        #cmd="ping -c 10 %s |grep round-trip |awk '{print $4}' |awk -F'/' '{print $2}'"%(ii)
        cmd="ping -c 6 %s |grep dev |awk '{print $4}' |awk -F'/' '{print $2}'"%(pingAimIps[ii])
        #print(ii)
        #print(showcontent02)
        #print(cmd)
        stdin,stdout,stderr = ssh.exec_command(cmd)
        res = stdout.read()
        #print(res)
        #print(type(res))
        result = res.split(b'\n')
        result01 = result[0]
        res = result01.split(b'\n')[0]
        resok = bytes.decode(res)
        #showcontent02 = showcontent02 + " " + pingAimIps[ii] + " " + resok
        showcontent02 = showcontent02 + " " + resok
        #print(showcontent02)
        #valueX[ii] = valueX[ii] + " " + pingAimIps[ii] + " " + resok
        #print(showcontent02)
        #print(valueX[ii])
 
    #print(showcontent01 + showcontent02)
        #print(showcontent02)
        ##print(resok)
        #showceontent02 =  showcontent02 + " " +resok
        #showcontent02 = showcontent01 + " " + ii + " " + resok
        #showcontent02 = HN + " " + ii + " " + resok
        #print(showcontent02)
        #print(showcontent02)
        #print(showcontent02)
        #print("\n")
#print(type(res))
        #print(type(result01))
#showcontent03 = showcontent01 + showcontent02
#print(showcontent03)
#showcontent03 = showcontent01 + " " + showcontent02
#print(showcontent03)
"""

  

posted on   秦瑞It行程实录  阅读(15)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
历史上的今天:
2015-07-19 桌面云
2015-07-19 git项目初始化
www.cnblogs.com/ruiyqinrui
点击右上角即可分享
微信分享提示