OCS Inventory 匯出到 Google 試算表的程式是從網路上其他高手分享出來(參考資料出處),並依個人需求調整。
如要取用請注意,因程式是用 python 寫的,程式段落可能會移位導致無法正常運作。
下列程式調整SELECT電腦設備類型為Notebook寫入試算表分頁。
程式內容:
#coding=utf-8
#-*- coding: utf-8 -*-
import MySQLdb as mysqldb
import sys
import gspread
import string
def DB_return(exec_cmd):
connection = mysqldb.connect('localhost', 'ocs', 'ocspasswd', 'ocsdb',charset='utf8');
with connection:
cursor = connection.cursor()
cursor.execute(exec_cmd)
rows_info = cursor.fetchall()
return rows_info
def GSheet_write(GSpreadSheet,GDriveJSON,tab_data,data,worksheet_name):
a_list=[]
for i, element in enumerate(tab_data):
a_list.append(tab_data[i])
a_tup = tuple(a_list)
start_row = 1
start_letter = 'A'
end_len = len(data[0]) - 1
if end_len > 26:
len_info = end_len - 26
len_a='A'
else:
len_info = end_len
len_a=''
end_letter = string.uppercase[len_info]
end_letter = len_a+end_letter
end_row = len(data)
range = "%s%d:%s%d" % (start_letter, start_row+1 , end_letter, end_row+1)
range_tab = "%s%d:%s%d" % (start_letter, start_row , end_letter ,start_row)
from oauth2client.service_account import ServiceAccountCredentials as SAC
try:
scope = ['https://spreadsheets.google.com/feeds']
key = SAC.from_json_keyfile_name(GDriveJSON, scope)
gc = gspread.authorize(key)
workbook = gc.open(GSpreadSheet)
if worksheet_name in [sheet.title for sheet in workbook.worksheets()]:
workbook.del_worksheet(workbook.worksheet(worksheet_name))
wks = workbook.add_worksheet(worksheet_name,end_row+1,end_len+1)
else:
wks = workbook.add_worksheet(worksheet_name,end_row+1,end_len+1)
except Exception as ex:
print('connect google fail ', ex)
sys.exit(1)
cell_list = wks.range(range)
try:
idx = 0
for (start_row , rowlist) in enumerate(data):
for (colnum, value) in enumerate(rowlist):
cell_list[idx].value = value
idx += 1
if idx >= len(cell_list):
break
wks.update_cells(cell_list)
except:
print "Exception"
cell_list_tab = wks.range(range_tab)
try:
idx = 0
for (start_row , value) in enumerate(a_tup):
cell_list_tab[idx].value = value
idx += 1
if idx >= len(cell_list_tab):
break
wks.update_cells(cell_list_tab)
except:
print "Exception"
GDriveJSON = 'GoogleAuthKey.json'
GSpreadSheet = '試算表檔名'
TYPE = 'Notebook'
rows_item = ['HardwareID', 'UserName', 'Computer', 'Type', 'CPU', 'RAM(MB)', 'Disk(MB)', 'Monitor']
rows_accest = DB_return("SELECT DISTINCT A.HARDWARE_ID,A.TAG,H.NAME,B.TYPE,H.PROCESSORT,H.MEMORY,S.DISKSIZE,M.CAPTION \
from \
accountinfo as A \
join hardware as H \
join monitors as M \
join storages as S \
join softwares as SF \
join bios as B \
where \
A.HARDWARE_ID=H.ID \
and A.HARDWARE_ID=S.HARDWARE_ID \
and A.HARDWARE_ID=B.HARDWARE_ID \
and S.TYPE='Fixed hard disk media' \
and A.HARDWARE_ID=M.HARDWARE_ID \
and A.HARDWARE_ID=SF.HARDWARE_ID \
and A.HARDWARE_ID=H.ID \
and A.TAG not like '%報廢%' \
and A.TAG not like '%出售%' \
and B.TYPE='" + TYPE + "' GROUP BY A.TAG")
GSheet_write(GSpreadSheet,GDriveJSON,rows_item,rows_accest,TYPE)
訂閱:
張貼留言 (Atom)
Nutanix平台虛擬機(UBUNTU),利用Veeam備份移轉至VMware平台,安裝套件、系統更新出現錯誤
mount: /var/lib/grub/esp: special device /dev/disk/by-id/scsi-SNUTANIX_VDISK_NFS_4_0_7672_2d41cbaa_025e_4fac_849c_9e620eff5bff-part1 does n...
-
一、建立新的LVM磁區(建立順序:PV、VG、LV) 使用 LVM 可在分割磁區時不必一次分割大量空間給各個 Partition,等日後哪個磁區空間不足時再分配之前未分配的空間或新增一顆硬碟來配給使用。 確認是否有安裝 LVM 套件: rpm -qa lvm2 ...
-
指令模式匯出 OVA: 使用指令模式需要到 VMWare 官網下載 VMWare Tool: https://my.vmware.com/web/vmware/details?downloadGroup=OVFTOOL400&productId=353 下載安裝...
-
之前 Windows 的遠端桌面連線工具使用上沒有什麼問題,, 某一天它開始出現<認證無效>這個訊息了!!! 請出 Google 尋找原因及解決方法: 原因: 連線無法用Credential Security Support Provider (Cre...
沒有留言:
張貼留言