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
| #!/usr/bin/ruby
$KCODE = 'utf8'
require 'mysql'
db = Mysql.real_connect('1.1.1.1','zabbix','abbix','zabbix',3389)
db.query('SET character_set_results = utf8 ;')
host = db.query("select hostid,host from hosts where host like '%sinanode.com%'")
time = Time.now - 3600*24
host.each_hash do |h|
items = db.query("select itemid,name from items where hostid = #{h['hostid']} ")
total = 0
free = 0
items.each_hash do |item|
if item['name'] =~ /内存总数/
total = db.query("select min(value_max) from trends_uint where itemid = '#{item['itemid']}' and clock > UNIX_TIMESTAMP('#{time.strftime('%F %X')}'); ").fetch_row
puts "#{h['host']} #{item['itemid']} #{item['name']} #{total}"
end
if item['name'] =~ /可用内存/
free = db.query("select min(value_max) from trends_uint where itemid = '#{item['itemid']}' and clock > UNIX_TIMESTAMP('#{time.strftime('%F %X')}'); ").fetch_row
puts "#{h['host']} #{item['itemid']} #{item['name']} #{free}"
end
end
if free != 0
usage = free.to_s.to_f/total.to_s.to_f*100
puts "#{h['host']} 最近一天最小空闲使用率: #{format("%.2f",usage)} total: #{total} free: #{free}"
end
end
|