zbxapi 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,125 @@
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: $
21
+ # $Revision: $
22
+ ##########################################
23
+
24
+ #------------------------------------------------------------------------------
25
+ #
26
+ # Class ZbxAP_ParameterError
27
+ #
28
+ # Exception class for parameter errors for Argument Processor calls.
29
+ #
30
+ #------------------------------------------------------------------------------
31
+
32
+ #setup our search path or libraries
33
+ path=File.expand_path(File.dirname(__FILE__) + "/./")+"/"
34
+
35
+ require path+'zdebug'
36
+ require path+'exceptions'
37
+
38
+ #------------------------------------------------------------------------------
39
+ #
40
+ # Class ZbxAPI_ExceptionBadAuth
41
+ #
42
+ # Exception class for bad authentication information
43
+ #
44
+ #------------------------------------------------------------------------------
45
+
46
+ class ZbxAPI_ExceptionBadAuth < RuntimeError
47
+ end
48
+
49
+ #------------------------------------------------------------------------------
50
+ #
51
+ # Class ZbxAPI_ExceptionBadServerUrl
52
+ #
53
+ # Exception class for bad host url, also used for connection
54
+ # refused errors
55
+ #
56
+ #------------------------------------------------------------------------------
57
+
58
+ class ZbxAPI_ExceptionBadServerUrl < RuntimeError
59
+ end
60
+
61
+ #------------------------------------------------------------------------------
62
+ #
63
+ # Class ZbxAPI_ExceptionArgumentError
64
+ #
65
+ # Exception class for incorrect arguments to a method
66
+ #
67
+ #------------------------------------------------------------------------------
68
+
69
+ class ZbxAPI_ExceptionArgumentError < RuntimeError
70
+ end
71
+
72
+ #------------------------------------------------------------------------------
73
+ #
74
+ # Class ZbxAPI_ParameterError
75
+ #
76
+ # Exception class for parameter errors for API calls.
77
+ #
78
+ #------------------------------------------------------------------------------
79
+
80
+ class ZbxAPI_ParameterError < RuntimeError
81
+ end
82
+
83
+ #------------------------------------------------------------------------------
84
+ #
85
+ # Class ZbxAPI_ExceptionVersion
86
+ #
87
+ # Exception class for API version errors
88
+ #
89
+ #------------------------------------------------------------------------------
90
+
91
+ class ZbxAPI_ExceptionVersion < RuntimeError
92
+ end
93
+
94
+ #------------------------------------------------------------------------------
95
+ #
96
+ # Class ZbxAPI_ExceptionLoginPermission
97
+ #
98
+ # Exception class for lack of API login permission
99
+ #
100
+ #------------------------------------------------------------------------------
101
+
102
+ class ZbxAPI_ExceptionLoginPermission < ZError
103
+ end
104
+
105
+ #------------------------------------------------------------------------------
106
+ #
107
+ # Class ZbxAPI_ExceptionPermissionError
108
+ #
109
+ # Exception class for general API permissions errors
110
+ #
111
+ #------------------------------------------------------------------------------
112
+
113
+ class ZbxAPI_ExceptionPermissionError < ZError
114
+ end
115
+
116
+ #------------------------------------------------------------------------------
117
+ #
118
+ # Class ZbxAPI_GeneralError
119
+ #
120
+ # Exception class for errors not encompassed in the above exceptions.
121
+ #
122
+ #------------------------------------------------------------------------------
123
+
124
+ class ZbxAPI_GeneralError < ZError
125
+ end
@@ -0,0 +1,122 @@
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: exceptions.rb 254 2010-12-27 19:45:42Z nelsonab $
21
+ # $Revision: 254 $
22
+ ##########################################
23
+
24
+ #setup our search path or libraries
25
+ path=File.expand_path(File.dirname(__FILE__) + "/./")+"/"
26
+
27
+ require path+'zdebug'
28
+
29
+ #------------------------------------------------------------------------------
30
+ #
31
+ # Class ZError
32
+ #
33
+ # This is the superclass for all Zabcon and ZabbixAPI exceptions.
34
+ #
35
+ #------------------------------------------------------------------------------
36
+ class ZError < RuntimeError
37
+
38
+ include ZDebug
39
+
40
+ attr_reader :help_func, :message, :retry
41
+
42
+ # list of valid params
43
+ # :help_func, the help function with more information for the exception
44
+ # : retry, is the exception eligable for retry?
45
+ def initialize(message=nil, params=nil)
46
+ debug(2,self.class,"Exception raised")
47
+ debug(2,params,"params")
48
+ raise "Exception not called correctly" if params.class!=Hash if !params.nil?
49
+ params={} if params.nil?
50
+ @help_func=params[:help_func]
51
+ @message=message
52
+ @local_msg="Error"
53
+ @retry = params[:retry]
54
+ super(message)
55
+ end
56
+
57
+ def show_message
58
+ puts "** #{self.class}"
59
+ if @message.nil? && @help_func.nil?
60
+ puts "** #{@local_msg}"
61
+ puts
62
+ else
63
+ if !@message.nil?
64
+ @message.each_line {|line|
65
+ puts "** #{line}"
66
+ }
67
+ puts
68
+ puts "---" if !@help_func.nil?
69
+ end
70
+ @help_func.call if !@help_func.nil?
71
+ end
72
+ end
73
+
74
+ #show the backtrace, if override is true it will be shown even if there is a help function
75
+ def show_backtrace(override=false)
76
+ if @help_func.nil? || override
77
+ puts "Backtrace:"
78
+ puts backtrace.join("\n")
79
+ end
80
+ end
81
+ def retry?
82
+ #the following may be worthy of a sig on "The Daily WTF", but this guarantees a boolean gets returned.
83
+ #@retry is not guaranteed to be a boolean.
84
+ if @retry
85
+ return true
86
+ else
87
+ return false
88
+ end
89
+ end
90
+ end
91
+
92
+ class ParameterError < ZError
93
+ def initialize(message=nil, params=nil)
94
+ super(message, params)
95
+ @local_msg="Parameter Error"
96
+ end
97
+ end
98
+
99
+ #----------------------------------------------------------
100
+
101
+ class ParameterError_Invalid < ZError
102
+ def initialize(message=nil, params=nil)
103
+ super(message, params)
104
+ @local_msg="Invalid Parameters"
105
+ end
106
+ end
107
+
108
+ class ParameterError_Missing < ZError
109
+ def initialize(message=nil, params=nil)
110
+ super(message, params)
111
+ @local_msg="Missing required parameters"
112
+ end
113
+ end
114
+
115
+ #----------------------------------------------------------
116
+
117
+ class ParseError < ZError
118
+ def initialize(message=nil, params=nil)
119
+ super(message, params)
120
+ @local_msg="Parse Error"
121
+ end
122
+ end
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