Background requirements are simple, analyze the memory usage of all devices, see if the memory of those devices is not enough to add memory ...
The following script logic is extracted itemid by extracting the HostID in the Zabbix database:
Then extract the trend data through the item name filter to get the maximum total memory and the minimum available memory for the day
Then in the calculation of the minimum memory availability in a day device,
The following is calculated by the free, of course, can also be calculated by used ...
#!/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*24host.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 memory/ 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 '] =~ /available memory/ 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 ']} minimum idle usage for the most recent day: #{format ("%.2f ", usage)} total: #{total} free: #{free} " endend
This article is from the "Xiaofeng Moon" blog, make sure to keep this source http://kinda22.blog.51cto.com/2969503/1585410
Extract memory data from Zabbix database and do memory utilization calculation