zabbix_graph 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0381d0fbc2dd4ef15d01c46370ae50ec9d8405f9
4
- data.tar.gz: 28fc072e6e504120877680cd6e2fb27106c710f1
3
+ metadata.gz: c03d15e5d41ea5ddcd6169c0a4306711d539688e
4
+ data.tar.gz: d44f97c90120f97b536e4063dda69c3586457e3e
5
5
  SHA512:
6
- metadata.gz: 506332bb3df617d3d618f0b0f482e785e92e1b39ae97dd97ef6990ace9bc3fca440fd9b2de96eef064d456ff0e0e40fac9f0fe6c6d64f75bdb5fb1e6c5cd12b1
7
- data.tar.gz: b4132ae8cdc5d07872fcc8e66f4ff5f4ed3916c9ac48fd5ab91e974ad0bcf2e9755736b6cbde1a21c5f58e8d8e7f177880e79cefac9f6ab57a168c85be55ca14
6
+ metadata.gz: 8556bd51e3b65b5e67122896ac22bc3b8efcaa56d2f3fea8ee5afe3eb53d8f9b4c7f4c9364a2fe3546633d3a68c6fa24280c98b77460bd404603ca96048d70e8
7
+ data.tar.gz: 8b85e0e2cbc2797728e2302dcd49e090a2b1f7c1e5907b13e031a2d2705118dbc3bd8c7494ef39ee6dc0564b79e2a6ddbf167a08a4794423e14bb14454dc6835
data/README.md CHANGED
@@ -1,26 +1,19 @@
1
1
  # ZabbixGraph
2
2
 
3
- TODO: Write a gem description
3
+ Select hosts and items with peco, and open adhoc graph.
4
4
 
5
5
  ## Installation
6
6
 
7
- Add this line to your application's Gemfile:
8
-
9
- ```ruby
10
- gem 'zabbix_graph'
11
- ```
12
-
13
- And then execute:
14
-
15
- $ bundle
16
-
17
- Or install it yourself as:
18
-
19
7
  $ gem install zabbix_graph
20
8
 
21
9
  ## Usage
22
10
 
23
- TODO: Write usage instructions here
11
+ ```
12
+ $ export ZABBIX_URL=https://your-zabbix.example.com
13
+ $ export ZABBIX_USER=...
14
+ $ export ZABBIX_PASSWORD=...
15
+ $ zabbix_graph
16
+ ```
24
17
 
25
18
  ## Contributing
26
19
 
@@ -1,3 +1,3 @@
1
1
  module ZabbixGraph
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/zabbix_graph.rb CHANGED
@@ -1,18 +1,34 @@
1
1
  require "zabbix_graph/version"
2
2
  require "zabbixapi"
3
3
  require "peco_selector"
4
+ require "optparse"
4
5
 
5
6
  module ZabbixGraph
6
7
  class CLI
7
8
  def self.start(argv)
8
- Opener.new.select_and_open
9
+ Opener.new(parse_argv(argv)).select_and_open
10
+ end
11
+
12
+ def self.parse_argv(argv)
13
+ options = {}
14
+
15
+ parser = OptionParser.new
16
+ parser.on('--host-graph') { options[:host_graph] = true }
17
+ parser.on('--item-graph') { options[:item_graph] = true }
18
+ parser.parse!(argv)
19
+
20
+ options
9
21
  end
10
22
  end
11
23
 
12
24
  class Opener
25
+ def initialize(options)
26
+ @options = options
27
+ @all_hosts = zbx.hosts.get({})
28
+ end
29
+
13
30
  def select_and_open
