import
MySQLdb
import
time,datetime
zdbhost
=
'172.16.8.200'
zdbuser
=
'zabbix'
zdbpass
=
'zabbixreport'
zdbport
=
3306
zdbname
=
'zabbix'
xlsfilename
=
'damo.xls'
keys
=
[
[
'CPU核心数'
,
'trends_uint'
,
'system.cpu.num'
,
'avg'
,'',
1
],
[
'CPU平均空闲值'
,
'trends'
,
'system.cpu.util[,idle]'
,
'avg'
,
'%.2f'
,
1
],
[
'CPU最小空闲值'
,
'trends'
,
'system.cpu.util[,idle]'
,
'min'
,
'%.2f'
,
1
],
[
'CPU5分钟负载'
,
'trends'
,
'system.cpu.load[percpu,avg5]'
,
'avg'
,
'%.2f'
,
1
],
[
'物理内存大小(单位G)'
,
'trends_uint'
,
'vm.memory.size[total]'
,
'avg'
,'',
1048576000
],
[
'可用平均内存(单位G)'
,
'trends_uint'
,
'vm.memory.size[available]'
,
'avg'
,'',
1048576000
],
[
'可用最小内存(单位G)'
,
'trends_uint'
,
'vm.memory.size[available]'
,
'min'
,'',
1048576000
],
[
'swap总大小(单位G)'
,
'trends_uint'
,
'system.swap.size[,total]'
,
'avg'
,'',
1048576000
],
[
'swap平均剩余(单位G)'
,
'trends_uint'
,
'system.swap.size[,free]'
,
'avg'
,'',
1048576000
],
[
'根分区总大小(单位G)'
,
'trends_uint'
,
'vfs.fs.size[/,total]'
,
'avg'
,'',
1073741824
],
[
'根分区平均剩余(单位G)'
,
'trends_uint'
,
'vfs.fs.size[/,free]'
,
'avg'
,'',
1073741824
],
[
'进入最大流量(单位Kbps)'
,
'trends_uint'
,
'net.if.in[eth0]'
,
'max'
,'',
1000
],
[
'进入平均流量(单位Kbps)'
,
'trends_uint'
,
'net.if.in[eth0]'
,
'avg'
,'',
1000
],
[
'出去最大流量(单位Kbps)'
,
'trends_uint'
,
'net.if.out[eth0]'
,
'max'
,'',
1000
],
[
'出去平均流量(单位Kbps)'
,
'trends_uint'
,
'net.if.out[eth0]'
,
'avg'
,'',
1000
],
]
class
ReportForm:
def
__init__(
self
):
self
.conn
=
MySQLdb.connect(host
=
zdbhost,user
=
zdbuser,passwd
=
zdbpass,port
=
zdbport,db
=
zdbname)
self
.cursor
=
self
.conn.cursor(cursorclass
=
MySQLdb.cursors.DictCursor)
self
.groupname
=
'qjsh'
self
.IpInfoList
=
self
.__getHostList()
def
__getHostList(
self
):
sql
=
%
self
.groupname
self
.cursor.execute(sql)
groupid
=
self
.cursor.fetchone()[
'groupid'
]
sql
=
%
groupid
self
.cursor.execute(sql)
hostlist
=
self
.cursor.fetchall()
IpInfoList
=
{}
for
i
in
hostlist:
hostid
=
i[
'hostid'
]
sql
=
%
hostid
ret
=
self
.cursor.execute(sql)
if
ret:
IpInfoList[
self
.cursor.fetchone()[
'host'
]]
=
{
'hostid'
:hostid}
return
IpInfoList
def
__getItemid(
self
,hostid,itemname):
sql
=
%
(hostid, itemname)
if
self
.cursor.execute(sql):
itemid
=
self
.cursor.fetchone()[
'itemid'
]
else
:
itemid
=
None
return
itemid
def
getTrendsValue(
self
,
type
, itemid, start_time, stop_time):
sql
=
%
(
type
,
type
, itemid, start_time, stop_time)
self
.cursor.execute(sql)
result
=
self
.cursor.fetchone()[
'result'
]
if
result
=
=
None
:
result
=
0
return
result
def
getTrends_uintValue(
self
,
type
, itemid, start_time, stop_time):
sql
=
%
(
type
,
type
, itemid, start_time, stop_time)
self
.cursor.execute(sql)
result
=
self
.cursor.fetchone()[
'result'
]
if
result:
result
=
int
(result)
else
:
result
=
0
return
result
def
getLastMonthData(
self
,
type
,hostid,table,itemname):
ts_first
=
int
(time.mktime(datetime.date(datetime.date.today().year,datetime.date.today().month
-
1
,
1
).timetuple()))
lst_last
=
datetime.date(datetime.date.today().year,datetime.date.today().month,
1
)
-
datetime.timedelta(
1
)
ts_last
=
int
(time.mktime(lst_last.timetuple()))
itemid
=
self
.__getItemid(hostid, itemname)
function
=
getattr
(
self
,
'get%sValue'
%
table.capitalize())
return
function(
type
,itemid, ts_first, ts_last)
def
getInfo(
self
):
for
ip,resultdict
in
zabbix.IpInfoList.items():
print
"正在查询 IP:%-15s hostid:%5d 的信息!"
%
(ip, resultdict[
'hostid'
])
for
value
in
keys:
print
"\t正在统计 key_:%s"
%
value[
2
]
if
not
value[
2
]
in
zabbix.IpInfoList[ip]:
zabbix.IpInfoList[ip][value[
2
]]
=
{}
data
=
zabbix.getLastMonthData(value[
3
], resultdict[
'hostid'
],value[
1
],value[
2
])
zabbix.IpInfoList[ip][value[
2
]][value[
3
]]
=
data
def
writeToXls2(
self
):
try
:
import
xlsxwriter
workbook
=
xlsxwriter.Workbook(xlsfilename)
worksheet
=
workbook.add_worksheet()
worksheet.write(
0
,
0
,
"主机"
.decode(
'utf-8'
))
i
=
1
for
ip
in
self
.IpInfoList:
worksheet.write(i,
0
,ip)
i
=
i
+
1
i
=
1
for
value
in
keys:
worksheet.write(
0
,i,value[
0
].decode(
'utf-8'
))
j
=
1
for
ip,result
in
self
.IpInfoList.items():
if
value[
4
]:
worksheet.write(j,i, value[
4
]
%
result[value[
2
]][value[
3
]])
else
:
worksheet.write(j,i, result[value[
2
]][value[
3
]]
/
value[
5
])
j
=
j
+
1
i
=
i
+
1
except
Exception,e:
print
e
def
__del__(
self
):
self
.cursor.close()
self
.conn.close()
if
__name__
=
=
"__main__"
:
zabbix
=
ReportForm()
zabbix.getInfo()
zabbix.writeToXls2()