zabcon 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,119 @@
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 258 2010-12-28 22:49:21Z nelsonab $
21
+ # $Revision: 258 $
22
+ ##########################################
23
+
24
+ require 'libs/zdebug'
25
+
26
+ #------------------------------------------------------------------------------
27
+ #
28
+ # Class ZError
29
+ #
30
+ # This is the superclass for all Zabcon and ZabbixAPI exceptions.
31
+ #
32
+ #------------------------------------------------------------------------------
33
+ class ZError < RuntimeError
34
+
35
+ include ZDebug
36
+
37
+ attr_reader :help_func, :message, :retry
38
+
39
+ # list of valid params
40
+ # :help_func, the help function with more information for the exception
41
+ # : retry, is the exception eligable for retry?
42
+ def initialize(message=nil, params=nil)
43
+ debug(2,self.class,"Exception raised")
44
+ debug(2,params,"params")
45
+ raise "Exception not called correctly" if params.class!=Hash if !params.nil?
46
+ params={} if params.nil?
47
+ @help_func=params[:help_func]
48
+ @message=message
49
+ @local_msg="Error"
50
+ @retry = params[:retry]
51
+ super(message)
52
+ end
53
+
54
+ def show_message
55
+ puts "** #{self.class}"
56
+ if @message.nil? && @help_func.nil?
57
+ puts "** #{@local_msg}"
58
+ puts
59
+ else
60
+ if !@message.nil?
61
+ @message.each_line {|line|
62
+ puts "** #{line}"
63
+ }
64
+ puts
65
+ puts "---" if !@help_func.nil?
66
+ end
67
+ @help_func.call if !@help_func.nil?
68
+ end
69
+ end
70
+
71
+ #show the backtrace, if override is true it will be shown even if there is a help function
72
+ def show_backtrace(override=false)
73
+ if @help_func.nil? || override
74
+ puts "Backtrace:"
75
+ puts backtrace.join("\n")
76
+ end
77
+ end
78
+ def retry?
79
+ #the following may be worthy of a sig on "The Daily WTF", but this guarantees a boolean gets returned.
80
+ #@retry is not guaranteed to be a boolean.
81
+ if @retry
82
+ return true
83
+ else
84
+ return false
85
+ end
86
+ end
87
+ end
88
+
89
+ class ParameterError < ZError
90
+ def initialize(message=nil, params=nil)
91
+ super(message, params)
92
+ @local_msg="Parameter Error"
93
+ end
94
+ end
95
+
96
+ #----------------------------------------------------------
97
+
98
+ class ParameterError_Invalid < ZError
99
+ def initialize(message=nil, params=nil)
100
+ super(message, params)
101
+ @local_msg="Invalid Parameters"
102
+ end
103
+ end
104
+
105
+ class ParameterError_Missing < ZError
106
+ def initialize(message=nil, params=nil)
107
+ super(message, params)
108
+ @local_msg="Missing required parameters"
109
+ end
110
+ end
111
+
112
+ #----------------------------------------------------------
113
+
114
+ class ParseError < ZError
115
+ def initialize(message=nil, params=nil)
116
+ super(message, params)
117
+ @local_msg="Parse Error"
118
+ end
119
+ end
data/libs/help.xml ADDED
@@ -0,0 +1,275 @@
1
+ <?xml version="1.0" encoding="ISO-8859-15"?>
2
+
3
+ <!--
4
+ GPL 2.0 http://www.gnu.org/licenses/gpl-2.0.html
5
+ Zabbix CLI Tool and associated files
6
+ Copyright (C) 2009,2010 Andrew Nelson nelsonab(at)red-tux(dot)net
7
+
8
+ This program is free software; you can redistribute it and/or
9
+ modify it under the terms of the GNU General Public License
10
+ as published by the Free Software Foundation; either version 2
11
+ of the License, or (at your option) any later version.
12
+
13
+ This program is distributed in the hope that it will be useful,
14
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ GNU General Public License for more details.
17
+
18
+ You should have received a copy of the GNU General Public License
19
+ along with this program; if not, write to the Free Software
20
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
+
22
+ ##########################################
23
+ Subversion information
24
+ $Id: help.xml 243 2010-11-11 15:59:02Z nelsonab $
25
+ $Revision: 243 $
26
+ ##########################################
27
+
28
+ Zabcon help file
29
+
30
+ All help items except for "help" are organized alphabetically.
31
+ -->
32
+
33
+ <!-- 80 Col Marker ######################################################## -->
34
+
35
+ <zabcon_help>
36
+ <help language="english">
37
+ <item command="help">
38
+ General help
39
+ Prompt
40
+ The prompt has the following format &quot;XXs &gt;&quot;
41
+ XX = number representing the current debug level, one or two digits
42
+ s = + or - symbol representing current login state.
43
+ + represents a current login
44
+
45
+ Command help
46
+ &lt;&gt; Mandatory argument/command
47
+ () Optional argument/command
48
+ help - Show this help list
49
+ info - show information about current state
50
+ exit - quit
51
+ quit - quit
52
+ history - Show current history list
53
+ login &lt;server&gt; &lt;username&gt; &lt;password&gt;
54
+ - server is a fully qualified url
55
+ load configuration &lt;config file&gt;
56
+ - loads configuration settings from a file and logs
57
+ into server
58
+ import &lt;file&gt; - Import configuration from an XML file
59
+ set &lt;command&gt; - Sets various variables
60
+ debug &lt;num&gt; - Sets the debug level to &lt;num&gt; **Deprecated**
61
+ prompt - Prompt related commands
62
+ debug &lt;1/0&gt; - Turn the debug portion of the prompt on or off
63
+ lines &lt;num&gt; - Set the screen height. 0 turns off Screen pausing
64
+
65
+ Variables:
66
+ Zabcon has basic variable support, the commands are as follows
67
+ set var/env - sets a variable or variables
68
+ show var/env - shows the currently set variables
69
+ unset var - unsets a variable
70
+
71
+ To use a variable preface the name with a $ character such as $variable.
72
+ If the $ character is escaped with a \ variable substitution will not occur.
73
+ Variables will be substituted if they are found within string blocks enclosed
74
+ by either single or double quotes.
75
+
76
+ One variable is automatically assigned upon login, auth. This is the
77
+ authorization token from the server.
78
+
79
+ Environmental variables cannot be used for substitution at this time, they
80
+ control the various aspects behind the scenes such as debug level.
81
+ The current list of environment variables is:
82
+ debug, language, sheight (screen height)
83
+
84
+
85
+ To view the commands which require a valid login use the command:
86
+
87
+ help commands
88
+
89
+ Zabcon Copyright (C) 2009,2010 Andrew Nelson. Zabcon comes with ABSOLUTELY
90
+ NO WARRANTY; for details see the text of the GNU General Public License 2.0
91
+ at http://www.gnu.org/licenses/gpl-2.0.html
92
+ </item>
93
+
94
+ <!-- 80 Col Marker ######################################################## -->
95
+
96
+ <item command="help_commands">
97
+ Help Commands
98
+ The following commands require a valid login
99
+
100
+ To get help on a specific command type:
101
+ help &lt;command&gt;
102
+
103
+ For instance "help get user" would return help for the "get user" command.
104
+
105
+ &lt;&gt; Mandatory argument/command
106
+ () Optional argument/command
107
+
108
+ get user - returns a list of all users
109
+ get host - returns a list of hosts
110
+ get item - returns a list of items
111
+ hostids=&lt;num&gt; - returns a list of items for host
112
+ extendoutput=true
113
+ - displays more information
114
+
115
+ add user - Adds a user to Zabbix
116
+ without parameters causes help message to be displayed
117
+ add host - Add a host
118
+ add host group - Adds a host group
119
+ add item - Add an item
120
+
121
+ delete user &lt;num&gt; - Deletes user with uid=num
122
+ delete host - Deletes a host
123
+
124
+ raw api - Allows for raw api calls, many of the underlying details
125
+ are handled by this call
126
+ raw json - Allows for raw json calls to the API, this allows for a
127
+ much more direct API call than the "raw api" call
128
+ No syntax checking is performed!
129
+ </item>
130
+
131
+ <!-- 80 Col Marker ######################################################## -->
132
+
133
+ <item command="add_host">
134
+ Add Host adds a host to the Zabbix server.
135
+
136
+ Required arguments:
137
+ host, port, ip and/or dns, status, proxy_hostid, groups, useip
138
+ Optional arguments:
139
+ available, disable_until, status, templates
140
+ maintenanceid, maintenance_from, maintenance_status, maintenance_type,
141
+ useipmi, ipmi_authtype, ipmi_available, ipmi_disable_until, ipmi_errors_from,
142
+ ipmi_ip, ipmi_password, ipmi_port, ipmi_privilege, ipmi_username,
143
+ snmp_available, snmp_disable_until, snmp_errors_from,
144
+ outbytes, error, lastaccess, errors_from, inbytes
145
+
146
+ example:
147
+ add host host=servername dns=host.dom port=10050 proxy_hostid=0 groups=[{groupid=1}] useip=0
148
+
149
+ Adding a host to a template:
150
+ add host host=servername dns=host.dom port=10050 proxy_hostid=0 groups=[{groupid=1}] useip=0 templates=[{templateid=10001}]
151
+
152
+ </item>
153
+
154
+ <!-- 80 Col Marker ######################################################## -->
155
+
156
+ <item command="delete_host">
157
+ Delete Host takes a list of host ids or one host id
158
+ example: delete host id=1,2,3
159
+ delete host id=1
160
+ </item>
161
+
162
+ <!-- 80 Col Marker ######################################################## -->
163
+
164
+ <item command="add_item">
165
+ Add Item
166
+ syntax: add item type=active hostid=num description="some text" key="item.key"
167
+
168
+ valid parameters:
169
+ hostid, snmpv3_securitylevel, snmp_community, publickey, delta, history,
170
+ key, snmp_oid, delay_flex, multiplier, delay, mtime, username, authtype,
171
+ data_type, ipmi_sensor,snmpv3_authpassphrase, prevorgvalue, units, trends,
172
+ snmp_port, formula, type, params, logtimefmt, snmpv3_securityname,
173
+ trapper_hosts, description, password, snmpv3_privpassphrase,
174
+ status, privatekey, valuemapid, templateid, value_type, groups
175
+
176
+ valid values for 'type':
177
+ active, passive, trapper
178
+ </item>
179
+
180
+ <!-- 80 Col Marker ######################################################## -->
181
+
182
+ <item command="add_user">
183
+ Add User requires arguments, valid fields are:
184
+ name, surname, alias, passwd, url, autologin, autologout, lang, theme, refresh
185
+ rows_per_page, type
186
+ example: add user name=someone alias=username passwd=pass autologout=0
187
+ </item>
188
+
189
+ <!-- 80 Col Marker ######################################################## -->
190
+
191
+ <item command="add_user_media">
192
+ Adds a media type to a user
193
+
194
+ Example:
195
+ add user media userid=33 mediatypeid=1 sendto=ddd severity=1 period=""
196
+ </item>
197
+
198
+ <!-- 80 Col Marker ######################################################## -->
199
+
200
+ <item command="get">
201
+ Get Commands
202
+ app
203
+ host
204
+ host group
205
+ host group id
206
+ item
207
+ user
208
+ trigger
209
+ </item>
210
+
211
+ <!-- 80 Col Marker ######################################################## -->
212
+
213
+ <item command="get_user">
214
+ Get User
215
+ syntax: get user show=headers limit=n
216
+ </item>
217
+
218
+ <!-- 80 Col Marker ######################################################## -->
219
+
220
+ <item command="get_item">
221
+ Get Item
222
+ syntax: get item show=headers limit=n
223
+ Returns a list of items based on the parameters given
224
+ hostids - A list or single host to return items for, if a list is given
225
+ it must be encased in [], example:
226
+ get item hostids=[1,2]
227
+ show - a list of headers encased in [] or "all" to show all headers
228
+ limit - how many items to show before stopping
229
+ The default limit is 100
230
+ </item>
231
+
232
+
233
+ <!-- 80 Col Marker ######################################################## -->
234
+
235
+ <item command="delete_user">
236
+ Delete User
237
+ syntax: delete user id=num
238
+ id must be a number
239
+ </item>
240
+
241
+ <!-- 80 Col Marker ######################################################## -->
242
+
243
+ <item command="delete_item">
244
+ Delete Item
245
+ syntax: delete item itemid=num
246
+ itemid must be a number
247
+ </item>
248
+
249
+ <!-- 80 Col Marker ######################################################## -->
250
+
251
+ <item command="raw_api">
252
+ raw api
253
+ syntax: raw api method parameter=value parameter=value
254
+
255
+ The following example will mimic the get user call:
256
+ +> raw api user.get extendoutput=true limit=100
257
+ </item>
258
+
259
+ <!-- 80 Col Marker ######################################################## -->
260
+
261
+ <item command="raw_api">
262
+ raw json
263
+ syntax: raw json {json call}
264
+
265
+ The following example will mimic the get user call:
266
+ +> raw json {"auth":"$auth","method":"user.get","id":2,"params":{"extendoutput":true,"limit":100},"jsonrpc":"2.0"}
267
+
268
+ note:
269
+ The automatically updated variable $auth was used. You can choose to use
270
+ this variable or you can use a different one, however the $auth variable
271
+ is the one provided by the server upon login.
272
+ </item>
273
+
274
+ </help>
275
+ </zabcon_help>
data/libs/input.rb ADDED
@@ -0,0 +1,55 @@
1
+ #GPL 2.0 http://www.gnu.org/licenses/gpl-2.0.html
2
+ #Zabbix CLI Tool and associated files
3
+ #Copyright (C) 2009,2010 Andrew Nelson nelsonab(at)red-tux(dot)net
4
+ #
5
+ #This program is free software; you can redistribute it and/or
6
+ #modify it under the terms of the GNU General Public License
7
+ #as published by the Free Software Foundation; either version 2
8
+ #of the License, or (at your option) any later version.
9
+ #
10
+ #This program is distributed in the hope that it will be useful,
11
+ #but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ #GNU General Public License for more details.
14
+ #
15
+ #You should have received a copy of the GNU General Public License
16
+ #along with this program; if not, write to the Free Software
17
+ #Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
+
19
+ ##########################################
20
+ # Subversion information
21
+ # $Id: input.rb 215 2010-09-25 21:22:37Z richlv $
22
+ # $Revision: 215 $
23
+ ##########################################
24
+
25
+ require 'readline'
26
+
27
+ class Input_Super
28
+
29
+ def get_line
30
+ #place holder
31
+ end
32
+ end
33
+
34
+ class Readline_Input < Input_Super
35
+
36
+ def set_prompt_func(prompt_func)
37
+ @promptfunc=prompt_func
38
+ end
39
+
40
+ def get_line
41
+ line = Readline.readline(@promptfunc.call, true)
42
+ return nil if line.nil?
43
+ if line =~ /^\s*$/ or Readline::HISTORY.to_a[-2] == line
44
+ Readline::HISTORY.pop
45
+ end
46
+ return line
47
+ end
48
+
49
+ end
50
+
51
+ class STDIN_Input < Input_Super
52
+ def get_line
53
+ gets
54
+ end
55
+ end