14
- hosts = zbx.hosts.get({})
15
- hosts = PecoSelector.select_from(hosts.sort_by do |h|
31
+ hosts = PecoSelector.select_from(@all_hosts.sort_by do |h|
16
32
  h['host']
17
33
  end.map do |h|
18
34
  [h['host'], h]
@@ -37,17 +53,85 @@ module ZabbixGraph
37
53
  end
38
54
  end
39
55
 
40
- query = [['action', 'batchgraph'], ['graphtype', '0']]
41
- selected_items.each do |i|
42
- query << ['itemids[]', i['itemid']]
56
+ if @options[:host_graph]
57
+ open_host_graph(selected_items)
58
+ elsif @options[:item_graph]
59
+ open_item_graph(selected_items)
60
+ else
61
+ open_history(selected_items)
43
62
  end
63
+ end
44
64
 
45
- url = URI.join(zabbix_url, "/history.php?#{URI.encode_www_form(query)}")
65
+ private
66
+
67
+ def open_history(items)
68
+ url = URI.join(zabbix_url, "/history.php?#{query_string_from_items(items)}")
46
69
 
47
70
  system 'open', url.to_s
48
71
  end
49
72
 
50
- private
73
+ def open_host_graph(items)
74
+ host_items = items.group_by do |i|
75
+ i['hostid']
76
+ end
77
+
78
+ grouped_items = host_items.map do |hostid, items|
79
+ host = @all_hosts.find {|h| h['hostid'] == hostid }
80
+ [host['name'], items]
81
+ end.sort_by do |hostname, _|
82
+ hostname
83
+ end
84
+
85
+ open_grouped_items(grouped_items)
86
+ end
87
+
88
+ def open_item_graph(items)
89
+ key_items = items.group_by do |i|
90
+ [i['name'], i['key_']]
91
+ end
92
+
93
+ grouped_items = key_items.sort_by do |name_key, _|
94
+ name_key.join
95
+ end.map do |name_key, items|
96
+ ["#{name_key[0]} (#{name_key[1]})", items]
97
+ end
98
+
99
+ open_grouped_items(grouped_items)
100
+ end
101
+
102
+ def open_grouped_items(grouped_items)
103
+ html = ""
104
+ grouped_items.each do |name, items|
105
+ src = URI.join(zabbix_url, "/chart.php?#{query_string_from_items(items)}").to_s
106
+ html << "<div>"
107
+ html << "<h2>" << name << "</h2>"
108
+ html << %{<img src="#{src}">}
109
+ html << "</div>"
110
+ end
111
+
112
+ path = File.join(temp_html_dir, "#{Time.now.to_f.to_s}.html")
113
+ open(path, 'w') do |f|
114
+ f.write html
115
+ end
116
+
117
+ system 'open', path
118
+ end
119
+
120
+ def temp_html_dir
121
+ dir = "/tmp/zabbix_graph"
122
+ FileUtils.mkdir_p(dir)
123
+
124
+ dir
125
+ end
126
+
127
+ def query_string_from_items(items)
128
+ query = [['action', 'batchgraph'], ['graphtype', '0']]
129
+ items.each do |i|
130
+ query << ['itemids[]', i['itemid']]
131
+ end
132
+
133
+ URI.encode_www_form(query)
134
+ end
51
135
 
52
136
  def zbx
53
137
  @zbx ||= ZabbixApi.connect(
data/zabbix_graph.gemspec CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.require_paths = ["lib"]
19
19
 
20
20
  spec.add_dependency "zabbixapi", "~> 2.4.2"
21
- spec.add_dependency "peco_selector"
21
+ spec.add_dependency "peco_selector", "~> 0.0.3"
22
22
 
23
23
  spec.add_development_dependency "bundler", "~> 1.7"
24
24
  spec.add_development_dependency "rake", "~> 10.0"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zabbix_graph
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryota Arai
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-10 00:00:00.000000000 Z
11
+ date: 2015-03-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: zabbixapi
@@ -28,16 +28,16 @@ dependencies:
28
28
  name: peco_selector
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: 0.0.3
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: 0.0.3
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -103,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
103
  version: '0'
104
104
  requirements: []
105
105
  rubyforge_project:
106
- rubygems_version: 2.2.2
106
+ rubygems_version: 2.4.5
107
107
  signing_key:
108
108
  specification_version: 4
109
109
  summary: Open Zabbix adhoc graph