zabcon 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/libs/zdebug.rb ADDED
@@ -0,0 +1,163 @@
1
+ #License:: LGPL 2.1 http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
2
+ #Copyright:: Copyright (C) 2009,2010 Andrew Nelson nelsonab(at)red-tux(dot)net
3
+ #
4
+ #This library is free software; you can redistribute it and/or
5
+ #modify it under the terms of the GNU Lesser General Public
6
+ #License as published by the Free Software Foundation; either
7
+ #version 2.1 of the License, or (at your option) any later version.
8
+ #
9
+ #This library is distributed in the hope that it will be useful,
10
+ #but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
+ #Lesser General Public License for more details.
13
+ #
14
+ #You should have received a copy of the GNU Lesser General Public
15
+ #License along with this library; if not, write to the Free Software
16
+ #Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
+
18
+ ##########################################
19
+ # Subversion information
20
+ # $Id: zdebug.rb 222 2010-10-07 14:25:08Z nelsonab $
21
+ # $Revision: 222 $
22
+ ##########################################
23
+
24
+ module ZDebug
25
+
26
+ # Either set_debug_level or set_facility_debug_level must be called before
27
+ # the debug functions can be used.
28
+ def set_debug_level(level) # sets the current debug level for printing messages
29
+ @@debug_level=level
30
+
31
+ # Create faciltiy_level if it's not created already
32
+ @@facility_level= {} if !defined?(@@facility_level)
33
+ end
34
+
35
+ # facility is a symbol, level is an integer
36
+ def set_facility_debug_level(facility,level)
37
+ # Create faciltiy_level if it's not created already
38
+ @@facility_level= {} if !defined?(@@facility_level)
39
+
40
+ @@facility_level[facility]=level
41
+
42
+ # Create debug level if it's not already created
43
+ @@debug_level=0 if !defined?(@@debug_level)
44
+ end
45
+
46
+ def debug_level
47
+ @@debug_level
48
+ end
49
+
50
+ # level - level to show message (Integer)
51
+ # variable - variable to show (Object)
52
+ # message - message to be prepended before variable (String)
53
+ # overload - do show or error if debug_level is not set
54
+ def debug(level,variable="",message=nil,truncate=nil,overload=false)
55
+ return if overload
56
+ raise "Call set_debug before using debug" if !defined?(@@debug_level)
57
+ if level<=@@debug_level
58
+ #parse the caller array to determine who called us, what line, and what file
59
+ caller[0]=~/(.*):(\d+):.*`(.*?)'/
60
+
61
+ #sometimes the debug function gets called from within an exception block, in which cases the backtrace is not
62
+ #available.
63
+ file_tmp=$1.nil? ? "n/a" : $1
64
+ debug_line=$2.nil? ? "" : $2
65
+ debug_func=$3.nil? ? "" : $3
66
+ tmp_split=file_tmp.split("/")
67
+
68
+ if (len=tmp_split.length)>2
69
+ debug_file=".../#{tmp_split[len-2]}/#{tmp_split[len-1]}"
70
+ else
71
+ debug_file=file_tmp
72
+ end
73
+
74
+ strval=""
75
+ if variable.nil?
76
+ strval="nil"
77
+ elsif variable.class==String
78
+ strval=variable
79
+ if !truncate.nil?
80
+ if truncate<strval.length then
81
+ o_strval=strval
82
+ strval=o_strval[0..(truncate/2)]
83
+ strval+= " ..... "
84
+ strval+=o_strval[(o_strval.length-(truncate/2))..o_strval.length]
85
+ end
86
+ end
87
+ else
88
+ strval=variable.inspect
89
+ if !truncate.nil?
90
+ if truncate<strval.length then
91
+ o_strval=strval
92
+ strval=o_strval[0..(truncate/2)]
93
+ strval+= " ..... "
94
+ strval+=o_strval[(o_strval.length-(truncate/2))..o_strval.length]
95
+ end
96
+ end
97
+ end
98
+
99
+ if !message.nil?
100
+ strval = message + ": " + strval
101
+ end
102
+ puts "D#{level} #{debug_file}:#{debug_func}:#{debug_line} #{strval}"
103
+ end
104
+ end
105
+
106
+ # Debug_facility is a copy of the above function in an effort to shorten
107
+ # code path.
108
+ # facility - symbol denoting logging facility
109
+ # level - level to show message (Integer)
110
+ # variable - variable to show (Object)
111
+ # message - message to be prepended before variable (String)
112
+ def debug_facility(facility,level,variable="",message=nil,truncate=nil)
113
+ facility_level=@@facility_level[facility]
114
+ raise "Call set_debug before using debug" if !defined?(@@debug_level)
115
+ raise "Unknown facility type: #{facility.to_s}" if facility_level.nil?
116
+ if level<=facility_level
117
+ #parse the caller array to determine who called us, what line, and what file
118
+ caller[0]=~/(.*):(\d+):.*`(.*?)'/
119
+
120
+ file_tmp=$1
121
+ debug_line=$2
122
+ debug_func=$3
123
+ tmp_split=file_tmp.split("/")
124
+
125
+ if (len=tmp_split.length)>2
126
+ debug_file=".../#{tmp_split[len-2]}/#{tmp_split[len-1]}"
127
+ else
128
+ debug_file=file_tmp
129
+ end
130
+
131
+ strval=""
132
+ if variable.nil?
133
+ strval="nil"
134
+ elsif variable.class==String
135
+ strval=variable
136
+ if !truncate.nil?
137
+ if truncate<strval.length then
138
+ o_strval=strval
139
+ strval=o_strval[0..(truncate/2)]
140
+ strval+= " ..... "
141
+ strval+=o_strval[(o_strval.length-(truncate/2))..o_strval.length]
142
+ end
143
+ end
144
+ else
145
+ strval=variable.inspect
146
+ if !truncate.nil?
147
+ if truncate<strval.length then
148
+ o_strval=strval
149
+ strval=o_strval[0..(truncate/2)]
150
+ strval+= " ..... "
151
+ strval+=o_strval[(o_strval.length-(truncate/2))..o_strval.length]
152
+ end
153
+ end
154
+ end
155
+
156
+ if !message.nil?
157
+ strval = message + ": " + strval
158
+ end
159
+ puts "D#{level}(#{facility.to_s}) #{debug_file}:#{debug_func}:#{debug_line} #{strval}"
160
+ end
161
+ end
162
+
163
+ end # end Debug module
@@ -0,0 +1,6 @@
1
+ server=http://localhost
2
+ username=admin
3
+ password=zabbix
4
+ lines=24
5
+ debug=0
6
+ language=english
data/zabcon.rb ADDED
@@ -0,0 +1,236 @@
1
+ #!/usr/bin/ruby
2
+
3
+ #GPL 2.0 http://www.gnu.org/licenses/gpl-2.0.html
4
+ #Zabbix CLI Tool and associated files
5
+ #Copyright (C) 2009,2010 Andrew Nelson nelsonab(at)red-tux(dot)net
6
+ #
7
+ #This program is free software; you can redistribute it and/or
8
+ #modify it under the terms of the GNU General Public License
9
+ #as published by the Free Software Foundation; either version 2
10
+ #of the License, or (at your option) any later version.
11
+ #
12
+ #This program is distributed in the hope that it will be useful,
13
+ #but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ #GNU General Public License for more details.
16
+ #
17
+ #You should have received a copy of the GNU General Public License
18
+ #along with this program; if not, write to the Free Software
19
+ #Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
+
21
+ ##########################################
22
+ # Subversion information
23
+ # $Id: zabcon.rb 250 2010-12-24 06:56:38Z nelsonab $
24
+ # $Revision: 250 $
25
+ ##########################################
26
+
27
+
28
+ #setup our search path or libraries
29
+ $: << File.expand_path(File.join(File.dirname(__FILE__), '.'))
30
+
31
+ begin
32
+ require 'rubygems'
33
+ rescue LoadError
34
+ puts
35
+ puts "Ruby Gems failed to load. Please install Ruby Gems using your systems"
36
+ puts "package management program or downlaod it from http://rubygems.org."
37
+ puts
38
+ exit 1
39
+ end
40
+
41
+ require 'optparse'
42
+ require 'ostruct'
43
+ require 'libs/zdebug'
44
+ require 'libs/zabcon_globals'
45
+
46
+ if RUBY_VERSION=="1.8.6" #Ruby 1.8.6 lacks the each_char function in the string object, so we add it here
47
+ String.class_eval do
48
+ def each_char
49
+ if block_given?
50
+ scan(/./m) do |x|
51
+ yield x
52
+ end
53
+ else
54
+ scan(/./m)
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+
61
+ class ZabconApp
62
+
63
+ def initialize
64
+ setup_opt_parser
65
+ end
66
+
67
+ def setup_opt_parser
68
+ @cmd_opts=OpenStruct.new
69
+ # @options.debug=0
70
+
71
+
72
+ @opts = OptionParser.new do |opts|
73
+ opts.banner = "Usage: #{$0} [options] [command file]"
74
+ opts.separator "------------------------------------"
75
+ opts.separator ""
76
+ opts.separator "If command file is specified Zabcon will read from the file"
77
+ opts.separator "line by line and execute the commands in order. If '-' is "
78
+ opts.separator "used, Zabcon will read from stdin as though it were a file."
79
+ opts.separator ""
80
+ opts.separator "Options"
81
+ opts.on("-h", "-?", "--help", "Display this help message") do
82
+ @cmd_opts.echo=false
83
+ @cmd_opts.help=true
84
+ puts opts
85
+ end
86
+ opts.on("-l", "--load FILE", "load configuration file supplied or ","default if none") do |file|
87
+ @cmd_opts.config_file=file
88
+ end
89
+ opts.on("--no-config", "Do not attempt to automatically load","the configuration file") do
90
+ @cmd_opts.load_config=false
91
+ end
92
+ opts.on("-d", "--debug LEVEL", Integer, "Specify debug level (Overrides config","file)") do |level|
93
+ @cmd_opts.debug=level
94
+ end
95
+ opts.on("-e", "--[no-]echo", "Enable startup echo. Default is on ","for interactive") do |echo|
96
+ @cmd_opts.echo=echo
97
+ end
98
+ opts.on("-s", "--separator CHAR", "Seperator character for csv styple output.",
99
+ "Use \\t for tab separated output.") do |sep|
100
+ @cmd_opts.table_separator=sep
101
+ end
102
+ opts.on("--no-header", "Do not show headers on output.") do
103
+ @cmd_opts.table_header=false
104
+ end
105
+ end
106
+ end
107
+
108
+ def setup_globals
109
+ env=EnvVars.instance # we must instantiate a singleton before using it
110
+ vars=GlobalVars.instance
111
+
112
+ env["debug"]=0
113
+ env["show_help"]=false
114
+ env["server"]=nil
115
+ env["username"]=nil
116
+ env["password"]=nil
117
+ env["lines"]=24
118
+ env["language"]="english"
119
+ env["logged_in"]=false
120
+ env["have_tty"]=STDIN.tty?
121
+ env["echo"]=STDIN.tty? ? true: false
122
+ env["config_file"]="zabcon.conf"
123
+ env["load_config"]=true
124
+
125
+ #output related environment variables
126
+ env["table_output"]=STDIN.tty? # Is the output a well formatted table, or csv like?
127
+ env["table_header"]=true
128
+ env["table_separator"]=","
129
+
130
+ end
131
+
132
+ #checks to ensure all dependencies are available, forcefully exits with an
133
+ # exit code of 1 if the dependency check fails
134
+ # * ruby_rev is a string denoting the minimum version of ruby suitable
135
+ # * *dependencies is an array of libraries which are required
136
+ def check_dependencies(required_rev,*dependencies)
137
+ puts "Checking dependencies" if EnvVars.instance["echo"]
138
+ depsok=true #assume we will not fail dependencies
139
+
140
+ required_rev=required_rev.split('.')
141
+ ruby_rev=RUBY_VERSION.split('.')
142
+ items=ruby_rev.length < required_rev.length ? ruby_rev.length : required_rev.length
143
+
144
+ for i in 0..items-1 do
145
+ if ruby_rev[i]<required_rev[i]
146
+ puts
147
+ puts "Zabcon requires Ruby version #{required_rev.join('.')} or higher."
148
+ puts "you are using Ruby version #{RUBY_VERSION}."
149
+ puts
150
+ exit(1)
151
+ elsif ruby_rev[i]>required_rev[i]
152
+ break
153
+ end
154
+ end
155
+
156
+ #Convert the inbound array to a hash
157
+ deps = Hash[*dependencies.collect { |v|
158
+ [v,true]
159
+ }.flatten]
160
+
161
+ deps.each_key {|dep|
162
+ val=Gem.source_index.find_name(dep).map {|x| x.name}==[]
163
+ puts " #{dep} : Not Installed" if val
164
+ depsok=false if val
165
+ }
166
+ if !depsok
167
+ puts
168
+ puts "One or more dependencies failed"
169
+ puts "Please see the dependencies file for instructions on installing the"
170
+ puts "required dependencies"
171
+ puts
172
+ exit(1)
173
+ end
174
+ end
175
+
176
+
177
+ def run
178
+ begin
179
+ setup_globals # step 1, set up the global environment variables
180
+ @opts.parse!(ARGV) # step 2, parse the command line and setup the class variable @cmd_opts
181
+
182
+ h = @cmd_opts.marshal_dump() #dump the hash to a temporary variable
183
+ cmd_hash={}
184
+ h.each_pair do |k,v|
185
+ cmd_hash[k.to_s]=v
186
+ end
187
+ EnvVars.instance.load_config(cmd_hash)
188
+ rescue OptionParser::InvalidOption => e
189
+ puts e
190
+ puts
191
+ puts @opts
192
+ exit(1)
193
+ rescue OptionParser::InvalidArgument => e
194
+ puts e
195
+ puts
196
+ puts @opts
197
+ exit(1)
198
+ rescue OptionParser::MissingArgument => e
199
+ puts e
200
+ puts
201
+ puts @opts
202
+ exit(1)
203
+ end
204
+
205
+ puts RUBY_PLATFORM if EnvVars.instance["echo"]
206
+
207
+ check_dependencies("1.8.6","parseconfig", "json", "highline")
208
+ #check_dependencies("0.0.0","parseconfig", "json", "highline")
209
+
210
+ begin
211
+ require 'readline'
212
+ rescue LoadError
213
+ puts "Readline support was not compiled into Ruby. Readline support is required."
214
+ exit
215
+ end
216
+
217
+ #If we don't have the each_char method for the string class include the module that has it.
218
+ if !String.method_defined?("each_char")
219
+ begin
220
+ require 'jcode'
221
+ rescue LoadError
222
+ puts "Module jcode is required for your version of Ruby"
223
+ end
224
+ end
225
+
226
+ require 'libs/zabcon_core' #Require placed after deps check
227
+
228
+ if @cmd_opts.help.nil?
229
+ zabcon=ZabconCore.new
230
+ zabcon.start()
231
+ end
232
+ end
233
+ end
234
+
235
+ zabconapp=ZabconApp.new()
236
+ zabconapp.run()
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zabcon
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - A. Nelson
14
+ autorequire:
15
+ bindir: .
16
+ cert_chain: []
17
+
18
+ date: 2010-12-28 00:00:00 -05:00
19
+ default_executable: zabcon
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: zbxapi
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: parseconfig
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :runtime
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: highline
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :runtime
62
+ version_requirements: *id003
63
+ description: Zabcon is a command line interface for Zabbix written in Ruby
64
+ email: nelsonab@red-tux.net
65
+ executables:
66
+ - zabcon.rb
67
+ extensions: []
68
+
69
+ extra_rdoc_files: []
70
+
71
+ files:
72
+ - zabcon.rb
73
+ - zabcon.conf.default
74
+ - README
75
+ - libs/argument_processor.rb
76
+ - libs/command_help.rb
77
+ - libs/command_tree.rb
78
+ - libs/defines.rb
79
+ - libs/exceptions.rb
80
+ - libs/help.xml
81
+ - libs/input.rb
82
+ - libs/printer.rb
83
+ - libs/zabcon_core.rb
84
+ - libs/zabcon_exceptions.rb
85
+ - libs/zabcon_globals.rb
86
+ - libs/zbxcliserver.rb
87
+ - libs/zdebug.rb
88
+ - ./zabcon.rb
89
+ has_rdoc: true
90
+ homepage: http://trac.red-tux.net/
91
+ licenses:
92
+ - LGPL 2.1
93
+ post_install_message:
94
+ rdoc_options: []
95
+
96
+ require_paths:
97
+ - .
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ hash: 3
104
+ segments:
105
+ - 0
106
+ version: "0"
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ hash: 3
113
+ segments:
114
+ - 0
115
+ version: "0"
116
+ requirements:
117
+ - Requires zbxapi, parseconfig and highline
118
+ rubyforge_project: zabcon
119
+ rubygems_version: 1.3.7
120
+ signing_key:
121
+ specification_version: 3
122
+ summary: Zabcon command line interface for Zabbix
123
+ test_files: []
124
+