grafana 0.8.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,112 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>
7
+ Top Level Namespace
8
+
9
+ &mdash; Documentation by YARD 0.9.11
10
+
11
+ </title>
12
+
13
+ <link rel="stylesheet" href="css/style.css" type="text/css" charset="utf-8" />
14
+
15
+ <link rel="stylesheet" href="css/common.css" type="text/css" charset="utf-8" />
16
+
17
+ <script type="text/javascript" charset="utf-8">
18
+ pathId = "";
19
+ relpath = '';
20
+ </script>
21
+
22
+
23
+ <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
24
+
25
+ <script type="text/javascript" charset="utf-8" src="js/app.js"></script>
26
+
27
+
28
+ </head>
29
+ <body>
30
+ <div class="nav_wrap">
31
+ <iframe id="nav" src="class_list.html?1"></iframe>
32
+ <div id="resizer"></div>
33
+ </div>
34
+
35
+ <div id="main" tabindex="-1">
36
+ <div id="header">
37
+ <div id="menu">
38
+
39
+ <a href="_index.html">Index</a> &raquo;
40
+
41
+
42
+ <span class="title">Top Level Namespace</span>
43
+
44
+ </div>
45
+
46
+ <div id="search">
47
+
48
+ <a class="full_list_link" id="class_list_link"
49
+ href="class_list.html">
50
+
51
+ <svg width="24" height="24">
52
+ <rect x="0" y="4" width="24" height="4" rx="1" ry="1"></rect>
53
+ <rect x="0" y="12" width="24" height="4" rx="1" ry="1"></rect>
54
+ <rect x="0" y="20" width="24" height="4" rx="1" ry="1"></rect>
55
+ </svg>
56
+ </a>
57
+
58
+ </div>
59
+ <div class="clear"></div>
60
+ </div>
61
+
62
+ <div id="content"><h1>Top Level Namespace
63
+
64
+
65
+
66
+ </h1>
67
+ <div class="box_info">
68
+
69
+
70
+
71
+
72
+
73
+
74
+
75
+
76
+
77
+
78
+
79
+ </div>
80
+
81
+ <h2>Defined Under Namespace</h2>
82
+ <p class="children">
83
+
84
+
85
+ <strong class="modules">Modules:</strong> <span class='object_link'><a href="Boolean.html" title="Boolean (module)">Boolean</a></span>, <span class='object_link'><a href="Grafana.html" title="Grafana (module)">Grafana</a></span>, <span class='object_link'><a href="Logging.html" title="Logging (module)">Logging</a></span>
86
+
87
+
88
+
89
+ <strong class="classes">Classes:</strong> <span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span>, <span class='object_link'><a href="FalseClass.html" title="FalseClass (class)">FalseClass</a></span>, <span class='object_link'><a href="Hash.html" title="Hash (class)">Hash</a></span>, <span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span>, <span class='object_link'><a href="Time.html" title="Time (class)">Time</a></span>, <span class='object_link'><a href="TrueClass.html" title="TrueClass (class)">TrueClass</a></span>
90
+
91
+
92
+ </p>
93
+
94
+
95
+
96
+
97
+
98
+
99
+
100
+
101
+
102
+ </div>
103
+
104
+ <div id="footer">
105
+ Generated on Sun Dec 10 16:07:39 2017 by
106
+ <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
107
+ 0.9.11 (ruby-2.2.8).
108
+ </div>
109
+
110
+ </div>
111
+ </body>
112
+ </html>
@@ -0,0 +1,7 @@
1
+
2
+ require_relative 'logging'
3
+ require_relative 'monkey_patches.rb'
4
+ require_relative 'grafana/client.rb'
5
+
6
+ module Grafana
7
+ end
@@ -0,0 +1,310 @@
1
+
2
+ module Grafana
3
+
4
+ # All Admin API Calls found under http://docs.grafana.org/http_api/admin/
5
+ #
6
+ # The Admin HTTP API does not currently work with an API Token.
7
+ # API Tokens are currently only linked to an organization and an organization role.
8
+ #
9
+ # They cannot be given the permission of server admin, only users can be given that permission.
10
+ # So in order to use these API calls you will have to use Basic Auth and the Grafana user must
11
+ # have the Grafana Admin permission.
12
+ #
13
+ # (The default admin user is called admin and has permission to use this API.)
14
+ #
15
+ module Admin
16
+
17
+ # get all admin settings
18
+ #
19
+ # @example
20
+ # admin_settings
21
+ #
22
+ # @return [Hash]
23
+ #
24
+ def admin_settings
25
+ logger.debug('Getting admin settings') if @debug
26
+ get('/api/admin/settings')
27
+ end
28
+
29
+ # get all grafana statistics
30
+ #
31
+ # @example
32
+ # admin_stats
33
+ #
34
+ # @return [Hash]
35
+ #
36
+ def admin_stats
37
+ logger.debug('Getting admin statistics') if @debug
38
+ get('/api/admin/stats')
39
+ end
40
+
41
+ # set User Permissions
42
+ #
43
+ # Only works with Basic Authentication (username and password).
44
+ #
45
+ # @param [Hash] params
46
+ # @option params [String] name login or email for user
47
+ # @option params [Mixed] permissions string or hash to change permissions
48
+ # [String] only 'Viewer', 'Editor', 'Read Only Editor' or 'Admin' allowed
49
+ # [Hash] grafana_admin: true or false
50
+ #
51
+ # @example
52
+ # update_user_permissions( user_name: 'admin', permissions: 'Viewer' )
53
+ # update_user_permissions( user_name: 'admin', permissions: { grafana_admin: true } )
54
+ #
55
+ # @return [Hash]
56
+ #
57
+ def update_user_permissions( params )
58
+
59
+ raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
60
+ raise ArgumentError.new('missing params') if( params.size.zero? )
61
+
62
+ user_name = validate( params, required: true, var: 'user_name', type: String )
63
+ permissions = validate( params, required: true, var: 'permissions' )
64
+ raise ArgumentError.new(format('wrong type. \'permissions\' must be an String or Hash, given %s', permissions.class.to_s ) ) unless( permissions.is_a?(String) || permissions.is_a?(Hash) )
65
+
66
+ valid_perms = ['Viewer','Editor','Read Only Editor','Admin']
67
+
68
+ if( permissions.is_a?( String ) && !valid_perms.include?(permissions) )
69
+
70
+ message = format( 'user permissions must be one of %s, given \'%s\'', valid_perms.join(', '), permissions )
71
+ # logger.warn( message )
72
+
73
+ return {
74
+ 'status' => 404,
75
+ 'name' => user_name,
76
+ 'permissions' => permissions,
77
+ 'message' => message
78
+ }
79
+
80
+ elsif( permissions.is_a?(Hash) && !permissions.dig(:grafana_admin).nil? )
81
+
82
+ grafana_admin = permissions.dig(:grafana_admin)
83
+
84
+ unless( grafana_admin.is_a?(Boolean) )
85
+
86
+ message = 'Grafana admin permission must be either \'true\' or \'false\''
87
+ # logger.warn( message )
88
+
89
+ return {
90
+ 'status' => 404,
91
+ 'name' => user_name,
92
+ 'permissions' => permissions,
93
+ 'message' => message
94
+ }
95
+ end
96
+ end
97
+
98
+ usr = user_by_name(user_name)
99
+
100
+ if( usr.nil? || usr.dig('status').to_i != 200 )
101
+ return {
102
+ 'status' => 404,
103
+ 'message' => format('User \'%s\' not found', user_name)
104
+ }
105
+ end
106
+
107
+ user_id = usr.dig('id')
108
+
109
+ if( permissions.is_a?(Hash) )
110
+
111
+ endpoint = format( '/api/admin/users/%s/permissions', user_id )
112
+ payload = {
113
+ isGrafanaAdmin: grafana_admin
114
+ }
115
+
116
+ logger.debug("Updating user id #{user_id} permissions (PUT #{endpoint})") if @debug
117
+ logger.debug(payload.to_json) if(@debug)
118
+
119
+ return put(endpoint, payload.to_json )
120
+ else
121
+
122
+ org = current_organization
123
+
124
+ endpoint = format( '/api/orgs/%s/users/%s', org['id'], user_id )
125
+ logger.debug( format( 'Updating user id %s permissions', user_id ) ) if @debug
126
+
127
+ payload = {
128
+ name: org.dig('name'),
129
+ orgId: org.dig('id'),
130
+ role: permissions.downcase.capitalize
131
+ }
132
+
133
+ logger.debug("Updating user id #{user_id} permissions (PATCH #{endpoint})") if @debug
134
+ logger.debug(payload.to_json) if(@debug)
135
+
136
+ return patch( endpoint, payload.to_json )
137
+ end
138
+ end
139
+
140
+ # Delete an Global User
141
+ #
142
+ # Only works with Basic Authentication (username and password).
143
+ #
144
+ # @param [Mixed] user_id Username (String) or Userid (Integer) for delete User
145
+ # The Admin User can't be delete!
146
+ #
147
+ # @example
148
+ # delete_user( 1 )
149
+ # delete_user( 'foo' )
150
+ #
151
+ # @return [Hash]
152
+ #
153
+ def delete_user( user_id )
154
+
155
+ raise ArgumentError.new(format('wrong type. user \'user_id\' must be an String (for an User name) or an Integer (for an User Id), given \'%s\'', user_id.class.to_s)) if( user_id.is_a?(String) && user_id.is_a?(Integer) )
156
+ raise ArgumentError.new('missing \'user_id\'') if( user_id.size.zero? )
157
+
158
+ if(user_id.is_a?(String))
159
+ usr = user_by_name(user_id)
160
+ user_id = usr.dig('id')
161
+ end
162
+
163
+ if( user_id.nil? )
164
+ return {
165
+ 'status' => 404,
166
+ 'message' => format( 'No User \'%s\' found', user_id)
167
+ }
168
+ end
169
+
170
+ if( user_id.is_a?(Integer) && user_id.to_i.zero? )
171
+ return {
172
+ 'status' => 403,
173
+ 'message' => format( 'Can\'t delete user id %d (admin user)', user_id )
174
+ }
175
+ end
176
+
177
+ endpoint = format('/api/admin/users/%d', user_id )
178
+ logger.debug( "Deleting user id #{user_id} (DELETE #{endpoint})" ) if @debug
179
+
180
+ delete( endpoint )
181
+ end
182
+
183
+ # Create new user
184
+ #
185
+ # Only works with Basic Authentication (username and password).
186
+ #
187
+ # @param [Hash] params
188
+ # @option params [String] user_name name for user (required)
189
+ # @option params [String] email email for user (required)
190
+ # @option params [String] login_name login name for user (optional) - if 'login_name' is not set, 'name' is used
191
+ # @option params [String] password password (required)
192
+ #
193
+ # @example
194
+ # params = {
195
+ # user_name: 'foo',
196
+ # email: 'foo@bar.com',
197
+ # password: 'pass'
198
+ # }
199
+ # add_user( params )
200
+ #
201
+ # @return [Hash|FalseClass]
202
+ #
203
+ def add_user( params )
204
+
205
+ raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
206
+ raise ArgumentError.new('missing params') if( params.size.zero? )
207
+
208
+ user_name = validate( params, required: true, var: 'user_name', type: String )
209
+ email = validate( params, required: true, var: 'email', type: String )
210
+ login_name = validate( params, required: false, var: 'login_name', type: String ) || user_name
211
+ password = validate( params, required: true, var: 'password', type: String )
212
+
213
+ usr = user_by_name(user_name)
214
+
215
+ if( usr.nil? || usr.dig('status').to_i == 200 )
216
+ return {
217
+ 'status' => 404,
218
+ 'id' => usr.dig('id'),
219
+ 'email' => usr.dig('email'),
220
+ 'name' => usr.dig('name'),
221
+ 'login' => usr.dig('login'),
222
+ 'message' => format( 'user \'%s\' with email \'%s\' exists', user_name, email )
223
+ }
224
+ end
225
+
226
+ #
227
+ payload = {
228
+ name: user_name,
229
+ email: email,
230
+ login: login_name,
231
+ password: password
232
+ }
233
+ payload.reject!{ |_k, v| v.nil? }
234
+
235
+ endpoint = '/api/admin/users'
236
+ logger.debug("Create user #{user_name} (PUT #{endpoint})") if @debug
237
+ logger.debug(payload.to_json) if(@debug)
238
+
239
+ post( endpoint, payload.to_json)
240
+ end
241
+
242
+
243
+ # Change Password for User
244
+ #
245
+ # Only works with Basic Authentication (username and password).
246
+ # Change password for a specific user.
247
+ #
248
+ # @param [Hash] params
249
+ # @option params [String] user_name user_name for user (required)
250
+ # @option params [String] password password to set (required)
251
+ #
252
+ # @example
253
+ # params = {
254
+ # user_name: 'foo',
255
+ # password: 'bar'
256
+ # }
257
+ # update_user_password( params )
258
+ #
259
+ # @return [Hash]
260
+ #
261
+ def update_user_password( params )
262
+
263
+ raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
264
+ raise ArgumentError.new('missing params') if( params.size.zero? )
265
+
266
+ user_name = validate( params, required: true, var: 'user_name', type: String )
267
+ password = validate( params, required: true, var: 'password', type: String )
268
+
269
+ usr = user_by_name(user_name)
270
+
271
+ if( usr.nil? || usr.dig('status').to_i != 200 )
272
+ return {
273
+ 'status' => 404,
274
+ 'message' => format('User \'%s\' not found', user_name)
275
+ }
276
+ end
277
+
278
+ user_id = usr.dig('id')
279
+
280
+ endpoint = format( '/api/admin/users/%d/password', user_id )
281
+ payload = {
282
+ password: password
283
+ }
284
+
285
+ logger.debug("Updating password for user id #{user_id} (PUT #{endpoint})") if @debug
286
+ logger.debug(payload.to_json) if(@debug)
287
+
288
+ put( endpoint, payload.to_json )
289
+ end
290
+
291
+ # Pause all alerts
292
+ #
293
+ # Only works with Basic Authentication (username and password).
294
+ #
295
+ # @example
296
+ # pause_all_alerts
297
+ #
298
+ # @return [Hash]
299
+ #
300
+ def pause_all_alerts
301
+
302
+ endpoint = '/api/admin/pause-all-alerts'
303
+ logger.debug("pause all alerts (POST #{endpoint})") if @debug
304
+
305
+ post( endpoint, nil )
306
+ end
307
+
308
+ end
309
+
310
+ end
@@ -0,0 +1,38 @@
1
+
2
+ module Grafana
3
+
4
+ # http://docs.grafana.org/http_api/alerting/
5
+ #
6
+ module Alerts
7
+
8
+ # Get alerts
9
+ # GET /api/alerts/
10
+ #
11
+ # Get one alert
12
+ # GET /api/alerts/:id
13
+ #
14
+ def alerts( id = nil ); end
15
+
16
+ # Pause alert
17
+ # POST /api/alerts/:id/pause
18
+ def alert_pause( id ); end
19
+
20
+ # Get alert notifications
21
+ # GET /api/alert-notifications
22
+ def alert_notifications; end
23
+
24
+ # Create alert notification
25
+ # POST /api/alert-notifications
26
+ def create_alert_notification( oarams ); end
27
+
28
+ # Update alert notification
29
+ # PUT /api/alert-notifications/1
30
+ def update_alert_notification( params ); end
31
+
32
+ # Delete alert notification
33
+ # DELETE /api/alert-notifications/:notificationId
34
+ def delete_alert_notification( id ); end
35
+
36
+ end
37
+
38
+ end