Extract memory data from Zabbix database and do memory utilization calculation

Source: Internet
Author: User
Tags memory usage

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.