-
Notifications
You must be signed in to change notification settings - Fork 7
/
dell_host_model.py
194 lines (167 loc) · 6 KB
/
dell_host_model.py
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
#!/usr/local/python27/bin/python2
# -*- coding: utf-8 -*
import time
import sys
import os
import commands
import json
class DellHostModel:
__ip = ''
__username = ''
__password = ''
__brand = ''
__model = '' # 型号
__sn = '' # 设备序列号
__cpu = {
"model": [],
"num": 0
}
__memory = {
"num": 0,
"MemoryStickItems": []
}
__disk = {
"num": 0,
"diskList": []
}
__raid = {
"model": '' # 阵列卡型号
}
# 暂无 命令
__hba = {
}
# 暂无
__power = {
}
# 远程管理卡
__remoteManageCard = {
"name": ''
}
__networkCard = {
"num": 0,
"model": []
}
def __init__(self, ip, username, password):
self.__username = username
self.__password = password
self.__ip = ip
def execComand(self,command, bJson=True):
data = commands.getoutput(command)
if bJson == False:
return data
data = data.replace('"', '')
arr = data.split("\n")
return json.dumps(arr)
def getBrand(self):
command = 'snmpwalk -v 2c -c public %s 1.3.6.1.4.1.674.10892.5.1.1.4.0 | cut -d : -f4 |cut -b 3-' % (self.__ip)
self.__brand = self.execComand(command, False)
self.__brand = self.__brand.replace("\"",'')
def getModel(self):
command = 'snmpwalk -v 2c -c public %s 1.3.6.1.4.1.674.10892.5.4.300.10.1.9.1 |cut -d : -f4 |cut -b 3-' % (self.__ip)
self.__model = self.execComand(command, False)
self.__model = self.__model.replace("\"", '')
def getSn(self):
command = 'snmpwalk -v 2c -c public %s 1.3.6.1.4.1.674.10892.2.1.1.11.0 |cut -d : -f4 |cut -b 3-' % (self.__ip)
self.__sn = self.execComand(command, False)
self.__sn = self.__sn.replace("\"", '')
def getCpu(self):
modelCommand = 'snmpwalk -c public -v 2c %s 1.3.6.1.4.1.674.10892.5.4.1100.30.1.23.1 |cut -d : -f4 |cut -b 3-' % (
self.__ip)
ret = self.execComand(modelCommand, False)
lines = ret.split("\n")
if not lines:
return
for line in lines:
self.__cpu['num'] += 1
self.__cpu['model'].append(line.replace("\"", ''))
def getMemory(self):
command = 'snmpwalk -v 2c -c public %s 1.3.6.1.4.1.674.10892.5.4.1100.50.1.14 |cut -d : -f4 |cut -b 2-' % (self.__ip)
ret = self.execComand(command, False)
lines = ret.split("\n")
if not lines:
return
for line in lines:
self.__memory['num'] += 1
self.__memory['MemoryStickItems'].append(line)
def getDisk(self):
command = 'snmpwalk -v 2c -c public %s 1.3.6.1.4.1.674.10892.5.5.1.20.130.4.1.11 |cut -d : -f4 |cut -b 2-' % (self.__ip)
ret = self.execComand(command, False)
lines = ret.split("\n")
if not lines:
return
for line in lines:
self.__disk['num'] += 1
self.__disk['diskList'].append(line)
def getRemoteManageCard(self):
command = 'snmpwalk -v 2c -c public %s 1.3.6.1.4.1.674.10892.5.4.300.60.1.8.1.1 | cut -d : -f4|cut -b 3-' % (self.__ip)
self.__remoteManageCard['name'] = self.execComand(command, False)
self.__remoteManageCard['name'] = self.__remoteManageCard['name'].replace("\"", '')
def getRaid(self):
command = 'snmpwalk -v 2c -c public %s 1.3.6.1.4.1.674.10892.5.5.1.20.130.1.1.2 |cut -d : -f4|cut -b 3-' % (self.__ip)
self.__raid['model'] = self.execComand(command, False)
self.__raid['model'] = self.__raid['model'].replace("\"", '')
def getNetworkCard(self):
command = 'snmpwalk -v 2c -c public %s 1.3.6.1.4.1.674.10892.5.4.1100.90.1.6 |cut -d : -f4 |cut -d - -f1 |cut -b 3-' % (self.__ip)
ret = self.execComand(command, False)
lines = ret.split("\n")
if not lines:
return
for line in lines:
self.__networkCard['num'] += 1
self.__networkCard['model'].append(line)
def getAllInfo(self):
try:
self.getBrand()
self.getModel()
self.getCpu()
self.getSn()
self.getMemory()
self.getDisk()
self.getRaid()
self.getNetworkCard()
self.getRemoteManageCard()
result = {}
result['brand'] = self.__brand
result['model'] = self.__model
result['sn'] = self.__sn
result['cpu'] = self.__cpu
result['memory'] = self.__memory
result['disk'] = self.__disk
result['raid'] = self.__raid
result['hba'] = self.__hba
result['networkCard'] = self.__networkCard
result['power'] = self.__power
result['remoteManageCard'] = self.__remoteManageCard
# print json.dumps(result)
tmpJson = json.dumps(result)
totalLen = len(tmpJson)
readLen = 0
while readLen < totalLen:
if readLen + 4096 < totalLen:
sys.stdout.write(tmpJson[readLen:readLen + 4096])
readLen += 4096
else:
sys.stdout.write(tmpJson[readLen:])
readLen = totalLen
sys.stdout.flush()
time.sleep(0.1)
except Exception, e:
msg = str(e).encode('utf-8')
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
msg += "DellHostModel.get_all_info execute exception\r\n"
info = (exc_type, fname, exc_tb.tb_lineno)
msg += str(info)
msg += str(info)
print msg
if __name__ == '__main__':
ip = sys.argv[1]
if len(sys.argv) == 3:
username = sys.argv[2]
password = sys.argv[3]
else:
username = ''
password = ''
DellHostModel = DellHostModel(ip, username, password)
DellHostModel.getAllInfo()
os._exit(0)