Module: Zabbix::Chart

Extended by:
ActiveSupport::Concern
Includes:
Connector
Defined in:
lib/zabbix/chart.rb

Instance Method Summary collapse

Methods included from Connector

#authenticate_with_cookie, #delete_host_monitor, #delete_trigger_monitor, #zabbix_connector, #zabbix_token

Instance Method Details

#_item_chart(url = "/chart.php", data) ⇒ Object

请求后端返回图形



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/zabbix/chart.rb', line 60

def _item_chart(url = "/chart.php", data)
  base_url = Zabbix.url
  host     = URI(base_url).host
  conn     = Faraday::Connection.new(base_url)
  # 请求接口
  conn.get(url) do |r|
    r.params.merge!(data)
    r.headers["Host"]   = host
    r.headers["Accept"] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
    r.headers["Cookie"] = zabbix_token
  end.body
end

#generate_name(itemid) ⇒ Object

生成随机名称



96
97
98
# File 'lib/zabbix/chart.rb', line 96

def generate_name(itemid)
  "#{SecureRandom.hex(8)}-#{itemid}.png"
end

#graph_chart(graphid, height = 400, width = 900, start_at = "now-6h", end_at = "now") ⇒ Object

请求生成 graph_chart 对象



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/zabbix/chart.rb', line 11

def graph_chart(graphid, height = 400, width = 900, start_at = "now-6h", end_at = "now")
  # 构造数据结构
  data = {
    from:    start_at,
    to:      end_at,
    graphid: graphid,
    height:  height,
    width:   width
  }
  # 请求后端返回图片对象
  _item_chart("/chart2.php", data)
end

#iface_chart(*itemids) ⇒ Object

接口图片



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/zabbix/chart.rb', line 25

def iface_chart(*itemids)
  # 构造数据结构
  data = {
    from:    "now-6h",
    to:      "now",
    itemids: [itemids].flatten,
    height:  400,
    width:   900
  }
  
  # 请求后端返回图片对象
  _item_chart(data)
end

#item_chart(itemid, height = 400, width = 900, start_at = "now-6h", end_at = "now") ⇒ Object

请求生成 item_chart 对象



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/zabbix/chart.rb', line 46

def item_chart(itemid, height = 400, width = 900, start_at = "now-6h", end_at = "now")
  # 构造数据结构
  data = {
    from:    start_at,
    to:      end_at,
    itemids: [itemid.to_i],
    height:  height,
    width:   width
  }
  # 请求后端返回图片对象
  _item_chart(data)
end

#save_chart_graph(itemid, height = 400, width = 900, start_at = "now-6h", end_at = "now") ⇒ Object

保存图片并返回文件名



74
75
76
77
# File 'lib/zabbix/chart.rb', line 74

def save_chart_graph(itemid, height = 400, width = 900, start_at = "now-6h", end_at = "now")
  filename = generate_name(itemid)
  save_file(filename, item_chart(itemid, height, width, start_at, end_at))
end

#save_file(filename, content) ⇒ Object

保存图片



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/zabbix/chart.rb', line 80

def save_file(filename, content)
  directory_base = "app/assets/images/chart/"

  # 生成绝对路径地址
  FileUtils.mkdir_p("#{Rails.root}/#{directory_base}") unless Dir.exist?("#{Rails.root}/#{directory_base}")
  filename.prepend(directory_base)

  # 存储数据
  File.open(filename, "wb") do |chart|
    chart.write(content)
  end

  "#{Rails.root}/#{filename}"
end

#save_iface_graph(*itemids) ⇒ Object

保存接口流量图片



40
41
42
43
# File 'lib/zabbix/chart.rb', line 40

def save_iface_graph(*itemids)
  filename = generate_name([itemids].flatten[0])
  save_file(filename, iface_chart(itemids))
end