獲取linux性能數據
import re
import os
import sys
import json
import socket
from urllib import request,parse
import urllib
from subprocess import PIPE,Popen
class SendData:
def __init__(self,IpAddr,serverHost,serverPort=8000):
"""
"""
self.result={"system":"linux","Ip":IpAddr}
self.host=serverHost
self.port=serverPort
self.url="http://%s:%s/api/savedata" %(self.host,self.port)
print(self.url)
self.headers={"User-Agent":"Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Mobile Safari/537.36"}
def getInfo(self,command):
"""
"""
with os.popen(command) as f:
com=f.read()
return com
def All_Command(self):
commands=[
"hostname",
"ifconfig |awk -F‘ ‘ ‘/HWaddr/{print $5}‘",
"cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c",
"free -m | awk ‘{print $4}‘| sed -n ‘2p‘",
"df -h | awk ‘{print $4}‘|sed -n ‘2p‘",
"uname -r"
]
result=list(map(self.getInfo,commands))
return result
def my_dict(self):
dicts=self.All_Command()
self.result["Host"]=dicts[0].strip()
self.result["Mac"]=dicts[1].strip()
self.result["Cpu"]=dicts[2].strip()
self.result["Mem"]=dicts[3].strip()
self.result["Disk"]=dicts[4].strip()
self.result["Name"]=dicts[5].strip()
return self.result
def sendData(self):
data=parse.urlencode(self.result).encode(‘utf-8‘)
req=request.Request(self.url,headers=self.headers,data=data)
page=request.urlopen(req).read()
return page
s=SendData("192.168.1.23","192.168.1.102")
print(s.sendData())
獲取linux性能數據