vspheremonitor 0.0.7 → 0.1.0

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.
data/bin/vspheremonitor CHANGED
@@ -6,45 +6,57 @@ begin
6
6
  require "json"
7
7
  require "yaml"
8
8
  require 'optparse'
9
- rescue LoadError
10
- puts 'Some of the required gems have not been found'
11
- exit
9
+ require 'awesome_print'
10
+ require 'cri'
11
+ rescue LoadError => e
12
+ puts "Unable to load library: #{e}"
13
+ exit 1
12
14
  end
13
15
 
14
- options = {}
15
- OptionParser.new do |opts|
16
- opts.banner = "Usage: vspheremonitor [options]"
17
16
 
18
- opts.on("-v", "--verbose", "Be less quiet") do |v|
19
- options[:verbose] = v
20
- end
17
+ command = Cri::Command.define do
18
+ name __FILE__
19
+ usage __FILE__ + " [options]"
20
+ summary 'Collect metrics from VMware vSphere'
21
21
 
22
- opts.on("-c", "--config FILE", "Specify the configuration file") do |conf|
23
- options[:configfile] = conf
22
+ flag :h, :help, 'show help for this command' do |value, cmd|
23
+ puts cmd.help
24
+ exit 0
24
25
  end
26
+ flag :v, :verbose, 'Be less quiet'
27
+ option :c, :conf, 'Specify the configuration file', :argument => :required
25
28
 
26
- end.parse!
29
+ run do |opts, args, cmd|
30
+ configfile = opts[:conf] || 'etc/vsphere.yaml'
27
31
 
28
- def run (options)
32
+ if opts[:verbose]
33
+ starttime = Time.now
34
+ puts "Initializing at #{starttime}"
35
+ end
29
36
 
30
- if options[:configfile].nil?
31
- configfile = 'etc/vsphere.yaml'
32
- else
33
- configfile = options[:configfile]
34
- end
37
+ puts "Loading configuration #{configfile}" if opts[:verbose]
38
+ config = YAML::load(File.read(configfile))
39
+ config[:opts] = opts
35
40
 
36
- config = YAML::load(File.read(configfile))
37
- vim = RbVmomi::VIM.connect :host => config[:host],
38
- :user => config[:user],
39
- :password => config[:password],
40
- :insecure => true
41
+ p = VSphereMonitoring.new(config)
41
42
 
42
- data = Hash.new
43
- data[:vsphere] = VSphereMonitoring.process_all_datacenters(config[:datacenters],vim)
44
- data
43
+ end
45
44
 
46
45
  end
47
46
 
48
- output = run(options)
49
- puts output.to_json
47
+ #options = {}
48
+ #OptionParser.new do |opts|
49
+ # opts.banner = "Usage: vspheremonitor [options]"
50
+ #
51
+ # opts.on("-v", "--verbose", "Be less quiet") do |v|
52
+ # options[:verbose] = v
53
+ # end
54
+ #
55
+ # opts.on("-c", "--config FILE", "Specify the configuration file") do |conf|
56
+ # options[:configfile] = conf
57
+ # end
58
+ #
59
+ #end.parse!
60
+
61
+ command.run(ARGV)
50
62
 
@@ -1,7 +1,4 @@
1
1
  ---
2
- :datacenters:
3
- - dc1
4
- - dc2
5
2
  :host: server.example.net
6
3
  :user: monitor
7
4
  :password: secret
@@ -4,10 +4,30 @@ require 'pp'
4
4
  require 'alchemist'
5
5
  require 'yaml'
6
6
 
7
- module VSphereMonitoring
8
- module_function
7
+ class VSphereMonitoring
8
+
9
+ def initialize (config)
10
+ @config = config
11
+ @verbose = config[:opts][:verbose]
12
+
13
+ speak("Building connection to vsphere")
14
+ @vim = RbVmomi::VIM.connect :host => config[:host],
15
+ :user => config[:user],
16
+ :password => config[:password],
17
+ :insecure => true
18
+
19
+ p = Hash.new
20
+ p[:vsphere] = process_all_datacenters()
21
+ puts p.to_json
22
+ end
23
+ #module_function
24
+
25
+ def speak(what)
26
+ puts what if @verbose
27
+ end
9
28
 
10
29
  def host_memory (host)
30
+ speak("gathering memory stats for #{host.name}")
11
31
  data = Hash.new
