fdwe2e 发表于 2014-12-3 08:48:44

从Zabbix数据库中提取内存采集的数据,做内存使用率计算

背景需求很简单,分析所有的设备的内存使用率,看那些设备的内存不够用是否需要加内存。。。下面的脚本逻辑,就是通过提取zabbix数据库中的hostid,在提取itemid。。
然后通过item name过滤提取趋势数据,获取一天中最大的内存总数和最小可用内存
然后在计算在一天中最小内存可用率的设备,

下面的是通过free来计算的,当然也可以通过used来计算了...

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



页: [1]
查看完整版本: 从Zabbix数据库中提取内存采集的数据,做内存使用率计算