#coding:utf8
#FileName=hd.py
import os
import codecs
from openpyxl.workbook import Workbook
from openpyxl.writer.excel import ExcelWriter
from openpyxl.cell import get_column_letter
from openpyxl.cell import Cell
from openpyxl import Workbook
from openpyxl import load_workbook
import subprocess
#读取数据收集文件
def readFile(fileUrl):
"""Read the file and return the file content"""
try:
#unicode file
fileObject = codecs.open(fileUrl,'r',encoding='utf-16')
except unicodeDecodeError:
print "Pls check the encoding for hd.txt whether [unicode]"
else:
print("Unspecified Error,Pls call Jmilk")
try:
fileContent = fileObject.readlines()
finally:
fileObject.close()
return fileContent
#数据分类函数
def getNodeCountList(readLines):
"""Get the different node status type and change the global variable"""
i = 0
for line in readLines:
lineItem = line.split(':')
if lineItem[0] == '---- \r\n':
i += 1
continue
if lineItem[0] == ' Node State':
if lineItem[1] == ' Ready count':
global ReadyCount
ReadyCount[i-1] = int(lineItem[2])
if lineItem[1] == ' OutForRepair count':
global OutForRepairCount
OutForRepairCount[i-1] = int(lineItem[2])
if lineItem[1] == ' HumanInvestigate count':
global HumanInvestigateCount
HumanInvestigateCount[i-1] = int(lineItem[2])
#生成Excel表格
def createTable():
"""Create the HI‘s & OFR nodes status table"""
wb = Workbook()
ws = wb.worksheets[0]
ws.title = u"NodeCount"
for i in list(range(1,26)):
ws.cell("A"+str(i)).value = '%s' % (cluster[i-1])
ws.cell("B"+str(i)).value = '%s' % (HumanInvestigateCount[i-1])
ws.cell("C"+str(i)).value = '%s' % (OutForRepairCount[i-1])
ws.cell("D"+str(i)).value = '%s' % (ReadyCount[i-1])
ws.cell("E"+str(i)).value = '%.2f%s' %((float(HumanInvestigateCount[i-1])/(HumanInvestigateCount[i-1]+OutForRepairCount[i-1]+ReadyCount[i-1]))*100,'%')
wb.save("Hd.xlsx")
#Python call powershell 使用powershell实现发送数据处理邮件
def python_call_powershell(bodyStr):
args = [r"C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe","-ExecutionPolicy","Unrestricted",r"E:\Users\userName\Desktop\SendMail.ps1",str(bodyStr)]
ps = subprocess.Popen(args,stdout=subprocess.PIPE)
psReturn = ps.stdout.read()
return psReturn
if __name__ == '__main__':
#Change to your user name
user = 'userName'
cluster = []
ReadyCount = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
OutForRepairCount = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
HumanInvestigateCount = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
percentage = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
fileUrl = 'E:\\Users\\' + user + '\\Desktop\\hd.txt'
if os.path.exists(fileUrl):
readContent = readFile(fileUrl)
getNodeCountList(readContent)
#createTable()
else:
print('Not exist the file!')
for i in list(range(0,24)):
percentage[i] = '%.2f%s' % ((float(HumanInvestigateCount[i])/(HumanInvestigateCount[i]+OutForRepairCount[i]+ReadyCount[i]))*100,'%')
bodyStr = [x for li in [cluster,HumanInvestigateCount,OutForRepairCount,ReadyCount,percentage] for x in li]
for index in list(range(0,24)):
print cluster[index]+'\t',str(HumanInvestigateCount[index])+'\t',str(OutForRepairCount[index])+'\t',str(ReadyCount[index])+'\t'+percentage[index]
print bodyStr
callResult = python_call_powershell(bodyStr)
print callResult