12
32
  data['total'] = host.hardware.memorySize.bytes.to.megabytes.to_f
13
33
  data['usage'] = host.summary.quickStats.overallMemoryUsage.megabytes.to_f
@@ -16,6 +36,7 @@ module VSphereMonitoring
16
36
  end
17
37
 
18
38
  def host_cpu (host)
39
+ speak("gathering cpu stats for #{host.name}")
19
40
  data = Hash.new
20
41
  data['Mhz'] = host.hardware.cpuInfo.hz / 1000 / 1000
21
42
  data['cores'] = host.hardware.cpuInfo.numCpuCores
@@ -26,6 +47,7 @@ module VSphereMonitoring
26
47
  end
27
48
 
28
49
  def host_stats (host)
50
+ speak("gathering host stats for #{host.name}")
29
51
  data = Hash.new
30
52
  data['memory'] = host_memory(host)
31
53
  data['cpu'] = host_cpu(host)
@@ -33,6 +55,7 @@ module VSphereMonitoring
33
55
  end
34
56
 
35
57
  def cluster_memory (cluster)
58
+ speak("gathering cluster memory stats for #{cluster.name}")
36
59
  data = Hash.new
37
60
  data['totalMemory'] = cluster.summary.totalMemory
38
61
  data['effectiveMemory'] = cluster.summary.effectiveMemory
@@ -40,6 +63,7 @@ module VSphereMonitoring
40
63
  end
41
64
 
42
65
  def cluster_cpu (cluster)
66
+ speak("gathering cluster cpu stats for #{cluster.name}")
43
67
  data = Hash.new
44
68
  data['totalCpu'] = cluster.summary.totalCpu
45
69
  data['numCpuCores'] = cluster.summary.numCpuCores
@@ -49,6 +73,7 @@ module VSphereMonitoring
49
73
  end
50
74
 
51
75
  def cluster_stats (cluster)
76
+ speak("gathering cluster stats for #{cluster.name}")
52
77
  data = Hash.new
53
78
  data['cpu'] = cluster_cpu(cluster)
54
79
  data['memory'] = cluster_memory(cluster)
@@ -61,6 +86,7 @@ module VSphereMonitoring
61
86
  end
62
87
 
63
88
  def datastore_stats (datastore)
89
+ speak("gathering datastore stats for #{datastore.name}")
64
90
  data = Hash.new
65
91
  datastore.RefreshDatastore
66
92
  capacity = datastore.summary.capacity
@@ -73,11 +99,14 @@ module VSphereMonitoring
73
99
  end
74
100
 
75
101
  def network_stats (network)
102
+ speak("gathering network stats for #{network.name}")
76
103
  data = Hash.new
77
104
  data
78
105
  end
79
106
 
80
107
  def process_datacenter (dc)
108
+ speak("collecting metrics for datacenter #{dc.name}")
109
+
81
110
  data = Hash.new
82
111
  data['hypers'] = Hash.new
83
112
  data['clusters'] = Hash.new
@@ -107,12 +136,14 @@ module VSphereMonitoring
107
136
  data
108
137
  end
109
138
 
110
- def process_all_datacenters (datacenterlist,vim)
139
+ def process_all_datacenters ()
140
+
141
+ rootFolder = @vim.serviceInstance.content.rootFolder
142
+ dclist = rootFolder.children.select {|d| d.class == RbVmomi::VIM::Datacenter }
111
143
 
112
144
  data = Hash.new
113
- datacenterlist.each do |datacenter|
114
- dc = vim.serviceInstance.find_datacenter datacenter
115
- data[dc.name] = VSphereMonitoring.process_datacenter(dc)
145
+ dclist.each do |datacenter|
146
+ data[datacenter.name] = process_datacenter(datacenter)
116
147
  end
117
148
  data
118
149
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vspheremonitor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-02 00:00:00.000000000 Z
12
+ date: 2012-11-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
@@ -91,8 +91,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
91
  version: '0'
92
92
  requirements: []
93
93
  rubyforge_project:
94
- rubygems_version: 1.8.24
94
+ rubygems_version: 1.8.23
95
95
  signing_key:
96
96
  specification_version: 3
97
97
  summary: A tool to get some basic statistics out of a vSphere installation.
98
98
  test_files: []
99
+ has_rdoc: