依赖

pip 包 psutil requests

脚本

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
# -*- coding: utf-8 -*-

import psutil
import json
import socket
import datetime
import time
import requests
import os

'''
向服务器推送数据,支持socket和http

本脚本应该位于 /opt/pusher.py
'''
class pusher(object):
def __init__(self, ip, port):
self.ip = ip
self.port = port

def pushed(self):
'''
推送数据到中心服务器
'''
try:
return self.pushWithHttp(self.ip, self.port).text
except:
return self.pushWithSocket(self.ip, self.port)

def pushWithHttp(self, ip, port):
'''
使用 HTTP 协议发送
'''
return requests.post("http://%s/User/LeaseAgent/push" % ip, {
'state': self.postData,
'sn': self.getSn()
})

def pushWithSocket(self, ip, port):
'''
使用 RPC
'''
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conn.connect((ip, port))
conn.send(data)
return conn.close()

def setData(self, data):
'''
设置数据
'''
self.postData = data
return self

def getData(self):
'''
整理数据
'''
return self.encodeData({
'useInfo': self.getUseInfo(),
'countInfo': self.getCountInfo(),
'systemInfo': self.getSystemInfo()
})

def encodeData(self, data):
'''
转义数据
'''
return json.dumps(data)

def getSystemInfo(self):
'''
获取系统信息
'''
return {
'bootTime': datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S"),
'sumBootTime': round((time.time() - psutil.boot_time()) / 86400, 1)
}

def getCountInfo(self):
'''
获取数量信息
'''
return {
'cpuCount': str(psutil.cpu_count(logical=False)) + ' / ' + str(psutil.cpu_count()),
'pidCound': len(psutil.pids())
}

def getUseInfo(self):
'''
获取CPU使用数据
'''
total_cpu = psutil.cpu_times().user+psutil.cpu_times().idle
user_cpu = psutil.cpu_times().user
mem = psutil.virtual_memory()
return {
'cpu': round(user_cpu / total_cpu * 100, 2),
'memory': round(mem.used / float(mem.total) * 100, 2),
'disk': round(psutil.disk_usage('/').used / float(psutil.disk_usage('/').total) * 100, 2),
}

def getSn(self):
'''
获取主板SN
'''
try:
if (os.name == 'nt'):
sn = os.popen('wmic bios get serialnumber')
return sn.read().split('\n\n')[1].rstrip().replace('\n', '')

output = os.popen('/usr/sbin/dmidecode | grep \'Serial Number\' | head -n 1')
return output.read().split(': ')[1].rstrip().replace('\n', '')
except:
return ''

'''
运行
'''
pusher = pusher('domain', 8083)
pusher.setData(pusher.getData().encode(encoding='utf-8'))
print (pusher.pushed())